<!-- Main Content -->

<div class="page-wrapper">

    <div class="container-fluid pt-25">

        <div class="row">

            <div class="col-md-12">

                <div class="alert alert-success alert-dismissible" role="alert" <?php if($this->session->flashdata('message')==''){ ?> style="display:none" <?php } ?> >

                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>

                    <?php echo $this->session->flashdata('message'); ?>

                </div>

            </div>

        </div>

        <!-- Row -->
        <?php $order_status = $this->config->item("order_status"); ?>
        <div class="row">

            <div class="col-sm-12">

                <div class="panel panel-default card-view">

                    <div class="panel-heading">

                        <div class="pull-left">

                            <h6 class="panel-title txt-dark">Order List</h6>

                        </div>
                        <span class="pull-right">
                            <div class="col-md-4">
                                <form action="<?php echo base_url(A.'/order-list'); ?>" method="get">
                                    <select name="order_status" class="form-control" onchange="this.form.submit()">
                                        <option value="">Select</option>
                                        <option value="">All</option>
                                        <?php if(!empty($order_status)){ foreach ($order_status as $key1 => $value1) : ?>
                                            <option value="<?php echo $key1; ?>"><?php echo $value1; ?></option>
                                        <?php endforeach; } ?>
                                    </select>  
                                </form>
                            </div>
                            <div class="col-md-4">
                                <a class="btn btn-info" data-toggle="modal" data-target="#searchModal"><i class="fa fa-search"></i>  Search</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            </div>
                            <div class="col-md-4">
                                <?php if($this->admin_type=='superadmin' || (in_array('4.4', $this->authority))){ ?>
                                    <a href="<?php echo base_url(A."/orderInvoiceExcel"); ?>" class="btn btn-info">Export <i class="fa fa-file-pdf-o" style="color:#fff;"></i></a>
                                <?php } ?>
                            </div>
                        </span>

                        <div class="clearfix"></div>

                    </div>

                    <div class="panel-wrapper collapse in">

                        <div class="panel-body">

                            <div class="table-wrap">

                                <div class="table-responsive">
                                    <table id="datable_3" class="table table-hover">

                                        <thead>

                                            <tr>
                                                <!-- <th><input type="checkbox" name="orderid" class="check-one"/></th> -->
                                                <th>Sr. No.</th>

                                                <th>Order id</th>
                                                <th>Invoice No.</th>

                                                <th>Order Date</th>

                                                <th>Price</th>

                                                <th>Order Status</th>
                                                <th>Comment</th>
                                                <th>Payment Method</th>

                                                <th>Payment Status</th>
                                                <th>TxnId</th>

                                                <th>Action</th>

                                            </tr>

                                        </thead>

                                        <tbody>

                                            <?php 
                                             if($orders){ foreach ($orders as $key => $value) : ?>

                                                <tr id="<?php echo "orders_".$value['orderid']; ?>">

                                                    <!-- <td><input type="checkbox" data-id="<?php //echo $value['orderid'] ?>" class="check-all"/></td> -->

                                                    <td><?php echo ($key+1); ?></td>

                                                    <td>
                                                        <?php if($this->admin_type=='superadmin' || (in_array('4.3', $this->authority))){ ?>
                                                            <a href="<?php echo base_url(A."/order-invoice/".$value['orderid']); ?>"><?php echo $value['orderid']; ?></a>
                                                        <?php }else{ ?>
                                                            <?php echo $value['orderid']; ?>
                                                        <?php } ?>
                                                    </td>
                                                    <td>
                                                        <?php echo $value['invoice_no']; ?>
                                                    </td>

                                                    <td><?php echo date("d M Y h:i A",strtotime($value['order_time'])); ?></td>

                                                    <td><?php echo $value['grandtotal']; ?></td>

                                                    <td>
                                                        <?php if($value['order_status']==4){ ?>
                                                            Delivered
                                                        <?php }else if($value['order_status']==6){ ?>
                                                            Refunded
                                                        <?php }else if($value['order_status']==5){ ?>
                                                            Cancelled
                                                        <?php }else{ ?>
                                                            <select name="order_status" class="form-control changeOrderStatus" data-orderid="<?php echo $value['orderid']; ?>" data-osname="<?php echo (array_key_exists($value['order_status'], $order_status)?$order_status[$value['order_status']]:""); ?>" style="height: auto;">
                                                                <!-- <?php //if($value['order_status']==5){ ?>
                                                                    <option selected disabled>Order Canceled</option> 
                                                                <?php //} ?> -->
                                                                <?php if(!empty($order_status)){ foreach ($order_status as $key1 => $value1) : ?>
                                                                    <?php //if($key1!=5){ ?>
                                                                        <option value="<?php echo $key1; ?>" <?php if($key1==$value['order_status']){ ?> selected="selected" <?php } ?> ><?php echo $value1; ?></option>
                                                                    <?php //} ?>
                                                                <?php endforeach; } ?>
                                                            </select>  
                                                        <?php } ?>
                                                    </td>
                                                    <td>
                                                        <!-- <a href="#" class="admin_comment" data-pk="<?php //echo $value['id']; ?>" data-type="textarea" data-url="<?php //echo base_url(A.'/editOrderAdminCommentAction'); ?>" data-name="admin_comment" data-id="<?php //echo $value['id']; ?>" data-title="Enter Comment" data-placehol><?php //echo $value['admin_comment']; ?></a> -->
                                                        <a href="javascript:void(0);" data-id="<?php echo $value['id']; ?>" class="orderComment" data-comment="<?php echo $value['admin_comment']; ?>"><?php echo ($value['admin_comment']=='')?'Add Comment':'View'; ?></a>
                                                    </td>
                                                    <td>
                                                        <?php if($value['payment_method']==1 || $value['payment_method']=='COD'){ ?>
                                                            COD
                                                        <?php } ?>
                                                        <?php if($value['payment_method']==2 || $value['payment_method']=='paynow'){ ?>
                                                            Online
                                                        <?php } ?>
                                                        <?php if($value['payment_method']==3 || $value['payment_method']=='omise'){ ?>
                                                            Omise
                                                        <?php } ?>
                                                    </td>
                                                    <td>
                                                        <?php if($value['payment_status']=='Pending'){ ?>
                                                            <!-- <form action="<?php //echo base_url(A.'/updateOrderPaymentStatus'); ?>" method="post">
                                                                <input type="hidden" name="orderid" value="<?php //echo $value['orderid']; ?>" /> -->
                                                                <select name="payment_status" class="form-control changePaymentStatus" data-orderid="<?php echo $value['orderid']; ?>" style="height: auto;">
                                                                    <option value="Pending">Pending</option>
                                                                    <option value="Complete">Complete</option>
                                                                </select>
                                                            <!-- </form> -->
                                                        <?php }else{ ?>
                                                            Completed
                                                        <?php } ?>
                                                    </td>
                                                    <td>
                                                        <?php echo $value['txn_id']; ?>
                                                    </td>

                                                    <th>
                                                        <?php if($this->admin_type=='superadmin' || (in_array('4.5', $this->authority))){ ?>
                                                            <a href="javascript:void(0)" class="order_delete" data-table="orders" data-value="<?php echo "orderid_".$value['orderid']; ?>"> 
                                                                <button class="btn btn-danger btn-icon-anim btn-square">
                                                                    <i class="fa fa-trash fa-lg"></i>
                                                                </button>
                                                            </a>
                                                        <?php } ?>
                                                    </th>

                                                </tr>

                                            <?php endforeach; } ?>

                                        </tbody>

                                    </table>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>  

            </div>

        </div>

        <!-- /Row -->

    </div>
