<?php

    session_start();

    include "../../db.php";

    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/header2.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 users WHERE uid = '$_GET[edit_id]'");

                    

                    if($edit_row = mysqli_fetch_assoc($edit_run)){



                        $cat_sql = mysqli_query($con, "SELECT * FROM category WHERE cid = '$edit_row[ucategory]'");

                        if($cat_row = mysqli_fetch_assoc($cat_sql)){

                            $cat_name = $cat_row["cname"];

                        } else{

                            $cat_name = "";

                        }



                        $subCat_sql = mysqli_query($con, "SELECT * FROM type_sub_category WHERE id = '$edit_row[u_subCategory]'");

                        if($subCat_row = mysqli_fetch_assoc($subCat_sql)){

                            $subCat_name = $subCat_row["subCatName"];

                        } else{

                            $subCat_name = "";

                        }

                    ?>

                <div class="card mt-3">

                    <h5 class="card-header text-dark  text-center font-weight-bold">Edit Organization Details</h5>

                    <div class="card-body">

                        <form class="user" method="post" enctype="multipart/form-data">

                            <div class="row">

                                <div class="col-md-12">

                                    <label for="Name">Organization Name</label>

                                    <input type="text" class="form-control" name="uname" placeholder="Enter Name"

                                        value="<?php echo $edit_row['uname']; ?>">

                                </div>

                            </div>

                            <div class="row mb-2 mt-2">

                                <div class="col-md-3">

                                    <label for="phoneNo">Phone No</label>

                                    <input type="number" class="form-control" name="ucontact"

                                        placeholder="Enter Phone Number*" value="<?php echo $edit_row['ucontact']; ?>">

                                </div>

                                <div class="col-md-3">

                                    <label for="qualification">Qualification</label>

                                    <input type="text" class="form-control" name="uqualification"

                                        placeholder="Enter Qualification*"

                                        value="<?php echo $edit_row['uqualification']; ?>">

                                </div>

                                <div class="col-md-3 mb-2">

                                    <label for="whatsapp">Whatsapp Number</label>

                                    <input type="number" class="form-control" name="whatsapp"

                                        value="<?php echo $edit_row["uwhatsapp"];?>" required>

                                </div>

                                <div class="col-md-3 mb-2">

                                    <label for="email">Email Id</label>

                                    <input type="email" class="form-control" name="email"

                                        value="<?php echo $edit_row["uemail"];?>" required>

                                </div>

                                <div class="col-md-3">

                                    <label for="category">Category</label>

                                    <select name="category" id="catTypeId" class="form-control mb-2" required>

                                        <option value="<?php echo $edit_row["ucategory"];?>"><?php echo $cat_name;?>

                                        </option>

                                        <?php

                                        $sql = "SELECT * FROM category ORDER BY cname ASC";

                                        $run = mysqli_query($con, $sql);



                                        while ( $cat_rows = mysqli_fetch_assoc($run) ) {

                                            echo '

                                            <option value="'.$cat_rows["cid"].'"> '.$cat_rows["cname"].' </option>';

                                        }

                                        ?>

                                    </select>

                                </div>

                                <div class="col-md-3">

                                    <label for="subCategory">SubCategory</label>

                                    <select name="subCategory" id="subCatTypeId" class="form-control mb-2">

                                        <option value="<?php echo $edit_row["u_subCategory"];?>">

                                            <?php echo $subCat_name;?>

                                        </option>

                                    </select>

                                </div>

                                <div class="col-md-3 mb-2">

                                    <label for="state">State</label>

                                    <input type="hidden" name="state" id="state-id"

                                        value="<?php echo $edit_row["state"];?>">

                                    <?php

                                    $state_sql = mysqli_query($con, "SELECT * FROM state WHERE id = '$edit_row[state]' ");

                                    if($state_row = mysqli_fetch_assoc($state_sql)){

                                        $state_name = $state_row["name"];

                                    }

                                    ?>

                                    <input type="text" list="state" class="form-control" id="state-input"

                                        value="<?php echo $state_name;?>">

                                    <datalist id="state">

                                        <?php 

                                            $sql = "SELECT id, name FROM state ORDER BY id 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-3 mb-2">

                                    <label for="dist">Select District</label>

                                    <input type="hidden" name="dist" id="district-id"

                                        value="<?php echo $edit_row["dist"];?>">

                                    <?php

                                        $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE id = '$edit_row[dist]' ");

                                        if($dist_row = mysqli_fetch_assoc($dist_sql)){

                                            $dist_name = $dist_row["distName"];

                                        }

                                        ?>

                                    <input type="text" list="district-list" class="form-control" id="district-input"

                                        value="<?php echo $dist_row["distName"];?>" required>

                                    <datalist id="district-list"></datalist>

                                </div>

                                <div class="col-md-6">

                                    <label for="address">Edit Address</label>

                                    <input type="text" class="form-control" name="saddress"

                                        placeholder="Enter Your Street Address*"

                                        value="<?php echo $edit_row['saddress']?>">

                                </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['pin']?>">

                                </div>

                                <div class="col-md-3 mb-2">

                                    <label for="logo">Logo <a target="_blank" href="<?php echo $edit_row["ulogo"];?>"><i

                                                class="fa fa-eye"></i> view</a></label>

                                    <input type="file" name="logo" class="form-control" placeholder="Upload logo">

                                    <input type="hidden" name="logoOld" class="form-control"

                                        value="<?php echo $edit_row["ulogo"]?>">

                                </div>

                                <div class="col-md-12 mb-2">

                                    <label for="about">About Organization</label>

                                    <textarea class="form-control ckeditor" name="about" id="content"

                                        rows="3"><?php echo $edit_row["uabout"];?></textarea>

                                </div>

                            </div>

                            <h6 class="badge-dark">Login Details</h6>

                            <div class="row mb-3">

                                <div class="col-md-6">

                                    <label for="loginNumber">Login Number</label>

                                    <input type="text" class="form-control" name="uphone" placeholder="Enter User Name"

                                        value="<?php echo $edit_row['uphone']?>">

                                </div>

                                <div class="col-md-6">

                                    <label for="passWOrd">Password</label>

                                    <input type="password" class="form-control" name="upass"

                                        placeholder="Enter Password" value="<?php echo $edit_row['upass']?>">

                                </div>

                            </div>

                            <button type="submit" class="btn btn-success" name="edit_users">

                                UPDATE

                            </button>



                            <a href="view-admin.php" class="btn btn-warning"> <i class="fas fa-ban"></i>

                                Cancel

                            </a>

                        </form>

                    </div>

                </div>

                <?php }

                } elseif(isset($_GET["add_admin"])){?>

                <div class="card mt-3">

                    <h5 class="card-header text-dark  text-center font-weight-bold">Add Admin</h5>

                    <div class="card-body">

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

                            <div class="row mb-3">

                                <div class="col-md-12">

                                    <label for="organizationName" class="">Organization Name</label>

                                    <input type="text" class="form-control" name="uname" placeholder="Enter Name"

                                        required>

                                </div>

                                <div class="col-md-3 mb-2">

                                    <label for="phoneNo">Phone No</label>

                                    <input type="number" class="form-control" name="ucontact"

                                        placeholder="Enter Phone Number*" required>

                                </div>

                                <div class="col-md-3 mb-2">

                                    <label for="adminDesignation">Admin Designation</label>

                                    <input type="text" class="form-control" name="uqualification"

                                        placeholder="Enter Qualification*" required>

                                </div>

                                <div class="col-md-3 mb-2">

                                    <label for="whatsapp">Whatsapp Number</label>

                                    <input type="number" class="form-control" name="whatsapp"

                                        placeholder="Enter Whatsapp Number*" required>

                                </div>

                                <div class="col-md-3 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-3 mb-2">

                                    <label for="category">Category</label>

                                    <select name="category" id="catTypeId" class="form-control mb-2" required>

                                        <?php

                                        $sql = "SELECT * FROM category ORDER BY cname ASC";

                                        $run = mysqli_query($con, $sql);



                                        while ( $cat_rows = mysqli_fetch_assoc($run) ) {

                                            echo '

                                            <option value="'.$cat_rows["cid"].'"> '.$cat_rows["cname"].' </option>';

                                        }

                                        ?>

                                    </select>

                                </div>

                                <div class="col-md-3 mb-2">

                                    <label for="subCategory">SubCategory</label>

                                    <select name="subCategory" id="subCatTypeId" class="form-control mb-2">

                                        <option value="">Select From The List</option>

                                    </select>

                                </div>

                                <div class="col-md-3 mb-2">

                                    <label for="state">State</label>

                                    <input type="hidden" name="state" id="state-id" value="">

                                    <input type="text" list="state" class="form-control" id="state-input"

                                        placeholder="Select from the list">

                                    <datalist id="state">

                                        <?php 

                                            $sql = "SELECT id, name FROM state ORDER BY id 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-3 mb-2">

                                    <label for="district">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" required>

                                    <datalist id="district-list"></datalist>

                                </div>

                                <div class="col-md-6 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-3 mb-2">

                                    <label for="logo">Logo</label>

                                    <input type="file" name="logo" id="logo" class="form-control"

                                        placeholder="Upload logo">

                                </div>

                                <div class="col-md-12 mb-2">

                                    <label for="about">About Organization</label>

                                    <textarea class="form-control ckeditor" name="about" id="content"

                                        rows="3"></textarea>

                                </div>

                            </div>

                            <div class="row">

                                <div class="col-md-12 mb-2">

                                    <h6 class="badge-dark m-0">Login Details</h6>

                                </div>

                                <div class="col-md-6 mb-2">

                                    <input type="text" class="form-control" name="uphone" placeholder="Enter User Name"

                                        required>

                                </div>

                                <div class="col-md-6 mb-2">

                                    <input type="password" class="form-control" name="upass"

                                        placeholder="Enter Password" required>

                                </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 Organization

                                </button>

                            </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 col-4">

                                <h4 class="text-gray-800">Admin List</h4>

                            </div>

                            <div class="col-md-6 col-8 d-flex justify-content-end">

                                <!--<div>

                                    <a class="btn btn-primary" href="view-admin.php?add_admin"><i

                                            class="fa fa-user-plus"></i> Add

                                        Admin</a>

                                </div>-->

                                <div class="d-flex">

                                    <div style="margin-left:18px">

                                        <div class="form-group">

                                            <input type="text" list="First" class="form-control" id="FirstFilterValue">

                                            <datalist id="First">

                                                <option value="">Search Admin</option>

                                                <?php 

                                                    $sql = "SELECT uname FROM users  WHERE utype = 'Admin' AND udisplay ='disable' ORDER BY uname ASC";

                                                    $res = mysqli_query($con,$sql);

                                                    while ($row = mysqli_fetch_assoc($res)) {

                                                ?>

                                                <option value="<?php echo $row['uname']; ?>">

                                                    <?php echo $row['uname']; ?></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"> 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>

                    </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-school"></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">Qualification : </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="modalqualification"></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() {

        $('#state-input').on('change', function() {

            var state = $(this).val();

            if (state !== '') {

                // Fetch state ID

                $.ajax({

                    url: 'fetch-state-id.php',

                    type: 'POST',

                    data: {

                        state: state

                    },

                    success: function(data) {

                        $('#state-id').val(

                            data); // Set the hidden input field with the fetched state ID

                    },

                    error: function() {

                        alert('Error fetching state ID. Please try again.');

                    }

                });



                // Fetch districts

                $.ajax({

                    url: 'fetch-district.php',

                    type: 'POST',

                    data: {

                        state: state

                    },

                    success: function(data) {

                        $('#district-list').html(

                            data); // Populate the district datalist with the fetched data

                    },

                    error: function() {

                        alert('Error loading districts. Please try again.');

                    }

                });

            } else {

                $('#state-id').val(''); // Clear the hidden input field if no state is selected

                $('#district-list').html(''); // Clear the district list if no state is selected

            }

        });



        $('#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')); // Set the hidden input field with the district ID

            } else {

                $('#district-id').val(''); // Clear the hidden input if the selection is not valid

            }

        });

    });

    </script>



    <script>

    $('#catTypeId').on('change', function() {

        var catTypeId = this.value;

        $.ajax({

            url: "fetch-subCatType-by-catType.php",

            type: "POST",

            data: {

                catTypeId: catTypeId

            },

            cache: false,

            success: function(result) {

                $("#subCatTypeId").html(result);

            }

        });

    });

    </script>



    <script>

    //dropdown TWO filter Start//

    $(document).ready(function() {

        var load_filter_flag = '';

        var FirstFilterValue = '';

        var SecondFilterValue = '';



        $("#filter").click(function() {

            FirstFilterValue = $('#FirstFilterValue').val();

            SecondFilterValue = $('#SecondFilterValue').val();

            load_filter_flag = 0;

            if (FirstFilterValue != '' || SecondFilterValue != '') {

                //alert(name);

                $.ajax({

                    url: 'view-admin-Ajax.php',

                    type: 'POST',

                    data: {

                        FirstFilterValue: FirstFilterValue,

                        SecondFilterValue: SecondFilterValue,

                        start: load_filter_flag

                    },

                    success: function(result) {

                        $('#full-table').hide();

                        $('#filter-table').show();

                        $('#filter-table-data').html(result);

                        load_filter_flag += 10;

                    },

                    complete: function(data) {

                        /* Hide loader container */

                        $("#loader").hide();

                    }

                });

            } else {

                alert('Plz Select an Option');

                $('#full-table').show();

                $('#filter-table').hide();

            }

        });



        function loadMoreFilter(start) {

            console.log('Filter' + start);

            if (load_filter_flag > 0) {

                //alert(name);

                $.ajax({

                    url: 'view-admin-Ajax.php',

                    type: 'POST',

                    data: {

                        FirstFilterValue: FirstFilterValue,

                        SecondFilterValue: SecondFilterValue,

                        start: start

                    },

                    success: function(result) {

                        $('#filter-table-data').append(result);

                        load_filter_flag += 10;

                    }

                });

            }

        }

        // Scroll Loading of Full Data //

        var load_flag = 0;

        loadMore(load_flag);



        function loadMore(start) {

            //console.log('total'+start);

            $.ajax({

                url: 'view-admin-Ajax.php',

                type: 'POST',

                data: 'start=' + start,

                success: function(result) {

                    $('#full-table-data').append(result);

                    load_flag += 10;

                },

                complete: function(data) {

                    /* Hide loader container */

                    $("#loader").hide();

                }

            });

        }



        $('#inner-body').scroll(function() {

            if ($('#inner-body').scrollTop() >= $(document).height() - $('#inner-body').height()) {

                //console.log('call');

                if (load_filter_flag > 0) {

                    loadMoreFilter(load_filter_flag);

                } else {

                    loadMore(load_flag);

                }

            }

        });

    });

    </script>



