<?php include 'header.php'; 
// if ($mainrow['plan'] == '0') {
//     echo "<script>location.href='billing.php'</script>";
// }


?>

<?php
$msg='';

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
$hourly = $_POST['hourly'];
$bdelay = $_POST['bid_delay'];
$tinterval = $_POST['time_interval'];
$tlimit = $_POST['tlimit'];
$blimit = $_POST['blimit'];
$max_bids = $_POST['max_bids'];
$schedule = $_POST['schedule'];
$stime = $_POST['stime'];
$etime = $_POST['etime'];

$query = "UPDATE autobidding SET hourly = '$hourly', bid_delay = '$bdelay', time_interval = '$tinterval', bids_limit = '$blimit', time_limit = '$tlimit', max_bids = '$max_bids', schedule = '$schedule', stime = '$stime', etime = '$etime' WHERE user_id = '$userId'";
if ($conn->query($query) === TRUE) {
     $msg = '<div class="alert alert-success alert-top-border alert-dismissible fade show" role="alert">
                    <i class="ri-check-double-line me-3 align-middle fs-16 text-success"></i><strong>Success</strong> - Settings updated
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>';
} else {
      $msg = '<div class="alert alert-danger alert-top-border alert-dismissible fade show" role="alert">
                    <i class="ri-error-warning-line me-3 align-middle fs-16 text-danger"></i><strong>Error</strong> - Settings updation error
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>';
}
    
}
if (isset($_GET['status'])) {
       $msg = '<div class="alert alert-success alert-top-border alert-dismissible fade show" role="alert">
                    <i class="ri-check-double-line me-3 align-middle fs-16 text-success"></i><strong>Success</strong> - Autobidding updated
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>';
}
$sql = "SELECT * FROM autobidding WHERE user_id='$userId'";
$result = $conn->query($sql);

if ($result->num_rows == 1) {
    $autorow = $result->fetch_assoc();
}

$sql = "SELECT * FROM skills WHERE user_id='$userId'";
$result = $conn->query($sql);

if ($result->num_rows == 1) {
    $skillsrow = $result->fetch_assoc();
    if (!$skillsrow['skills']){
        $noskills = 1;
    }
}



?>

<div class="page-content">
    <div class="container-fluid">

                
                
        <!-- start page title -->
        <div class="row">
            <div class="col-12">
                <div class="page-title-box d-sm-flex align-items-center justify-content-between bg-transparent">
                    <h4 class="mb-sm-0">Autobidding</h4>

                    <div class="page-title-right">
                        <ol class="breadcrumb m-0">
                            <li class="breadcrumb-item"><a href="javascript: void(0);">Dashboard</a></li>
                            <li class="breadcrumb-item active">Autobidding</li>
                        </ol>
                    </div>

                </div>
            </div>
        </div>
        <!-- end page title -->
 <?php if ($noskills) {
        ?>
        <div class="mb-4">
        <div class="mb-4 alert border-warning alert-dismissible fade show" role="alert">
                    <i class="ri-error-warning-line me-3 text-warning align-middle fs-16"></i><strong>Please sync your skills from freelancer to bid on projects.</strong> <a href="skills.php" class="text-success">Sync Skills</a>
                   
                </div>
                </div>
                <?php } ?>
        <center>
            <div class="col-lg-6">
                <?php echo $msg; ?>

                <div class="card card-body text-center">
                    <div class="avatar-xl mx-auto mb-3 mt-4">
                        <div class="avatar-title bg-transparent text-success rounded" style="font-size: 100px">
                            <i class="ri-robot-line"></i>
                        </div>
                    </div>
                    <h2>Autobidding</h2>
                    <p class="card-text text-muted">Configure your autobidding by below settings.</p>



