<?php
$where 					= " WHERE 1=1 AND (is_celebrant = 1 AND is_funeral = 0 ";
$job_contract_ids		= $gsi->getJobIdsFrom("enquiry","funeral","celebrant");
if(isset($job_contract_ids) && $job_contract_ids !=""){
	$where 	.= " OR id IN($job_contract_ids)) ";
}else{
	$where 	.= " )";
}
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}

$j_id = utility::app_crypt($_REQUEST['j_id'],'d');
if($_REQUEST['id'] != $j_id){
	die("Invaild URL");
}
###########################################  STRAT FILTER CONITIONS    #####################################
$search 	= isset($_REQUEST['search']) ? $_REQUEST['search'] : "";
if($search) {
	if(strpos($search," ") !== false){
		$search_array = explode(" ",$search);
		$search_str1 = $search_array[0];
		$search_str2 = $search_array[1];
		$where	.= " AND ((first_name = '$search_str1' AND last_name = '$search_str2') OR (first_name = '$search_str2' AND last_name = '$search_str1')) ";
		$next_page_condition.="&search=$search";
	}else{
		$where	.= " AND (id = '$search' OR company_name LIKE '%$search%' OR first_name LIKE '%$search%' OR last_name LIKE '%$search%' OR email LIKE '%".utility::app_crypt($search,'e')."%' OR postcode LIKE '%$search%') ";
		$next_page_condition.="&search=$search";
	}
}

$phone_no 	= isset($_REQUEST['phone_no']) ? $_REQUEST['phone_no'] : "";
if($phone_no) {
	//$enc_phone_no = utility::app_crypt($phone_no,"e");
    //$where	.= " AND (phone_no1 = '$enc_phone_no' OR phone_no2 = '$enc_phone_no' OR phone_no3 = '$enc_phone_no') ";
	$job_ids = "";
	$sql1 = "select * FROM job_phone_log where phone LIKE '%".$phone_no."%' ";
	$rs1 =  mysqli_query($conn,$sql1);
	if(mysqli_num_rows($rs1) > 0){
		while($row1 = mysqli_fetch_assoc($rs1)){
			$job_ids .= utility::app_crypt($row1["phone_log"],"d").",";
		}
		$job_ids = substr($job_ids,0,-1);
	}
    $where	.= " AND id IN($job_ids) ";
    $next_page_condition.="&phone_no=$phone_no";
}

$email_search 	= isset($_REQUEST['email_search']) ? $_REQUEST['email_search'] : "";
if($email_search) {
	//$enc_phone_no = utility::app_crypt($email_search,"e");
    //$where	.= " AND (phone_no1 = '$enc_phone_no' OR phone_no2 = '$enc_phone_no' OR phone_no3 = '$enc_phone_no') ";
	$job_ids = "";
	$sql2 = "select * FROM job_email_log where email LIKE '%".$email_search."%' ";
	$rs2 =  mysqli_query($conn,$sql2);
	if(mysqli_num_rows($rs2) > 0){
		while($row2 = mysqli_fetch_assoc($rs2)){
			$job_ids .= utility::app_crypt($row2["email_log"],"d").",";
		}
		$job_ids = substr($job_ids,0,-1);
	}
    $where	.= " AND id IN($job_ids) ";
    $next_page_condition.="&email_search=$email_search";
}

$sector 	= isset($_REQUEST['sector']) ? implode(",",$_REQUEST['sector']) : "";
if($sector) {
    $where	.= " AND sector LIKE '%$sector%' ";
	$sector = explode(",",$sector);
    $next_page_condition.="&sector=$sector";
}

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

