<?php
if(isset($_SESSION['msg'])){?>
	<div class="alert alert-success"><strong>Success!</strong> <?php echo $_SESSION['msg']; unset($_SESSION['msg']);?></div>
<?php }?>
<style>
.content-wrapper {
    background: #f2f8f9;
    padding-left:0px;
	padding-right:0px;
    width: 100%;
    -webkit-flex-grow: 1;
    flex-grow: 1;
}
.page-body-wrapper:not(.auth-page) {
    padding-top:50px;
}
div.dataTables_wrapper div.dataTables_filter {
    text-align: right;
    margin-top: -45px;
}
</style>
<div class="col-lg-12 grid-margin stretch-card">
    <div class="card">
        <div class="card-body">            	
            <h4 class="card-title">Order List</h4>
            <div class="table-responsive">
            <form method="post" enctype="multipart/form-data">                    
                <div class="row">                                                                                        
                    <div class="col-md-1">
                        <div class="form-group">
                            <label for="exampleInputCity1">Order Date </label>
                            <input type="date" name="date" class="form-control" value="<?php echo date('d/m/Y');?>"/>                                                       
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">State <span style="color:#F00;">*</span></label>
                            <select name="state" class="form-control" id="state" onchange="getDistrict(this.value);" required> 
                                <option value="">Select State</option>
                                <?php
                                $sqlState = mysqli_query($cn, "select `state` from `tbl_retailer` group by `state` order by `state`");
                                while($sqlStateData = mysqli_fetch_array($sqlState)){?>
                                <option value="<?php echo $sqlStateData['state'];?>" <?php if(isset($_GET['edit_id'])){if($sqlStateData['state']==$sqlData['state']){ echo "selected";}}?>><?php echo $sqlStateData['state'];?></option>
                                <?php }?>
                            </select>                           
                        </div>
                    </div>
                
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">District</label>
                            <select name="district" class="form-control" id="district" onchange="getBeat(this.value);"> 
                                <option value="">Select District</option>                                
                            </select>                           
                        </div>
                    </div>
                                                           
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">Beat</label>
                            <select name="beat" class="form-control" id="beat" onchange="getRetailer(this.value);"> 
                                <option value="">Select Beat</option>                                
                            </select>
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">Retailer Name</label>
                            <select name="shop_id" class="form-control" id="shop_id"> 
                                <option value="">Select Retailer</option>                                
                            </select>
                        </div>
                    </div>
                    
                    <div class="col-md-1">
                        <div class="form-group">        
                            <label for="exampleInputName1">&nbsp;</label>                
                            <button type="submit" class="btn btn-success mr-2 form-control" name="search">Search</button>                                    	                    	
                        </div>                
                    </div>
                    
                    <div class="col-md-1">
                        <div class="form-group">        
                            <label for="exampleInputName1">&nbsp;</label>                
                            <a href="excel/Examples/orderList.php" class="btn btn-primary btn-labeled btn-labeled-left btn-sm"><b><i class="icon-printer4 "></i></b> Download Order</a>                                    	                    	
                        </div>                
                    </div>                                                                                                                                                                   
                </div>            	            				                    
            </form>
            <table class="table table-striped" id="orderTable">
              <thead>
                    <tr>
                        <th width="5%">Order Date</th>
                        <th width="5%" style="text-align:center;">Invoice Id</th>                                                               
                        <th width="60%">Order Description</th>
                        <th width="5%">Total</th>
                        <th width="10%" style="text-align:center">Action</th>                                                                          
                    </tr>                                               		
              </thead>
              <tbody>
              <?php
				if(isset($_POST['search']))
				{
					$searchQuery = "";
					if(!empty($_POST['date']))
					{
						$searchQuery .= " and `date`='".$_POST['date']."'";
					}
					if(!empty($_POST['state']))
					{
						$searchQuery .= " and `state`='".$_POST['state']."'";
					}
					if(!empty($_POST['district']))
					{
						$searchQuery .= " and `district`='".$_POST['district']."'";
					}
					if(!empty($_POST['beat']))
					{
						$searchQuery .= " and `beat`='".$_POST['beat']."'";
					}
					if(!empty($_POST['beat']))
					{
						$searchQuery .= " and `beat`='".$_POST['beat']."'";
					}
					$_SESSION['orderQuery'] = "select * from `tbl_order` where `order_id`!='' $searchQuery order by `order_id`";
					$sqlBusiness = mysqli_query($cn,"select * from `tbl_order` where `order_id`!='' $searchQuery group by `order_id` order by `order_id` DESC ");
				}
				else
				{
					$_SESSION['orderQuery'] = "select * from `tbl_order` where `order_id`!='' order by `order_id`";
					$sqlBusiness = mysqli_query($cn,"select * from `tbl_order` where `order_id`!='' group by `order_id` order by `order_id` DESC ");				
				}										
				while($sqlBusinessData = mysqli_fetch_array($sqlBusiness))
				{ 														                   						   				     																				
					$sqlquery     = mysqli_query($cn,"select * from `tbl_order` where `order_id`='".$sqlBusinessData['order_id']."'");					
					$productCount = mysqli_num_rows($sqlquery); 							
                    ?>
                    <tr>                       
                        <td><?php echo date('d M, Y',strtotime($sqlBusinessData['date']));?></td>
                        <td style="text-align:center;"><a href="home.php?pages=orderDetail&order_id=<?php echo $sqlBusinessData['order_id'];?>" target="_blank"><?php echo str_pad($sqlBusinessData['order_id'], 4, "0", STR_PAD_LEFT);?></a></td>                                                                
                        <td>
                        	<p><strong> Customer : </strong> <?php echo "<a href='javascript:void(0);'>".shopName($sqlBusinessData['shop_id'])."</a>";?></p>
                            <p><?php echo shopState($sqlBusinessData['shop_id']);?></p>                            
                            <a href="javascript:void(0);"><?php echo productName($sqlBusinessData['product_id']);?></a> 
                            <?php if($productCount>1){?><br /><span>+<?php echo $productCount-1;?> More Product</span><?php }?>
                        </td>                    
                        <td>&#x20B9;<?php echo $sqlBusinessData['order_total'];?></span></td>                                                                                                                           				
						<td style="text-align:center"><!--<a href="home.php?pages=orderInvoice&order_id=<?php echo $sqlBusinessData['order_id'];?>" class="btn btn-primary btn-labeled btn-labeled-left btn-sm"><b><i class="icon-printer4 "></i></b> Download</a></span>--></td>
                    </tr>
                <?php }?>
              </tbody>
            </table>
            </div>        
        </div>                
    </div>
</div>
<?php
if(isset($_GET['deleteID1']))
{
	$orderId         = $_GET['deleteID1'];
	$sqlDelete       = mysqli_query($cn,"delete from `tbl_order` where `receipt_id`='".$orderId."'");
	mysqli_query($cn,"delete from `tbl_order_item` where `receipt_id`='".$orderId."'");
	$_SESSION['msg'] = "Receipt Deleted Successfully.";
	header("location:home.php?pages=orderList");	
}

if(isset($_GET['deleteID2']))
{
	$orderId         = $_GET['deleteID2'];
	$sqlDelete       = mysqli_query($cn,"delete from `tbl_order` where `order_id`='".$orderId."'");
	mysqli_query($cn,"delete from `tbl_order_item` where `order_id`='".$orderId."'");
	$_SESSION['msg'] = "Order Deleted Successfully.";
	header("location:home.php?pages=orderList");	
}

?>