<?php 
require_once("header.php");
require_once("session_check.php");

//CODE FOR CHANGE STATUS TO ACTIVE
if(isset($_GET['action']) && $_GET['action'] == "active")
{
	$sql_update = "UPDATE `sc_orders` SET `work_status` = 'Completed',`payment_status` = 'Paid',`work_date` = '".date('Y-m-d H:i:s',time())."' WHERE `order_id` = '".$_GET['order_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);

	$_SESSION['change_succ_msg'] = 'Work status changed successfully.';
	header("location: order_list.php");
	exit;
}

//CODE FOR DELETE ORDER
if(isset($_GET['action']) && $_GET['action'] == "delete")
{
	mysqli_query($connection,"DELETE FROM `sc_orders` WHERE `order_id` = '".$_GET['order_id']."'");
	
	$_SESSION['del_succ_msg'] = 'Order deleted successfully.';
	header("location: order_list.php");
	exit;
}
?>

<?php require_once("left_panel.php");?>
<div class="content">
	<div class="workplace">
	    <?php if(isset($_SESSION['change_succ_msg'])) { ?>
			<div class="alert alert-success">                
			<b><?php echo $_SESSION['change_succ_msg'];?></b>
			</div> 
		<?php
		unset($_SESSION['change_succ_msg']);
		}
		?>
		
		<?php if(isset($_SESSION['del_succ_msg'])) { ?>
			<div class="alert alert-success">                
			<b><?php echo $_SESSION['del_succ_msg'];?></b>
			</div> 
		<?php
		unset($_SESSION['del_succ_msg']);
		}
		?>
		<div class="row-fluid">
			<div class="span12">
			    <form action="" method="get">
				<div class="head clearfix">
					<div class="isw-grid"></div>
					<h1>Search Orders</h1>                               
				</div>
				<div class="block-fluid table-sorting clearfix">
    				<div class="row-form clearfix">
    					<div class="span2">Start Date</div>
    					<div class="span3"><input type="text" name="start_date" id="datepicker" value="<?php echo $_REQUEST['start_date'];?>" style="width:100px;" /></div>                            
    					<div class="span2">End Date</div>
    					<div class="span3"><input type="text" name="end_date" id="datepicker2" value="<?php echo $_REQUEST['end_date'];?>" style="width:100px;" /></div>   
    					<div class="span2"><button class="btn btn-inverse" type="submit" name="search">Search</button>&nbsp;&nbsp;<button class="btn btn-inverse" type="button" name="reset" onclick="window.location.href='order_list.php'">Reset</button></div>                            
				    </div>
				</div>
				<div>&nbsp;</div>
				</form>                    
				<div class="head clearfix">
					<div class="isw-grid"></div>
					<h1>Orders List</h1>                               
				</div>
				<div class="block-fluid table-sorting clearfix">
					<?php if(isset($_REQUEST['search'])) { ?>
				    <div>&nbsp;</div>
					<form action="export.php" method="get">
					<input type="hidden" name="st_date" id="st_date" value="<?php echo $_REQUEST['start_date'];?>" />
					<input type="hidden" name="en_date" id="en_date" value="<?php echo $_REQUEST['end_date'];?>" />
					<div style="text-align:right; margin-right:10px;"><button class="btn btn-inverse" type="submit" name="export">Export</button></div>
					</form>
					<?php } ?>
					<table cellpadding="0" cellspacing="0" width="100%" class="table" id="tSortable">
						<thead>
							<tr>
								<th width="10%">Serial No.</th>
								<th width="27%">Customer</th>
								<th width="25%">Order ID & Order Date</th>
								<th width="13%">Work Status</th>
								<th width="25%">Action</th>
							</tr>
						</thead>
						<tbody>
						<?php
						$sr_no = 1;
						if(isset($_REQUEST['search']))
						{
							if($_REQUEST['start_date']!="" && $_REQUEST['end_date']!="")
							{
								$start_date_export = explode("/",$_REQUEST['start_date']);
								$start_date = $start_date_export[2]."-".$start_date_export[0]."-".$start_date_export[1];
								
								$end_date_export = explode("/",$_REQUEST['end_date']);
								$end_date = $end_date_export[2]."-".$end_date_export[0]."-".$end_date_export[1];
								
								$sql_record = "SELECT * FROM `sc_orders` WHERE `consultation_charge_paid` = 'Yes' AND `post_date` BETWEEN '".$start_date."' and '".$end_date."' ORDER BY `order_id` DESC";
							}
							if($_REQUEST['start_date']!="" && $_REQUEST['end_date']=="")
							{
								$start_date_export = explode("/",$_REQUEST['start_date']);
								$start_date = $start_date_export[2]."-".$start_date_export[0]."-".$start_date_export[1];
								
								$sql_record = "SELECT * FROM `sc_orders` WHERE `consultation_charge_paid` = 'Yes' AND `post_date` BETWEEN '".$start_date."' and '".date('Y-m-d')."' ORDER BY `order_id` DESC";
							}
							if($_REQUEST['start_date']=="" && $_REQUEST['end_date']!="")
							{
								$end_date_export = explode("/",$_REQUEST['end_date']);
								$end_date = $end_date_export[2]."-".$end_date_export[0]."-".$end_date_export[1];
								
								$sql_record = "SELECT * FROM `sc_orders` WHERE `consultation_charge_paid` = 'Yes' AND `post_date` BETWEEN '2019-12-31' and '".$end_date."' ORDER BY `order_id` DESC";
							}
							if($_REQUEST['start_date']=="" && $_REQUEST['end_date']=="")
							{
								$sql_record = "SELECT * FROM `sc_orders` WHERE `consultation_charge_paid` = 'Yes' ORDER BY `order_id` DESC";
							}							
						}
						else
						{
							$sql_record = "SELECT * FROM `sc_orders` WHERE `consultation_charge_paid` = 'Yes' ORDER BY `order_id` DESC";
						}
						$exe_record = mysqli_query($connection,$sql_record);
						$num_record = mysqli_num_rows($exe_record);
						if($num_record>0)
						{
							while($fetch_record = mysqli_fetch_array($exe_record))
							{
								$fetch_user = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `sc_users` WHERE `user_id` = '".$fetch_record['user_id']."'"));
								
								$orderid = stripslashes($fetch_record['orderid']);
								$name = stripslashes($fetch_user['full_name']);
								$phone = stripslashes($fetch_user['phone']);
								$grand_total = stripslashes($fetch_record['price']);
								$post_date = date('d.m.Y h:i A',strtotime($fetch_record['post_date']));
								$work_status = $fetch_record['work_status'];
								$work_date = date('d.m.Y h:i A',strtotime($fetch_record['work_date']));
							?>
							<tr>
								<td><?php echo $sr_no;?></td>
								<td><b><?php echo $name;?></b><br /><strong>Phone No : </strong><?php echo $phone;?><br /><strong>Total Cost : </strong>Rs. <?php echo $grand_total;?></td>
								<td><b>Order ID :</b> <?php echo $orderid;?><br><b>Order Date :</b> <?php echo $post_date;?></td>
								<td><?php if($work_status == 'Completed') { ?><a href="#" style="color:#009900;">Completed<br><?php echo $work_date;?></a><?php } else { ?><a href="order_list.php?order_id=<?php echo $fetch_record['order_id']?>&user_id=<?php echo $fetch_record['user_id']?>&action=active" title="Click to completed" onclick="return confirm('Are you sure you want to change work status?');" style="color:#FF0000;">Pending</a><?php } ?></td>
								<td><button class="btn" type="button" name="back" onclick="window.location.href='order_details.php?order_id=<?php echo $fetch_record['order_id']?>'">View Details</button><a href="order_list.php?order_id=<?php echo $fetch_record['order_id']?>&action=delete" title="Delete" onclick="return confirm('Are you sure you want to delete this order?');" style="float:left; display:block; margin:0 5px 0 0;"><ul class="the-icons clearfix"><i class="isb-delete"></i></ul></a></td>
							</tr>
							<?php
							$sr_no++;
							}
						}
						else
						{
						?>
						<tr>
							<td><font color="#FF0000">No order posted yet</font></td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
						</tr>
						<?php
						}
						?>
						</tbody>
					</table>
				</div>
			</div>                                
		</div>
	</div>
</div>

<?php require_once("footer.php"); ?>