<script type="text/javascript" src="<?php echo base_url('assets/admin/vendors/bower_components/x-editable/dist/bootstrap3-editable/js/bootstrap-editable.js'); ?>"></script>
<script src="<?php echo base_url('assets/admin/dist/js/form-xeditable-data.js'); ?>"></script>
<script type="text/javascript">
    $('.admin_comment').editable({
        validate: function(value) {
            var letters = /^[A-Za-z]+$/;
            //if($.trim(value) == '') return 'This field is required';
            //if(!value.match(letters)) return 'This field only contain alphabets'
        }
    });
</script>
<div id="searchModal" class="modal fade" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Search</h4>
        </div>
        <form action="<?php echo base_url(A."/order-list"); ?>" method="get" enctype="multipart/form-data">
          <div class="modal-body">
              <div class="form-group">
                <label>Orderid</label>
                <input type="text" name="orderid" class="form-control" placeholder="Enter Orderid" />
              </div>
              <div class="form-group">
                <label>Invoice No.</label>
                <input type="text" name="invoice_no" class="form-control" placeholder="Enter Invoice No." />
              </div>
          </div>
          <div class="modal-footer">
            <input type="submit" class="btn btn-success" value="Submit">
          </div>
        </form>
      </div>

    </div>
</div>
<div class="modal fade" id="pmModal" tabindex="-1" role="dialog" aria-labelledby="pmModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="pmModalLabel">Payment Status</h5>
        <button type="button" class="close closeOs" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form action="<?php echo base_url(A.'/updateOrderPaymentStatus'); ?>" method="post">
          <input type="hidden" name="orderid" id="oid" value="" />
          <input type="hidden" name="payment_status" id="ostatus" value="" />
          <div class="modal-body text-center">
            <h4>Mark payment as completed? You will not be able to reverse this action.</h4>
          </div>
          <div class="modal-footer" style="text-align:center;">
            <button type="submit" class="btn btn-primary">Yes</button>
            <button type="button" class="btn btn-danger closeOs">No</button>
          </div>
      </form>
    </div>
  </div>
