<?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

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'];
    $date_time = date("Y-m-d H:i:s");

    // Image upload
    $image_name = $_FILES['image']['name'];
    $tmp_name = $_FILES['image']['tmp_name'];
    $upload_path = "/upload/" . $image_name;
    move_uploaded_file($tmp_name, $upload_path);

    // Insert into DB
    $sql = "INSERT INTO add_vehicle (vehicle_type, vehicle_brand, vehicle_model, share_location, registration_no, alternate_no, apartment, block, user_id, date_time, type, trakername, image)
            VALUES ('$vehicle_type', '$vehicle_brand', '$vehicle_model', '$share_location', '$registration_no', '$alternate_no', '$apartment', '$block', '$user_id', '$date_time', '$type', '$trakername', '$image_name')";

    if (mysqli_query($conn, $sql)) {
        echo "Vehicle added successfully!";
        // header("Location: vehicle_list.php"); // Redirect to list page
        // exit();
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}
?>

                                                                                        
                                                    <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" 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" 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" 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" 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" 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" class="form-control">
        </div>

        <!-- Apartment -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Apartment</label>
            <input type="text" name="apartment" class="form-control">
        </div>

        <!-- Block -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Block</label>
            <input type="text" name="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" class="form-control" required>
        </div>

        <!-- Type -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Type</label>
            <input type="text" name="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" class="form-control">
        </div>

        <!-- Vehicle Image -->
        <div class="col-lg-6 mb-3">
            <label class="form-label">Vehicle Image</label>
            <input type="file" name="image" accept="image/*" class="form-control" required>
        </div>

        <!-- Submit -->
        <div class="col-12 mt-3">
            <button type="submit" name="submit" class="btn btn-primary">Add 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'; 
?>