<?php
require_once './admin_header.php';
require_once './PHP_Code/connection.php';
?>


<style>
    th{
        border-right: 1px solid #e9ecef;
        border-left: 1px solid #e9ecef;
        border-bottom: 1px solid #e9ecef;
    }
    td{
        padding: 0px !important;
        border-right: 1px solid #e9ecef;
        border-left: 1px solid #e9ecef;
        border-bottom: 1px solid #e9ecef;
    }
</style>

<?php
$month = date('m');
$day = date('d');
$year = date('Y');

$today = $year . '-' . $month . '-' . $day;

$date = date("Y/m/d");
$st_date = $date . ' 00:00:05';
$end_date = $date . ' 23:50:05';
?>

<div class="app-main__outer">
    <div class="app-main__inner" style="padding-top: 30px;padding-bottom: 0px;padding-left: 0px;padding-right: 30px !important">
        <div class="tab-content">
            <div class="tab-pane tabs-animation fade show active" id="tab-content-0" role="tabpanel">

                <h4>Date Wise Report</h4>
                <div class="tab-content">
                    <div class="tab-pane tabs-animation fade active show" id="tab-content-0" role="tabpanel">
                        <div class="main-card mb-3 card">
                            <div class="col-lg-12">
                                <div class="form-row" style="margin-left: 20px;margin-top: 20px">
                                    <div class="col-md-2">
                                        <label for="bill_date" class="col1">Start Date</label>
                                        <input name="Bill Date" id="start_date" value="<?php echo $today; ?>" tabindex="1" placeholder="બીલ તા." type="date" class="form-control mr-sm-4">
                                        <div id="msg_bill_date"></div>
                                    </div>
                                    <div class="col-md-2">
                                        <label for="bill_date" class="col1">End Date</label>
                                        <input name="Bill Date" id="end_date" value="<?php echo $today; ?>" tabindex="1" placeholder="બીલ તા." type="date" class="form-control mr-sm-4">
                                        <div id="msg_bill_date"></div>
                                    </div>
                                    <div class="col-md-3">
                                        <button class="btn btn-primary form-control" tabindex="3" style="margin: 25px;font-size: 18px;width: auto !important" id="view_button"> <i class="fa fa-search"></i> View </button>
                                        <button class="btn btn-primary form-control" tabindex="3" style="margin: 25px;font-size: 18px;width: auto !important" id="print_button"> <i class="fa fa-print"></i> Print </button>
                                    </div>

                                </div>
                                <!--<h5 class="card-title"   style="float: none;text-align: center;margin-top: 10px">દૈનિક દુધની માહીતી - <?php echo date("d/m/Y"); ?></h5>-->
                            </div>
                            <div id="print_divtag">

                                <div class="card-body" id="broket_div" tabindex="6">
                                    <div class="col-lg-12" id="table_body">

                                         



                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<!--<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>-->