</body>



</html>





<?php

if (isset($_POST['submit'])) {

    $uname = mysqli_real_escape_string($con, strip_tags($_POST['uname']));

    $ucontact = mysqli_real_escape_string($con, strip_tags($_POST['ucontact']));

    $uqualification = mysqli_real_escape_string($con, strip_tags($_POST['uqualification']));

    $saddress = mysqli_real_escape_string($con, strip_tags($_POST['saddress']));

    $state = mysqli_real_escape_string($con, strip_tags($_POST['state']));

    $dist = mysqli_real_escape_string($con, strip_tags($_POST['dist']));

    $pin = mysqli_real_escape_string($con, strip_tags($_POST['pin']));

    $uphone = mysqli_real_escape_string($con, strip_tags($_POST['uphone']));

    $upass = mysqli_real_escape_string($con, strip_tags($_POST['upass']));

    $category = mysqli_real_escape_string($con, strip_tags($_POST['category']));

    $subCategory = mysqli_real_escape_string($con, strip_tags($_POST['subCategory']));

    $email = mysqli_real_escape_string($con, strip_tags($_POST['email']));

    $whatsapp = mysqli_real_escape_string($con, strip_tags($_POST['whatsapp']));

    $about = $_POST['about'];



    $admin_ref = $_SESSION['uref'];



    // Create user reference code

    $firstTwoDigitMobileNumber = substr($ucontact, 0, 2);

    $lastTwoDigitMobileNumber = substr($ucontact, -2);

    $upperUserStr = strtoupper($uname);

    $firstTwoCharactersOfUser = substr($upperUserStr, 0, 4);

    $ref_code = rand() . $firstTwoCharactersOfUser . time();



    // Check if the phone number already exists

    $chk_ph = mysqli_query($con, "SELECT uphone FROM users WHERE uphone = '$uphone'");

    $chk_ph_count = mysqli_num_rows($chk_ph);

    if ($chk_ph_count < 1) {

        $uref = rand() . '_' . time();



        // Handle file upload

        $logopath = "";

        if (!empty($_FILES["logo"]["name"])) {

            $target_dir = "../upload/logo/";

            $imageFileType = strtolower(pathinfo($_FILES["logo"]["name"], PATHINFO_EXTENSION));

            

            // Generate a unique file name

            $newFileName = uniqid() . '_' . time() . '.' . $imageFileType;

            $target_file = $target_dir . $newFileName;

            $uploadOk = 1;

        

            // Check if image file is an actual image or fake image

            $check = getimagesize($_FILES["logo"]["tmp_name"]);

            if ($check !== false) {

                $uploadOk = 1;

            } else {

                $uploadOk = 0;

            }

        

            // Check file size (5MB maximum)

            if ($_FILES["logo"]["size"] > 5000000) {

                $uploadOk = 0;

            }

        

            // Allow certain file formats

            if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {

                $uploadOk = 0;

            }

        

            // Check if $uploadOk is set to 0 by an error

            if ($uploadOk == 0) {

                echo "<script>alert('Sorry, your file was not uploaded.');</script>";

            } else {

                // if everything is ok, try to upload file

                if (move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)) {

                    $logopath = $target_file;

                } else {

                    echo "<script>alert('Sorry, there was an error uploading your file.');</script>";

                }

            }

        }        



        $ins_sql = "INSERT INTO users (uname, uref_code, ucontact, uqualification, saddress, state, dist, pin, uphone, upass, uref, utype, admin_ref, ucreated, ucategory, u_subCategory, uemail, uwhatsapp, uabout, ulogo) VALUES ('$uname', '$ref_code', '$ucontact', '$uqualification', '$saddress', '$state', '$dist', '$pin', '$uphone', '$upass', '$uref', 'Admin', '$admin_ref', '$currentTime', '$category', '$subCategory', '$email', '$whatsapp', '$about', '$logopath')";



        if (mysqli_query($con, $ins_sql)) {

            echo "<script>window.location = 'view-admin.php';</script>";

        } else {

            echo "<script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = 'view-admin.php?add_admin';

                }, 1200);

            </script>";

        }

    } else {

        echo "<script>

            alert('Mobile Number Exists');

            setTimeout(() => {

                window.location = 'view-admin.php?add_admin';

            }, 500);

        </script>";

    }

}

