<?php
$where 					= " WHERE 1=1 AND (is_quotation = 1 OR is_job = 1) ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}
###########################################  STRAT FILTER CONITIONS    #####################################
$search 	= isset($_REQUEST['search']) ? $_REQUEST['search'] : "";
if($search) {
    $where	.= " AND (id = '$search' OR company_name LIKE '%$search%' OR first_name1 LIKE '%$search%' OR last_name1 LIKE '%$search%' OR first_name2 LIKE '%$search%' OR last_name2 LIKE '%$search%' OR email LIKE '%$search%' OR postcode LIKE '%$search%') ";
    $next_page_condition.="&search=$search";
}
$name 	= isset($_REQUEST['name']) ? $_REQUEST['name'] : "";
if($name) {
    $where	.= " AND name LIKE '%$name%' ";
    $next_page_condition.="&name=$name";
}
$email 	= isset($_REQUEST['email']) ? $_REQUEST['email'] : "";
if($email) {
    $where	.= " AND email LIKE '%$email%' ";
    $next_page_condition.="&email=$email";
}
$postcode 	= isset($_REQUEST['postcode']) ? $_REQUEST['postcode'] : "";
if($postcode) {
    $where	.= " AND postcode LIKE '%$postcode%' ";
    $next_page_condition.="&postcode=$postcode";
}

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

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

