<?php
session_start();
include_once "../config.php";
require_once '../database1.php';
require_once "../check_admin.php";

class userOrder extends database
{
    public function getAll($status, $customerID, $fromDate, $toDate, $orderID, $productID, $page)
    { 
        $where = array();
        $whereHTML = '';
        if($status != null)
        {
            $whereHTML.= ' AND c.cl_status=? ';
            $where[] = $status;
        }

        if($customerID != null)
        {
            $whereHTML.= ' AND c.cl_user_id=? ';
            $where[] = $customerID;
        }
        
        if($fromDate != null)
        {
            $whereHTML.= ' AND DATE(c.cl_created_at)>=? ';
            $where[] = date("Y-m-d", strtotime($fromDate));
        }
        
        if($toDate != null)
        {
            $whereHTML.= ' AND DATE(c.cl_created_at)<=? ';
            $where[] = date("Y-m-d", strtotime($toDate));
        }
        
        if($orderID != null)
        {
            $whereHTML.= ' AND c.cl_id=? ';
            $where[] = $orderID;
        }
        
        if($productID != null)
        {
            $whereHTML.= ' AND (p.cl_ean=? OR p.cl_product_id=?) ';
            $where[] = $productID;
            $where[] = $productID;
        }
        
        //return $this->fetchAll('tb_checkout', $where, 'cl_created_at', 'DESC', ($page - 1), 500);
        $start = $page - 1;
        
        $sql = ' SELECT c.* FROM tb_checkout c INNER JOIN tb_checkout_item ci ON c.cl_id=ci.cl_checkout_id INNER JOIN tb_product p ON ci.cl_product_id=p.cl_id '.
                ' WHERE c.cl_deleted_at IS null ' . $whereHTML . ' GROUP BY c.cl_id ORDER BY c.cl_created_at DESC LIMIT ' . $start * 500 . ',' . 500;
        
        return $this->callQuery($sql, $where);
    }
    
    public function getUser($uid)
    { 
        $where['id']=$uid;
        return $this->fetchRow('user',$where);
    }
}
$msg='';
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}

$obj = new userOrder();	
$status = null;
$customerID = null;
$fromDate = null;
$toDate = null;
$orderID = null;
$productID = null;
if(isset($_POST['data']))
{
    $data = $_POST['data'];
    $status = $data['status'];
    $customerID = $data['customer_id'];
    
    if($data['from_date'] != '')
    {
        $fromDate = $data['from_date'];
    }
    
    if($data['to_date'] != '')
    {
        $toDate = $data['to_date'];
    }
    
    if($data['orderID'] != '')
    {
        $orderID = $data['orderID'];
    }
    
    if($data['productID'] != '')
    {
        $productID = $data['productID'];
    }
    
    //$userOrders = $obj->getAll($status, $customerID, $fromDate, $toDate, $page);
}


$page = 1;
if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 1)
{
    $page = htmlspecialchars($_GET['page']);
}

