<?php
session_start();
include 'include/config.php';

if(!isset($_SESSION['id'])){
    // echo "<script> window.location.href = 'index.php' </script>";
  }
  
  
  

?>

<?php  include 'include/head.php'; ?>
                    
    <!-- Start Content-->
    <div class="container-fluid">
                        
                        <!-- start page title -->
                        <div class="row">
                            <div class="col-12">
                                <div class="page-title-box">
                                    <div class="page-title-right">
                                        <ol class="breadcrumb m-0">
                                            <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                                            <li class="breadcrumb-item active">Add New User</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Add Vehicle</h4>
                                </div>
                            </div>
                        </div>
                        <!-- end page title --> 

                        <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">

                    <?php
                    
                    // if(isset($_POST['submit'])){
                    //     $name = $_POST['name'];
                    //     $image = $_FILES['image']['name'];
                    //     $tmp_name = $_FILES['image']['tmp_name'];
                    //     $image_url = 'https://createapp.co.in/ftc_services/upload/cat_img/' . $image;
                    //       $fourRandomDigit = mt_rand(1000,9999);
                                        
                    //      $sql = "INSERT INTO `service_category`(`cat_name`, `cat_image`, `unique_id`) VALUES ('$name','$image_url','$fourRandomDigit')";
                    //     // print_r($sql);
                    //      $res = mysqli_query($conn,$sql);
                    //     if($res){
                    //       move_uploaded_file($tmp_name, '../upload/cat_img/'.$image);
                    //         echo "<h6> Category Submit </h6>";
                    //     }else{
                    //         echo "<h6> Not Submit Category</h6>";
                    //     }
                                        
                    // }

                    // if(isset($_GET['id'])){
                    //     $id = $_GET['id'];
                    //     $url = 'update-notification.php?id='. $id ;

                    //     $sql1 = "SELECT * FROM notification WHERE id='$id' ";
                    //     $res1 = mysqli_query($conn, $sql1);
                    //     $row = mysqli_fetch_assoc($res1);
                    // }
                    ?>
                   <?php

$id = $_GET['id'] ?? '';

if ($id == '') {
    die("Vendor ID missing in the URL.");
}

$query = mysqli_query($conn, "SELECT * FROM vender WHERE id='$id'");
$data = mysqli_fetch_assoc($query);

