<?php
$message = (isset($_SESSION['message'])) ? $_SESSION['message'] :"";
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : "insert";

$collapse_add_new_mileage = "collapse";
$add_btn_hide = "";
$cancel_btn_hide = "";
$search_list = "block";
$listing = "block";

$where 	= " WHERE 1=1 ";


$query_string = "";

$button_value 	= "Add";
$table_name		= "job_contract";

$next_page_condition 	= "";

if($_REQUEST['hide_top']){
	$next_page_condition.="&hide_top=1";
}
if($_REQUEST['hide_left']){
	$next_page_condition.="&hide_left=1";
}

if($_REQUEST['job_id']){
	$job_id = $_REQUEST['job_id'];
	$where 	.= " AND job_id = $job_id ";
	$next_page_condition.="&job_id=".$job_id;
}

if(isset($_REQUEST['source'])){
	$source = $_REQUEST['source'];
	$next_page_condition.="&source=$source";
}


$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
	//$next_page_condition.="&start=$start";
}
$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){
	$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	$collapse_add_new_mileage = "";
	$add_btn_hide = "hide";
	$cancel_btn_hide = "hide";
	$search_list = "none";
	$listing = "none";
	
	$action 	= "update";
	$button_value 	= "Update";
	$page_name		= "Edit Contract";	
		
    $id = $_REQUEST['id'];
    $sql= "SELECT * FROM job_contract WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

    $id= $row_edit['id'];
	$job_id= $row_edit['job_id'];
    $policy_type= $row_edit['policy_type'];
    $contract_value= $row_edit['contract_value'];    
    $renewal_date= $row_edit['renewal_date'];
	$contract_status= $row_edit['contract_status'];
}
################################################# END EDIT ################################################# 


################################################## START update ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	$id= $_REQUEST['id'];
	$job_id= $_REQUEST['job_id'];
    $policy_type	= (isset($_REQUEST['policy_type']) && $_REQUEST['policy_type'] != '') ? $_REQUEST['policy_type'] : '0';
    $contract_value= $_REQUEST['contract_value'];    
	$renewal_date	 = utility::setFormatedDate($_REQUEST['renewal_date']);
	$contract_status	= (isset($_REQUEST['contract_status']) && $_REQUEST['contract_status'] != '') ? $_REQUEST['contract_status'] : '0';

    $sql= "UPDATE job_contract SET job_id = '$job_id',policy_type = '$policy_type',contract_value = '$contract_value',renewal_date = '$renewal_date',renewal_date = '$renewal_date',contract_status = '$contract_status',updated_by = '".$_SESSION['user_id']."' WHERE id = $id";
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace(array("page=add","action=edit"), array("page=list",""), $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	
	$_SESSION['message']	= ucfirst("Contract updated sucessfully");
	$redirect = "index?page=list$query_string";
	header("Location: $redirect");
	exit();
	
}
################################################# END update ################################################# 
    
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	    
	utility::addslashesOnAllInput(array("query_string"));
	$job_id = $_REQUEST['job_id'];
	$policy_type	= (isset($_REQUEST['policy_type']) && $_REQUEST['policy_type'] != '') ? $_REQUEST['policy_type'] : '0';
    $contract_value = $_REQUEST['contract_value'];
	$renewal_date	 = utility::setFormatedDate($_REQUEST['renewal_date']);
	$contract_status	= (isset($_REQUEST['contract_status']) && $_REQUEST['contract_status'] != '') ? $_REQUEST['contract_status'] : '0';
	$source	 = $_REQUEST['source'];
		
    $sql= "INSERT INTO $table_name (job_id,policy_type,contract_value,renewal_date,contract_status,source,updated_by) VALUES('$job_id','$policy_type','$contract_value','$renewal_date','$contract_status','$source','".$_SESSION['user_id']."')";
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add", "page=list", $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	
	$_SESSION['message']	= ucfirst("Contract added sucessfully");
	$redirect = "index?page=list&name=$name$query_string";
	header("Location: $redirect");
	exit();
}
################################################# END INSERT #################################################
###########################################  STRAT FILTER CONITIONS    #####################################
$description 	= isset($_REQUEST['description']) ? $_REQUEST['description'] : "";
if($description) {
    $where	.= " AND description LIKE '%$description%' ";
    $next_page_condition.="&description=$description";
}

$camp_name_search 	= isset($_REQUEST['camp_name_search']) ? $_REQUEST['camp_name_search'] : "";
if($camp_name_search) {
    $where	.= " AND camp_name LIKE '%$camp_name_search%' ";
    $next_page_condition.="&camp_name_search=$camp_name_search";
}

if($_REQUEST['job_id_search']){
	$job_id_search = $_REQUEST['job_id_search'];
	$where 	.= " AND job_id ='$job_id_search' ";
	$next_page_condition.="&job_id_search=".$_REQUEST['job_id_search'];
}

