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

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

<?php
if(isset($_GET['pend'])){
    $idddd = $_GET['pend'];
    $status = "done";
    
    $update = mysqli_query($conn,"UPDATE `vender` SET `status`='$status' WHERE `id`='$idddd'");
    if($update){
        echo "<script>document.location.href='vender.php'</script>";
    }
}

if(isset($_GET['done'])){
    $idd = $_GET['done'];
    $status = "pending";
    
    $updatee = mysqli_query($conn,"UPDATE `vender` SET `status`='$status' WHERE `id`='$idd'");
    if($updatee){
        echo "<script>document.location.href='vender.php'</script>";
    }
}
?>

                                        <?php
                                        if(isset($_GET['user'])){
                                            $id = $_GET['user'];
                                        
                                            $sql = "DELETE FROM `verder` WHERE id='$id'";
                                           // print_r($sql);
                                            if(mysqli_query($conn,$sql)){
                                             echo "<script>alert('Your Data deleted successfully');</script>";
                                            // echo "<script>document.location('user.php');</script>";
                                            echo "<script>window.location.href='user.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">Vender List</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Vender List</h4>
                                </div>
                            </div>
                        </div>
                        <!-- end page title --> 
                          <!--<div>-->
                              <!--<a href="add_vender.php"  class="btn btn-success"> Add Vender</a>-->
                          <!--</div>-->

                        <div class="row">
                            <div class="col-12">
                                <div class="card">
                                    <div class="card-body">
                                        
                                        <?php
                                        if(isset($_GET['id'])){
                                            $id = $_GET['id'];
                                        
                                            $sql = "DELETE FROM `vender` WHERE id='$id'";
                                           // print_r($sql);
                                            if(mysqli_query($conn,$sql)){

                                            echo "<script>document.location('vender.php');</script>";
                                            }
                                           
                                        }
                                        ?>
                                        <div class="table-responsive">
                                            <div class="tab-pane show active" id="basic-datatable-preview">
                                                <table id="datatable" class="table table-striped dt-responsive nowrap w-100">
                                                    
                                            <?php


// Get vehicle ID from URL
$id = $_GET['user'] ?? 0;

// Fetch existing data
$sql = "SELECT * FROM add_vehicle WHERE id = '$id'";
$result = mysqli_query($conn, $sql);
$data = mysqli_fetch_assoc($result);

// Update logic
if (isset($_POST['submit'])) {
    $vehicle_type = $_POST['vehicle_type'];
    $vehicle_brand = $_POST['vehicle_brand'];
    $vehicle_model = $_POST['vehicle_model'];
    $share_location = $_POST['share_location'];
    $registration_no = $_POST['registration_no'];
    $alternate_no = $_POST['alternate_no'];
    $apartment = $_POST['apartment'];
    $block = $_POST['block'];
    $user_id = $_POST['user_id'];
    $type = $_POST['type'];
    $trakername = $_POST['trakername'];

    // Image handling
    $image_name = $data['image']; // Default to existing image
    if (!empty($_FILES['image']['name'])) {
        $image_name = $_FILES['image']['name'];
        $tmp_name = $_FILES['image']['tmp_name'];
        move_uploaded_file($tmp_name, "/upload/" . $image_name);
    }

    // Update query
    $update = "UPDATE add_vehicle SET 
        vehicle_type = '$vehicle_type',
        vehicle_brand = '$vehicle_brand',
        vehicle_model = '$vehicle_model',
        share_location = '$share_location',
        registration_no = '$registration_no',
        alternate_no = '$alternate_no',
        apartment = '$apartment',
        block = '$block',
        user_id = '$user_id',
        type = '$type',
        trakername = '$trakername',
        image = '$image_name'
        WHERE id = '$id'";

    if (mysqli_query($conn, $update)) {
        echo "Vehicle updated successfully!";
        // header("Location: vehicle_list.php");
        // exit();
    } else {
        echo "Update failed: " . mysqli_error($conn);
    }
}
?>

<!-- Form -->
<form action="" method="POST" enctype="multipart/form-data">
    <div class="row">
        <!-- Vehicle Type -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Vehicle Type</label>
            <input type="text" name="vehicle_type" value="<?= $data['vehicle_type'] ?>" class="form-control" required>
        </div>

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

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

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

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

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

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

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

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

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

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

        <!-- Image Upload -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Vehicle Image</label><br>
            <?php if ($data['image']): ?>
                <img src="../upload/<?= $data['image'] ?>" width="100" style="margin-bottom:10px;">
            <?php endif; ?>
            <input type="file" name="image" class="form-control">
        </div>

        <!-- Submit -->
        <div class="col-12 mt-3">
            <button type="submit" name="submit" class="btn btn-primary">Update Vehicle</button>
        </div>
    </div>
</form>

                               
                                            </div> <!-- end preview-->
                                        
                                            
                                        </div> <!-- end tab-content-->

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

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


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