<?php
    // session_start();
    include "../db.php";
    require '../auth.php';
    restrictAccess(['master_admin']);
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('M j, Y g:i a');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {
        header("Location: logout.php");
        exit();
    }

    if ( $_SESSION['utype'] != 'master_admin' && $_SESSION['uref'] != 'fssdf4543frts' ) {
        header("Location: logout.php");
        exit();
    }
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "include/meta.php"; ?>
    <?php include "include/css.php"; ?>
    <?php include "include/php-functions.php"; ?>
    <script src="https://cdn.ckeditor.com/4.6.2/standard-all/ckeditor.js"></script>
</head>

<body>
    <!--  Header -->
    <?php include "include/header.php"; ?>
    <!-- #header -->

    <section id="inner-body">
        <div class="body-content">
            <div class="container-fluid">
                <?php if(isset($_GET["edit_id"])){
                    $edit_id = $_GET["edit_id"];
                    $edit_run = mysqli_query($con, "SELECT * FROM work_place WHERE wp_id = '$_GET[edit_id]'");
                    
                    if($edit_row = mysqli_fetch_assoc($edit_run)){
                        $country_sql = mysqli_query($con, "SELECT * FROM countries WHERE id = '$edit_row[wp_country_id]' ");
                        if($country_row = mysqli_fetch_assoc($country_sql)){
                            $country_name = $country_row["name"];
                        }
                        $state_sql = mysqli_query($con, "SELECT * FROM state WHERE id = '$edit_row[wp_state_id]' ");
                        if($state_row = mysqli_fetch_assoc($state_sql)){
                            $state_name = $state_row["name"];
                        }
                        $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE id = '$edit_row[wp_dist_id]' ");
                        if($dist_row = mysqli_fetch_assoc($dist_sql)){
                            $dist_name = $dist_row["distName"];
                        }
                    ?>
                <div class="card mt-3">
                    <h5 class="card-header text-dark  text-center font-weight-bold">Edit Vendor/Hotel Details</h5>
                    <div class="card-body">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <div class="row mb-2 mt-2">
                                <div class="col-md-4 mb-2">
                                    <label for="Name">Vendor/Hotel Name</label>
                                    <input type="text" class="form-control" name="uname"
                                        pattern="^[A-Za-z]+(?:\s[A-Za-z]+)*$"
                                        title="Only alphabets and space between two word are allowed, and extra space not allowed."
                                        value="<?php echo $edit_row['wp_name']; ?>">
                                </div>
                                <div class="col-md-4">
                                    <label for="phoneNo">Phone No</label>
                                    <input type="number" class="form-control" name="ucontact"
                                        placeholder="Enter Phone Number*" value="<?php echo $edit_row['wp_contact']; ?>"
                                        minlength="10" maxlength="10" oninput="this.value=this.value.slice(0,10)">
                                </div>
                                <div class="col-md-4 mb-2">
                                    <label for="whatsapp">Whatsapp Number</label>
                                    <input type="number" class="form-control" name="whatsapp"
                                        value="<?php echo $edit_row["wp_whatsapp"];?>" required minlength="10"
                                        maxlength="10" oninput="this.value=this.value.slice(0,10)">
                                </div>
                                <div class="col-md-4 mb-2">
                                    <label for="email">Email Id</label>
                                    <input type="email" class="form-control" name="email"
                                        value="<?php echo $edit_row["wp_email"];?>" required>
                                </div>
                                <div class="col-md-4 mb-2">
                                    <label for="country">Edit Country:</label>
                                    <input type="hidden" name="country" id="country-id"
                                        value="<?php echo $edit_row["wp_country_id"];?>">
                                    <input type="text" list="country-list" class="form-control" id="country-input"
                                        value="<?php echo $country_name;?>">
                                    <datalist id="country-list">
                                        <?php 
                                            $sql = "SELECT id, name FROM countries WHERE name = 'India' ORDER BY name ASC";
                                            $res = mysqli_query($con, $sql);
                                            while ($row = mysqli_fetch_assoc($res)) {
                                        ?>
                                        <option data-id="<?php echo $row['id']; ?>" value="<?php echo $row['name']; ?>">
                                            <?php echo $row['name']; ?>
                                        </option>
                                        <?php } ?>
                                    </datalist>
                                </div>

                                <div class="col-md-4 mb-2">
                                    <label for="state">Edit State:</label>
                                    <input type="hidden" name="state" id="state-id"
                                        value="<?php echo $edit_row["wp_state_id"];?>">
                                    <input type="text" list="state-list" class="form-control" id="state-input"
                                        value="<?php echo $state_name;?>">
                                    <datalist id="state-list">
                                        <?php
                                            if (!empty($edit_row["wp_state_id"])) {
                                                echo "<option data-id='{$edit_row["wp_state_id"]}' value='{$state_name}'>{$state_name}</option>";
                                            }
                                        ?>
                                    </datalist>
                                </div>

                                <div class="col-md-4 mb-2">
                                    <label for="dist">Edit District:</label>
                                    <input type="hidden" name="dist" id="district-id"
                                        value="<?php echo $edit_row["wp_dist_id"];?>">
                                    <input type="text" list="district-list" class="form-control" id="district-input"
                                        value="<?php echo $dist_name;?>" required>
                                    <datalist id="district-list">
                                        <?php
                                            if (!empty($edit_row["wp_dist_id"])) {
                                                echo "<option data-id='{$edit_row["wp_dist_id"]}' value='{$dist_name}'>{$dist_name}</option>";
                                            }
                                        ?>
                                    </datalist>
                                </div>
                                <div class="col-md-5">
                                    <label for="address">Edit Address</label>
                                    <input type="text" class="form-control" name="saddress"
                                        placeholder="Enter Your Street Address*"
                                        value="<?php echo $edit_row['wp_address']?>">
                                </div>
                                <div class="col-md-3">
                                    <label for="pinCode">Edit Pin Code</label>
                                    <input type="number" class="form-control" name="pin" placeholder="Pin*"
                                        value="<?php echo $edit_row['wp_pin']?>">
                                </div>
                                <div class="col-md-12 mb-2">
                                    <label for="about">About Vendor/Hotel</label>
                                    <textarea class="form-control ckeditor" name="about" id="content"
                                        rows="3"><?php echo $edit_row["wp_about"];?></textarea>
                                </div>
                            </div>
                            <button type="submit" class="btn btn-success" name="edit_users">
                                UPDATE
                            </button>

                            <a href="view-vendor.php" class="btn btn-warning text-white"> <i
                                    class="fa fa-arrow-left"></i>
                                Back
                            </a>
                        </form>
                    </div>
                </div>
                <?php }
                } elseif(isset($_GET["add_vendor"])){?>
                <div class="card mt-3">
                    <h5 class="card-header text-dark  text-center font-weight-bold">Add Vendor/Hotel</h5>
                    <div class="card-body">
                        <form method="POST" enctype="multipart/form-data">
                            <div class="row mb-3">
                                <div class="col-md-4 mb-2">
                                    <label for="uName" class="">Vendor/Hotel Name</label>
                                    <input type="text" class="form-control" name="uname"
                                        pattern="^[A-Za-z]+(?:\s[A-Za-z]+)*$"
                                        title="Only alphabets and space between two word are allowed, and extra space not allowed."
                                        placeholder="Enter Name" required>
                                </div>
                                <div class="col-md-4 mb-2">
                                    <label for="phoneNo">Phone No</label>
                                    <input type="number" class="form-control" name="ucontact" id="ucontact"
                                        placeholder="Enter Your phone Number" oninput="validateWhatsAppNumber()"
                                        required>
                                    <small id="ucontact-error" style="color: red; display: none;">Please enter a valid
                                        10-digit number.</small>

                                    <script>
                                    function validateWhatsAppNumber() {
                                        const input = document.getElementById('ucontact');
                                        const error = document.getElementById('ucontact-error');

                                        // Convert input value to string for consistent length checking
                                        const number = input.value.toString();

                                        if (number.length === 10 && /^[0-9]+$/.test(number)) {
                                            error.style.display = 'none'; // Hide error if valid
                                        } else {
                                            error.style.display = 'block'; // Show error if invalid
                                        }
                                    }
                                    </script>
                                </div>
                                <div class="col-md-4 mb-2">
                                    <label for="whatsapp">Whatsapp Number</label>
                                    <input type="number" class="form-control" name="whatsapp"
                                        placeholder="Enter Whatsapp Number*" required minlength="10" maxlength="10"
                                        oninput="this.value=this.value.slice(0,10)">
                                </div>
                                <div class="col-md-4 mb-2">
                                    <label for="email">Email Id</label>
                                    <input type="email" class="form-control" name="email" placeholder="Enter Email Id*"
                                        required>
                                </div>
                                <div class="col-md-4 mb-2">
                                    <label for="country">Country</label>
                                    <input type="hidden" name="country" id="country-id" value="">
                                    <input type="text" list="country-list" class="form-control" id="country-input"
                                        placeholder="Select from the list" autocomplete="new-password">
                                    <datalist id="country-list">
                                        <?php 
                                            $sql = "SELECT id, name FROM countries WHERE name = 'India' ORDER BY name ASC";
                                            $res = mysqli_query($con, $sql);
                                            while ($row = mysqli_fetch_assoc($res)) {
                                        ?>
                                        <option data-id="<?php echo $row['id']; ?>" value="<?php echo $row['name']; ?>">
                                            <?php echo $row['name']; ?>
                                        </option>
                                        <?php } ?>
                                    </datalist>
                                </div>

                                <div class="col-md-4 mb-2">
                                    <label for="state">State</label>
                                    <input type="hidden" name="state" id="state-id" value="">
                                    <input type="text" list="state-list" class="form-control" id="state-input"
                                        placeholder="Select from the list" autocomplete="new-password">
                                    <datalist id="state-list"></datalist>
                                </div>

                                <div class="col-md-4 mb-2">
                                    <label for="dist">District</label>
                                    <input type="hidden" name="dist" id="district-id" value="">
                                    <input type="text" list="district-list" class="form-control" id="district-input"
                                        placeholder="Select from the list" autocomplete="new-password">
                                    <datalist id="district-list"></datalist>
                                </div>
                                <div class="col-md-5 mb-2">
                                    <label for="address">Address</label>
                                    <input type="text" class="form-control" name="saddress"
                                        placeholder="Enter Your Street Address*" required>
                                </div>
                                <div class="col-md-3 mb-2">
                                    <label for="pinCode">Pin Code</label>
                                    <input type="number" class="form-control" name="pin" placeholder="Pin*" required>
                                </div>
                                <div class="col-md-12 mb-2">
                                    <label for="about">About Vendor/Hotel</label>
                                    <textarea class="form-control ckeditor" name="about" id="content"
                                        rows="3"></textarea>
                                </div>
                            </div>
                            <div class="">
                                <button type="submit" class="btn btn-success text-sm-center mt-4" name="submit">
                                    <i class="fa fa-plus"></i> Add Vendor/Hotel
                                </button>
                                <a href="view-vendor.php" class="btn btn-warning btn-user mt-4 text-white"> <i
                                        class="fa fa-arrow-left"></i> Back </a>
                            </div>
                        </form>
                    </div>
                </div>
                <?php } else{?>
                <div class="card mt-3 mb-3">
                    <div class="card-header">
                        <div class="row align-items-center">
                            <div class="col-md-6">
                                <div class="d-flex justify-content-between">
                                    <h4 class="text-gray-800 m-0">Vendor/Hotel List</h4>
                                    <a class="btn btn-primary btn-sm" href="view-vendor.php?add_vendor"><i
                                            class="fa fa-hotel"></i> Add
                                        Vendor</a>
                                </div>
                            </div>
                            <div class="col-md-6 mt-1">
                                <div class="d-flex">
                                    <div class="w-100">
                                        <div class="form-group">
                                            <input type="text" list="First" class="form-control form-control-sm"
                                                id="FirstFilterValue" placeholder="Search By Vendor/Hotel Name">
                                            <datalist id="First">
                                                <?php 
                                                    $sql = "SELECT wp_name FROM work_place WHERE wp_recycle != '0' ORDER BY wp_name ASC";
                                                    $res = mysqli_query($con,$sql);
                                                    while ($row = mysqli_fetch_assoc($res)) {
                                                ?>
                                                <option value="<?php echo $row['wp_name']; ?>">
                                                    <?php echo $row['wp_name']; ?></option>
                                                <?php } ?>
                                            </datalist>
                                        </div>
                                        <input type="hidden" class="form-control" value="" id="SecondFilterValue">
                                    </div>
                                    <div class="#">
                                        <button type="button" id="filter" class="btn btn-primary btn-sm"> Search
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <!--loader -->
                            <div class="col-12" id='loader'>
                                <ul style="list-style: none; padding-left: 0;">
                                    <?php
                                    for ($count = 0; $count < 4; $count++) {
                                        echo '<li>
                                        <div class="ph-item">
                                        <div cl ass="ph-test">
                                        <div class="ph-row">
                                        <div class="ph-col-12 big"></div>
                                        <div class="ph-col-12"></div>
                                        <div class="ph-col-12"></div>
                                        <div class="ph-col-12"></div>
                                        <div class="ph-col-12"></div>
                                        <div class="ph-col-12"></div>
                                        </div>
                                        </div>
                                        </div>
                                        </li>';
                                        }
                                    ?>
                                </ul>
                            </div>

                            <!-- loader -->
                            <div class="col-12" id="full-table">
                                <ul class="timeline" id="full-table-data" style="list-style: none; padding: 0;">
                                    </>
                                </ul>
                            </div>

                            <div class="col-12" id="filter-table" style="display: none;">
                                <ul class="timeline" id="filter-table-data" style="list-style: none; padding: 0;">
                                    </>
                                </ul>
                            </div>
                            <div class="text-center mt-3" id="load-more-container">
                                <button id="loadMoreBtn" class="btn btn-info btn-sm text-white">Load More</button>
                                <div id="endMessage" class="text-muted mt-2" style="display: none;">End of result</div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php }?>
            </div>
        </div>

        <?php include "include/footer.php"; ?>
    </section>

    <!-- JavaScript Libraries -->

    <?php include "include/js.php"; ?>

    <!-- Modal Structure -->
    <div class="modal fade" id="adminDetailsModal" data-bs-backdrop="static" tabindex="-1" role="dialog"
        aria-labelledby="modelTitleId" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 style="color:orange;font-size: 20px"> <i class="fa fa-user"></i> <span id="modalName"></span>
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6 col-6 text-end">
                            <h5 class="modal-css">User ID : </h5>
                            <h5 class="modal-css">Password : </h5>
                            <h5 class="modal-css">Address : </h5>
                            <h5 class="modal-css">State : </h5>
                            <h5 class="modal-css">Dist : </h5>
                            <h5 class="modal-css">Pin : </h5>
                            <h5 class="modal-css">Date : </h5>
                        </div>
                        <div class="col-md-6 col-6">
                            <h5 class="modal-id" id="modalId"></h5>
                            <h5 class="modal-id" id="modalPass"></h5>
                            <h5 class="modal-css" id="modalAddress"></h5>
                            <h5 class="modal-css" id="modalState"></h5>
                            <h5 class="modal-css" id="modalDist"></h5>
                            <h5 class="modal-css" id="modalPin"></h5>
                            <h5 class="modal-css" id="modalDate"></h5>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    <script>
    CKEDITOR.replace('content', {
        height: 300,
        filebrowserUploadUrl: "upload.php"
    });
    </script>

    <script>
    $(document).ready(function() {
        // Country selection event
        $('#country-input').on('change', function() {
            var countryName = $(this).val();
            var countryOption = $('#country-list option').filter(function() {
                return this.value === countryName;
            });

            if (countryOption.length > 0) {
                var countryId = countryOption.data('id');
                $('#country-id').val(countryId);

                // Fetch states based on country ID
                $.ajax({
                    url: 'fetch-state.php',
                    type: 'POST',
                    data: {
                        country_id: countryId
                    },
                    success: function(data) {
                        $('#state-list').html(data);
                        $('#state-id').val('');
                        $('#state-input').val('');
                        $('#district-list').html('');
                        $('#district-id').val('');
                        $('#district-input').val('');
                    },
                    error: function() {
                        alert('Error loading states. Please try again.');
                    }
                });
            } else {
                $('#country-id').val('');
                $('#state-list').html('');
                $('#district-list').html('');
            }
        });

        // State selection event
        $('#state-input').on('change', function() {
            var stateName = $(this).val();
            var stateOption = $('#state-list option').filter(function() {
                return this.value === stateName;
            });

            if (stateOption.length > 0) {
                var stateId = stateOption.data('id');
                $('#state-id').val(stateId);

                // Fetch districts based on state ID
                $.ajax({
                    url: 'fetch-district.php',
                    type: 'POST',
                    data: {
                        state_id: stateId
                    },
                    success: function(data) {
                        $('#district-list').html(data);
                        $('#district-id').val('');
                        $('#district-input').val('');
                    },
                    error: function() {
                        alert('Error loading districts. Please try again.');
                    }
                });
            } else {
                $('#state-id').val('');
                $('#district-list').html('');
            }
        });

        // District selection event
        $('#district-input').on('change', function() {
            var districtName = $(this).val();
            var districtOption = $('#district-list option').filter(function() {
                return this.value === districtName;
            });

            if (districtOption.length > 0) {
                $('#district-id').val(districtOption.data('id'));
            } else {
                $('#district-id').val('');
            }
        });
    });
    </script>

    <script>
    $(document).ready(function() {
        var load_filter_flag = 0;
        var load_flag = 0;

        var FirstFilterValue = '';
        var SecondFilterValue = '';

        // Initial unfiltered data load
        loadMore(load_flag);

        // Filter Button Click
        $("#filter").click(function() {
            FirstFilterValue = $('#FirstFilterValue').val();
            SecondFilterValue = $('#SecondFilterValue').val();

            load_filter_flag = 0;
            load_flag = 0;

            $('#loadMoreBtn').show();
            $('#endMessage').hide();

            if (FirstFilterValue != '' || SecondFilterValue != '') {
                $.ajax({
                    url: 'view-vendor-Ajax.php',
                    type: 'POST',
                    data: {
                        FirstFilterValue: FirstFilterValue,
                        SecondFilterValue: SecondFilterValue,
                        start: load_filter_flag
                    },
                    beforeSend: function() {
                        $("#loader").show();
                        $("#loadMoreBtn").hide();
                    },
                    success: function(result) {
                        $('#full-table').hide();
                        $('#filter-table').show();
                        $('#filter-table-data').html(result);

                        const newItems = $('<div>').html(result).find('li');
                        load_filter_flag += newItems.length;

                        if (newItems.length < 10) {
                            $('#loadMoreBtn').hide();
                            $('#endMessage').show();
                        } else {
                            $('#loadMoreBtn').show();
                            $('#endMessage').hide();
                        }
                    },
                    complete: function() {
                        $("#loader").hide();
                    }
                });
            } else {
                alert('Please select at least one filter option.');
                $('#full-table').show();
                $('#filter-table').hide();
            }
        });

        // Load More Button Click
        $('#loadMoreBtn').click(function() {
            if ($('#filter-table').is(':visible')) {
                loadMoreFilter(load_filter_flag);
            } else {
                loadMore(load_flag);
            }
        });

        // Load more for filtered data
        function loadMoreFilter(start) {
            $.ajax({
                url: 'view-vendor-Ajax.php',
                type: 'POST',
                data: {
                    FirstFilterValue: FirstFilterValue,
                    SecondFilterValue: SecondFilterValue,
                    start: start
                },
                beforeSend: function() {
                    $("#loader").show();
                    $("#loadMoreBtn").hide();
                },
                success: function(result) {
                    $('#filter-table-data').append(result);
                    const newItems = $('<div>').html(result).find('li');
                    load_filter_flag += newItems.length;

                    if (newItems.length < 10) {
                        $('#loadMoreBtn').hide();
                        $('#endMessage').show();
                    } else {
                        $('#loadMoreBtn').show();
                        $('#endMessage').hide();
                    }
                },
                complete: function() {
                    $("#loader").hide();
                }
            });
        }

        // Load more for unfiltered data
        function loadMore(start) {
            $.ajax({
                url: 'view-vendor-Ajax.php',
                type: 'POST',
                data: {
                    start: start
                },
                beforeSend: function() {
                    $("#loader").show();
                    $("#loadMoreBtn").hide();
                },
                success: function(result) {
                    $('#full-table-data').append(result);
                    const newItems = $('<div>').html(result).find('li');
                    load_flag += newItems.length;

                    if (newItems.length < 10) {
                        $('#loadMoreBtn').hide();
                        $('#endMessage').show();
                    } else {
                        $('#loadMoreBtn').show();
                        $('#endMessage').hide();
                    }
                },
                complete: function() {
                    $("#loader").hide();
                }
            });
        }
    });
    </script>


