<?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->columns('task_duration,customer.salesman,customer.rto,customer.name,customer.contact_no,customer.chassis_no,customer.Product,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',false);

$xcrud->relation('customer.salesman','salesman','id','name');
$xcrud->relation('customer.rto','rto','id','name');

$xcrud->button('javascript:;','Task Remark','fa fa-check','',array('data-toggle'=>'modal','data-target'=>'#taskRemarkReportModal','onclick'=>'openTaskRemarkReportModal({CId})'));

//$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',false);

$xcrud->column_width('task_duration','200px');
$xcrud->unset_title();
$xcrud->unset_add();
$xcrud->unset_remove();
$xcrud->unset_edit();
?>

<div class="row">
	<div class="col-sm-12">
		<button class="btn btn-blue btn-block"><span style='font-weight:bold'>Reprot</span>
		 <?php
			echo " ".strtoupper($_GET['type']);
			if(isset($_POST['report_from_status'])){
				echo " <span style='font-weight:bold'>From: </span>".$_POST['report_from_status'];
			}
			if(isset($_POST['report_to_status'])){
				echo " <span style='font-weight:bold'>To: </span>".$_POST['report_to_status'];
			}
		 ?>
		</button>
	</div>
	<div class="col-sm-12">&nbsp</div>
	<div class="col-sm-12">
		<form method="post">
			<div class="col-sm-2"><input type="date" class="form-control" name="report_start_date" required <?php  if(isset($_POST['report_start_date'])){ echo "value='{$_POST['report_start_date']}'";};?> ></div>
			<div class="col-sm-2"><input type="date" class="form-control" name="report_end_date" required <?php  if(isset($_POST['report_end_date'])){ echo "value='{$_POST['report_end_date']}'";};?> ></div>
			<div class="col-sm-2">
				<select name="report_from_status" class="form-control" required>
					<option value="">From</option>
					<option value="Pending" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='Pending'){ echo "selected";};?> >Pending</option>
					<option value="Allotment" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='Allotment'){ echo "selected";};?> >Allotment</option>
					<option value="Proceed" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='Proceed'){ echo "selected";};?> >Proceed</option>
					<option value="Upload" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='Upload'){ echo "selected";};?> >Upload</option>
					<option value="Insp" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='Insp'){ echo "selected";};?> >Insp</option>
					<option value="Payment" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='Payment'){ echo "selected";};?> >Payment</option>
					<option value="SemiSuccessfull" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='SemiSuccessfull'){ echo "selected";};?> >SemiSuccessfull</option>
					<option value="hsrp" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='hsrp'){ echo "selected";};?> >hsrp</option>
					<option value="Fitment" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='Fitment'){ echo "selected";};?> >Fitment</option>
					<option value="Ready" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='Ready'){ echo "selected";};?> >Ready</option>
					<option value="RC" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='RC'){ echo "selected";};?> >RC</option>
					<option value="Register" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='Register'){ echo "selected";};?> >Register</option>
					<option value="IssueDocs" <?php  if(isset($_POST['report_from_status']) && $_POST['report_from_status']=='IssueDocs'){ echo "selected";};?> >IssueDocs</option>
				</select>
			</div>
			<div class="col-sm-2">
				<select name="report_to_status" class="form-control" required>
					<option value="">To</option>
					<option value="Pending" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='Pending'){ echo "selected";};?> >Pending</option>
					<option value="Allotment" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='Allotment'){ echo "selected";};?> >Allotment</option>
					<option value="Proceed" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='Proceed'){ echo "selected";};?> >Proceed</option>
					<option value="Upload" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='Upload'){ echo "selected";};?> >Upload</option>
					<option value="Insp" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='Insp'){ echo "selected";};?> >Insp</option>
					<option value="Payment" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='Payment'){ echo "selected";};?> >Payment</option>
					<option value="SemiSuccessfull" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='SemiSuccessfull'){ echo "selected";};?> >SemiSuccessfull</option>
					<option value="hsrp" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='hsrp'){ echo "selected";};?> >hsrp</option>
					<option value="Fitment" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='Fitment'){ echo "selected";};?> >Fitment</option>
					<option value="Ready" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='Ready'){ echo "selected";};?> >Ready</option>
					<option value="RC" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='RC'){ echo "selected";};?> >RC</option>
					<option value="Register" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='Register'){ echo "selected";};?> >Register</option>
					<option value="IssueDocs" <?php  if(isset($_POST['report_to_status']) && $_POST['report_to_status']=='IssueDocs'){ echo "selected";};?> >IssueDocs</option>
				</select>
			</div>
			<div class="col-sm-2">
				<button type="submit" name="view_task_report" class="btn btn-success">View</button>
			</div>
		</form>
	</div>
	<div class="col-sm-12">
