<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = 'Dashboard For Challan';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
include("includes/linkshead.php");

$weeklyfrmdt = date('Y-m-d', strtotime('monday last week'));
$weeklytodt = date('Y-m-d', strtotime('sunday last week'));
//$weeklytodt = strtotime("next sunday");
?>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>

</head>
<body style="background: white;">
    <div class="container-fluid">

        <div class="row">
            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12"></div>
            <div class='col-lg-1 col-md-1 col-sm-12 col-xs-12 pad text-right'>
                <i class="fa fa-caret-left weeklyfrmdtbtn" aria-hidden="true" style="color: #1B1B1B;font-size: 35px;"></i>
            </div>
            <div class='col-lg-1 col-md-1 col-sm-12 col-xs-12 pad'  style="width: 10%;">
                <input type='date' class='form-control weeklyfrmdt' name='weeklyfrmdt' id="weeklyfrmdt" value='<?php echo $weeklyfrmdt ?>'>
            </div>
            <div class='col-lg-2 col-md-2 col-sm-12 col-xs-12 pad'  style="width: 10%;">
                <input type='date' class='form-control weeklytodt weeklyfrmdtbtn' name='weeklytodt' id="weeklytodt" value='<?php echo $weeklytodt; ?>' readonly>
            </div>
            <div class='col-lg-1 col-md-1 col-sm-12 col-xs-12 pad' >
                <a><i class="fa fa-caret-right weeklytodtbtn" aria-hidden="true"  style="color: #1B1B1B;font-size: 35px;"></i></a>
            </div>
        </div>


        <div class="row">
            <div class="col-lg-2 col-md-2 col-sm-12 col-xs-12"></div>
            <div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
                <div class="chart-container">
                    <canvas id="myChart2" style="position: relative; height:120vw;"></canvas>
                </div>
            </div>
        </div>
    </div>


    <script>
        $(document).ready(function () {
            $("body").on("click", ".weeklyfrmdtbtn", PrevWeekChart);
            $("body").on("click", ".weeklytodtbtn", NextWeekChart);
            $("body").on("blur", ".weeklyfrmdt", WeeklyTopCustChartDateFunc);


            function PrevWeekChart() {
                PrevNextWeekChart('P');
            }

            function NextWeekChart() {
                PrevNextWeekChart('N');
            }


            function WeeklyTopCustChartDateFunc() {
                var wfrmdate = new Date($(".weeklyfrmdt").val());
                if (wfrmdate.getDay() !== 1) {
                    alert("Date is not Monday");
                    var curr = new Date();
                    var firstday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
                    var formattedDate = firstday.getFullYear() + "-" + (firstday.getMonth() + 1) + "-" + firstday.getDate();
                    $(".weeklyfrmdt").val(formattedDate);
                } else {
                    var dt = wfrmdate;
                    var ndt1 = dt.setDate(dt.getDate() + 6);
                    var ndt = new Date(ndt1);

                    var dd = String(ndt.getDate()).padStart(2, '0');
                    var mm = String(ndt.getMonth() + 1).padStart(2, '0'); //January is 0!
                    var yyyy = ndt.getFullYear();

                    var today = yyyy + '-' + mm + '-' + dd;

                    $(".weeklytodt").val(today);
                    TopCustomerChart();
                }
            }

            function PrevNextWeekChart(para) {
                var dt = new Date($(".weeklyfrmdt").val());
                var mpara = para;
                if (mpara == 'P')
                    var ndt1 = dt.setDate(dt.getDate() - 7);
                else {
                    var ndt1 = dt.setDate(dt.getDate() + 7);
                }
                var ndt = new Date(ndt1);

                var dd = String(ndt.getDate()).padStart(2, '0');
                var mm = String(ndt.getMonth() + 1).padStart(2, '0'); //January is 0!
                var yyyy = ndt.getFullYear();

                var today = yyyy + '-' + mm + '-' + dd;

                $(".weeklyfrmdt").val(today);
                WeeklyTopCustChartDateFunc();
            }


            TopCustomerChart();
            function TopCustomerChart() {
                $('#myChart2').replaceWith('<canvas id="myChart2" style="position: relative; height:120vw;"></canvas>');
                var ctx2 = document.getElementById('myChart2').getContext('2d');
                var weeklyfrmdt = $(".weeklyfrmdt").val();
                var weeklytodt = $(".weeklytodt").val();

                $.ajax({
                    type: 'GET',
                    dataType: 'json',
                    url: "getmislowitemgrp.php",
                    data: {weeklyfrmdt: weeklyfrmdt, weeklytodt: weeklytodt},
                    success: function (response) {

                        var mitemgrp = [], mqty = [];
                        $.each(response, function () {
                            mitemgrp.push(this.grpname);
                            mqty.push(this.datediff);
                        });

                        var data = {
                            labels: [mitemgrp[0], mitemgrp[1], mitemgrp[2], mitemgrp[3], mitemgrp[4]],
                            datasets: [{
                                    label: 'Weekly',
                                    data: [mqty[0], mqty[1], mqty[2], mqty[3], mqty[4]],
                                    backgroundColor: [
                                        'rgb(66,133,244)',
                                        'rgb(234,67,53)',
                                        'rgb(251,188,5)',
                                        'rgb(52,168,83)',
                                        'rgb(181,181,181)'
                                    ],
                                    borderColor: [
                                        'rgba(66,133,244,1)',
                                        'rgba(234,67,53,1)',
                                        'rgba(251,188,5,1)',
                                        'rgba(52,168,83,1)',
                                        'rgba(181,181,181,1)'
                                    ],
                                    borderWidth: 1
                                }]
                        };

                        var chartOptions = {
                            responsive: true,
                            legend: {
                                position: "top"
                            },
                            title: {
                                display: true,
                                text: "Non Performing Item Groups - Weekly",
                                fontSize: 25
                            },
                            scales: {
                                yAxes: [{
                                        ticks: {
                                            beginAtZero: true
                                        },
                                        scaleLabel: {
                                            display: true,
                                            labelString: 'Previous Date',
                                            fontSize: 18
                                        }
                                    }],
                                xAxes: [{
                                        scaleLabel: {
                                            display: true,
                                            labelString: 'Item - Groups',
                                            fontSize: 18
                                        }
                                    }]
                            }
                        }
                        var DailyBar = new Chart(ctx2, {
                            type: "horizontalBar",
                            data: data,
                            options: chartOptions
                        });

//                        DailyBar.update();
                    },
                    error: function (request, textStatus, errorThrown) {
                    }
                });
            }
        });
    </script>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
