<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        
        <div class="row">
            <div class="col-sm-12">
                <div class="pull-right m-t-15">     
                    <?php if(isset($startdate)){?>
                    <form method="Post" action="<?php echo base_url()."receiving_notes";?>">
                        
                    </form>
                    <?php }?>
                </div>
                <h4 class="page-title">Receiving Notes:</h4>
            </div>
        </div>
       
        <div class="row">
            <div class="col-sm-12">
                
                
                
                <div class="card-box table-responsive">
                    <h4 class="header-title m-t-0 m-b-30">Firlters</h4>
                    
                    <div class="row m-b-20" id="filters">
                        <form method="Post" action="<?php echo base_url()."receiving_notes";?>">
                            <div class="row">
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label for="supplier" class="control-label">Supplier</label>
                                    <select id="supplier" name="supplier" class="form-control" >
                                        <?php
                                        if (isset($suppliers)) {?>
                                            <option value=""></option>
                                            <?php foreach ($suppliers as $supplier) {
                                                ?>
                                                <option value="<?php echo $supplier['id_business_supplier']; ?>"><?php echo $supplier['supplier_name'].' - '.$supplier['contact_person']; ?></option>
                                                <?php
                                            }
                                        }
                                        ?>
                                    </select>
                                </div> 
                            </div>
                            
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label for="brand" class="control-label">Brand</label>
                                    <select onchange="" id="brand" name="brand" class="form-control" >
                                        <?php
                                        if (isset($brands)) {?>
                                            <option value=""></option>
                                             <?php foreach ($brands as $brand) {?>
                                            <option value="<?php echo $brand['id_business_brands']; ?>"><?php echo $brand['business_brand_name'];?></option>
                                            
                                             <?php } } ?>
                                    </select>
                                </div> 
                            </div>
                            
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label for="product" class="control-label">Products</label>
                                    <select id="product" name="product" class="form-control" onchange="">
                                        <option value=""></option>
                                    </select>
                                </div> 
                            </div>
                       
                            <div class="col-md-6">
                                <label for="supplier" class="control-label">Date Range</label>
                                <div class="form-group">
                                    
                                    <div id="reportrange" name="reportrange" class="form-control" style="display:inline">
                                        <i class="glyphicon glyphicon-calendar fa fa-calendar"></i>
                                        <span></span>
                                    </div>
                                    <input type="hidden" name="startdate" id="startdate" value="<?php echo $startdate;?>"><input type="hidden" name="enddate" id="enddate" value="<?php echo $enddate;?>">
                                    <button type="submit" class="btn btn-sm btn-pink btn-bordred waves-effect w-md waves-light m-l-5">Run</button>   
                                </div>
                            </div>
                            
                        </div>
                        </form>
                    </div>
                    <hr>
                    <div class="row">   
                        <div class="col-md-12">
                            <table id="tblreport" class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>PO ID</th>
                                        <th>Supplier</th>
                                        <th>Product ID</th>
                                        <th>Brand</th>
                                        <th>Product</th>
                                        <th>ID / Batch</th>
                                        <th>Store</th>
                                        <th>By</th>
                                        <th>Rec. Date</th>
                                        <th>Rec. Qty</th>
                                        <th>Rec. Rate</th>
                                        <th>GRN. Cost</th>
                                    </tr>
                                </thead>

                                 <tbody>
                                    <?php $grn_qty=0; $grn_cost=0; foreach ($grns as $an){?>
                                    <tr>
                                        <td><?php echo $an['idpurchase_order'];?></td>     
                                        <td><?php echo $an['supplier_name'];?></td>     
                                        <td><?php echo $an['id_business_products'];?></td>
                                        <td><?php echo $an['business_brand_name'];?></td>
                                        <td><?php echo $an['product'];?></td>
                                        <td><?php echo $an['id_batch'].' / '.$an['batch_number'];?></td>
                                        <td><?php echo $an['business_store'];?></td> 
                                        <td><?php echo $an['by'];?></td> 
                                        <td><?php echo $an['grn_created_date'];?></td> 
                                        <td style="text-align: right;"><?php echo $an['grn_qty_received']; $grn_qty=$grn_qty+$an['grn_qty_received']; ?></td> 
                                        <td style="text-align: right;"><?php echo $an['grn_unit_price'];?></td> 
                                        <td style="text-align: right;"><?php echo $an['grn_qty_received'] * $an['grn_unit_price'];$grn_cost=$grn_cost + ($an['grn_qty_received'] * $an['grn_unit_price']);  ?></td> 
                                    </tr>
                                    <?php } ?>
                                </tbody>
                                <tfoot>
                                    <th>PO ID</th>
                                        <th>Product ID</th>
                                        <th>Supplier</th>
                                        <th>Brand</th>
                                        <th>Product</th>
                                        <th>ID / Batch</th>
                                        <th>Store</th>
                                        <th>By</th>
                                        <th>Rec. Date</th>
                                        <th style="text-align: right;"><?php echo $grn_qty;?></th>
                                        <th></th>
                                        <th style="text-align: right;"><?php echo $grn_cost;?></th>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            </div><!-- end col -->
        </div>
        <!-- end row -->
        
    </div>
    <script>
        $(document).ready(function() {
            <?php if(isset($startdate)){?>
               var startdate = '<?php echo date('F j, Y', strtotime($startdate)); ?>';
                var enddate = '<?php echo date('F j, Y', strtotime($enddate)); ?>';
                $('#reportrange span').html(startdate + ' - ' + enddate);

                $('#reportrange').daterangepicker({
                    format: 'MM/DD/YYYY',
                    startDate: moment().subtract(15, 'days'),
                    endDate: moment(),
                    minDate: '01/01/2012',
                    maxDate: '12/31/2090',
        //                    dateLimit: {
        //                        days: 60
        //                    },
                    showDropdowns: true,
                    showWeekNumbers: true,
                    timePicker: false,
                    timePickerIncrement: 1,
                    timePicker12Hour: true,
                    ranges: {
                        'Today': [moment()],
                        'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                        'Last 7 Days': [moment().subtract(6, 'days'), moment().add(1, 'days')],
                        'Last 30 Days': [moment().subtract(29, 'days'), moment().add(1, 'days')],
                        'This Month': [moment().startOf('month'), moment().endOf('month')],
                        'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
                    },
                    opens: 'left',
                    drops: 'down',
                    buttonClasses: ['btn', 'btn-sm'],
                    applyClass: 'btn-success',
                    cancelClass: 'btn-default',
                    separator: ' to ',
                    locale: {
                        applyLabel: 'Submit',
                        cancelLabel: 'Cancel',
                        fromLabel: 'From',
                        toLabel: 'To',
                        customRangeLabel: 'Custom',
                        daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
                        monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
                        firstDay: 1
                    }
                }, function(start, end, label) {
                    //console.log(start.toISOString(), end.toISOString(), label);
                    startdate = start;
                    enddate = end;
                    $("#startdate").val(startdate.format('YYYY-MM-DD'));
                    $("#enddate").val(enddate.format('YYYY-MM-DD'));
                    $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
                });
            <?php } ?>
            $('#tblreport').DataTable({
                <?php if(DOWNLOAD == true || $this->session->userdata('role') == 'Admin' || $this->session->userdata('role')=='Super User'){ ?>
                    dom: "Bfrtlip",
                    buttons: [{extend: "copy", className: "btn-sm btn-custom btn-trans"}, {extend: "csv", className: "btn-sm btn-pink btn-trans"},
                        {extend: "excel", className: "btn-sm btn-warning btn-trans"}, {extend: "pdf", className: "btn-sm btn-purple btn-trans"}],
                <?php } else { ?>
                    dom: "frtlip",
                <?php } ?>     
                    responsive: !0
                });
                
                //Add code Brand product....
                $(document.body).on("change", "#brand", function() {
                    var brandid = $(this).val();
                    if (brandid && brandid != "") {
                        $(".loader").fadeIn("slow");
                        $("#product").select2('val', 'All');
                        $.ajax({
                            type: 'POST',
                            //url: '<?php //echo base_url("purchaseorder_controller/brand_product");    ?>/' + brandid,
                            url: '<?php echo base_url("purchaseorder_controller/brand_product"); ?>',
                            data: {brandid: brandid, businessid: $("#businessid option:selected").val()},
                            dataType: "json",
                            cache: false,
                            async: true,
                            success: function(data) {
                                $('#product').html('');
                                $('#product').append('<option value = ""></option>');
                                if (data.length > 0) {
                                    for (var i = 0; i < data.length; i++) {
                                        $('#product').append('<option product-price = ' + data[i]['purchase_price'] + ' value = ' + data[i]['id_business_products'] + '>' + data[i]['product'] + ' ' + data[i]['category'] +' ' + data[i]['qty_per_unit'] +' ' + data[i]['measure_unit'] +'</option>');
                                    }
                                } else {
                                    $('#product').append('<option value = ""></option>');
                                    
                                }
                            }
                        });
                        $(".loader").fadeOut("slow");
                        $("#product").select2();
                    }
                });
                
        });
            
            
            
            
            
        </script>