<?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>
<style>
#ck-img-preview img {
    max-width: 100%;
    height: auto;
}
</style>

<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 notice WHERE admin_ref = '$_SESSION[uref]' AND notice_id = '$_GET[edit_id]'");
                    
                    if($edit_row = mysqli_fetch_assoc($edit_run)){
                        $country_sql = mysqli_query($con, "SELECT * FROM countries WHERE id = '$edit_row[notice_country]' ");
                        if($country_row = mysqli_fetch_assoc($country_sql)){
                            $country_name = $country_row["name"];
                            $country_id = $edit_row["notice_country"];
                        }else{
                            $country_name = "";
                            $country_id = "";
                        }
                        
                        $state_sql = mysqli_query($con, "SELECT * FROM state WHERE id = '$edit_row[notice_state]' ");
                        if($state_row = mysqli_fetch_assoc($state_sql)){
                            $state_name = $state_row["name"];
                            $state_id = $edit_row["notice_state"];
                        }else{
                            $state_name = "";
                            $state_id = "";
                        }

                        $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE id = '$edit_row[notice_dist]' ");
                        if($dist_row = mysqli_fetch_assoc($dist_sql)){
                            $dist_name = $dist_row["distName"];
                            $dist_id = $edit_row["notice_dist"];
                        }else{
                            $dist_name = "";
                            $dist_id = "";
                        }
                    ?>
                <div class="card mt-3">
                    <h5 class="card-header text-dark  text-center font-weight-bold">Edit Notice Details</h5>
                    <div class="card-body">
                        <form class="user" method="post" enctype="multipart/form-data">
                            <div class="row mb-3">
                                <div class="col-md-9 mb-2">
                                    <label for="organizationName" class="">Notice Title</label>
                                    <input type="text" class="form-control" name="title"
                                        value="<?php echo $edit_row["notice_title"];?>" required>
                                </div>
                                <div class="col-md-3 mb-2">
                                    <label for="organizationName" class="">Notice Date</label>
                                    <input type="date" class="form-control" name="notice_date"
                                        value="<?php echo $edit_row["notice_date"];?>">
                                </div>
                                <div class="col-md-3 mb-2">
                                    <label for="category">Notice For</label>
                                    <select name="category" id="catTypeId" class="form-control mb-2" required>
                                        <option value="<?php echo $edit_row["notice_for"];?>">
                                            <?php echo $edit_row["notice_for"];?></option>
                                        <option value="All">All</option>
                                        <option value="Associate">Associate</option>
                                        <option value="Sub Associate">Sub Associate</option>
                                        <option value="Employee">Employee</option>
                                        <option value="Student">Student</option>
                                    </select>
                                </div>
                                <div class="col-md-3 mb-2">
                                    <label for="country">Edit Country:</label>
                                    <input type="hidden" name="country" id="country-id"
                                        value="<?php echo $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 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-3 mb-2">
                                    <label for="state">Edit State:</label>
                                    <input type="hidden" name="state" id="state-id" value="<?php echo $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($state_id)) {
                                                echo "<option data-id='{$state_id}' value='{$state_name}'>{$state_name}</option>";
                                            }
                                        ?>
                                    </datalist>
                                </div>

                                <div class="col-md-3 mb-2">
                                    <label for="dist">Edit District:</label>
                                    <input type="hidden" name="dist" id="district-id" value="<?php echo $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($dist_id)) {
                                                echo "<option data-id='{$dist_id}' value='{$dist_name}'>{$dist_name}</option>";
                                            }
                                        ?>
                                    </datalist>
                                </div>
                                <div class="col-md-12 mb-2">
                                    <label for="about">Notice Script</label>
                                    <textarea class="form-control ckeditor" name="about" id="content" rows="3">
                                        <?php echo $edit_row["notice_script"];?>
                                    </textarea>
                                </div>
                                <div class="col-md-6 mb-2">
                                    <label for="notice">Notice Image</label>
                                    <input type="file" name="notice" id="img" class="form-control"
                                        placeholder="Upload Image">
                                    <input type="hidden" name="noticeOld"
                                        value="<?php echo $edit_row["notice_image"];?>">
                                </div>
                                <div class="col-md-6 mb-2">
                                    <a href="<?php echo $edit_row["notice_image"];?>" target="_blank">
                                        <img src="<?php echo $edit_row["notice_image"];?>" class="img-fluid"
                                            style="height:200px;" alt="">
                                    </a>
                                </div>
                            </div>

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

                            <a href="view-notice.php" class="btn btn-warning"> <i class="fas fa-ban"></i>
                                Cancel
                            </a>
                        </form>
                    </div>
                </div>
                <?php }
                } elseif(isset($_GET["view_id"])){
                    $view_id = $_GET["view_id"];
                    $view_run = mysqli_query($con, "SELECT * FROM notice WHERE admin_ref = '$_SESSION[uref]' AND notice_id = '$_GET[view_id]'");
                    
                    if($view_row = mysqli_fetch_assoc($view_run)){
                        $country_sql = mysqli_query($con, "SELECT * FROM countries WHERE id = '$view_row[notice_country]' ");
                        if($country_row = mysqli_fetch_assoc($country_sql)){
                            $country_name = $country_row["name"];
                            $country_id = $view_row["notice_country"];
                        }else{
                            $country_name = "";
                            $country_id = "";
                        }

                        $state_sql = mysqli_query($con, "SELECT * FROM state WHERE id = '$view_row[notice_state]' ");
                        if($state_row = mysqli_fetch_assoc($state_sql)){
                            $state_name = $state_row["name"];
                            $state_id = $view_row["notice_state"];
                        }else{
                            $state_name = "";
                            $state_id = "";
                        }

                        $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE id = '$view_row[notice_dist]' ");
                        if($dist_row = mysqli_fetch_assoc($dist_sql)){
                            $dist_name = $dist_row["distName"];
                            $dist_id = $view_row["notice_dist"];
                        }else{
                            $dist_name = "";
                            $dist_id = "";
                        }
                    ?>
                <div class="card mt-3">
                    <h5 class="card-header text-dark  text-center font-weight-bold">
                        <?php echo $view_row["notice_title"];?></h5>
                    <div class="card-body">
                        <div class="notice-image">
                            <img src="<?php echo $view_row["notice_image"];?>" class="img-fluid" alt="">
                        </div>
                        <div class="notice-preiew">
                            <div id="ck-img-preview"><strong>Notice Details :
                                </strong><?php echo $view_row["notice_script"];?>
                            </div>
                            <p><strong>Notice Date : </strong><?php echo $view_row["notice_date"];?></p>
                            <p><strong>Notice Country : </strong><?php echo $country_name;?></p>
                            <p><strong>Notice State : </strong><?php echo $state_name;?></p>
                            <p><strong>Notice District : </strong><?php echo $dist_name;?></p>
                            <p><strong>Notice For : </strong><?php echo $view_row["notice_for"];?></p>
                            <p><strong>Notice Cteate Date : </strong><?php echo $view_row["notice_createdate"];?></p>
                        </div>

                        <a href="view-notice.php" class="btn btn-warning"> <i class="fas fa-ban"></i>
                            Cancel
                        </a>
                    </div>
                </div>
                <?php }
                } elseif(isset($_GET["add_notice"])){?>
                <div class="card mt-3">
                    <h5 class="card-header text-dark  text-center font-weight-bold">Add Notice</h5>
                    <div class="card-body">
                        <form method="POST" enctype="multipart/form-data">
                            <div class="row mb-3">
                                <div class="col-md-9 mb-2">
                                    <label for="organizationName" class="">Notice Title</label>
                                    <input type="text" class="form-control" name="title"
                                        placeholder="Enter Notice Title" required>
                                </div>
                                <div class="col-md-3 mb-2">
                                    <label for="organizationName" class="">Notice Date</label>
                                    <input type="date" class="form-control" name="notice_date" 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>
                                        <option value="All">All</option>
                                        <option value="Associate">Associate</option>
                                        <option value="Sub Associate">Sub Associate</option>
                                        <option value="Employee">Employee</option>
                                        <option value="Student">Student</option>
                                    </select>
                                </div>
                                <div class="col-md-3 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">
                                    <datalist id="country-list">
                                        <?php 
                                            $sql = "SELECT id, name FROM countries 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-3 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">
                                    <datalist id="state-list"></datalist>
                                </div>

                                <div class="col-md-3 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">
                                    <datalist id="district-list"></datalist>
                                </div>
                                <div class="col-md-12 mb-2">
                                    <label for="about">Notice Script</label>
                                    <textarea class="form-control ckeditor" name="about" id="content"
                                        rows="3"></textarea>
                                </div>
                                <div class="col-md-12 mb-2">
                                    <label for="notice">Notice Image</label>
                                    <input type="file" name="notice" id="img" class="form-control"
                                        placeholder="Upload Image" accept=".jpg,.jpeg,.png">
                                </div>
                            </div>
                            <div class="mt-4">
                                <button type="submit" class="btn btn-success text-sm-center" name="submit">
                                    <i class="fa fa-plus"></i> Add Notice
                                </button>
                                <a href="view-notice.php" class="btn btn-warning 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 mb-2">
                                <div class="d-flex justify-content-between">
                                    <h4 class="text-gray-800">Notice List</h4>
                                    <a class="btn btn-primary btn-sm" href="view-notice.php?add_notice"><i
                                            class="fa fa-bell"></i> Add
                                        Notice</a>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <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 Notice Title">
                                            <datalist id="First">
                                                <?php 
                                                    $sql = "SELECT notice_title FROM notice WHERE admin_ref = '$_SESSION[uref]' AND notice_recycle = '1' ORDER BY notice_id DESC";
                                                    $res = mysqli_query($con,$sql);
                                                    while ($row = mysqli_fetch_assoc($res)) {
                                                ?>
                                                <option value="<?php echo $row['notice_title']; ?>">
                                                    <?php echo $row['notice_title']; ?></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"; ?>

    <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>
    //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-notice-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-notice-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-notice-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'])) {
    $title = mysqli_real_escape_string($con, strip_tags($_POST['title']));
    $notice_date = mysqli_real_escape_string($con, strip_tags($_POST['notice_date']));
    $country = mysqli_real_escape_string($con, strip_tags($_POST['country']));
    $state = mysqli_real_escape_string($con, strip_tags($_POST['state']));
    $dist = mysqli_real_escape_string($con, strip_tags($_POST['dist']));
    $noticefor = mysqli_real_escape_string($con, strip_tags($_POST['category']));
    $about = $_POST['about'];

    $admin_ref = $_SESSION['uref'];

    // Handle file upload
    $noticepath = "";
    if (!empty($_FILES["notice"]["name"])) {
            $target_dir = "../upload/notice/";
            $imageFileType = strtolower(pathinfo($_FILES["notice"]["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["notice"]["tmp_name"]);
            if ($check !== false) {
                $uploadOk = 1;
            } else {
                $uploadOk = 0;
            }
        
            // Check file size (5MB maximum)
            if ($_FILES["notice"]["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["notice"]["tmp_name"], $target_file)) {
                    $noticepath = $target_file;
                } else {
                    echo "<script>alert('Sorry, there was an error uploading your file.');</script>";
                }
            }
    }        

    $ins_sql = "INSERT INTO notice (notice_title, notice_script, notice_image, notice_for, notice_country, notice_state, notice_dist, notice_date, notice_createdate, admin_ref, notice_recycle) VALUES ('$title', '$about', '$noticepath', '$noticefor', '$country', '$state', '$dist', '$notice_date', '$currentTime', '$admin_ref', '1')";

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


<?php
if(isset($_POST['edit_users'])){
    $title = mysqli_real_escape_string($con, strip_tags($_POST['title']));
    $notice_date = mysqli_real_escape_string($con, strip_tags($_POST['notice_date']));
    $country = mysqli_real_escape_string($con, strip_tags($_POST['country']));
    $state = mysqli_real_escape_string($con, strip_tags($_POST['state']));
    $dist = mysqli_real_escape_string($con, strip_tags($_POST['dist']));
    $noticefor = mysqli_real_escape_string($con, strip_tags($_POST['category']));
    $about = $_POST['about'];

    // Handle file upload
    $noticepath = "";
    if (!empty($_FILES["notice"]["name"])) {
        $target_dir = "../upload/notice/";
        $imageFileType = strtolower(pathinfo($_FILES["notice"]["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["notice"]["tmp_name"]);
        if ($check !== false) {
            $uploadOk = 1;
        } else {
            $uploadOk = 0;
        }
    
        // Check file size (5MB maximum)
        if ($_FILES["notice"]["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["notice"]["tmp_name"], $target_file)) {
                unlink("$_POST[noticeOld]");
                $noticepath = $target_file;
            } else {
                echo "<script>alert('Sorry, there was an error uploading your file.');</script>";
            }
        }
    }  else{
        $noticepath = $_POST["noticeOld"];
    }

    $up_sql = "UPDATE notice SET notice_title = '$title', notice_country = '$country', notice_state = '$state', notice_dist = '$dist', notice_date = '$notice_date', notice_for = '$noticefor', notice_script = '$about', notice_image = '$noticepath' WHERE notice_id = '$_GET[edit_id]'";

    if(mysqli_query($con, $up_sql)){
        ?>
<script>
alert('Successfully Updated');
window.location = 'view-notice.php';
</script>
<?php
    }
    else{
        ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'view-notice.php?edit_id=<?php echo $_GET["edit_id"]; ?>';
}, 1200);
</script>
<?php
    }
} 
?>

<?php
if ( isset($_GET["del_id"]) ) {
$disable_sql = "UPDATE notice SET notice_recycle = '0' WHERE notice_id = '$_GET[del_id]'";
    if ( mysqli_query($con, $disable_sql) ) {
        ?>
<script>
window.location = "view-notice.php";
</script>
<?php
    } else {
        ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'view-notice.php';
}, 1200);
</script>
<?php
    }
}
?>