</div>
<div class="modal fade" id="osModal" tabindex="-1" role="dialog" aria-labelledby="osModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="osModalLabel">Order Status</h5>
        <button type="button" class="close closeOs" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form action="<?php echo base_url(A.'/updateOrderStatus'); ?>" method="post">
          <input type="hidden" name="orderid" id="osid" value="" />
          <input type="hidden" name="order_status" id="osstatus" value="" />
          <div class="modal-body text-center">
            <h4>Mark delivery as <span id="osname"></span>? You will not be able to reverse this action.</h4>
          </div>
          <div class="modal-footer" style="text-align:center;">
            <button type="submit" class="btn btn-primary">Yes</button>
            <button type="button" class="btn btn-danger closeOs">No</button>
          </div>
      </form>
    </div>
  </div>
</div>
<div class="modal fade" id="ocModal" tabindex="-1" role="dialog" aria-labelledby="ocModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="ocModalLabel">Comment</h5>
        <button type="button" class="close" aria-label="Close"  data-dismiss="modal">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form action="<?php echo base_url(A.'/updateOrderComment'); ?>" method="post">
          <input type="hidden" name="id" id="ocid" value="" />
          <div class="modal-body text-center">
            <textarea class="form-control" name="admin_comment" id="ocText"></textarea>
          </div>
          <div class="modal-footer" style="text-align:center;">
            <button type="submit" class="btn btn-primary">Update</button>
          </div>
      </form>
    </div>
  </div>
</div>
<script type="text/javascript">
    $('.orderComment').on('click', function () {
        var oid = $(this).attr('data-id');
        var text = $(this).attr('data-comment');
        $('#ocid').val(oid);
        $('#ocText').val(text);
        // $('#pmModal').modal('show');
        $('#ocModal').modal({
            backdrop: 'static',
            keyboard: false
        });
    });
    $('.changePaymentStatus').on('change', function () {
        var oid = $(this).attr('data-orderid');
        var status = $(this).val();
        $('#oid').val(oid);
        $('#ostatus').val(status);
        // $('#pmModal').modal('show');
        $('#pmModal').modal({
            backdrop: 'static',
            keyboard: false
        });
    });
    $('.changeOrderStatus').on('change', function () {
        var osid = $(this).attr('data-orderid');
        var osstatus = $(this).val();
        if(osstatus==8){
            var osname = "On the way";
        }else if(osstatus==6){
            var osname = "Refunded";
        }else if(osstatus==4){
            var osname = "Delivered";
        }else if(osstatus==5){
            var osname = "Cancelled";
        }else{
            var osname = "Pending";
        }
        $('#osid').val(osid);
        $('#osname').text(osname);
        $('#osstatus').val(osstatus);
        // $('#osModal').modal('show');
        $('#osModal').modal({
            backdrop: 'static',
            keyboard: false
        });
    });
    $('.closeOs').on('click', function () {
        location.reload();
    });

    // $(window).on('load',function(){
    //     var base = "<?php //echo base_url(); ?>/<?php //echo A; ?>";
    //     var ids = "<?php //echo implode($zorderid, ','); ?>";
    //     $('#invoiceExcel').attr('href',base+'/orderInvoiceExcel?orderid='+ids);
    // });
</script>

<script type="text/javascript">
    $(".check-one").on("click",function(){ 
        if($(this).is(":checked")) { 
            $(this).closest("table").find('.check-all').prop('checked',true); 
        }else{ 
            $(this).closest("table").find('.check-all').prop('checked',false); 
        } 
    });

    $("#downloadZip").on("click",function(){
        var table_id = $(this).data('tableid');
        var table = $(this).data('table');
        var field = $("#"+table_id).find('.check-one').attr('name');
        var id = [];var length=0;
        $("#"+table_id+" tbody").find("input[class='check-all']:checked").each(function(){            
            id.push($(this).data("id"));
            length++;
        });

        var ids = id.join(",");
        if(ids!=""){
            var base = "<?php echo base_url(); ?><?php echo A; ?>";
            var url = base+"/downloadOrderInvoicePdfZip?orderid="+ids;
            window.location = url;
        }else{
            alert("Select atleast one checkbox!");
            $(this).val('');
        }
    });
</script>

<script type="text/javascript">

    var site_url = "<?php echo base_url(); ?>";
    var urlLink = "<?php echo A; ?>";

    $(".order_delete").on("click",function(){

        var id = $(this).attr("data-value");

        var split = id.split("_");

        var table = $(this).attr("data-table");

        var image = ($(this).attr("data-image")=="undefined")?"":$(this).attr("data-image");

        var result = confirm("Are you sure you want to delete?");

        if(result) {

            $.ajax({

                url: site_url+urlLink+"/orderDelete",

                type: "POST",

                data: split[0]+"="+split[1]+"&table="+table+"&image="+image,

                success: function(result){

                    if(result==1)

                    {

                        $("#"+table+"_"+split[1]).remove();

                    }

                }

            });

        }

    });
</script>