$type_of_celebrant 	= isset($_REQUEST['type_of_celebrant']) ? implode(",",$_REQUEST['type_of_celebrant']) : "";
if($type_of_celebrant) {
    $where	.= " AND type_of_celebrant LIKE '%$type_of_celebrant%' ";
	$type_of_celebrant = explode(",",$type_of_celebrant);
    $next_page_condition.="&type_of_celebrant=$type_of_celebrant";
}
$accreditation_observer 	= isset($_REQUEST['accreditation_observer']) ? implode(",",$_REQUEST['accreditation_observer']) : "";
if($accreditation_observer) {
    $where	.= " AND accreditation_observer LIKE '%$accreditation_observer%' ";
	$accreditation_observer = explode(",",$accreditation_observer);
    $next_page_condition.="&accreditation_observer=$accreditation_observer";
}
$prospect_status 	= isset($_REQUEST['prospect_status']) ? $_REQUEST['prospect_status'] : "";
if($prospect_status) {
    $where	.= " AND prospect_status = '$prospect_status' ";
    $next_page_condition.="&prospect_status=$prospect_status";
}
$is_archive 	= isset($_REQUEST['is_archive']) ? $_REQUEST['is_archive'] : "";
if($is_archive) {	
    $where	.= " AND is_archive = '$is_archive' ";
    $next_page_condition.="&is_archive=$is_archive";
}else{
	$where	.= " AND is_archive = '0' ";
}
/*$show_archive 	= isset($_REQUEST['show_archive']) ? $_REQUEST['show_archive'] : "";
if(!($show_archive)) {
	$archive_status_id = $config->archive_status_id;
    if(!$job_id){
		$where	.= " AND prospect_status != '$archive_status_id' ";
		$next_page_condition.="&show_archive=$show_archive";
	}
}else{
		$archive_status_id = $config->archive_status_id;
		$where	.= " AND prospect_status = '$archive_status_id' ";
		$next_page_condition.="&show_archive=$show_archive";
}*/
$renewal_date 	= isset($_REQUEST['renewal_date']) ? $_REQUEST['renewal_date'] : "";
if($renewal_date) {	
	$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){
		$where1 = " AND job_contract.renewal_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){
		$where1 = " AND job_contract.renewal_date >= '$from_date' ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date'];
	}else if($to_date){
		$where1 = " AND job_contract.renewal_date <= '$to_date' ";
		$next_page_condition.="&to_date=".$_REQUEST['to_date'];
	}
	$job_ids = "";
	$sql1 = "SELECT * FROM job INNER JOIN job_contract ON job.id=job_contract.job_id WHERE 1=1 $where1 ";
	$rs1 =  mysqli_query($conn,$sql1);
	if(mysqli_num_rows($rs1) > 0){
		while($row1 = mysqli_fetch_assoc($rs1)){
			$job_ids .= $row1["job_id"].",";
		}
		$job_ids = substr($job_ids,0,-1);
	}
	$where 	.= " AND id IN($job_ids) ";
    $next_page_condition.="&renewal_date=$renewal_date";
}else{
	$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 celebrant_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 celebrant_date >= '$from_date' ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date'];
	}else if($to_date){
		$where.= " AND celebrant_date <= '$to_date' ";
		$next_page_condition.="&to_date=".$_REQUEST['to_date'];
	}
}
$admin_id_search 	= isset($_REQUEST['admin_id_search']) ? $_REQUEST['admin_id_search'] : "";
if($_REQUEST['admin_id_search']){
	$consultant_ids = "";
	$sql1 = "select job_id FROM job_admin_mapping where admin_id = '".$admin_id_search."' ";
	$rs1 =  mysqli_query($conn,$sql1);
	if(mysqli_num_rows($rs1) > 0){
		while($row1 = mysqli_fetch_assoc($rs1)){
			$consultant_ids .= $row1["job_id"].",";
		}
		$consultant_ids = substr($consultant_ids,0,-1);
	}
	$where 	.= " AND id IN($consultant_ids) ";
	$next_page_condition.="&admin_id_search=".$_REQUEST['admin_id_search'];
}
###########################################  	END  FILTER CONITIONS 	#####################################
$admin_in = "";
$admin_in_ids = "";
if($show_only_my_things  == 1){
	$sql1 = "select job_id FROM job_admin_mapping where admin_id = '".$_SESSION['user_id']."' ";
	$rs1 =  mysqli_query($conn,$sql1);
	if(mysqli_num_rows($rs1) > 0){
		while($row1 = mysqli_fetch_assoc($rs1)){
			$admin_in_ids .= $row1["job_id"].",";
		}
		$admin_in_ids = substr($admin_in_ids,0,-1);
	}
	
	if($admin_in_ids){
		$admin_in = " AND id IN($admin_in_ids)";
	}
	
	$where.=$admin_in;
}
###########################################  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 = "Quotation 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);

