<?php 
require_once('includes/check-login.php');
require_once('includes/config.php'); 
$tablename="save_order";	
$addurl="service-view.php";
$id=isset($_GET['id']) ? mysqli_real_escape_string($con,$_GET['id']) :'';
$expired=isset($_GET['expired']) ? mysqli_real_escape_string($con,$_GET['expired']) :'';
if($_SESSION['role_id']==1){
$sql = "SELECT * from ".$tablename." INNER JOIN order_item ON $tablename.id=order_item.order_id WHERE $tablename.id='$id'";
}else{
$sql="SELECT * from ".$tablename." INNER JOIN order_item ON $tablename.id=order_item.order_id WHERE $tablename.id='$id' and $tablename.vendor_id='".$_SESSION['userid']."'"; 
}
$result = $con->query($sql);
if(mysqli_num_rows($result)==0){
    header("location:".$base_url."job-request.php");
    exit();     
}
$row=mysqli_fetch_array($result);
$order_date=isset($row['order_date']) ? $row['order_date']:''; 
$name=isset($row['name']) ? $row['name']:'';
$email=isset($row['email']) ? $row['email']:'';
$mobile=isset($row['phone']) ? $row['phone']:'';
$job_date=isset($row['job_date']) ? $row['job_date']:'';
$job_time=isset($row['job_time']) ? $row['job_time']:'';
$street=isset($row['street']) ? $row['street']:'';
$address=isset($row['address']) ? $row['address']:'';
$user_id=isset($row['user_id']) ? $row['user_id']:'';
$vendor_id=isset($row['vendor_id']) ? $row['vendor_id']:'';
$payment_mode=isset($row['payment_mode']) ? $row['payment_mode']:'';
$job_status=isset($row['job_status']) ? $row['job_status']:''; 
$sql="SELECT ud.* FROM users as u INNER JOIN user_details as ud ON u.user_id=ud.user_id where u.active='1' and u.user_id='$user_id' order by u.user_id desc";
$result1 = $con->query($sql);
$billname=""; 
$billemail=""; 
$billphone=""; 
if(mysqli_num_rows($result1)>0){
$rowname=mysqli_fetch_array($result1);    
 $billname=isset($rowname['name']) ? $rowname['name']:''; 
 $billemail=isset($rowname['email']) ? $rowname['email']:''; 
 $billphone=isset($rowname['phone']) ? $rowname['phone']:''; 
}
$reorder=$con->query("SELECT oi.* FROM save_order as sa INNER JOIN order_item as oi ON sa.id=oi.order_id where sa.id='$id' order by sa.id desc");
$push_id=array();
$ser_id=array();
$subser_id=array();
while($row=mysqli_fetch_array($reorder)){
   array_push($push_id,$row['child_id']); 
   array_push($ser_id,$row['cat_id']); 
   array_push($subser_id,$row['subcat_id']); 
}
$implode=implode(",",$push_id);
$implode1=implode(",",$ser_id);
$implode2=implode(",",$subser_id);

$checkvendor=$con->query("select U.user_id, U.user_name, ur.role_id, U.last_logged_on, r.role_name FROM users U JOIN user_roles ur ON U.user_id = ur.user_id JOIN roles r ON ur.role_id = r.role_id INNER JOIN serviceprice ON serviceprice.vendor_id = ur.user_id  WHERE U.active = 1 AND ur.role_id = '2' and child_id IN($implode) and service_id IN($implode1) and subservice_id IN($implode2) and vendor_id!='$vendor_id'");

