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

$formname = 'Dashboard';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

include("includes/linkshead.php");

$weeklyfrmdt = date('Y-m-d', strtotime('monday this week'));
$weeklytodt = date('Y-m-d', strtotime('sunday this week'));
//$weeklytodt = strtotime("next sunday");
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.0.0-beta2/Chart.min.js"></script>
<!--<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-2 col-md-2 col-sm-12 col-xs-12"></div>-->
            <div class='col-lg-3 col-md-5 col-sm-2 col-xs-2 pad text-right'>
                <label class='label-control tmarg'>Product</label>
            </div>
            <div class="col-lg-5 col-md-5 col-sm-4 col-xs-4 pad">
                <select class="form-control  select2 product" id="product" name="product" style="width: 100%" >
                    <option value=""></option>
                    <?php
                    $gproduct = mysqli_query($conn, "SELECT `prodcode`,`name` FROM `product` GROUP by name ORDER by name")or die(mysqli_error($conn));
                    while ($product = mysqli_fetch_array($gproduct)) {
                        $prodcode = $product['prodcode'];
                        $name = $product['name'];

                        Echo "<option value='$prodcode'>$prodcode || $name</option>";
                    }
                    ?>
                </select>
            </div>

<!--            <div class='col-lg-1 col-md-1 col-sm-4 col-xs-4 pad text-left' style="width: 5%;">
                <button type="button" name="" class="form-control btn-primary dailybtn" style="background-color: #1B1B1B;">Go</button>
            </div>-->
        </div>


        <div class="row">
            <div class="col-lg-2 col-md-2 col-sm-12 col-xs-12"></div>

            <!--            <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6">
                            <div class="chart-container">
                                <canvas id="myChart" style="position: relative; height:40vw; width:80vw"></canvas>
                            </div>
                        </div>-->
            <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
                <div class="chart-container">
                    <canvas id="myChart2" style="position: relative; height:150vw;"></canvas>
                </div>
            </div>
            <!--            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                            <div class="chart-container">
                                <canvas id="myChart" style="position: relative; height:120vw;"></canvas>
                            </div>
                        </div>-->
        </div>
    </div>


    <script>
        $(document).ready(function () {
            $("body").on("change", ".product", DailyChart);
//            $("body").on("click", ".weeklyfrmdtbtn, .weeklytodtbtn", WeeklyChart);
//            $("body").on("blur", ".weeklyfrmdt", WeeklyChartDateFunc);


            DailyChart();
            function DailyChart() {
                $('#myChart2').replaceWith('<canvas id="myChart2" style="position: relative; height:150vw;"></canvas>');
                var ctx2 = document.getElementById('myChart2').getContext('2d');
                var prodcode = $(".product").val();
                $.ajax({
                    type: 'GET',
                    dataType: 'json',
                    url: "getprodsalecomp.php",
                    data: {prodcode:prodcode},
                    success: function (response) {
                        var month = [], prev = [], curr = [], totprev = 0, totcurr = 0;
                        $.each(response, function () {

                            month.push(this.month);
                            prev.push(this.prev);
                            curr.push(this.curr);

                            totprev = totprev + parseFloat(this.prev);
                            totcurr = totcurr + parseFloat(this.curr);
                        });

                        var lbltext = "Product Sales Comparison";
                        var data = {
                            labels: month,
                            datasets: [
                                {
                                    label: "Previous Year",
                                    backgroundColor: "rgb(234,67,53)",
                                    borderColor: "rgb(234,67,53)",
                                    borderWidth: 1,
                                    data: prev
                                },
                                {
                                    label: "Current Year",
                                    backgroundColor: 'rgb(66,133,244)',
                                    borderColor: 'rgb(66,133,244)',
                                    borderWidth: 1,
                                    data: curr
                                },
                                {
                                    label: "",
                                    backgroundColor: 'rgb(255,255,255)',
                                    borderColor: 'rgb(255,255,255)',
                                    borderWidth: 1,
                                    data: 0
                                }
                            ]
                        };

                        var chartOptions = {
                            responsive: true,
                            legend: {
                                position: "top"
                            },
                            title: {
                                display: true,
                                text: lbltext,
                                fontSize: 25
                            },
                            scales: {
                                yAxes: [{
                                        ticks: {
                                            beginAtZero: true
                                        },
                                        scaleLabel: {
                                            display: true,
                                            labelString: 'Qty. (Sales - Returns)',
                                            fontSize: 20
                                        }
                                    }],
                                xAxes: [{
                                        scaleLabel: {
                                            display: true,
                                            labelString: 'Month',
                                            fontSize: 20
                                        }
                                    }]
                            },
                            onClick: function (e, element) {
                            }
                        }
                        var DailyBar = new Chart(ctx2, {
                            type: "bar",
                            data: data,
                            options: chartOptions
                        });



//                        document.getElementById("myChart2").onclick = function (evt, element) {
//                            var activePoints = DailyBar.getElementsAtEvent(evt);
//                            var firstPoint = activePoints[0];
//                            var label = DailyBar.data.labels[firstPoint._index];
//                            var value = DailyBar.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
//                            var bartitle = activePoints[0]._model.datasetLabel;
////                            console.log(activePoints);
//
//                            if (firstPoint !== undefined) {
//                                var series = label.substr(0, 2), ctype = '';
//                                if (bartitle.toUpperCase() == 'Cash'.toUpperCase()) {
//                                    ctype = 'Y';
//                                } else if (bartitle.toUpperCase() == 'Credit'.toUpperCase()) {
//                                    ctype = 'N';
//                                } else {
//                                    ctype = 'CR';
//                                }
//                                var location = 'mischal_rep.php?series=' + series + '&count=' + value +
//                                        '&ctype=' + ctype + '&fdate=' + date +
//                                        '&tdate=' + date + '&cancel=no';
//                                window.open(location, '_blank');
//                            }
////                            alert(label + ": " + value + ':' + bartitle);
//                        };
//                        DailyBar.update();
                    },
                    error: function (request, textStatus, errorThrown) {
                    }
                });
            }


//            WeeklyChart();
//            function WeeklyChart() {
//                $('#myChart').replaceWith('<canvas id="myChart" style="position: relative; height:120vw;"></canvas>');
//                var ctx = document.getElementById('myChart').getContext('2d');
//                var weeklyfrmdt = $(".weeklyfrmdt").val();
//                var weeklytodt = $(".weeklytodt").val();
//
//                $.ajax({
//                    type: 'GET',
//                    dataType: 'json',
//                    url: "getmisweekly.php",
//                    data: {weeklyfrmdt: weeklyfrmdt, weeklytodt: weeklytodt},
//                    success: function (response) {
//
//                        var series = [], cash = [], credit = [], courier = [], chaldata = [];
//                        $.each(response, function () {
//                            series.push(this.series);
//                            cash.push(this.cash);
//                            credit.push(this.cred);
//                            courier.push(this.courier);
//                        });
//
//                        var data = {
//                            labels: series,
//                            datasets: [
//                                {
//                                    label: "Cash",
//                                    backgroundColor: "rgb(234,67,53)",
//                                    borderColor: "rgb(234,67,53)",
//                                    borderWidth: 1,
//                                    data: cash
//                                },
//                                {
//                                    label: "Credit",
//                                    backgroundColor: 'rgb(66,133,244)',
//                                    borderColor: 'rgb(66,133,244)',
//                                    borderWidth: 1,
//                                    data: credit
//                                },
//                                {
//                                    label: "Courier",
//                                    backgroundColor: "rgb(251,188,5)",
//                                    borderColor: "rgb(251,188,5)",
//                                    borderWidth: 1,
//                                    data: courier
//                                }
//                            ]
//                        };
//
//                        var chartOptions = {
//                            responsive: true,
//                            legend: {
//                                position: "top"
//                            },
//                            title: {
//                                display: true,
//                                text: "Weekly Challans",
//                                fontSize: 25
//                            },
//                            scales: {
//                                yAxes: [{
//                                        ticks: {
//                                            beginAtZero: true
//                                        }
//                                    }]
//                            }
//                        }
//                        var WeeklyBar = new Chart(ctx, {
//                            type: "bar",
//                            data: data,
//                            options: chartOptions
//                        });
//
////                        DailyBar.update();
//                    },
//                    error: function (request, textStatus, errorThrown) {
//                    }
//                });
//            }

//
//            var myChart = new Chart(ctx, {
//                type: 'pie',
//                data: {
//                    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange', 'black', 'skyblue'],
//                    datasets: [{
//                            label: '# of Votes',
//                            data: [12, 19, 3, 5, 2, 3, 4, 4.5],
//                            backgroundColor: [
//                                'rgba(255, 99, 132, 0.2)',
//                                'rgba(54, 162, 235, 0.2)',
//                                'rgba(255, 206, 86, 0.2)',
//                                'rgba(75, 192, 192, 0.2)',
//                                'rgba(153, 102, 255, 0.2)',
//                                'rgba(255, 159, 64, 0.2)',
//                                'rgba(255, 159, 64, 0.2)'
//                            ],
//                            borderColor: [
//                                'rgba(255, 99, 132, 1)',
//                                'rgba(54, 162, 235, 1)',
//                                'rgba(255, 206, 86, 1)',
//                                'rgba(75, 192, 192, 1)',
//                                'rgba(153, 102, 255, 1)',
//                                'rgba(255, 159, 64, 1)'
//                            ],
//                            borderWidth: 1
//                        }]
//                },
//                options: {
//                    scales: {
//                        yAxes: [{
//                                ticks: {
//                                    beginAtZero: true
//                                }
//                            }]
//                    }
//                }
//            });
        });
    </script>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
