<?php include 'header.php'; ?>
<?php
$msg='';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $words = $_POST['words'];
    $instructions = $_POST['instructions'];

    // Prepare the SQL statement with placeholders
    $query = "UPDATE upwork_ai SET words = ?, instructions = ? WHERE user_id = ?";

    // Prepare and bind parameters
    $stmt = $conn->prepare($query);
    $stmt->bind_param("ssi", $words, $instructions, $userId);

    // Execute the statement
    if ($stmt->execute()) {
        $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['enabled'])) {
       $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> - AI bids will be placed from now.
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>';
}

if (isset($_GET['disabled'])) {
       $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> - Normal bids will be placed from now.
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>';
}
$sql = "SELECT * FROM upwork_ai WHERE user_id='$userId'";
echo $sql;
$result = $conn->query($sql);

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

<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">AI Bidding </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">AI Bidding</li>
                        </ol>
                    </div>

                </div>
            </div>
        </div>
        <!-- end page title -->

        <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>AI Bidding</h2>
                    <p class="card-text text-muted">Configure your AI bidding.</p>


                    <form method="POST" class="p-4">

                      



                            <div class="mb-4">
                                <label class="float-start" for="words">Bid words </label>
                                <input type="number" name="words" placeholder="Enter number of words" max="150" min="50" class="form-control" value="<?php echo $airow['words']; ?>"
                                    id="words" required>
                            </div>
                            
 <div>
                                <label class="float-start" for="instructions">instructionss</label>
                                <textarea name="instructions" required placeholder="Enter any specific instructionss" rows="10" class="form-control" ><?php echo $airow['instructions']; ?></textarea>
                            </div>
                            
                            
                          

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





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

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