$matching=$con->query("select reject_order.* FROM reject_order JOIN save_order ON reject_order.order_id = save_order.id WHERE reject_order.order_id = '$id'  and reject_order.status='0'");
if(isset($_POST['submit'])){
    $ven_id=$_POST['vendor_id']; 
    $query = "update save_order SET vendor_id='".$ven_id."',job_status='0' WHERE id='".$id."'";
    $update =mysqli_query($con,"update reject_order SET status='1' WHERE order_id='".$id."'");
	$exe = mysqli_query( $con, $query );		
	if( $exe ) {				
			$_SESSION['save_ordersucc_msg'] = 'Service Price updated successfully';
	}else{				
			$_SESSION['save_ordererr_msg'] = 'Error occured';
	}
}
require_once('includes/header.php'); 
require_once('includes/menu.php'); 
?>
<style>.text-primary {color: #337ab7 !important;font-weight: bold;font-size: 18px;}.text-success {color: #28a745!important;font-weight: bold;font-size: 18px;}.text-danger {color: #dc3545!important;font-weight: bold;font-size: 18px;}</style>
<!-- Datatable CSS -->
<link href='data_tables/datatables.min.css' rel='stylesheet' type='text/css' />
        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">                                
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>                    
                        <li class="active"><a href="job-request.php">Services ID  #<?php echo $id;?></a></li>           
                    </ol>                
            </section>
            <!-- Main content -->
            <section class="content">                
                <!-- Default box -->
                <div class="box box-form">
    <div class="box-body">
    <div class="row">
<div class="col-xs-12">
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#"><b>Order # <?php echo $id;?></b></a>
    </div>
    <ul class="nav navbar-nav" style="float: right;">
        <?php if($job_status==0){?>
      <li><a href="javascript:void(0)" class="text-primary" onclick="jobstatus_change('<?php echo $id;?>','1','save_order')">Approved</a></li>
      <?php }if($job_status==1){?>
      <li><a href="javascript:void(0)" class="text-success" onclick="jobstatus_change('<?php echo $id;?>','2','save_order')">Completed</a></li>
      <?php }if($job_status==0 || $job_status==1){?>
      <li><a href="javascript:void(0)" class="text-danger" onclick="jobstatus_change('<?php echo $id;?>','3','save_order')">Canceled</a></li>
      <?php }?>
    </ul>
  </div>
</nav>
<hr>
<?php 
if((mysqli_num_rows($matching)>0) || !empty($expired)){
if(!empty($_GET['vendor_id'])){
if(((mysqli_num_rows($checkvendor)>0) && ($job_status==3)) || !empty($expired)){?>
 <form method="post">
    <div class="form-group row">
      <div class="col-xs-2">
        <label for="ex1">Vendor Name </label>
      </div>
      <div class="col-xs-3">
        <select class="form-control" name="vendor_id" required>
            <option value="" disabled selected>Choose Vendor</option>
            <?php if($checkvendor->num_rows > 0) {
                while($row = $checkvendor->fetch_assoc()) {?>
				<option value="<?php echo $row['user_id'];?>"><?php echo $row['user_name'];?></option>
			<?php }}?>
        </select>
      </div>
      <div class="col-xs-3">
        <input class="form-control btn btn-success" name="submit" type="submit" value="Submit">
      </div>
    </div>
  </form>
<?php } else {?>
<h3>Vendor Not Available!</h3>
<?php }}?>
 <hr> 
<?php }?>

<div class="row">
    			<div class="col-xs-6">
    				<address>
    				<strong>Billed To:</strong><br>
    					<?php echo $billname;?><br>
    					<?php echo $billemail;?><br>
    					<?php echo $billphone;?><br>
    				</address>
    			</div>
    			<div class="col-xs-6 text-right">
    				<address>
        			<strong>Shipped To:</strong><br>
    					<?php echo $name;?><br>
    					<?php echo $street;?><br>
    					<?php echo $address;?><br>
    				</address>
    			</div>
    		</div>
    		<div class="row">
    			<div class="col-xs-6">
    				<address>
    					<strong>Payment Method:</strong><br>
    					<?php echo $payment_mode;?><br>
    					<?php echo $billemail;?>
    				</address>
    				<?php if($job_status==0){
                               $stacom="Pending"; 
                            }else if($job_status==1){
                               $stacom="Approved";  
                            }else if($job_status==2){
                               $stacom="Completed";  
                            }else{
                               $stacom="Canceled";  
                            }?>
                    <address>
    					<strong>Status:</strong><br>
    					<?php echo $stacom;?><br>
    				</address>        
    			</div>
    			<div class="col-xs-6 text-right">
    				<address>
    					<strong>Job Date:</strong><br>
    					<h4><b><?php echo date('F d, Y',strtotime($job_date));?> <?php echo date('g:i A',strtotime($job_time));?></h4></b>
    				</address>
    			</div>
    		</div>
    	</div>
    </div>
    
    <div class="row">
    	<div class="col-md-12">
    		<div class="panel panel-default">
    			<div class="panel-heading">
    				<h3 class="panel-title"><strong>Order summary</strong></h3>
    			</div>
    			<div class="panel-body">
    				<div class="table-responsive">
    					<table class="table table-condensed">
    						<thead>
                                <tr>
        							<td><strong>Item</strong></td>
        							<td class="text-center"><strong>Price</strong></td>
        							<td class="text-center"><strong>Quantity</strong></td>
        							<td class="text-right"><strong>Totals</strong></td>
                                </tr>
    						</thead>
    						<tbody>
    							<!-- foreach ($order->lineItems as $line) or some such thing here -->
    							<?php $total=0;
    							foreach ($result as $row1) {
    							$total+=$row1['service_total'];?>
    							<tr>
    								<td><?php echo $row1['service_name'];?></td>
    								<td class="text-center"><?php echo $row1['service_price'];?></td>
    								<td class="text-center"><?php echo $row1['service_qty'];?></td>
    								<td class="text-right"><?php echo $row1['service_total'];?></td>
    							</tr>
    							<?php }?>
    							<tr>
    								<td class="thick-line"></td>
    								<td class="thick-line"></td>
    								<td class="thick-line text-center"><strong>Subtotal</strong></td>
    								<td class="thick-line text-right"><?php echo $total;?></td>
    							</tr>
    							<!--<tr>
    								<td class="no-line"></td>
    								<td class="no-line"></td>
    								<td class="no-line text-center"><strong>Shipping</strong></td>
    								<td class="no-line text-right">$15</td>
    							</tr>-->
    							<tr>
    								<td class="no-line"></td>
    								<td class="no-line"></td>
    								<td class="no-line text-center"><strong>Total</strong></td>
    								<td class="no-line text-right" style="font-weight: bold;font-size: 22px;"><?php echo $total;?></td>
    							</tr>
    						</tbody>
    					</table>
    				</div>
    					<?php $url = "job-request.php";
  echo "<a href='$url' class='btn btn-primary'>Back</a>";?>
    			</div>
    		</div>
    	</div>
    </div>
</div>
                    <!-- /.box-footer-->                    
                </div>
                <!-- /.box -->

            </section>
            <!-- /.content -->                                        
        </div>
<!-- /.content-wrapper -->        
<script src="js/common.js"></script>  
<script src="js/datatables.js"></script>  
<script>
function jobstatus_change(id,status,tbl_name){
$.ajax({
		url 	: "jobstatus.php",
		type 	: "POST",
		data 	: {id:id,status:status,table:tbl_name},
		success : function(data){  		
		setTimeout(function(){location.reload();},1000);
		}
});
}    
</script>
<?php require_once('includes/footer.php'); ?>