$total_no_of_enquiries = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));
#################################   DELETE    #########################################
// when we click on delete this area of php code will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete')
{
	$phone_log = utility::app_crypt($_REQUEST["id"],"e");
	$email_log = utility::app_crypt($_REQUEST["id"],"e");
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = '".$_REQUEST['id']."' ");
	mysqli_query($conn,"DELETE FROM job_phone_log WHERE phone_log = '".$phone_log."' ");
	mysqli_query($conn,"DELETE FROM job_email_log WHERE email_log = '".$email_log."' ");
	include('common_delete.php');
	$_SESSION['info_message'] = "Quotation deleted sucessfully";
	header("location: $file_name?page=view_celebrant".$next_page_condition.$paging);
	exit();
}
#################################  END DELETE    #########################################
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ To dublicate @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//
if(isset($_REQUEST['action']) && $_REQUEST['action']=='duplicate'){
	include('duplicate.inc.php');
}
//@@@@@@@ Dublicate End @@@@@@@@@@@@@@@//
###########################################  End dublicate  #####################################
#################################  Export CSV    #########################################
if(isset($_REQUEST['action']) && $_REQUEST['action']=='export_job_details'){
  include("export_job_details_to_csv.inc.php");
}
###########################################  End Export  #####################################
?>
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><?=$page_header;?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12">
			 <div class="card">
				<!----------------------- 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="card">
					<div class="card-body">
					<!-- search form -->
					<?php include_once('celebrant_management_search_options.inc.php');?>
					<!-- /.search form -->
					Total No. of enquiries <?=$total_no_of_enquiries;?>
					<!--<div style="float:right;margin-bottom:4px;">Export Enquiries Details &nbsp;&nbsp;<a href="index?page=view&action=export_job_details<?=$next_page_condition;?>"><i class="fa fa-download" style="font-size:20px;"></i></a></div>-->
					
						<form action="index?page=add_celebrant" method="post" name="form1" id="form1">
						<div class="card-footer">
						   <button type="submit" name="add" class="btn btn-primary">Add Quotation</button>
						</div>
						<? if($result>0){?>
						<div class="table-responsive">
						 <table id="example1" class="table table-bordered table-striped">
							<thead>
							  <tr>
								<!--<th>#</th>-->
								<th>Company Name / Contact Name</th>
								<th>Phone no.</th>
								<th>Date</th>
								<th>Quotation Items</th>
								<th style="width:90px;">L. S. Date</th>
								<!--<th>Phone Conversation</th>-->
								<th>Appointment</th>
								<th>Notes / Reminders</th>
								<th>Document</th>
								<th>Edit</th>
								<th>Delete</th>
							  </tr>
							</thead>
							<tbody>
								<?
									while($row =mysqli_fetch_array($sql)){ 
									$row=utility::validate_crypt_data($row);
								?>
							  <tr>
								<!--<td><a href="index?page=view_enquiry_progress&from=view_celebrant&id=<?//=$row['id'];?>"><?php //echo $row['id']; ?></a></td>-->
								<td>
									<a href="index?page=view_enquiry_progress&from=view_celebrant&j_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging;?>">
									<?php 
										if(isset($row['company_name']) && $row['company_name'] !=""){
											echo $row["company_name"];
										}
										if(isset($row['first_name']) && $row['first_name'] !=""){
											echo " / ".ucfirst($row['first_name'])." ".ucfirst($row['last_name']); 
										}									
									?>
									</a>
								</td>
								<td><?php echo $row['phone_no']; ?></td>
								<td nowrap><?php echo utility::getFormatedDate($row['enquiry_date']); ?></td>
								<td><a href="" data-toggle="modal" data-target="#myModal" class="quotationitems" alt="<?=$row['id'];?>" style="">Quotation Items (<?=$gsi->getTotalQuotationItemsByJobId($row['id']);?>)</a></td>
								<td nowrap>
									<?php 
										$last_date = $gsi->getLastNotesDateByJobId($row['id']);
										if($last_date !=""){
											echo utility::getFormatedDate($last_date); 
										}
									?>
								</td>
								<!--<td><a href="<?=$config->frontEndURL?>/admin/enquiry_management/index?page=view_celebrant_phone_conversation&from=view_celebrant&source=c&job_id=<?=$row['id'].$next_page_condition.$paging?>"  alt="<?=$row['id'];?>" style="">Phone Conversation</a></td>-->
								<td><a href="" data-toggle="modal" data-target="#myModal" class="appointment" alt="<?=$row['id'];?>" style="">Appointment(<?=$gsi->getNumberOfAppointmentByJobId($row['id']);?>)</a></td>
								<td>
									<a href="" data-toggle="modal" data-target="#myModal" class="notes" alt="<?=$row['id'];?>" style="">Notes(<?=$gsi->getNumberOfNotesByJobId($row['id']);?>)</a> / <a href="" data-toggle="modal" data-target="#myModal" class="reminders" alt="<?=$row['id'];?>" style="">Reminders(<?=$gsi->getNumberOfReminderByJobId($row['id']);?>)</a>
								</td>
								<td><a href="" data-toggle="modal" data-target="#myModal" class="document" alt="<?=$row['id'];?>" style="">Document(<?=$gsi->getNumberOfDocumentByJobId($row['id']);?>)</a></td>
								<td><a href="index?page=add_celebrant&action=edit&j_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging;?>"> <i class="fa fa-fw fa-edit"></i> </a></td>
								<td><a href="index?page=view_celebrant&action=delete&j_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging; ?>" onclick=" javascript:return confirm('Are you sure! You want to delete');"><i class="fa fa-fw fa-trash"></i> </a></td>
							  </tr>
								<? } ?>
								<!-- Paging starts here  -->
								<? 
									$next_page_condition .="&page=view";
									include("paging/paging_row.inc.php") ?>
								<!-- Paging ends starts here  -->
							</tbody>
						 </table>
						 </div>
						 <? }else{ ?>
							No quotation found...
						 <? } ?>
					</div>
					<div class="card-footer">
					   <button type="submit" name="add" class="btn btn-primary">Add Quotation</button>
					</div>
					</form>
				</div>
				</div>
			</div>
		</div>
	</div>
