<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        
        <div class="row">
            <div class="col-sm-12">
                <div class="pull-right m-t-15">     
                   
                </div>
                <h4 class="page-title">Return Notes:</h4>
            </div>
        </div>
       
        <div class="row">
            <div class="col-sm-12">
                <div class="card-box table-responsive">

                     <div class="row m-b-20" id="filters">
                        <form method="Post" action="<?php echo base_url()."return_notes";?>">
                            <div class="row">
                            
                            <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="reportrange" 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>

                    <table id="tblreport" class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>ID</th>
                                
                                <th>Product ID</th>
                                <th>Brand</th>
                                <th>Product</th>
                                <th>ID / Batch</th>                               
                                <th>Store</th>
                                <th>By</th>
                                 <th>Return. Date</th>
                                <th>Return. Qty</th>
                                <th>PO ID</th>
                                <th>GRN ID</th>
                                <th>Return Value</th>
                            </tr>
                        </thead>

                         <tbody>
                            <?php $return_value=0; $return_qty=0; foreach ($return_notes as $rn){?>
                            <tr>
                                <td><?php echo $rn['id_return_notes'];?></td>                                
                                <td><?php echo $rn['id_business_products'];?></td>
                                <td><?php echo $rn['business_brand_name'];?></td>
                                <td><?php echo $rn['product'];?></td>
                                <td><?php echo $rn['id_batch'].' / '.$rn['batch_number'];?></td>
                                <td><?php echo $rn['business_store'];?></td> 
                                <td><?php echo $rn['by'];?></td> 
                                <td><?php echo $rn['return_note_date'];?></td> 
                                <td style="text-align: right;"><?php echo $rn['return_qty']; $return_qty=$return_qty+$rn['return_qty']; ?></td> 
                                <td><?php echo $rn['purchase_order_id'];?></td>
                                <td><?php echo $rn['grn_id'];?></td>
                                <td><?php echo $rn['return_value']; $return_value=$return_value+$rn['return_value'];?></td>
                            </tr>
                            <?php } ?>
                        </tbody>
                        <tfoot>
                             <th>ID</th>
                                
                                <th>Product ID</th>
                                <th>Brand</th>
                                <th>Product</th>
                                <th>ID / Batch</th>
                                
                                <th>Store</th>
                                <th>By</th>
                                 <th>Return. Date</th>
                                <th>Return. Qty</th>
                                <th>PO ID</th>
                                <th>GRN ID</th>
                                <th><strong><?php echo $return_value; ?></strong></th>
                        </tfoot>
                    </table>
                </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({
                    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"}],
                    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>