<form method="POST" class="p-4">
    <div style="display: flex; justify-content: center;">
        <?php if ($autorow['status']) { ?>
            <div>
                <div class="form-check form-switch form-switch-lg form-switch-success">
                    <input class="form-check-input" type="checkbox" role="switch" id="autobid" checked>
                </div>
                <label class="form-check-label" style="margin-bottom: 40px" for="autobid">Turn Off</label>
            </div>
        <?php } else { ?>
            <div>
                <div class="form-check form-switch form-switch-lg form-switch-success">
                    <input class="form-check-input" type="checkbox" role="switch" id="autobid">
                </div>
                <label class="form-check-label" style="margin-bottom: 40px" for="autobid">Turn On</label>
            </div>
        <?php } ?>
        <script>
            var status = "<?php echo $autorow['status']; ?>";
            var switchElement = document.getElementById('autobid');

            if (status === "1") {
                switchElement.addEventListener('change', function() {
                    location.href = 'disable.php';
                });
            } else {
                switchElement.addEventListener('change', function() {
                    location.href = 'enable.php';
                });
            }
        </script>
    </div>

    <div class="row row-cols-1 row-cols-md-2 g-3 mt-4">
        <div class="col">
            <label for="delay" class="form-label">Bid delay (in seconds)</label>
            <input type="number" name="bid_delay" placeholder="Enter delay" max="60" min="1" class="form-control" value="<?php echo $autorow['bid_delay']; ?>" id="delay">
        </div>

        <div class="col">
            <label for="interval" class="form-label">Time interval</label>
            <select class="form-select" name="time_interval" id="interval">
                <?php
                $bid_interval = $autorow['time_interval'];
                $options = array(
                    1 => "Every 1 minute",
                    2 => "Every 2 minutes",
                    3 => "Every 3 minutes",
                    4 => "Every 4 minutes",
                    5 => "Every 5 minutes",
                    6 => "Every 6 minutes",
                    7 => "Every 7 minutes",
                    8 => "Every 8 minutes",
                    9 => "Every 9 minutes"
                );

                foreach ($options as $value => $text) {
                    $selected = ($bid_interval == $value) ? "selected" : "";
                    echo "<option value='$value' $selected>$text</option>";
                }
                ?>
            </select>
        </div>
    </div>

    <div class="row row-cols-1 row-cols-md-2 g-3 mt-4">
        <div class="col">
            <label for="hourly" class="form-label">Bid on Hourly projects</label>
            <select name="hourly" class="form-select" id="hourly">
                <?php
                $options = array(
                    0 => "No",
                    1 => "Yes",
                );
                foreach ($options as $value => $text) {
                    $selected = ($autorow['hourly'] == $value) ? "selected" : "";
                    echo "<option value='$value' $selected>$text</option>";
                }
                ?>
            </select>
        </div>

        <div class="col">
            <label for="max_bids" class="form-label">Don't bid if more than</label>
            <select name="max_bids" class="form-select" id="max_bids">
                <?php
                $options = array(
                    25 => "25 bids",
                    50 => "50 bids",
                    75 => "75 bids",
                    100 => "100 bids",
                );
                foreach ($options as $value => $text) {
                    $selected = ($autorow['max_bids'] == $value) ? "selected" : "";
                    echo "<option value='$value' $selected>$text</option>";
                }
                ?>
            </select>
        </div>
    </div>

    <div class="row row-cols-1 row-cols-md-2 g-3 mt-4">
        <div class="col">
            <label for="tlimit" class="form-label">Time limit</label>
            <select name="tlimit" class="form-select" id="tlimit">
                <?php
                $options = array(
                    0 => "No time limit",
                    1 => "Turn off after 1 hour",
                    2 => "Turn off after 2 hours",
                    3 => "Turn off after 3 hours",
                    4 => "Turn off after 4 hours",
                    5 => "Turn off after 5 hours",
                    6 => "Turn off after 6 hours",
                    7 => "Turn off after 7 hours",
                    8 => "Turn off after 8 hours",
                    9 => "Turn off after 9 hours",
                    10 => "Turn off after 10 hours",
                    11 => "Turn off after 11 hours",
                    12 => "Turn off after 12 hours",
                    13 => "Turn off after 13 hours",
                    14 => "Turn off after 14 hours",
                    15 => "Turn off after 15 hours",
                    16 => "Turn off after 16 hours",
                    17 => "Turn off after 17 hours",
                    18 => "Turn off after 18 hours",
                    19 => "Turn off after 19 hours",
                    20 => "Turn off after 20 hours",
                    21 => "Turn off after 21 hours",
                    22 => "Turn off after 22 hours",
                    23 => "Turn off after 23 hours",
                );
                foreach ($options as $value => $text) {
                    $selected = ($autorow['time_limit'] == $value) ? "selected" : "";
                    echo "<option value='$value' $selected>$text</option>";
                }
                ?>
            </select>
        </div>

        <div class="col">
            <label for="blimit" class="form-label">Bids Limit</label>
            <select name="blimit" class="form-select" id="blimit">
                <?php
                $options = array(
                    0 => "No Bids limit",
                    10 => "Turn off after 10 bids",
                    20 => "Turn off after 20 bids",
                    30 => "Turn off after 30 bids",
                    40 => "Turn off after 40 bids",
                    50 => "Turn off after 50 bids",
                    60 => "Turn off after 60 bids",
                    70 => "Turn off after 70 bids",
                    80 => "Turn off after 80 bids",
                    90 => "Turn off after 90 bids",
                    100 => "Turn off after 100 bids",
                );
                foreach ($options as $value => $text) {
                    $selected = ($autorow['bids_limit'] == $value) ? "selected" : "";
                    echo "<option value='$value' $selected>$text</option>";
                }
                ?>
            </select>
        </div>
    </div>

    <div class="row mt-5">
        <div class="col">
            <label for="schedule" class="form-label">Schedule autobidding</label>
            <select name="schedule" class="form-control">
                <option value="0" <?php if (!$autorow['schedule']) { echo "selected"; } ?> >No</option>
                <option value="1" <?php if ($autorow['schedule']) { echo "selected"; } ?> >Yes</option>
            </select>
        </div>

        <div class="col">
            <label for="stime" class="form-label">Start Time (SAST)</label>
            <input type="time" name="stime" class="form-control" value="<?php echo $autorow['stime']; ?>" id="stime">
        </div>

        <div class="col">
            <label for="etime" class="form-label">End Time (SAST)</label>
            <input type="time" name="etime" class="form-control" value="<?php echo $autorow['etime']; ?>" id="etime">
        </div>
    </div>

    <button type="submit" class="btn btn-success mt-4 w-100">Submit</button>
</form>
                </div>
            </div>





        </center>
        <!-- container-fluid -->
    </div>

    <?php include 'footer.php'; ?>