$userOrders = $obj->getAll($status, $customerID, $fromDate, $toDate, $orderID, $productID, $page);
include '../header.php';
?>
            <!-- MAIN CONTENT-->
            <div class="main-content">
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="overview-wrap">
                                    <h2 class="title-1">All User Orders</h2>
                                    
                                </div>
								
                            </div>
                        </div>
                        
                        <form class="row m-t-25 m-row col-sm-12" method="POST">
							<div class="col-lg-12">
                                                         
                                <div class="table--no-card m-b-30 top-margin pull-left col-sm-12">
                                    <div class="well row" >
                                        <div class="col-sm-3 col-xs-12">
                                            <?php
                                            if($fromDate != null)
                                            {
                                                $fromDate = date("d M Y", strtotime($fromDate));
                                            }
                                            ?>
                                            <input type="text" class="datepicker form-control"
                                            value="<?php echo $fromDate ?>" name="data[from_date]" placeholder="Order From Date"/></div>
                                         <div class="col-sm-3 col-xs-12">
                                             
                                             <?php
                                            if($toDate != null)
                                            {
                                                $toDate = date("d M Y", strtotime($toDate));
                                            }
                                            ?>
                                             <input type="text" 
                                                    value="<?php echo $toDate ?>"
                                                    class="datepicker form-control" name="data[to_date]" placeholder="Order To Date"/></div>
                                          <div class="col-sm-3 col-xs-12">
                                              <select class="form-control" name="data[status]">
                                                  <option value="">---Select Status---</option>
                                                   <option value="Processing" <?php if($status == 'Processing'){echo 'selected';} ?>>Processing</option>
                                                   <option value="Processing2" <?php if($status == 'Processing2'){echo 'selected';} ?>>Processing2</option>
                                                   <option value="Processing3" <?php if($status == 'Processing3'){echo 'selected';} ?>>Processing3</option>
                                                 <option value="Processing4" <?php if($status == 'Processing4'){echo 'selected';} ?>>Processing4</option>    
                                                   <option value="Processed"<?php if($status == 'Processed'){echo 'selected';} ?>>Processed</option>
                                                   <option value="Processed2"<?php if($status == 'Processed2'){echo 'selected';} ?>>Processed2</option>
                                                   <option value="Processed3"<?php if($status == 'Processed3'){echo 'selected';} ?>>Processed3</option>
                                                   <option value="Processed7"<?php if($status == 'Processed7'){echo 'selected';} ?>>Processed7</option>
                                                   <option value="Processed8"<?php if($status == 'Processed8'){echo 'selected';} ?>>Processed8</option>
                                                 <option value="Waiting on payment" <?php if($status == 'Waiting on payment'){echo 'selected';} ?>>Waiting on payment</option>
                                                   <option value="Payment Received" <?php if($status == 'Payment Received'){echo 'selected';} ?>>Payment Recieved</option>
                                                 <option value="Being prepared for shipping" <?php if($status == 'Being prepared for shipping'){echo 'selected';} ?>>Being prepared for shipping</option>
                                                  <option value="Being prepared for shipping2" <?php if($status == 'Being prepared for shipping2'){echo 'selected';} ?>>Being prepared for shipping2</option>
                                                   <option value="Being prepared for shipping3" <?php if($status == 'Being prepared for shipping3'){echo 'selected';} ?>>Being prepared for shipping3</option>
                                                   <option value="Shipped" <?php if($status == 'Shipped'){echo 'selected';} ?>>Shipped</option>
                                                 <option value="Delivered" <?php if($status == 'Delivered'){echo 'selected';} ?>>Delivered</option>
                                                  <option value="Cancelled"<?php if($status == 'Cancelled'){echo 'selected';} ?>>Cancelled</option>
                                                   <option value="Items ordered" <?php if($status == 'Items ordered'){echo 'selected';} ?>>Items ordered</option>
                                                  <option value="Items received"<?php if($status == 'Items received'){echo 'selected';} ?>>Items received</option>
                                                  <option value="Money refunded" <?php if($status == 'Money refunded'){echo 'selected';} ?>>Money refunded</option>
                                                  <option value="Money requested <?php if($status == 'Money requested'){echo 'selected';} ?>>Money requested</option>
                                              </select>
                                          </div>

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

                                          	<input type="text" class="form-control" placeholder="Customer" id="customer_name" name="data[customer_name]"/>
                                          	<span id="customer_text"></span>
                                          	<input type="hidden" id="customer_id" name="data[customer_id]" />
                                          </div>
                                         
                                         <div class="col-sm-3 col-xs-12 mt-1">
                                             <input type="text" class="form-control" name="data[orderID]" placeholder="order ID" value="<?php echo $orderID ?>"/>
                                             
                                         </div>
                                          <div class="col-sm-3 col-xs-12 mt-1">
                                             <input type="text" class="form-control" name="data[productID]" id="productID" value="<?php echo $productID ?>" placeholder="EAN or Product ID"/>
                                             
                                         </div>
                                           <div class="col-sm-3 col-xs-12 mt-1"><button type="submit" class="btn btn-success" >Search</button></div>
                                      	<?php

                                      	if(isset($data['customer_id']) && trim($data['customer_id']) != '')
                                      	{
                                      		echo 'You Selected ' . $data['customer_name'];
                                      	}
                                      	?>
                                    </div>
                                    
                                    
                                    <div class="well" style="background-color: gray;border: black;border-radius: 15px;padding: 15px;margin-top: 15px">
                                            <div class="row">
                                                <div class="col-md-3">
                                                    <input type="checkbox" class="main_checkbox" />
                                                </div>
                                                <div class="col-md-4">
                                                    <select class="form-control" id="main_status">
                                                        <option value="">---Select Status---</option>
                                                         <option value="Processing" >Processing</option>
                                                 <option value="Processed">Processed</option>
                                                 <option value="Waiting on payment" >Waiting on payment</option>
                                                   <option value="Payment Received" >Payment Recieved</option>
                                                   <option value="Being prepared for shipping" >Being prepared for shipping</option>
                                                 <option value="Shipped" >Shipped</option>
                                                 <option value="Delivered" >Delivered</option>
                                                  <option value="Cancelled">Cancelled</option>
                                                   <option value="Items ordered">Items ordered</option>
                                                  <option value="Items received">Items received</option>
                                                  <option value="Money refunded">Money refunded</option>
                                                  <option value="Money requested">Money requested</option>
                                                    </select>
                                                </div>
                                                <div class="col-md-5">
                                                    <button type="button" class="btn btn-success" id="change_status_all">Change Status of all Checked</button>
                                                </div>
                                                
                                                <div class="col-12" id="bulk_error_text" style="margin-top:15px">
                                                    
                                                </div>
                                            </div>
                                        </div>
                                    
                                    <div class="table-responsive" style="width:100%;margin-top: 15px">
                                        
                                    <table class="table table-striped table-hover dt-responsive ACP_table" style="width:100%;margin-top: 15px">
                                        <thead>
                                            <tr>
                                                <th>
                                                  
                                                </th>
                                                <th>Order No.</th>	
                                                <th>Type</th>
                                                <th>Name &amp; Shipping Address</th>
                                                <th>Mobile</th>
                                                <th>Email</th>
                                                        <th>Country</th>

                                                <th>
                                                        Final Amount
                                                </th>
                                                <!--  <th>
    Wallet
