<?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{
        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;
?>

<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">
                <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="card-body">
                                <form class="form-inline mb-3">
                                    <label class="card-title col-md-2">All Bill</label>
                                    <label class="card-title col-md-2"></label>
                                    <div class="mb-2 mr-sm-2 mb-sm-0 position-relative form-group ">
                                        <label for="Bill Date" class="mr-sm-2">Start Date - </label>
                                        <input name="Bill Date" id="bill_date" value="<?php echo $today; ?>" tabindex="1" placeholder="Bill Date" type="date" class="form-control mr-sm-4">
                                        <label for="Bill Date" class="mr-sm-2"> End Date - </label>
                                        <input name="Bill Date" id="end_bill_date" value="<?php echo $today; ?>" tabindex="2" placeholder="Bill Date" type="date" class="form-control">

                                    </div>

                                    <button class="btn btn-primary" type="button" tabindex="3" id="btn-show">Show</button>
                                    <div class="col-md-12" style="text-align: center" id="msg_bill_date"></div>
                                </form>


                                <div class="row">

                                    <div class="col-lg-12">
                                        <?php
                                        $date = date("Y/m/d");
                                        $st_date = $date . ' 00:00:05';
                                        $end_date = $date . ' 23:50:05';
//                                        $query = 'select * from tbl_party where bill_date="'.$date.'" and flag="1"';
                                        $query = 'select * from tbl_party where bill_date BETWEEN "' . $st_date . '" and "' . $end_date . '" and flag="1"';
//                                        echo 'query '.$query;
                                        ?>
                                        <table class="mb-0 table table-hover">
                                            <thead>
                                                <tr>
                                                    <th>#</th>
                                                    <th>Bill Date</th>
                                                    <th>Bill No</th>
                                                    <th>Vehicle No</th>
                                                    <th>State-Code</th>
                                                    <th>Quantity</th>
                                                    <th>Unit Price</th>
                                                    <th>CGST</th>
                                                    <th>SGST</th>
                                                    <th>IGST</th>
                                                    <th>Amount</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody id="table_body">
                                                <?php
                                                $result = mysqli_query($con, $query);
                                                if (mysqli_num_rows($result) > 0) {
                                                    $i = 1;
                                                    $unit_price = 0;
                                                    $cgst = 0;
                                                    $sgst = 0;
                                                    $igst = 0;
                                                    $amount = 0;
                                                    while ($row = mysqli_fetch_assoc($result)) {
                                                        $amount = $amount + $row["amount"];
                                                        $cgst = $cgst + $row["CGST"];
                                                        $sgst = $sgst + $row["SGST"];
                                                        $igst = $igst + $row["IGST"];
                                                        $unit_price = $unit_price + $row["quantity"] * $row["unit_price"];
                                                        ?>
                                                        <tr>
                                                            <th scope="row"><?php echo $i; ?></th>
                                                            <td><?php echo date("d-m-Y", strtotime($row["bill_date"])); ?></td>
                                                            <td><?php echo 'INVOICE ' . $row["bill_no"]; ?></td>
                                                            <td><?php echo $row["vehicle_no"]; ?></td>
                                                            <td><?php echo $row["state_code"]; ?></td>
                                                            <td><?php echo $row["quantity"]; ?></td>
                                                            <td><?php echo $row["unit_price"]; ?></td>
                                                            <td><?php echo $row["CGST"]; ?></td>
                                                            <td><?php echo $row["SGST"]; ?></td>
                                                            <td><?php echo $row["IGST"]; ?></td>
                                                            <td><?php echo $row["amount"]; ?></td>
                                                            <td><a href="JavaScript:Void(0);" class="btn_print_view" tabindex="4" id="<?php echo $row["id"]; ?>"><i class="fa fa-print"> View</i></a></td>
                                                        </tr>
                                                        <?php
                                                        $i = $i + 1;
                                                    }
                                                    ?>
                                                    <tr>
                                                        <td colspan="5" style="text-align: right"><b>Total</b></td>
                                                        <td></td>
                                                        <td><b><?php echo $unit_price; ?></b></td>
                                                        <td><b><?php echo $cgst; ?></b></td>
                                                        <td><b><?php echo $sgst; ?></b></td>
                                                        <td><b><?php echo $igst; ?></b></td>
                                                        <td><b><?php echo $amount; ?></b></td>
                                                        <td></td>
                                                    </tr>
                                                    <?php
                                                } else {
                                                    echo "0 results";
                                                }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">

    $(document).ready(function () {


        $('#btn-show').click(function () {
            var start_bill_date = $("#bill_date").val();
            var end_bill_date = $("#end_bill_date").val();
//            alert('start_bill_date '+start_bill_date+' end_bill_date '+end_bill_date);

            var send = {'start_bill_date': start_bill_date, 'end_bill_date': end_bill_date};

            if (start_bill_date == '')
            {
                $("#msg_bill_date").html(" * Select Bill Start Date").css({'color': 'red'});
            } else if (end_bill_date == '')
            {
                $("#msg_bill_date").html(" * Select Bill End Date").css({'color': 'red'});
            } else
            {
//                alert('Success ');
                debugger;
                $.ajax({
                    url: './PHP_Code/action_register.php',
                    type: 'POST',
                    data: send,
                    success: function (data)
                    {
                        debugger;
//                        alert('data ' + data);
                        $('#table_body').html('');
                        $('#table_body').html(data);
//                        $('#print_button').fadeIn(1000);

//                        $('html, body').animate({
//                            scrollTop: $("#btn_generate_bill").offset().top
//                        }, 500);
                    },
                    error: function (err) {
                        alert('error ' + err);
                    }
                });
            }
        });



        $(document.body).on('click', '.btn_print_view', function () {
//            alert('view print ' + $(this).attr('id'));
            var leez_id = $(this).attr('id');
            var send = {'leez_id': leez_id}
            if (leez_id == '')
            {
                alert('Please Select Valid Record!');
            } else
            {
                $.ajax({
                    url: './PHP_Code/action_register.php',
                    type: 'POST',
                    data: send,
                    success: function (data)
                    {
                        debugger;
//                        alert('data ' + data);
                        var url = "http://localhost/E_Bill_Software/view_bill.php";
                        window.open(url, '_blank');
                    },
                    error: function (err) {
                        alert('error ' + err);
                    }
                });
            }
        });
    })

</script>

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