if (isset($_POST['submit'])) {
    // Form data
    $name = $_POST['name'];
    $address = $_POST['address'];
    $city = $_POST['city'] ?? '';
    $pincode = $_POST['pincode'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $password = $_POST['password'] ?? '';
    $email = $_POST['email'] ?? '';
    $state = $_POST['state'] ?? '';
    $lat = $_POST['lat'] ?? '';
    $log = $_POST['log'] ?? '';
    $kyc_status = $_POST['kyc_status'] ?? 'pending';
    $covid_19 = $_POST['covid_19'] ?? '';
    $police_verification = $_POST['police_verification'] ?? '';
    $main_service_name = $_POST['main_service_name'] ?? '';
    $rating = $_POST['rating'] ?? 0;
    $per_hour_sallary = $_POST['per_hour_sallary'] ?? 0;
    $booking_status = $_POST['booking_status'] ?? 'available';
    $about_us = $_POST['about_us'] ?? '';
    $vender_fmc_token = $_POST['vender_fmc_token'] ?? '';
    $wallet = $_POST['wallet'] ?? 0;
    $locality = $_POST['locality'] ?? '';

    // File uploads
    $upload_path = 'upload/';

    // Profile Image
    $profile_image = $_FILES['profile_image']['name'];
    if ($profile_image != '') {
        move_uploaded_file($_FILES['profile_image']['tmp_name'], $upload_path . $profile_image);
    } else {
        $profile_image = $data['profile_image'];
    }

    // Aadhar
    $adhar_card = $_FILES['adhar_card']['name'];
    if ($adhar_card != '') {
        move_uploaded_file($_FILES['adhar_card']['tmp_name'], $upload_path . $adhar_card);
    } else {
        $adhar_card = $data['adhar_card'];
    }

    // PAN
    $pan_card = $_FILES['pan_card']['name'];
    if ($pan_card != '') {
        move_uploaded_file($_FILES['pan_card']['tmp_name'], $upload_path . $pan_card);
    } else {
        $pan_card = $data['pan_card'];
    }

    // SQL Update
    $sql = "UPDATE vender SET
        name='$name', phone='$phone', address='$address', city='$city', pincode='$pincode',
        password='$password', email='$email', state='$state', lat='$lat', log='$log',
        adhar_card='$adhar_card', pan_card='$pan_card', kyc_status='$kyc_status',
        covid_19='$covid_19', police_verification='$police_verification',
        main_service_name='$main_service_name', rating='$rating', per_hour_sallary='$per_hour_sallary',
        booking_status='$booking_status', profile_image='$profile_image',
        about_us='$about_us', vender_fmc_token='$vender_fmc_token', wallet='$wallet', locality='$locality'
        WHERE id='$id'";

    $result = mysqli_query($conn, $sql);

    if ($result) {
        echo "<h4>Vendor data updated successfully!</h4>";
    } else {
        echo "<h4>Error updating vendor data: " . mysqli_error($conn) . "</h4>";
    }
}
?>


             <form method="POST" enctype="multipart/form-data">
    <div class="row">

        <!-- Name -->
        <div class="col-lg-12 mb-3">
            <label class="form-label">Name</label>
            <input type="text" name="name" class="form-control" value="<?= $data['name'] ?>" required>
        </div>

        <!-- Phone -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Phone</label>
            <input type="text" name="phone" class="form-control" value="<?= $data['phone'] ?>" required>
        </div>

        <!-- Email -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Email</label>
            <input type="email" name="email" class="form-control" value="<?= $data['email'] ?>">
        </div>

        <!-- Password -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Password</label>
            <input type="text" name="password" class="form-control" value="<?= $data['password'] ?>" required>
        </div>

        <!-- Address -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Address</label>
            <input type="text" name="address" class="form-control" value="<?= $data['address'] ?>" required>
        </div>

        <!-- City -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">City</label>
            <input type="text" name="city" class="form-control" value="<?= $data['city'] ?>">
        </div>

        <!-- State -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">State</label>
            <input type="text" name="state" class="form-control" value="<?= $data['state'] ?>">
        </div>

        <!-- Pincode -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Pincode</label>
            <input type="text" name="pincode" class="form-control" value="<?= $data['pincode'] ?>">
        </div>

        <!-- Latitude -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Latitude</label>
            <input type="text" name="lat" class="form-control" value="<?= $data['lat'] ?>">
        </div>

        <!-- Longitude -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Longitude</label>
            <input type="text" name="log" class="form-control" value="<?= $data['log'] ?>">
        </div>

        <!-- Aadhar Upload -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Aadhar Card</label>
            <input type="file" name="adhar_card" class="form-control">
            <small>Current: <?= $data['adhar_card'] ?></small>
        </div>

        <!-- PAN Upload -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">PAN Card</label>
            <input type="file" name="pan_card" class="form-control">
            <small>Current: <?= $data['pan_card'] ?></small>
        </div>

        <!-- Profile Image -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Profile Image</label>
            <input type="file" name="profile_image" class="form-control">
            <small>Current: <?= $data['profile_image'] ?></small>
        </div>

        <!-- KYC Status -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">KYC Status</label>
            <select name="kyc_status" class="form-control">
                <option value="pending" <?= $data['kyc_status'] == 'pending' ? 'selected' : '' ?>>Pending</option>
                <option value="approved" <?= $data['kyc_status'] == 'approved' ? 'selected' : '' ?>>Approved</option>
            </select>
        </div>

        <!-- COVID-19 -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">COVID-19 Status</label>
            <input type="text" name="covid_19" class="form-control" value="<?= $data['covid_19'] ?>">
        </div>

        <!-- Police Verification -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Police Verification</label>
            <input type="text" name="police_verification" class="form-control" value="<?= $data['police_verification'] ?>">
        </div>

        <!-- Main Service -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Main Service Name</label>
            <input type="text" name="main_service_name" class="form-control" value="<?= $data['main_service_name'] ?>">
        </div>

        <!-- Rating -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Rating</label>
            <input type="number" step="0.1" name="rating" class="form-control" value="<?= $data['rating'] ?>">
        </div>

        <!-- Per Hour Salary -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Per Hour Salary</label>
            <input type="number" name="per_hour_sallary" class="form-control" value="<?= $data['per_hour_sallary'] ?>">
        </div>

        <!-- Booking Status -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Booking Status</label>
            <select name="booking_status" class="form-control">
                <option value="available" <?= $data['booking_status'] == 'available' ? 'selected' : '' ?>>Available</option>
                <option value="booked" <?= $data['booking_status'] == 'booked' ? 'selected' : '' ?>>Booked</option>
            </select>
        </div>

        <!-- About Us -->
        <div class="col-lg-12 mb-3">
            <label class="form-label">About Us</label>
            <textarea name="about_us" class="form-control" rows="4"><?= $data['about_us'] ?></textarea>
        </div>

        <!-- FMC Token -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Vender FMC Token</label>
            <input type="text" name="vender_fmc_token" class="form-control" value="<?= $data['vender_fmc_token'] ?>">
        </div>

        <!-- Wallet -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Wallet</label>
            <input type="number" name="wallet" class="form-control" value="<?= $data['wallet'] ?>">
        </div>

        <!-- Locality -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Locality</label>
            <input type="text" name="locality" class="form-control" value="<?= $data['locality'] ?>">
        </div>

        <!-- Submit -->
        <div class="col-lg-12 text-end">
            <button type="submit" class="btn btn-primary" name="submit">Update</button>
        </div>
    </div>
</form>

                                </div>
                                <!-- end card-body -->
                            </div>
                            <!-- end card-->
                        </div>
                            <!-- end col -->
            </div> <!-- end row-->


                    
    </div> <!-- container -->


<?php  
include 'include/footer.php'; 
?>
<!-- editor -->
        <script src='https://cdn.ckeditor.com/4.13.0/standard/ckeditor.js'></script>
        <script src='https://code.jquery.com/jquery-3.4.1.min.js'></script>
        <script id="rendered-js" >
            CKEDITOR.replace('editor1');
            CKEDITOR.on('instanceReady', function (evt)
            {
            var editor = evt.editor;

            editor.on('change', function (e) {
                var contentSpace = editor.ui.space('contents');
                var ckeditorFrameCollection = contentSpace.$.getElementsByTagName('iframe');
                var ckeditorFrame = ckeditorFrameCollection[0];
                var innerDoc = ckeditorFrame.contentDocument;
                var innerDocTextAreaHeight = $(innerDoc.body).height();
                console.log(innerDocTextAreaHeight);
            });
            });
            //# sourceURL=pen.js
            
            function checkmobile() {
    jQuery.ajax({
    url: "check_availability.php",
    data:'checkmobilel='+$("#ac_number").val(),
    type: "POST",
    success:function(data){
        $("#check-mobile").html(data);
    },
    error:function (){}
    });
}
        </script>
        
        