<?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"; ?>
</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 countries WHERE id = '$edit_id'");
                    if($edit_row = mysqli_fetch_assoc($edit_run)){
                    ?>
                <div class="card mt-3">
                    <h5 class="card-header text-dark  text-center font-weight-bold">Edit Country Name</h5>
                    <div class="card-body" style="height: calc(100vh - 160px);">
                        <form method="POST">
                            <div class="from-group">
                                <label>Edit Country Name</label>
                                <input type="text" class="form-control" id="stateName" placeholder="Name of Country *"
                                    name="stateName" value="<?php echo $edit_row["name"]; ?>" required>
                            </div>
                            <div class="from-group">
                                <button type="submit" class="btn btn-success text-sm-center mt-4" name="edit_submit">
                                    <i class="fa fa-plus"></i> Edit Country Name
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                <?php }
                } else{?>
                <div class="card mt-3">
                    <h5 class="card-header text-dark text-center font-weight-bold">Add Country</h5>
                    <div class="card-body">
                        <form method="POST">
                            <div class="from-group">
                                <label>Enter Country Name</label>
                                <input type="text" class="form-control" id="stateName"
                                    placeholder="Enter Country Name *" name="stateName" required>
                            </div>
                            <div class="from-group">
                                <button type="submit" class="btn btn-success text-sm-center mt-4" name="submit">
                                    <i class="fa fa-plus"></i> Add Country
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
                <?php }?>
                <?php if(!isset($_GET["edit_id"])){?>
                <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="m-0 text-dark font-weight-bold">Country List</h4>
                            </div>
                            <div class="col-md-6 col-8">
                                <div class="d-flex justify-content-end">
                                    <div class="filter-input">
                                        <div class="form-group">
                                            <input type="text" list="First" placeholder="Search Country Name"
                                                class="form-control" id="FirstFilterValue">
                                            <datalist id="First">
                                                <?php 
                                                    $sql = "SELECT name FROM state ORDER BY id ASC";
                                                    $res = mysqli_query($con,$sql);
                                                    while ($row = mysqli_fetch_assoc($res)) {
                                                ?>
                                                <option value="<?php echo $row['name']; ?>">
                                                    <?php echo $row['name']; ?></option>
                                                <?php } ?>
                                            </datalist>
                                        </div>
                                    </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: 0;">
                                    <?php 
                                    for ($count = 0; $count < 4; $count++) {
                                        echo '<li>
                                            <div class="ph-item">
                                            <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>
                                            </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>
    //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: 'country-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: 'country-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: 'country-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'])){
    $stateName = mysqli_real_escape_string($con, strip_tags($_POST['stateName']));
    $ins_sql = "INSERT INTO countries (name) VALUES ('$stateName')";
    if(mysqli_query($con, $ins_sql)){
        ?>
<script>
window.location = 'country.php';
</script>
<?php
    }
    else{
    ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'country.php';
}, 1200);
</script>
<?php
    }
}

    
if ( isset($_GET["del_id"]) ) {
    $del_sql = "DELETE FROM countries WHERE id = '$_GET[del_id]'";
    if ( mysqli_query($con, $del_sql) ) {
        ?>
<script>
window.location = "country.php";
</script>
<?php
    } else {
        ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'country.php';
}, 500);
</script>
<?php
    }
        
}


if(isset($_POST['edit_submit'])){
    $stateName = mysqli_real_escape_string($con, strip_tags($_POST['stateName']));
    $up_sql = "UPDATE countries SET name = '$stateName' WHERE id = '$_GET[edit_id]'";
    if(mysqli_query($con, $up_sql)){
        ?>
<script>
window.location = 'country.php';
</script>
<?php
    }
    else{
        ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'country.php?edit_id=<?php echo $_GET["edit_id"]; ?>';
}, 500);
</script>
<?php
    }
}
?>