</th>
<th>
    Promocode
</th>-->
                                                <th data-priority="2">

                                                    Status
                                                </th>
                                                <th>
                                                        Created At
                                                </th>
                                                <th>
                                                        Invoice
                                                </th>

                                                <th data-priority="1">Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        
                                           <?php
                                            foreach($userOrders AS $orders)
                                            {
                                                    $count = 1;
                                                    $uid=$orders['cl_user_id'];
                                                    if($orders['cl_user_id'])
                                                    {
                                                    //$us = $obj->getUser($uid);
                                                    }
                                            ?>
                                            <tr class="action-container">
                                                                                                
                                                <td>
                                                    <input type="checkbox" class="child_checkbox" cluster-id="<?php echo htmlspecialchars($orders['cl_id']) ?>"/>
                                                </td>
                                                <td data-order="<?php echo $count++ ?>">OR-<?php echo htmlspecialchars($orders['cl_id']);?></td>
                                                <td><?php if($orders['cl_checkout_type'] == 'ind'){ echo 'IND';}else{echo 'COM';}?></td>
                                                <td><?php 
                                                if($orders['cl_checkout_type'] == 'ind')
                                                {
                                                        echo htmlspecialchars($orders['cl_fname']).' '.htmlspecialchars($orders['cl_surname']);
                                                }
                                                else
                                                {
                                                        echo htmlspecialchars($orders['cl_company_name']);
                                                        echo '<br/>';
                                                        echo htmlspecialchars('Registration Number : ' . $orders['cl_registration_number']) . '<br/>';
                                                        echo htmlspecialchars('VAT Number : ' . $orders['cl_vat_number']);
                                                }
                                                ?>
                                                <br/><?php echo htmlspecialchars($orders['cl_address']);?><br/><?php echo htmlspecialchars($orders['cl_city']);?><br><?php echo htmlspecialchars($orders['cl_county']);?><br/><?php echo htmlspecialchars($orders['cl_pin']);?></td>
                                                <td><?php echo htmlspecialchars($orders['cl_mobile']);?></td>
                                                <td><?php echo htmlspecialchars($orders['cl_email']);?></td>
                                                        <td><?php echo htmlspecialchars($orders['cl_country']);?></td>

                                                <td><?php echo htmlspecialchars($orders['cl_total']) ?></td>
                                                <!--	 <td><?php if($orders['cl_wallet_used'] == 1){echo $orders['cl_wallet_amount'];} ?></td>
                                                <td><?php if($orders['cl_promocode_id'] != 0){echo 'Used';} ?></td>-->
                                                <td>
                                                    <?php
                                                        if($orders['cl_status'] == 'Processing')
                                                                {
                                                                    $bgcolor='#FF9100';
                                                                }
                                                                else if($orders['cl_status'] == 'Processing2' || $orders['cl_status'] == 'Processing3' || $orders['cl_status'] == 'Processing4')
                                                                {
                                                                    $bgcolor='#FF9100';
                                                                }
                                                                else if($orders['cl_status'] == 'Processed' || $orders['cl_status'] == 'Processed2' || $orders['cl_status'] == 'Processed3')
                                                                {
                                                                    $bgcolor='#00EF10';
                                                                }
                                                                else if($orders['cl_status'] == 'Waiting on payment')
                                                                {
                                                                    $bgcolor='#FFEF00';
                                                                }
                                                                else if($orders['cl_status'] == 'Being prepared for shipping' || $orders['cl_status'] == 'Being prepared for shipping2' || $orders['cl_status'] == 'Being prepared for shipping3')
                                                                {
                                                                    $bgcolor='#00D3EB';
                                                                }
                                                                else if($orders['cl_status'] == 'Shipped')
                                                                {
                                                                    $bgcolor='#00D3EB';
                                                                }
                                                                else if($orders['cl_status'] == 'Delivered')
                                                                {
                                                                    $bgcolor='#91FF00';
                                                                }
                                                                else if($orders['cl_status'] == 'Cancelled')
                                                                {
                                                                    $bgcolor='#FF0000';
                                                                }
                                                                else if($orders['cl_status'] == 'Payment Received')
                                                                {
                                                                    $bgcolor='#BCFF00';
                                                                }
                                                                else if($orders['cl_status'] == 'Items ordered')
                                                                {
                                                                    $bgcolor='#00FF80';
                                                                }
                                                                else if($orders['cl_status'] == 'Items received')
                                                                {
                                                                    $bgcolor='#91FF00';
                                                                }
                                                                else if($orders['cl_status'] == 'Money refunded' || $orders['cl_status'] == 'Money requested')
                                                                {
                                                                    $bgcolor='#FF004D';
                                                                }
                                                                else
                                                                {
                                                                        $bgcolor='#FF0000';
                                                                }


                                                    echo '<span style="width:15px;height:15px;background-color:' . $bgcolor . ';border-radius:25px;float: left;
margin-right: 5px;
margin-top: 3px;"></span>' . $orders['cl_status'];
                                                  ?>
                                                </td>
                                                <td><?php echo htmlspecialchars(date("d M Y H:i:s", strtotime($orders['cl_created_at'])));?></td>
                                                <td style="font-size: 10px">
                                                        <?php
                                                        if($orders['cl_auto_invoice_doc'] != null)
                                                                { ?>
                                                        <a href="<?php echo MAIN_DOMAIN ?>/uploads/invoice/<?php echo $orders['cl_auto_invoice_doc'] ?>" target="_blank" class="btn btn-success" style="font-size: 10px">Auto</a>
                                                        <?php
                                                }

                                                if($orders['cl_invoice_doc'] != null)
                                                                {
                                                        ?>
                                                        <a target="_blank" href="<?php echo DOMAIN ?>/uploads/invoice/<?php echo $orders['cl_invoice_doc'] ?>" class="btn btn-success" style="font-size: 10px" >Uploaded</a>
                                                        <?php
                                                }
                                                ?>
                                                </td>

                                                <td> <a id="edit" href="edit?id=<?php  echo htmlspecialchars($orders['cl_id']); ?>" class="btn btn-success"><i class="fa fa-edit"></i></a></td>
                                        </tr>
                                        <?php
                                        }
                                        ?>
                                           
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <td colspan="12">
                                                    <?php
                                                    if($page > 1)
                                                    {
                                                    ?>
                                                    <button type="submit" formaction="list?page=<?php echo htmlspecialchars($page) - 1 ?>" class="track-btn">Previous</button>

                                                    <?php
                                                    }
                                                    if(count($userOrders) == 500)
                                                    {
                                                    ?>
                                                    <button type="submit" formaction="list?page=<?php echo htmlspecialchars($page) + 1 ?>" class="track-btn">Next</button>
                                                    <?php
                                                    }
                                                    ?>
                                                </td>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                        </div>
                            
                          
                    </form>
                        
                    <script>
                        $(document).ready(function(){
                                $('.active').removeClass('active');
                                $('#store_menu_ul').show();
                                $('#customer-menu').addClass('active');
                        }); 


                         var options = {source: function(request,response) {
                        $.ajax ({
                            url: "../ajax/customers/get_customers",
                            data: {name: request.term},
                            dataType: "json",
                            type:'POST',
                            success: function( data ) {
                            response( $.map( data, function( item ) {
                                return {
                                    label: item.cl_name + ' ' + item.cl_surname + ' (' + item.username + ')',
                                    value: item.cl_name + ' ' + item.cl_surname + ' (' + item.username + ')',
                                    data:item
                                }
                            }));
                        } 
                        })
                    },
                    minLength: 1,
                    select: function( event, ui ) {
                      //  $('#customer_name').val(ui.item.data.username);
                        $('#customer_id').val(ui.item.data.id);
                        $('#customer_text').text(ui.item.data.cl_name + ' ' + ui.item.data.cl_surname + ' (' + ui.item.data.username + ') Selected');
                    }
                  };


                  $(document).on('keydown.autocomplete','#customer_name',function(){
                      $(this).autocomplete(options);
                  });


                  $(document).on('click', '.main_checkbox', function(){
                    if($(this).is(':checked'))
                    {
                        $('.child_checkbox').prop('checked', true);
                    }
                    else
                    {
                        $('.child_checkbox').prop('checked', false);
                    }

                  })
                  
                  function saveAllStatus(data)
                  {
                    var status = $('#main_status').val();
                    $.post('../ajax/user_orders/bulk_save_status',{status:status, data:data}, function(){
                        $('#bulk_error_text').html('<div class="alert alert-success">Status Updating</div>');
                        location.reload();
                     },'json')
                    .fail(function(xhr) {
                        $('#bulk_error_text').html('<div class="alert alert-danger">Something Went Wrong</div>');
                        window.location.href = '#body_part';
                    },'json');;
                    
                  }
                  
                  $(document).on('click', '#change_status_all', function(){
                    $('#bulk_error_text').html('<div class="alert alert-warning">Updating as Bulk.....</div>');
                    
                    var data = new Object();
                    var count = 0;
                        $('.child_checkbox:checked').each(function(i, value){
                            data[count++] = $(this).attr('cluster-id');
                            
                        });
                        
                        saveAllStatus(data);
                  })
                </script>               

  <?php
  include '../footer.php';
  ?>