<?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 state WHERE id = '$edit_id'");

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

                        

                        $sql2 = "SELECT * FROM countries WHERE id  ='$edit_row[country_id]' ";

                        $run2 = mysqli_query($con, $sql2);

                        $rows2 = mysqli_fetch_assoc($run2);

                        $stateName  =  $rows2['name'] ;

                    ?>

                <div class="card mt-3">

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

                    <div class="card-body" style="height: calc(100vh - 160px);">

                        <form method="POST">

                            <div class="row">

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

                                    <div class="from-group">

                                        <label>Edit State Name</label>

                                        <select name="countryId" id="countryId" class="form-control" required>

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

                                                <?php echo $stateName ; ?></option>

                                            <?php

                                                $sql = "SELECT * FROM countries ORDER BY id ASC";

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



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

                                                    echo '

                                                    <option value="'.$state_rows["id"].'"> '.$state_rows["name"].' </option>

                                                    ';

                                                }

                                            ?>

                                        </select>

                                    </div>

                                </div>

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

                                    <div class="from-group">

                                        <label>Edit State Name</label>

                                        <input type="text" class="form-control" id="stateName"

                                            placeholder="Name of State *" name="stateName"

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

                                    </div>

                                </div>

                            </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 State 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 State</h5>

                    <div class="card-body">

                        <form method="POST">

                            <div class="row">

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

                                    <div class="from-group">

                                        <label>Country Name</label>

                                        <select name="countryId" class="form-control" id="state">

                                            <option value="">Select Country Name</option>

                                            <?php 

                                                $sql = "SELECT name, id FROM countries ORDER BY id DESC";

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

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

                                                    $sid = $row['id'];

                                                    $state = $row['name'];

                                            ?>

                                            <option value="<?php echo $sid;?>">

                                                <?php echo $state;?>

                                            </option>

                                            <?php }?>

                                        </select>

                                    </div>

                                </div>

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

                                    <div class="from-group">

                                        <label>Enter State Name</label>

                                        <input type="text" class="form-control" id="stateName"

                                            placeholder="Enter State Name *" name="stateName" required>

                                    </div>

                                </div>

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

                                </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">State 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 State 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: 'state-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: 'state-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: 'state-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'])){

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

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

    $ins_sql = "INSERT INTO state (name, country_id) VALUES ('$stateName', '$countryId')";

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

        ?>

<script>

window.location = 'state.php';

</script>

<?php

    }

    else{

    ?>

<script>

alert('OOPS !!!!');

setTimeout(() => {

    window.location = 'state.php';

}, 1200);

</script>

<?php

    }

}



    

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

    $del_sql = "DELETE FROM state WHERE id = '$_GET[del_id]'";

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

        ?>

<script>

window.location = "state.php";

</script>

<?php

    } else {

        ?>

<script>

alert('OOPS !!!!');

setTimeout(() => {

    window.location = 'state.php';

}, 500);

</script>

<?php

    }

        

}





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

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

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

    $up_sql = "UPDATE state SET name = '$stateName', country_id = '$countryId' WHERE id = '$_GET[edit_id]'";

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

        ?>

<script>

window.location = 'state.php';

</script>

<?php

    }

    else{

        ?>

<script>

alert('OOPS !!!!');

setTimeout(() => {

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

}, 500);

</script>

<?php

    }

}

?>