<?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();

    }

    // Get the status from the URL, if available

    $statusFromUrl = isset($_GET['status']) ? $_GET['status'] : '';

    

    if($statusFromUrl == 'Pending'){

        $statusId = '0';

    }elseif($statusFromUrl == 'Ongoing'){

        $statusId = '1';

    }elseif($statusFromUrl == 'Completed'){

        $statusId = '2';

    }elseif($statusFromUrl == 'Postponed'){

        $statusId = '3';

    }elseif($statusFromUrl == 'Rejected'){

        $statusId = '4';

    }





    $statusOptions = [

        '0' => 'Pending',

        '1' => 'Ongoing',

        '2' => 'Completed',

        '3' => 'Postponed',

        '4' => 'Rejected'

    ];

?>



<!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">

                <div class="card mt-3 mb-3">

                    <div class="card-header">

                        <ul class="search-details">

                            <li>

                                <h4 class="text-gray-800 m-0">Service List</h4>

                            </li>

                            <li>

                                <input type="date" class="form-control form-control-sm" id="startDate"

                                    placeholder="Start Date">

                            </li>

                            <li>

                                <input type="date" class="form-control form-control-sm" id="endDate"

                                    placeholder="End Date">

                            </li>

                            <li>

                                <select name="status" class="form-control form-control-sm" id="pststus">

                                    <option value="">Status</option>

                                    <option value="0">pending</option>

                                    <option value="1">Ongoing</option>

                                    <option value="2">Completed</option>

                                    <option value="3">Postpond</option>

                                    <option value="4">Reject</option>

                                </select>

                            </li>

                            <li>

                                <select name="category" class="form-control form-control-sm" id="pcategory">

                                    <option value="">Category</option>

                                    <?php 

                                        $sql = "SELECT * FROM category";

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

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

                                    ?>

                                    <option data-id="<?php echo $row['cid']; ?>" value="<?php echo $row['cid']; ?>">

                                        <?php echo $row['cname']; ?></option>

                                    <?php } ?>

                                </select>

                            </li>

                            <li>

                                <div class="form-group">

                                    <input type="text" list="Third" class="form-control form-control-sm"

                                        id="ThirdFilterValue" placeholder="Product">

                                    <datalist id="Third">

                                        <?php 

                                            $sql = "SELECT DISTINCT data_title FROM data WHERE data_catagory = 'service'  AND data_recycle = '1' ORDER BY data_sl_no ASC";

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

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

                                        ?>

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

                                            <?php echo $row['data_title']; ?></option>

                                        <?php } ?>

                                    </datalist>

                                </div>

                                <input type="hidden" class="form-control" value="" id="ThirdFilterValue">

                            </li>

                            <li>

                                <div class="d-flex">

                                    <div class="form-group">

                                        <input type="text" list="First" class="form-control form-control-sm"

                                            id="FirstFilterValue" placeholder="RA">

                                        <datalist id="First">

                                            <?php 

                                                    $sql = "SELECT DISTINCT uname FROM users  WHERE utype = 'Franchiee'  AND udisplay !='disable' AND admin_ref = '$_SESSION[uref]' 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="FirstFilterValue">



                                    <button type="button" id="filter" class="btn btn-primary mb-2 btn-sm"><i

                                            class="fa fa-search"></i>

                                    </button>

                                </div>

                            </li>

                        </ul>

                    </div>

                    <div class="card-body">

                        <div class="row">

                            <!-- Status Navbar -->

                            <div class="col-12">

                                <div class="row p-2">

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

                                        <ul class="status-nav mb-3">

                                            <?php foreach ($statusOptions as $statusValue => $statusName): ?>

                                            <li>

                                                <a class="btn btn-warning <?php echo ($statusId === (string)$statusValue) ? 'active' : ''; ?>"

                                                    href="productList-status.php?status=<?php echo $statusName; ?>">

                                                    <?php echo $statusName; ?>

                                                </a>

                                            </li>

                                            <?php endforeach; ?>

                                        </ul>

                                    </div>

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

                                        <div id="calculation"></div>

                                    </div>

                                </div>

                            </div>

                            <!--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>

            </div>

        </div>



        <?php include "include/footer.php"; ?>

    </section>



    <!-- 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">Product Name:</h5>

                            <h5 class="modal-css">Category:</h5>

                            <h5 class="modal-css">RA Name:</h5>

                            <h5 class="modal-css">RAs Name:</h5>

                            <h5 class="modal-css">Employee Name:</h5>

                            <h5 class="modal-css">Client Name:</h5>

                            <h5 class="modal-css">Status:</h5>

                            <h5 class="modal-css">Date:</h5>

                            <h5 class="modal-css">Referred By:</h5>

                        </div>

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

                            <h5 class="modal-id" id="modalProductName"></h5>

                            <h5 class="modal-id" id="modalPCat"></h5>

                            <h5 class="modal-id" id="modalFranName"></h5>

                            <h5 class="modal-id" id="modalSubFranName"></h5>

                            <h5 class="modal-id" id="modalEmployeeName"></h5>

                            <h5 class="modal-id" id="modalClientName"></h5>

                            <h5 class="modal-id" id="modalStatusName"></h5>

                            <h5 class="modal-id" id="modalDate"></h5>

                            <h5 class="modal-id" id="modalRef"></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>



    <!-- Final Document Modal -->

    <div class="modal fade" id="finalDocumentModal" tabindex="-1" role="dialog"

        aria-labelledby="finalDocumentModalLabel" aria-hidden="true">

        <div class="modal-dialog" role="document">

            <div class="modal-content">

                <div class="modal-header">

                    <h5 class="modal-title">Upload Final Document</h5>

                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>

                </div>

                <div class="modal-body">

                    <!-- Content will be loaded dynamically here -->

                </div>

            </div>

        </div>

    </div>



    <!-- View Product Modal -->

    <div class="modal fade" id="viewProductModal" tabindex="-1" role="dialog" aria-labelledby="viewProductModalLabel"

        aria-hidden="true">

        <div class="modal-dialog modal-lg" role="document">

            <div class="modal-content">

                <div class="modal-header">

                    <h5 class="modal-title">Product Details</h5>

                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>

                </div>

                <div class="modal-body">

                    <!-- Content will be loaded dynamically here -->

                </div>

            </div>

        </div>

    </div>



    <?php include "include/js.php"; ?>



    <script>

    $(document).ready(function() {

        var load_filter_flag = 0;

        var FirstFilterValue = '';

        var SecondFilterValue = '';

        var ThirdFilterValue = '';

        var StatusValue = '<?php echo $statusId; ?>'; // Set from URL-based statusId

        var CategoryValue = '';

        var StartDate = '';

        var EndDate = '';



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

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

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

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

            CategoryValue = $('#pcategory').val();

            StartDate = $('#startDate').val();

            EndDate = $('#endDate').val();



            load_filter_flag = 0;

            if (FirstFilterValue || SecondFilterValue || ThirdFilterValue || StatusValue ||

                CategoryValue || StartDate || EndDate) {

                $.ajax({

                    url: 'productList-status-Ajax.php',

                    type: 'POST',

                    data: {

                        FirstFilterValue: FirstFilterValue,

                        SecondFilterValue: SecondFilterValue,

                        ThirdFilterValue: ThirdFilterValue,

                        StatusValue: StatusValue, // Send the StatusValue from PHP-derived URL parameter

                        CategoryValue: CategoryValue,

                        StartDate: StartDate,

                        EndDate: EndDate,

                        start: load_filter_flag

                    },

                    success: function(result) {

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

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

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

                        load_filter_flag += 10;

                    },

                    complete: function() {

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

                    }

                });

            } else {

                alert('Please select at least one option.');

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

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

            }

        });



        function loadMoreFilter(start) {

            if (load_filter_flag > 0) {

                $.ajax({

                    url: 'productList-status-Ajax.php',

                    type: 'POST',

                    data: {

                        FirstFilterValue: FirstFilterValue,

                        SecondFilterValue: SecondFilterValue,

                        ThirdFilterValue: ThirdFilterValue,

                        StatusValue: StatusValue,

                        CategoryValue: CategoryValue,

                        StartDate: StartDate, // Include start date

                        EndDate: EndDate, // Include end date

                        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: 'productList-status-Ajax.php',

                type: 'POST',

                data: {

                    StatusValue: StatusValue,

                    start: start

                },

                success: function(result) {

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

                    load_flag += 10;

                },

                complete: function(data) {

                    /* Hide loader container */

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

                }

            });

        }



        function loadCalculationSummary() {

            $.ajax({

                url: 'calculation-summary.php',

                type: 'POST',

                data: {

                    load_filter_flag: load_filter_flag,

                    FirstFilterValue: FirstFilterValue,

                    SecondFilterValue: SecondFilterValue,

                    ThirdFilterValue: ThirdFilterValue,

                    StatusValue: StatusValue,

                    CategoryValue: CategoryValue,

                    StartDate: StartDate,

                    EndDate: EndDate

                },

                success: function(result) {

                    $('#calculation').html(result);

                },

                error: function() {

                    alert('Error loading summary.');

                }

            });

        }



        // Call this function initially or when any filter values change

        loadCalculationSummary();



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

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

                if (load_filter_flag > 0) {

                    loadMoreFilter(load_filter_flag);

                } else {

                    loadMore(load_flag);

                }

            }

        });

    });

    </script>





</body>



</html>