<?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_run = mysqli_query($con, "SELECT cname FROM category WHERE cid = '$_GET[edit_id]'");

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

                ?>

                <div class="card mb-4">

                    <div class="card-header">

                        <h5 class="text-dark text-center font-weight-bold m-0">Edit Category Details</h5>

                    </div>

                    <div class="card-body mb-4">

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

                            <div class="form-group row">

                                <div class="col-sm-12 mb-1 mb-sm-0">

                                    <input type="text" class="form-control mb-2" id="cname" name="cname"

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

                                </div>

                            </div>

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

                                <i class="fa fa-refresh"></i> Update Category

                            </button>

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

                                Cancel

                            </a>

                        </form>

                    </div>

                </div>

                <?php }} else{?>

                <div class="card mb-4">

                    <div class="card-header">

                        <h5 class="text-dark text-center font-weight-bold m-0">Add Category</h5>

                    </div>

                    <div class="card-body mb-4">

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

                            <div class="form-group row">

                                <div class="col-md-12 mb-1 mb-sm-0">

                                    <input type="text" class="form-control mb-2" id="cname"

                                        placeholder="Enter Category Name *" name="cname" required>

                                </div>

                            </div>

                            <button type="submit" class="btn btn-primary" name="new_category">

                                <i class="fas fa-plus"></i> Add New Category

                            </button>

                        </form>

                    </div>

                </div>



                <div class="card">

                    <div class="card-header">

                        <div class="row align-items-center">

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

                                <h5 class="text-gray-800">Category List</h5>

                            </div>

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

                                <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 Category</option>

                                                <?php 

                                                    $sql = "SELECT cname FROM category  WHERE catby = '1' ORDER BY cid ASC";

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

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

                                                ?>

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

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

    document.getElementById('toggleBtn').addEventListener('click', function() {

        var target = document.getElementById('target');

        target.classList.toggle('show');

    });



    document.getElementById('cancelBtn').addEventListener('click', function(e) {

        e.preventDefault(); // Prevent the default anchor action

        var target = document.getElementById('target');

        target.classList.remove('show');

    });

    </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-category-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-category-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-category-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['new_category'])){

        $cname = $_POST['cname'];



        $ins_sql = "INSERT INTO category (cname, catby, cposition) VALUES ('$cname', '$_SESSION[uid]', '1')";



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

            ?>

<script>

window.location = 'category.php';

</script>

<?php

        }

        else{

            ?>

<script>

alert('OOPS !!!!');

setTimeout(() => {

    window.location = 'category.php';

}, 1200);

</script>

<?php

        }

    }

    ?>

<?php

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



        $del_sql = "UPDATE category SET cposition = '0' WHERE cid = '$_GET[del_id]'";



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

            ?>

<script>

window.location = "category.php";

</script>

<?php

        } else {

            ?>

<script>

alert('OOPS !!!!');

setTimeout(() => {

    window.location = 'category.php';

}, 500);

</script>

<?php

        }

        

    }

    ?>

<?php

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

        $cname = $_POST['cname'];

        $up_sql = "UPDATE category SET cname = '$cname' WHERE cid = '$_GET[edit_id]'";



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

            ?>

<script>

window.location = 'category.php';

</script>

<?php

        }

        else{

            ?>

<script>

alert('OOPS !!!!');

setTimeout(() => {

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

}, 500);

</script>

<?php

        }

    }

?>