<?php
include 'header.php';
include('xcrud/xcrud.php');
echo Xcrud::load_css();
$xcrud=Xcrud::get_instance();
$xcrud->table('modification');
$xcrud->join('CId','customer','customer_id');
$xcrud->relation('customer.salesman','salesman','id','name');
$xcrud->relation('customer.rto','rto','id','name');

//$xcrud->fields('Pending,Pending_date,Allotment,Allotment_date,Proceed,Proceed_date,Upload,Upload_date,Insp,Insp_date,Payment,Payment_date,Semi,Semi_date,Hsrp,Hsrp_date,Fitment,Fitment_date,Ready,Ready_date,Register,Register_date,Rc,Rc_date,IssueDocs,IssueDocs_date',true);

$xcrud->unset_title();
$xcrud->unset_add();
$xcrud->unset_view();
$xcrud->unset_edit();
$xcrud->unset_remove();
?>
<div class="row">
	<div class="col-sm-12"><button class="btn btn-blue btn-block">User Update Report</button></div>
	<div class="col-sm-12">&nbsp</div>
	<div class="col-sm-12">
		<div class="row">
		<form method="post">
			<div class="col-sm-3">Range
				<input type="text" id="status_date_rage" name="status_date_rage"/>
			</div>
			<div class="col-sm-3">From
				<select name="from_status" class="form-control" style="display: inline !important;width:50%">
					<option value="Pending_date">Pending</option>
					<option value="Allotment_date">Allotment</option>
					<option value="Proceed_date">Proceed</option>
					<option value="Upload_date">Upload</option>
					<option value="Insp_date">Insp</option>
					<option value="Payment_date">Payment</option>
					<option value="Semi_date">SemiSuccessfull</option>
					<option value="Hsrp_date">hsrp</option>
					<option value="Fitment_date">Fitment</option>
					<option value="Ready_date">Ready</option>
					<option value="Rc_date">RC</option>
					<option value="Register_date">Register</option>
					<option value="IssueDocs_date">IssueDocs</option>
				</select>
			</div>
			<div class="col-sm-3">To
				<select name="to_status" class="form-control" style="display: inline !important;width:50%">
					<option value="Pending_date">Pending</option>
					<option value="Allotment_date">Allotment</option>
					<option value="Proceed_date">Proceed</option>
					<option value="Upload_date">Upload</option>
					<option value="Insp_date">Insp</option>
					<option value="Payment_date">Payment</option>
					<option value="Semi_date">SemiSuccessfull</option>
					<option value="Hsrp_date">hsrp</option>
					<option value="Fitment_date">Fitment</option>
					<option value="Ready_date">Ready</option>
					<option value="Rc_date">RC</option>
					<option value="Register_date">Register</option>
					<option value="IssueDocs_date">IssueDocs</option>
				</select>
			</div>
			<div class="col-sm-2"><button type="submit" name="view_update" class="btn btn-success">View</button></div>
		</form>
		</div>
<?php
if(isset($_POST['view_update'])){
	$from_status_array=explode("_",$_POST['from_status']);
	$to_status_array=explode("_",$_POST['to_status']);
	echo "<h4 style='text-align:center'>Date: ".$_POST['status_date_rage']." From:".$from_status_array[0]." To:".$to_status_array[0]."</h4>";
	$date_range=str_replace(' ','',$_POST['status_date_rage']);
	$date_array=explode("-",$date_range);
	$start_date=date('Y-m-d',strtotime(str_replace('/','-',$date_array[0])));
	$end_date=date('Y-m-d',strtotime(str_replace('/','-',$date_array[1])));
	$xcrud->where("(modification.{$_POST['from_status']} >= '{$start_date} 00:00:00' AND modification.{$_POST['from_status']} <= '{$end_date} 23:59:59') AND (modification.{$_POST['to_status']} >= '{$start_date} 00:00:00' AND modification.{$_POST['to_status']} <= '{$end_date} 23:59:59')");
	$xcrud->columns('customer.salesman,customer.name,customer.rto,customer.name,customer.contact_no,customer.complete_chassis_no,customer.Product,customer.status,Pending,Pending_date,Allotment,Allotment_date,Proceed,Proceed_date,Upload,Upload_date,Insp,Insp_date,Payment,Payment_date,Semi,Semi_date,Hsrp,Hsrp_date,Fitment,Fitment_date,Ready,Ready_date,Register,Register_date,Rc,Rc_date,IssueDocs,IssueDocs_date,days_count',false);
	//$xcrud->subselect('days_count','DATEDIFF(Insp_date,Pending_date)');
	$xcrud->subselect('days_count',"DATEDIFF({$_POST['to_status']},{$_POST['from_status']})");
	
}else{
	$xcrud->columns('customer.salesman,customer.name,customer.rto,customer.name,customer.contact_no,customer.complete_chassis_no,customer.Product,customer.status,Pending,Pending_date,Allotment,Allotment_date,Proceed,Proceed_date,Upload,Upload_date,Insp,Insp_date,Payment,Payment_date,Semi,Semi_date,Hsrp,Hsrp_date,Fitment,Fitment_date,Ready,Ready_date,Register,Register_date,Rc,Rc_date,IssueDocs,IssueDocs_date,days_count',false);
	$xcrud->subselect('days_count',"CASE WHEN Allotment_date IS NULL THEN DATEDIFF(Pending_date,Pending_date) WHEN Proceed_date IS NULL THEN DATEDIFF(Allotment_date,Pending_date) WHEN Upload_date IS NULL THEN DATEDIFF(Proceed_date,Pending_date) WHEN Insp_date IS NULL THEN DATEDIFF(Upload_date,Pending_date) WHEN Payment_date IS NULL THEN DATEDIFF(Insp_date,Pending_date) WHEN Semi_date IS NULL THEN DATEDIFF(Payment_date,Pending_date) WHEN Hsrp_date IS NULL THEN DATEDIFF(Fitment_date,Pending_date) WHEN Ready_date IS NULL THEN DATEDIFF(Fitment_date,Pending_date) WHEN Register_date IS NULL THEN DATEDIFF(Ready_date,Pending_date) WHEN Rc_date IS NULL THEN DATEDIFF(Ready_date,Pending_date) WHEN IssueDocs_date IS NULL THEN DATEDIFF(Ready_date,Pending_date) ELSE DATEDIFF(IssueDocs_date,Pending_date) END");
}
$xcrud->label('days_count','Days Count');
echo $xcrud->render();
?>
	</div>
</div>
</div>
</div>
</div>
<?php
include "footer.php";
?>
<script>
	//https://wakirin.github.io/Lightpick/
    var picker = new Lightpick({
    field: document.getElementById('status_date_rage'),
    singleDate: false,
    onSelect: function(start, end){
        var str = '';
        str += start ? start.format('Do MMMM YYYY') + ' to ' : '';
        str += end ? end.format('Do MMMM YYYY') : '...';
        //document.getElementById('result-2').innerHTML = str;
    }
});
</script> 
<?php
echo Xcrud::load_js();
?>