<?php
if(isset($_POST['view_task_report'])){
	
	$report_start_date="";
	$report_end_date="";
	
	if($_POST['report_from_status']=='Pending'){
		$report_start_date='customer.created_date';
	}else if($_POST['report_from_status']=='Allotment'){
		$report_start_date='customer.allotment_time';
	}else if($_POST['report_from_status']=='Proceed'){
		$report_start_date='customer.proceed_time';
	}else if($_POST['report_from_status']=='Upload'){
		$report_start_date='customer.upload_time';
	}else if($_POST['report_from_status']=='Insp'){
		$report_start_date='customer.insp_time';
	}else if($_POST['report_from_status']=='Payment'){
		$report_start_date='customer.payment_time';
	}else if($_POST['report_from_status']=='SemiSuccessfull'){
		$report_start_date='customer.semi_successfull_time';
	}else if($_POST['report_from_status']=='hsrp'){
		$report_start_date='customer.hsrp_time';
	}else if($_POST['report_from_status']=='Fitment'){
		$report_start_date='customer.fitment_time';
	}else if($_POST['report_from_status']=='Ready'){
		$report_start_date='customer.ready_time';
	}else if($_POST['report_from_status']=='RC'){
		$report_start_date='customer.rc_time';
	}else if($_POST['report_from_status']=='Register'){
		$report_start_date='customer.register_time';
	}else if($_POST['report_from_status']=='IssueDocs'){
		$report_start_date='customer.issue_docs_time';
	}
	
	if($_POST['report_to_status']=='Pending'){
		$report_end_date='customer.created_date';
	}else if($_POST['report_to_status']=='Allotment'){
		$report_end_date='customer.allotment_time';
	}else if($_POST['report_to_status']=='Proceed'){
		$report_end_date='customer.proceed_time';
	}else if($_POST['report_to_status']=='Upload'){
		$report_end_date='customer.upload_time';
	}else if($_POST['report_to_status']=='Insp'){
		$report_end_date='customer.insp_time';
	}else if($_POST['report_to_status']=='Payment'){
		$report_end_date='customer.payment_time';
	}else if($_POST['report_to_status']=='SemiSuccessfull'){
		$report_end_date='customer.semi_successfull_time';
	}else if($_POST['report_to_status']=='hsrp'){
		$report_end_date='customer.hsrp_time';
	}else if($_POST['report_to_status']=='Fitment'){
		$report_end_date='customer.fitment_time';
	}else if($_POST['report_to_status']=='Ready'){
		$report_end_date='customer.ready_time';
	}else if($_POST['report_to_status']=='RC'){
		$report_end_date='customer.rc_time';
	}else if($_POST['report_to_status']=='Register'){
		$report_end_date='customer.register_time';
	}else if($_POST['report_to_status']=='IssueDocs'){
		$report_end_date='customer.issue_docs_time';
	}
	
	$report_start_time=$_POST['report_start_date']." 00:00:00";
	$report_end_time=$_POST['report_end_date']." 23:59:59";
	$query_str="{$report_start_date} >= '{$report_start_time}' AND {$report_end_date} <= '{$report_end_time}'";
	$xcrud->where($query_str);
	
	$diff=date_diff($date1,$date2);
	
	//$xcrud->subselect('task_duration',"SELECT timediff($report_end_date,$report_start_date) FROM customer WHERE customer_id={CId}");
	//$xcrud->subselect('task_duration',"SELECT CONCAT(FLOOR(TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) / (7 * 24 * 60 * 60)), ' week ',FLOOR((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) % (7 * 24 * 60 * 60)) / (24 * 60 * 60)), ' day ',FLOOR((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) % (24 * 60 * 60)) / (60 * 60)), ' hour ',FLOOR((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) % (60 * 60)) / 60), ' minute ',(TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) % 60), ' second') AS duration FROM customer WHERE customer_id={CId}");
	
	//$xcrud->subselect('task_duration',"SELECT CONCAT_WS(' ',IF(w > 0, CONCAT(w, ' week'), NULL),IF(d > 0, CONCAT(d, ' day'), NULL),IF(h > 0, CONCAT(h, ' hr'), NULL),IF(m > 0, CONCAT(m, ' min.'), NULL),CONCAT(s, ' sec.')) AS duration FROM (SELECT FLOOR(TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) / (7 * 24 * 60 * 60)) AS w,FLOOR((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) % (7 * 24 * 60 * 60)) / (24 * 60 * 60)) AS d,FLOOR((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) % (24 * 60 * 60)) / (60 * 60)) AS h,FLOOR((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) % (60 * 60)) / 60) AS m,(TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) % 60) AS s FROM customer WHERE customer_id={{modification.CId}}) AS time_parts");
	
	$xcrud->subselect('task_duration', "
    CONCAT_WS(' ',
        IF(TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) DIV (7*24*60*60) > 0, 
            CONCAT(TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) DIV (7*24*60*60), ' week'), 
            NULL
        ),
        IF((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) MOD (7*24*60*60)) DIV (24*60*60) > 0, 
            CONCAT((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) MOD (7*24*60*60)) DIV (24*60*60), ' day'), 
            NULL
        ),
        IF((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) MOD (24*60*60)) DIV (60*60) > 0, 
            CONCAT((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) MOD (24*60*60)) DIV (60*60), ' hr'), 
            NULL
        ),
        IF((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) MOD (60*60)) DIV 60 > 0, 
            CONCAT((TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) MOD (60*60)) DIV 60, ' min.'), 
            NULL
        ),
        CONCAT(TIMESTAMPDIFF(SECOND, $report_start_date, $report_end_date) MOD 60, ' sec.')
    )
");
	
	$xcrud->label('task_duration','Task Duration');
	
	
	echo $xcrud->render();
	//print_r($xcrud);
}
?>
	</div>
</div>
</div>
</div>
</div>

<!-- Compare data model-->
<div id="taskRemarkReportModal" class="modal fade" role="dialog">
  <div class="modal-dialog modal-lg">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Task Remarks</h4>
      </div>
      <div class="modal-body">
		<table class="table table-bordered table-bordered">
			<thead><tr><th>Type</th><th>Late By</th><th>Remark</th></tr></thead>
			<tbody id="task_remark_report_div"></tbody>
		</table>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
<?php
include "footer.php";
echo Xcrud::load_js();
?>
<script>
function openTaskRemarkReportModal(id){
	dataString='action=getTaskRemarkReport&id='+id;
	
    $.ajax({
        type: 'POST',
        url: 'ajax_data.php',
		data:dataString,
        success: function (response) {
            $('#task_remark_report_div').html(response);
        }
    });
}
</script>