</section>

<script type="text/javascript">
$(function () {
	$(".job_contract").click(function(){	
		$("#myModalLabel").text("Job Contract Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/job_contract/index?page=list&source=c&job_id="+job_id+hide_top_left);
	});
	$(".phone_conversation").click(function(){	
		$("#myModalLabel").text("Phone Conversation Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/enquiry_management/index?page=view_celebrant_phone_conversation&source=c&job_id="+job_id+hide_top_left);
	});
	$(".quotationitems").click(function(){	
		$("#myModalLabel").text("Quotation Items");
		$(".btn-default").hide();
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/quotation_items/index?page=addquickquotationitems&source=e&job_id="+job_id+hide_top_left);
	});
	$(".appointment").click(function(){	
		$("#myModalLabel").text("Appointment Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/appointment/index?page=view&source=c&job_id="+job_id+hide_top_left);
	});
	$(".notes").click(function(){	
		$("#myModalLabel").text("Notes Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/notes/index?page=view&source=c&job_id="+job_id+hide_top_left);
	});
	$(".reminders").click(function(){	
		$("#myModalLabel").text("Reminders Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/notes/index?page=view_reminder&source=c&job_id="+job_id+hide_top_left);
	});
	/* $(".document").click(function(){	
		$("#myModalLabel").text("Documents Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/document_management/index?page=view&source=c&job_id="+job_id+hide_top_left);
	}); */
	$(".document").click(function(){	
		$("#myModalLabel").text("Documents Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/modules/fileuploads/image-files.php?id="+job_id+"&page=view&source=c"+hide_top_left);
	});
	$(".timesheet").click(function(){	
		$("#myModalLabel").text("Timesheet Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/timesheet_management/index?page=view&source=c&job_id="+job_id+hide_top_left);
	});
	$(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
		location.reload();
	});
});
</script>