</body>

</html>

<?php
// Referral code generator
function generateReferralCode($con) {
    do {
        $letters = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, 2);
        $numbers = substr(str_shuffle('0123456789'), 0, 4);
        $referralCode = str_shuffle($letters . $numbers);

        // Ensure unique referral code
        $query = "SELECT COUNT(*) AS count FROM work_place WHERE wp_refcode = '" . mysqli_real_escape_string($con, $referralCode) . "'";
        $result = mysqli_query($con, $query);
        $row = mysqli_fetch_assoc($result);
    } while ($row['count'] > 0);

    return $referralCode;
}

if (isset($_POST['submit'])) {
    $wp_name     = mysqli_real_escape_string($con, $_POST['uname']);
    $wp_contact  = mysqli_real_escape_string($con, $_POST['ucontact']);
    $wp_whatsapp = mysqli_real_escape_string($con, $_POST['whatsapp']);
    $wp_email    = mysqli_real_escape_string($con, $_POST['email']);
    $wp_country  = mysqli_real_escape_string($con, $_POST['country']);
    $wp_state    = mysqli_real_escape_string($con, $_POST['state']);
    $wp_dist     = mysqli_real_escape_string($con, $_POST['dist']);
    $wp_address  = mysqli_real_escape_string($con, $_POST['saddress']);
    $wp_pin      = mysqli_real_escape_string($con, $_POST['pin']);
    $wp_about    = $_POST['about'];

    // Defaults
    $wp_password = "1111";  
    $wp_refcode  = generateReferralCode($con);
    $wp_cDate    = $currentTime;  
    $wp_recycle  = 1;

    // Insert query
    $sql = "INSERT INTO work_place 
            (wp_name, wp_contact, wp_whatsapp, wp_email, wp_country_id, wp_state_id, wp_dist_id, wp_address, wp_pin, wp_about, wp_password, wp_refcode, wp_recycle, wp_cDate)
            VALUES 
            ('$wp_name', '$wp_contact', '$wp_whatsapp', '$wp_email', '$wp_country', '$wp_state', '$wp_dist', '$wp_address', '$wp_pin', '$wp_about', '$wp_password', '$wp_refcode', '$wp_recycle', '$wp_cDate')";

    if (mysqli_query($con, $sql)) {
        echo "<script>alert('Work Place Added Successfully!'); window.location='view-vendor.php';</script>";
    } else {
        echo "Error: " . mysqli_error($con);
    }
}
?>