<script type="text/javascript">
    $(document).ready(function () {
        // alert('hello');
//        $('#print_button').hide();
        $("#print_show_bill").hide();

        $(document.body).on('click', '#print_button', function () {
//            $('.table td:nth-child(10)').hide();
//            $('.table th:nth-child(10)').hide();
//            printDiv();
//            $('.table td:nth-child(10)').show();
//            $('.table th:nth-child(10)').show();
            printdiv_jquery();
        });

        $(document.body).on('click', '#print_show_bill', function () {
//        $('#print_show_bill').click(function () {
//            alert('print');
            $('#show_bill').toggle();
        })

        function printdiv_jquery()
        {
            var contents = $("#print_divtag").html();
            var frame1 = $('<iframe />');
            frame1[0].name = "frame1";
            frame1.css({"position": "absolute", "top": "-1000000px"});
            $("body").append(frame1);
            var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
            frameDoc.document.open();
            //Create a new HTML document.
            frameDoc.document.write('<html><head><title>Milk Report</title>');
            frameDoc.document.write('</head><body>');
            //Append the external CSS file.
            frameDoc.document.write('<style>@media print {    body {transform: scale(1);}    table {page-break-inside: avoid;}}th{border-top: 1px solid black !important;border-bottom: 1px solid black !important;}td{padding: 0px !important;}</style> <body onload="window.print()"><link href="./main.css" rel="stylesheet"/>');
            //Append the DIV contents.
            frameDoc.document.write(contents);
            frameDoc.document.write('</body></html>');
            frameDoc.document.close();
//            setTimeout(function () {
//                window.frames["frame1"].focus();
//                window.frames["frame1"].print();
//                frame1.remove();
//            }, 500);
        }

        function printDiv()
        {
            //                                                alert('click');
            var divToPrint = document.getElementById('print_div');
//            var newWin = window.open('', '_blank');
            var newWin = window.open('', 'Print-Window');
            newWin.document.open();
            newWin.document.write('<html><style>table{border: 1px solid black}label{color: black;}th{border: 1px solid black !important}td{border: 1px solid black !important}</style> <body onload="window.print()"><link href="./main.css" rel="stylesheet">' + divToPrint.innerHTML + '</body></html>');
//            newWin.document.write(divToPrint.innerHTML);
            newWin.document.close();
            setTimeout(function () {
                newWin.close();
            }, 10);
        }

        function print_data()
        {
            var printme = document.getElementById('print_div');
            var wme = window.open("", "", "width=900,height=700");
            wme.document.write(printme.outerHTML);
            wme.document.close();
            wme.focus();
            wme.print();
            wme.close();
        }

        $(document.body).on('blur', '#select_customer_name', function () {
            if ($("#select_customer_name").val() == "Select")
            {
                $("#msg_select_cust_name").fadeIn(500);
                $("#msg_select_cust_name").html("* Customer Name Not Selected").css({'color': 'red'});
                $("#msg_select_cust_name").focusin();
            } else
            {
                $("#msg_select_cust_name").fadeOut(1000);
            }
        });

        $(document.body).on('blur', '#start_date', function () {
            if ($("#start_date").val() == "")
            {
                $("#msg_start_date").fadeIn(500);
                $("#msg_start_date").html("* Start Date Not Selected").css({'color': 'red'});
                $("#msg_start_date").focusin();
            } else
            {
                $("#msg_start_date").fadeOut(1000);
            }
        });

        $(document.body).on('blur', '#end_date', function () {
            if ($("#end_date").val() == "")
            {
                $("#msg_end_date").fadeIn(500);
                $("#msg_end_date").html("* End Date Not Selected").css({'color': 'red'});
                $("#msg_end_date").focusin();
            } else
            {
                $("#msg_end_date").fadeOut(1000);
            }
        });

        function pad2(n) {
            return (n < 10 ? '0' : '') + n;
        }

        $(document.body).on('click', '#view_button', function () {

//            var customer_id = $("#select_customer_name").children(":selected").attr("id");
            var end_date = $("#end_date").val();
            var start_date = $("#start_date").val();

            if ($("#start_date").val() == "")
            {
                $("#msg_start_date").fadeIn(500);
                $("#msg_start_date").html("* Start Date Not Selected").css({'color': 'red'});
                $("#msg_start_date").focusin();
            } else if ($("#end_date").val() == "")
            {
                $("#msg_end_date").fadeIn(500);
                $("#msg_end_date").html("* End Date Not Selected").css({'color': 'red'});
                $("#msg_end_date").focusin();
            } else
            {

                debugger;
                var date = new Date(start_date);
                var month = pad2(date.getMonth() + 1);//months (0-11)
                var day = pad2(date.getDate());//day (1-31)
                var year = date.getFullYear();

                var start_date_text = day + '/' + month + '/' + year;
                $("#title_name_date").html("");

                var date1 = new Date(end_date);
                var month1 = pad2(date1.getMonth() + 1);//months (0-11)
                var day1 = pad2(date1.getDate());//day (1-31)
                var year1 = date1.getFullYear();

                var end_date_text = day1 + '/' + month1 + '/' + year1;
//                alert('end_date ' + end_date);
//                alert('end_date_text ' + end_date_text);

                $("#title_name_date").html("");
                $("#title_name_date").html("<b>દુધની માહીતી - તારીખ " + start_date_text + " થી " + end_date_text + " સુધી.</b>");
                var header_text = "<b>દુધની માહીતી - તારીખ " + start_date_text + " થી " + end_date_text + " સુધી.</b>";

                var send = {'start_date_month': start_date, 'end_date_month': end_date, 'header_date': header_text};
                $.ajax({
                    url: './PHP_Code/Date_wise_report.php',
                    type: 'POST',
                    data: send,
                    success: function (data)
                    {
                        debugger;
//                            alert('data ' + data);
                        $('#table_body').html('');
                        $('#table_body').html('');
                        $('#table_body').html(data);

                    },
                    error: function (err) {
                        alert('error ' + err);
                    }
                });
            }
        });

        function clear()
        {
            $("#select_customer_name").val("Select");
        }
    });

</script>

<?php
require_once './admin_footer.php';
?>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Confirmation Message</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p class="mb-0">Are you sure to delete this record ?</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="btn_mode_hide" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-danger" id="btn_delete_confirm">Delete</button>
            </div>
        </div>
    </div>
</div>