?>





<?php

if(isset($_POST['edit_users'])){

    $uname = mysqli_real_escape_string($con, strip_tags($_POST['uname']));

    $ucontact = mysqli_real_escape_string($con, strip_tags($_POST['ucontact']));

    $uqualification = mysqli_real_escape_string($con, strip_tags($_POST['uqualification']));

    $saddress = mysqli_real_escape_string($con, strip_tags($_POST['saddress']));

    $state = mysqli_real_escape_string($con, strip_tags($_POST['state']));

    $dist = mysqli_real_escape_string($con, strip_tags($_POST['dist']));

    $pin = mysqli_real_escape_string($con, strip_tags($_POST['pin']));

    $uphone = mysqli_real_escape_string($con, strip_tags($_POST['uphone']));

    $upass = mysqli_real_escape_string($con, strip_tags($_POST['upass']));

    $category = mysqli_real_escape_string($con, strip_tags($_POST['category']));

    $subCategory = mysqli_real_escape_string($con, strip_tags($_POST['subCategory']));

    $email = mysqli_real_escape_string($con, strip_tags($_POST['email']));

    $whatsapp = mysqli_real_escape_string($con, strip_tags($_POST['whatsapp']));

    $about = $_POST['about'];



    // Handle file upload

    $logopath = "";

    if (!empty($_FILES["logo"]["name"])) {

        $target_dir = "../upload/logo/";

        $imageFileType = strtolower(pathinfo($_FILES["logo"]["name"], PATHINFO_EXTENSION));

        

        // Generate a unique file name

        $newFileName = uniqid() . '_' . time() . '.' . $imageFileType;

        $target_file = $target_dir . $newFileName;

        $uploadOk = 1;

    

        // Check if image file is an actual image or fake image

        $check = getimagesize($_FILES["logo"]["tmp_name"]);

        if ($check !== false) {

            $uploadOk = 1;

        } else {

            $uploadOk = 0;

        }

    

        // Check file size (5MB maximum)

        if ($_FILES["logo"]["size"] > 5000000) {

            $uploadOk = 0;

        }

    

        // Allow certain file formats

        if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {

            $uploadOk = 0;

        }

    

        // Check if $uploadOk is set to 0 by an error

        if ($uploadOk == 0) {

            echo "<script>alert('Sorry, your file was not uploaded.');</script>";

        } else {

            // if everything is ok, try to upload file

            if (move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)) {

                unlink("$_POST[logoOld]");

                $logopath = $target_file;

            } else {

                echo "<script>alert('Sorry, there was an error uploading your file.');</script>";

            }

        }

    }  else{

        $logopath = $_POST["logoOld"];

    }



    $up_sql = "UPDATE users SET uname = '$uname', ucontact = '$ucontact', uqualification = '$uqualification',  saddress = '$saddress', state = '$state', dist = '$dist', pin = '$pin',  uphone = '$uphone', upass = '$upass', ucreated = '$currentTime', ucategory = '$category', u_subCategory = '$subCategory', uemail = '$email', uwhatsapp = '$whatsapp', uabout = '$about', ulogo = '$logopath' WHERE uid = '$_GET[edit_id]'";



    if(mysqli_query($con, $up_sql)){

        ?>

<script>

alert('Successfully Updated');

window.location = 'view-admin.php';

</script>

<?php

    }

    else{

        ?>

<script>

alert('OOPS !!!!');

setTimeout(() => {

    window.location = 'view-admin.php?edit_id=<?php echo $_GET["edit_id"]; ?>';

}, 1200);

</script>

<?php

    }

} 

?>



<?php

if ( isset($_GET["del_id"]) ) {

$disable_sql = "UPDATE users SET udisplay='' WHERE uid = '$_GET[del_id]'";

    if ( mysqli_query($con, $disable_sql) ) {

        ?>

<script>

window.location = "view-admin.php";

</script>

<?php

    } else {

        ?>

<script>

alert('OOPS !!!!');

setTimeout(() => {

    window.location = 'view-admin.php';

}, 1200);

</script>

<?php

    }

}

?>