<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <h4 class="page-title">Cash Expenses:</h4>
                <div class="btn-group pull-right m-t-15">
                    <button type="button"  onclick="openaddnew();" class="btn btn-custom waves-effect waves-light" >Create New <span class="m-l-5"><i class="fa fa-plus"></i></span></button>
                </div>
            </div>
        </div>
        
        <div class="row">
            <div class="col-sm-12" >
                <div class="card-box">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="row pull-right">
                                <div class="col-md-12">
                                    <form action="<?php echo base_url(); ?>product_orders" method="post" class="form-inline">                               
                                        <div class="form-inline">
                                            <div class="form-group">
                                                <label for="orderstatus" class="control-label">Order Status</label>
                                                <select class="form-control m-r-10" id="orderstatus" name="order_status" >
                                                    <option <?php if(isset($status)){if($status == 'open'){echo 'selected="selected"';}}?> value="open">Open</option>
                                                    <option <?php if(isset($status)){if($status == 'invoiced'){echo 'selected="selected"';}}?>  value="invoiced">Invoiced</option>
                                                    <option <?php if(isset($status)){if($status == 'shipped'){echo 'selected="selected"';}}?>  value="shipped">Shipped</option>
                                                    <option <?php if(isset($status)){if($status == 'delivered'){echo 'selected="selected"';}}?>  value="delivered">Delivered</option>
                                                    <option <?php if(isset($status)){if($status == 'cancelled'){echo 'selected="selected"';}}?>  value="cancelled">Cancelled</option>
                                                </select>

                                                <label for="date_range" class="control-label m-l-10">Select Date Range</label>
                                                <input style="width: 250px" name="date_range" id="reportrange" class="form-control" type="text" value="<?php if(isset($date_range)){echo $date_range;}?>"  />

                                                <button type="submit" class="btn btn-primary btn waves-effect waves-light  m-l-10">Search</button>
                                            </div> 
                                        </div>
                                    </form>
                                </div>
                            </div>
                            <div class="row pull-left">
                                <div class="col-md-12">
                                    <h4 class="header-title"><b>Fliter Orders</b></h4>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>

        <div class = "row">
            <div class="col-sm-12" >
                <div class="card-box">
                    <div class="row">
                        <h4 class="header-title m-t-0 m-b-30">&nbsp;</h4>
                        
                        <table id="tblorders" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                            <thead>
                                <tr>
                                    <th>Order ID</th>
                                    <th>Order Date</th>
                                    <th>Customer</th>
                                    <th>Product Name</th>
                                    <th>Quantity</th>
                                    <th>Unit Price</th>
                                    <th>Price</th>

                                    <th>Status</th>                               
                                    <th>Balance</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($orders as $order): ?>
                                <tr>
                                    <td><?php echo $order['id_customer_order']; ?></td>
                                    <td><?php echo $order['mDate']; ?></td>
                                    <td><?php echo $order['customer_name']; ?></td>
                                    
                                    <td><?php echo $order['product_name']; ?></td>                                    
                                    
                                    <td><?php echo $order['qty']; ?></td>
                                    <td><?php echo $order['price']; ?></td>
                                    <td><?php echo $order['price'] * $order['qty']; ?></td>
                                    
                                    <td><strong><?php echo $order['order_status'];?></strong></td>
                                    <td><?php echo $order['balance']; ?></td>
                                    <td>
                                    <?php if(TRACK_INVENTORY==true){?>
                                        <?php if($order['order_status'] == 'open'){ ?>
                                            <form action="open_order_invoice" method="post" target="_blank">
                                                <input type="hidden" class="form-control" id="order-id" name="order-id" value="<?php echo $order['id_customer_order']; ?>">
                                                <input type="hidden" name="csrf_test_name" id="retail_modal_csrf" value=""/>                                                
                                                <button id="btngenorderinvoice" onclick="$('#retail_modal_csrf').val($('#cook').val());" class="btn btn-sm btn-pink waves-effect waves-light">Generate Invoice</button>                                                
                                            </form>
                                        <?php } else if($order['order_status']=='invoiced' && $order['balance'] > 0) { ?>

                                            <a target="_blank" href="<?php echo base_url().'open_recovery_order_invoice/'.$order['id_invoice'].'/'.$order['visit_id']; ?>" class="btn btn-purple btn btn-xs waves-effect w-md waves-success m-b-5"> <span>Recover</span> </a>

                                        <?php } else if($order['order_status']=='invoiced' && $order['balance'] <= 0) { ?>

                                             <button type="button" class="btn btn-info btn btn-xs waves-effect w-md waves-success m-b-5">Mark Delivered</button>    

                                        <?php } else { ?>
                                            <button type="button" onclick="vieworder(<?php echo $order['id_customer_order']; ?>, <?php echo $order['customer_id']; ?>);" order_id="<?php echo $order['id_customer_order']; ?>" customer_id="<?php echo $order['customer_id']; ?>" class="btn btn-warning btn btn-xs waves-effect w-md waves-success m-b-5 viewOrder">View</button>
                                        <?php } ?>
                                    <?php } ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                
                
                    </div>
                </div>
            </div>                
        </div>

    </div>    
</div>


<script>

$(document).ready(function() {

$('#reportrange').daterangepicker({
            format: 'MM/DD/YYYY',
            
            minDate: '01/01/2012',
            maxDate: '12/31/2099',
            dateLimit: {
                days: 183
            },
            showDropdowns: true,
            showWeekNumbers: true,
            timePicker: false,
            timePickerIncrement: 1,
            timePicker12Hour: true,
            ranges: {
                'Today': [moment(), moment().add(1, 'days')],
                'Yesterday': [moment().subtract(1, 'days'), moment()],
                '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')],
                'Last Two Months': [moment().subtract(2, 'month').startOf('month'), moment().subtract('month').endOf('month')],
                'Last Four Months': [moment().subtract(4, 'month').startOf('month'), moment().subtract('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;
        //     $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
        });


});

</script>