<?php
if(isset($_POST['edit_users'])){
    $wp_id = $_GET["edit_id"];
    $wp_name      = mysqli_real_escape_string($con, $_POST['uname']);
    $wp_contact   = mysqli_real_escape_string($con, $_POST['ucontact']);
    $wp_whatsapp  = mysqli_real_escape_string($con, $_POST['whatsapp']);
    $wp_email     = mysqli_real_escape_string($con, $_POST['email']);
    $wp_country   = intval($_POST['country']);
    $wp_state     = intval($_POST['state']);
    $wp_dist      = intval($_POST['dist']);
    $wp_address   = mysqli_real_escape_string($con, $_POST['saddress']);
    $wp_pin       = mysqli_real_escape_string($con, $_POST['pin']);
    $wp_about     = $_POST['about'];

    // ensure country, state, dist are valid IDs (if needed)
    if ($wp_country <= 0) { $wp_country = NULL; }
    if ($wp_state <= 0) { $wp_state = NULL; }
    if ($wp_dist <= 0) { $wp_dist = NULL; }

    // SQL update query
    $sql = "UPDATE work_place 
            SET 
                wp_name        = '$wp_name',
                wp_contact     = '$wp_contact',
                wp_whatsapp    = '$wp_whatsapp',
                wp_email       = '$wp_email',
                wp_country_id  = " . ($wp_country !== NULL ? "'$wp_country'" : "NULL") . ",
                wp_state_id    = " . ($wp_state !== NULL ? "'$wp_state'" : "NULL") . ",
                wp_dist_id     = " . ($wp_dist !== NULL ? "'$wp_dist'" : "NULL") . ",
                wp_address     = '$wp_address',
                wp_pin         = '$wp_pin',
                wp_about       = '$wp_about'
            WHERE wp_id = '$wp_id'";

    if (mysqli_query($con, $sql)) {
        echo "<script>alert('Work Place updated successfully!');window.location.href='view-vendor.php';</script>";
    } else {
        echo "Error updating record: " . mysqli_error($con);
    }
}
?>

<?php
if ( isset($_GET["del_id"]) ) {
$disable_sql = "UPDATE work_place SET wp_recycle = '0' WHERE wp_id = '$_GET[del_id]'";

    if ( mysqli_query($con, $disable_sql) ) {
        ?>
<script>
window.location = "view-vendor.php";
</script>
<?php
    } else {
        ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'view-vendor.php';
}, 1200);
</script>
<?php
    }
}
?>