if($_REQUEST['admin_id_search']){
	$admin_id_search = $_REQUEST['admin_id_search'];
	$where 	.= " AND admin_id LIKE '%$admin_id_search%' ";
	$next_page_condition.="&admin_id_search=".$_REQUEST['admin_id_search'];
}
$from_renewal_date = isset($_REQUEST['from_renewal_date']) ? utility::setFormatedDate($_REQUEST['from_renewal_date']) : "";
$to_renewal_date = isset($_REQUEST['to_renewal_date']) ? utility::setFormatedDate($_REQUEST['to_renewal_date']) : "";
if($from_renewal_date && $to_renewal_date){
	$where .= " AND renewal_date BETWEEN '$from_renewal_date 00:00:00' and '$to_renewal_date 23:59:59' ";
    $next_page_condition.="&from_renewal_date=".$_REQUEST['from_renewal_date']."&to_renewal_date=".$_REQUEST['to_renewal_date']." ";
}else if($from_renewal_date){
	$where.= " AND renewal_date >= '$from_renewal_date' ";
	$next_page_condition.="&from_renewal_date=".$_REQUEST['from_renewal_date'];
}else if($to_renewal_date){
	$where.= " AND renewal_date <= '$to_renewal_date' ";
	$next_page_condition.="&to_renewal_date=".$_REQUEST['to_renewal_date'];
}
###########################################  	END  FILTER CONITIONS 	#####################################
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Contract Management ";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ORDER BY renewal_date DESC LIMIT $eu, $limit ";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

$total_no = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));
#################################   DELETE    #########################################
// when we click on delete this area of php mileage will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete'){
	$id	= $_REQUEST['id'];
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = $id ");
	
	$_SESSION['message']	= ucfirst("Contract deleted sucessfully");
	$redirect = "index?page=list";
	header("Location: $redirect");
	exit();
}
#################################  END DELETE    #########################################

#################################  Export CSV    #########################################
if(isset($_REQUEST['action']) && $_REQUEST['action']=='export_mileage'){
  //include("export_mileage_to_csv.inc.php");
}
##########################################################################################
?>

<!--<section class="content-header">
  <h1 style="margin-bottom:0px">
	<? //echo str_replace(array("_"),array(" "),$page_header); ?>
  </h1>		
</section>-->
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><? echo str_replace(array("_"),array(" "),$page_header); ?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<!----------------------- Message ------------------------------->
	<? if(isset($_SESSION['message'])){ ?>
	<section class="content-header">
	  <div class="alert alert-info alert-dismissable" style="margin-bottom:0px;">
		<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
		<? echo $_SESSION['message'];
			unset($_SESSION['message']);
		?>
	  </div>
	</section>
	<? } ?>
<!----------------------- End Message ------------------------------->
	<section class="content">
		<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12">
				<div class="card">

				<div class="card-body">
				<div class="" style="">
					<?php include_once('add_list_job_contract.inc.php');?>
				</div>
				Total : <?=$total_no;?>
				
				<!--<div style="float:right;margin:5px;"  class="hide_search_div">Export Mileage &nbsp;&nbsp;<a href="index?page=list&action=export_mileage<?=$next_page_condition;?>"><i class="fa fa-download" style="font-size:20px;"></i></a></div>-->
				<!--<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">-->					
				<form action="?page=list&name=<?=$name?>" method="post" name="form1" id="form1">
				<? if($result>0){
				?>
					<!-- Start  records header -->
				<div id="contract_listing" style="display:<?=$listing;?>;">
				<table id="example1" class="table table-bordered table-striped">
					<thead>
						<tr>
							<th>Job Id</th>
							<th>Policy type</th>
							<th>Contract status</th>
							<th>Contract value</th>
							<th>Renewal date</th>
							<th class="hide_search_div">Edit</th>						
							<th class="hide_search_div">Delete</th>
						</tr>
					</thead>
					<tbody>
					<!-- End records header -->
					<?
					$total_campaign = 0;
					$campaign = 0;
					while($row = mysqli_fetch_array($sql)){
					?>
					<!-- Start data -->
					<tr>
						<td><?php echo $gsi->getEnqQuoteJobById($row['job_id']); ?></td>
						<td><?php echo ucfirst(Keycode::getkeycodeById($row["policy_type"]));?></td>
						<td><?php echo ucfirst(Keycode::getkeycodeById($row["contract_status"]));?></td>
						<td><?php echo $row['contract_value']; ?></td>
						<td><?php echo utility::getFormatedDateReturn($row['renewal_date']); ?></td>
						<td class="hide_search_div"><a href="index?page=list&action=edit&id=<?=$row['id'].$next_page_condition.$paging;?>"> <img src="../images/edit.gif" border="0" /> </a></td>
						<td class="hide_search_div"><a
							href="index?page=list&action=delete&id=<?=$row['id'].$next_page_condition.$paging; ?>"
							onclick=" javascript:return confirm('Are you sure! You want to delete');"><img
							src="../images/del.gif" border="0" /> </a></td>
					</tr>
					<!-- End data -->
					<? 
					}?>
					
					<?}else{?>
					<tr>
						<td colspan="10">No Contract found...</td>
					</tr>
					<?php }?>
					</tbody>
					
				</table>
				</div>
					<!-- Paging starts here  -->
					<? 
					$next_page_condition .="&page=list";
					include("paging/paging_row.inc.php") ?>
					<!-- Paging ends starts here  -->
						
					<!--</table>-->
					</form>
					</div>
				</div>
			</div>
		</div>
		</div>
	</section>
<style>
@media print
{
	.hide_search_div{
		display:none;
	}
}
</style>	