$show_archive 	= isset($_REQUEST['show_archive']) ? $_REQUEST['show_archive'] : "";
if(!($show_archive)) {
	$archive_status_id = $config->archive_status_id;
    if(!$job_id){
		$where	.= " AND enquiry_status != '$archive_status_id' ";
		$next_page_condition.="&show_archive=$show_archive";
	}
}
$from_date = isset($_REQUEST['from_date']) ? utility::setFormatedDate($_REQUEST['from_date']) : "";
$to_date = isset($_REQUEST['to_date']) ? utility::setFormatedDate($_REQUEST['to_date']) : "";
if($from_date && $to_date){
	$where .= " AND enquiry_date BETWEEN '$from_date 00:00:00' and '$to_date 23:59:59' ";
    $next_page_condition.="&from_date=".$_REQUEST['from_date']."&to_date=".$_REQUEST['to_date']." ";
}else if($from_date){
	$where.= " AND enquiry_date >= '$from_date' ";
	$next_page_condition.="&from_date=".$_REQUEST['from_date'];
}else if($to_date){
	$where.= " AND enquiry_date <= '$to_date' ";
	$next_page_condition.="&to_date=".$_REQUEST['to_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
$table_name		= "job";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Payment Management";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ORDER BY id DESC LIMIT $eu, $limit";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);
##############################################################################################################
?>
 <section class="content-header">
  <h1>
	<?=$page_header;?>
  </h1>
</section>
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<div class="box">
				<!----------------------- Message ------------------------------->
				<? if(isset($_SESSION['info_message'])){ ?>
				  <div class="alert alert-info alert-dismissable">
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
					<? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
				  </div>
				  <? } ?>
				<!----------------------- End Message ------------------------------->
				<div class="box-body">
				<!-- search form -->
				<?php include_once('enquiry_management_search_options.inc.php');?>
				<!-- /.search form -->
				<!--------- Color codeing --------------------------------->
				<div class="col-xs-6">
					<div style="float:left;clear:right;font-weight:bold;">Payment Status</div>
					<?
						foreach(bebcrm::$paymentModeColorArray as $key => $value){
							$mode_of_payment_value = bebcrm::$paymentStatus[$key];
							$modeOfPaymentStatus[$mode_of_payment_value] = $value;
						}
						foreach($modeOfPaymentStatus as $k => $v){	
							echo "<div style='background:$v;width:auto;height:auto;float:left;clear:right;margin-left:5px;border:1px solid;padding:0 3px 0 3px;'>$k</div>";
						}
					?>
				</div>
				<div class="col-xs-6">
					<div style="float:left;clear:right;font-weight:bold;">Invoice Status</div>
					<?
						foreach(bebcrm::$invoiceModeColorArray as $key => $value){
							$mode_of_invoice_value = bebcrm::$invoiceStatus[$key];
							$modeOfInvoiceStatus[$mode_of_invoice_value] = $value;
						}
						foreach($modeOfInvoiceStatus as $k => $v){	
							echo "<div style='background:$v;width:auto;height:auto;float:left;clear:right;margin-left:5px;border:1px solid;padding:0 3px 0 3px;'>$k</div>";
						}
					?>
				</div>
				<div style="clear:both;margin-bottom:10px;"></div>
				<!--------- End Color codeing --------------------------------->
					<form action="index?page=add" method="post" name="form1" id="form1">
					<? if($result>0){?>
					 <table id="example1" class="table table-bordered table-striped">
						<thead>
						  <tr>
							<th>Q. / Jobs Id</th>
							<th>1st and 2nd Contact Name</th>
							<th>Email</th>
							<!--<th>Date</th>-->
							<th style="text-align:center;">Invoice</th>
							<th style="text-align:center;">Payment</th>
						  </tr>
						</thead>
						<tbody>
							<?
								while($row =mysqli_fetch_array($sql)){ 
									//$bg_color = bebcrm::$paymentModeColorArray[utility::getTableFieldById("payment","payment_status","job_id",$row["id"])];
									$bg_color = bebcrm::$paymentModeColorArray[$bebcrm->getMaxPaymentStatusByJobId($row["id"])];
							?>
						  <tr style="background-color:<?=$bg_color;?>;">
							<td><a href="<?=$config->siteURL;?>/enquiry_management/index?page=view_enquiry_progress&from=payment&id=<?=$row['id'];?>"><?php echo $row['id']; ?></a></td>
							<td>
								<?php 
									if(isset($row['first_name1']) && $row['first_name1'] !=""){
										echo ucfirst($row['first_name1'])." ".ucfirst($row['last_name1']); 
									}
									if(isset($row['first_name2']) && $row['first_name2'] !=""){
										echo " / ".ucfirst($row['first_name2'])." ".ucfirst($row['last_name2']); 
									}
								?>
							</td>
							<td><?php echo $row['email']; ?></td>
							<!--<td><?php //echo utility::getFormatedDate($row['common_date']); ?></td>-->
							<?
							//$bg_color_for_invoice = bebcrm::$invoiceModeColorArray[utility::getTableFieldById("invoice","invoice_status","job_id",$row["id"])];
							$bg_color_for_invoice = bebcrm::$invoiceModeColorArray[$bebcrm->getMaxInvoiceStatusByJobId($row["id"])];
							if(!(isset($bg_color_for_invoice) && $bg_color_for_invoice !="")){
								$bg_color_for_invoice = bebcrm::$invoiceModeColorArray[1];
							}
							?>
							<td style="text-align:center;background-color:<?=$bg_color_for_invoice;?>;"><a href="" data-toggle="modal" data-target="#myModal" class="invoice1" alt="<?=$row['id'];?>" style="">Invoice</a></td>
							<td style="text-align:center;"><a href="" data-toggle="modal" data-target="#myModal" class="payment" alt="<?=$row['id'];?>" style="">Payment</a></td>
						  </tr>
							<? } ?>
							<!-- Paging starts here  -->
							<? 
								$next_page_condition .="&page=view_enquiry";
								include("paging/paging_row.inc.php") ?>
							<!-- Paging ends starts here  -->
						</tbody>
					 </table>
					 <? }else{ ?>
						No enquiry found...
					 <? } ?>
				</div>
				</form>
			</div>
		</div>
	</div>
</section>
<script type="text/javascript">
$(function () {
	$(".invoice1").click(function(){	
		$("#myModalLabel").text("Invoice Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/payment_management/index?page=view_invoice&source=e&job_id="+job_id+hide_top_left);
	});
	$(".payment").click(function(){	
		$("#myModalLabel").text("Payment Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/payment_management/index?page=view_payment&source=e&job_id="+job_id+hide_top_left);
	});
	$(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
		location.reload();
	});
});
</script>