<?php
$where 					= " WHERE 1=1 AND (is_enquiry = 1 AND is_celebrant = 0 AND is_funeral = 0 ";
$job_contract_ids		= $gsi->getJobIdsFrom("celebrant","funeral","enquiry");
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");
}
####################################### ARCHIVING/UNARCHIVING ORGANISATION #######################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'archive'){	
	foreach($_REQUEST["enquiry_ids"] as $enq_id){
		mysqli_query($conn,"UPDATE job SET is_archive = '1' WHERE id = '$enq_id' ");
	}
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
	$query_string = str_ireplace("action=archive", "", $query_string);
	$query_string = "&".$query_string;
	
	$_SESSION['info_message'] = "Enquiry(s) archived sucessfully";
	header("Location: index?page=view$query_string");
	exit();
}
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'unarchive'){	
	foreach($_REQUEST["enquiry_ids"] as $enq_id){
		mysqli_query($conn,"UPDATE job SET is_archive = '0' WHERE id = '$enq_id' ");
	}
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
	$query_string = str_ireplace("action=unarchive", "", $query_string);
	$query_string = "&".$query_string;
	
	$_SESSION['info_message'] = "Enquiry(s) unarchived sucessfully";
	header("Location: index?page=view$query_string");
	exit();
}
################################################## END ARCHIVING/UNARCHIVING ORGANISATION ###################################
###########################################  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']) : array();
if($sector) {
    $where	.= " AND sector LIKE '%$sector%' ";
	$sector = explode(",",$sector);
    $next_page_condition.="&sector=$sector";
}
/*$business_type 	= isset($_REQUEST['business_type']) ? $_REQUEST['business_type'] : "";
if($business_type) {
    $where	.= " AND business_type = '$business_type' ";
    $next_page_condition.="&business_type=$business_type";
}*/
$enquiry_source 	= isset($_REQUEST['enquiry_source']) ? $_REQUEST['enquiry_source'] : "";
if($enquiry_source) {
    $where	.= " AND enquiry_source = '$enquiry_source' ";
    $next_page_condition.="&enquiry_source=$enquiry_source";
}
$county 	= isset($_REQUEST['county']) ? $_REQUEST['county'] : "";
if($county) {
    $where	.= " AND county = '$county' ";
    $next_page_condition.="&county=$county";
}

$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 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'];
	}
}
$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 = "Enquiry 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'] = "Enquiry deleted sucessfully";
	header("location: $file_name?page=view".$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('enquiry_management_search_options.inc.php');?>
				<!-- /.search form -->
				<div class="row">
				<div class="col-12">
					<div style="float:left;clear:right;font-weight:bold;">Prospect Traffic Light</div>
					<?
						foreach(gsi::$prospectTrafficLightColorArray as $key => $value){
							$prospect_value = gsi::$prospectTrafficStatusArray[$key];
							$modeOfStatus[$prospect_value] = $value;
						}
						$l = 0;
						foreach($modeOfStatus as $k => $v){
							$l++;
							echo "<div style='cursor:pointer;background:$v;width:auto;height:auto;float:left;clear:right;margin-left:5px;/* border:1px solid;padding:0 3px 0 3px; */border: 0px solid;padding: 1px 6px 2px 6px;border-radius: 15px;font-weight: bold;font-size: 12px;box-shadow: 0 2px 2px 0 rgba(153, 153, 153, 0.14), 0 3px 1px -2px rgba(153, 153, 153, 0.2), 0 1px 5px 0 rgba(153, 153, 153, 0.12);' alt='$l' class='activity_status_button'>$k</div>";
						}
					?>
					<a href="index?page=view" style='cursor:pointer;background:#E91E63;color:#fff;width:auto;height:auto;float:left;clear:right;margin-left:5px;border: 0px solid;padding: 1px 6px 2px 6px;border-radius: 15px;font-weight: bold;font-size: 12px;box-shadow: 0 2px 2px 0 rgba(153, 153, 153, 0.14), 0 3px 1px -2px rgba(153, 153, 153, 0.2), 0 1px 5px 0 rgba(153, 153, 153, 0.12);'>View all</a>
				</div>
				</div>
				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" method="post" name="form1" id="form1">
					<div class="card-footer">
					   <button type="submit" name="add" class="btn btn-primary">Add Enquiry</button>
					   <? if(!$is_archive){ ?>
					   <button type="button" name="archive_enquiry" id="archive_enquiry" class="btn btn-warning pull-right">Archive</button>
					   <? }else{ ?>
					   <button type="button" name="unarchive_enquiry" id="unarchive_enquiry" class="btn btn-warning pull-right">Unarchive</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 style="width:90px;">L. S. Date</th>
							<th class="no-sort">Mileage</th>
							<th>Appointment</th>
							<th style="width:130px;">Notes / Reminders</th>
							<th>Document</th>
							<th>Edit</th>
							<th>Delete</th>
							<th class="no-sort" style="text-align:center;">
								<div class="icheck-primary d-inline">
									<input type="checkbox" name="check_all" id="check_all">
									<label for="check_all"></label>
								</div>
							</th>
						  </tr>
						</thead>
						<tbody>
							<?
								while($row =mysqli_fetch_array($sql)){ 
								$row=utility::validate_crypt_data($row);
									$bg_color = "";
									$bg_color_key = "";
									$status = "";
									if($row["prospect_traffic_lights"] != 0){
										$bg_color = gsi::$prospectTrafficLightColorArray[$row["prospect_traffic_lights"]];
										$bg_color_key = $row["prospect_traffic_lights"];
										
										if($bg_color_key == 51){
											$status = 1;
										}elseif($bg_color_key == 52){
											$status = 2;
										}elseif($bg_color_key == 53){
											$status = 3;
										}elseif($bg_color_key == 54){
											$status = 4;
										}elseif($bg_color_key == 55){
											$status = 5;
										}
									}
							?>
						  <tr class="activity<?=$status;?>" alt="<?=$bg_color_key;?>" style="background-color:<?=$bg_color;?>;">
							<!--<td><a href="index?page=view_enquiry_progress&from=view_enquiry&id=<?//=$row['id'];?>"><?php //echo $row['id']; ?></a></td>-->
							<td nowrap>
								<a href="index?page=view_enquiry_progress&from=view_enquiry&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 contenteditable><?php echo $row['phone_no']; ?></td>
							<td nowrap><?php echo utility::getFormatedDate($row['enquiry_date']); ?></td>
							<td nowrap>
								<?php 
									$last_date = $gsi->getLastNotesDateByJobId($row['id']);
									if($last_date !=""){
										echo utility::getFormatedDate($last_date); 
									}
								?>
							</td>
							<td><a href="" data-toggle="modal" data-target="#myModal" class="mileage" alt="<?=$row['id'];?>" style="">Mileage(<?=$gsi->getNumberOfMileageByJobId($row['id']);?>)</a></td>
							<td><a href="" data-toggle="modal" data-target="#myModal" class="appointment" alt="<?=$row['id'];?>" style="">Appointment(<span id="appointment_count<?=$row['id'];?>"><?=$gsi->getNumberOfAppointmentByJobId($row['id']);?></span>)</a></td>
							<td>
								<a href="" data-toggle="modal" data-target="#myModal" class="notes" alt="<?=$row['id'];?>" style="">Notes(<span id="notes_count<?=$row['id'];?>"><?=$gsi->getNumberOfNotesByJobId($row['id']);?></span>)</a> / <a href="" data-toggle="modal" data-target="#myModal" class="reminders" alt="<?=$row['id'];?>" style="">Reminders(<span id="reminder_count<?=$row['id'];?>"><?=$gsi->getNumberOfReminderByJobId($row['id']);?></span>)</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&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&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>
							<td align="center">
								<div class="icheck-primary d-inline">
									<input class="checkbox" type="checkbox" name="enquiry_ids[<?=$row['id']?>]" value="<?=$row['id']?>" id="primary">
									<label for="primary"></label>
								</div>
							</td>
						  </tr>
							<? } ?>
							
							
						</tbody>
					 </table>
					 </div>
					 <? }else{ ?>
						No enquiry found...
					 <? } ?>
					 <div class="card-footer clearfix">
								<!-- Paging starts here  -->
								<? 
									$next_page_condition .="&page=view";
									include("paging/paging_row.inc.php") 
								?>
								<!-- Paging ends starts here  -->
							</div>
				</div>
				<div class="box-footer">
                   <button type="submit" name="add" class="btn btn-primary">Add Enquiry</button>
                </div>
				</form>
			</div>
		</div>
	  </div>
	</div>
  </div>
</section>

<script type="text/javascript">
$(function () {

	/* $("#activity_status_label").click(function(){
		$(".activity1").show();
		$(".activity2").show();
		$(".activity3").show();
	}); */
	$(".activity_status_button").click(function(){
		var activity = $(this).attr("alt");
		if(activity == 1){
			$(".activity"+activity).show();
			$(".activity2").hide();
			$(".activity3").hide();
			$(".activity4").hide();
			$(".activity5").hide();
			$(".activity").hide();
		}else if(activity == 2){
			$(".activity"+activity).show();
			$(".activity1").hide();
			$(".activity3").hide();
			$(".activity4").hide();
			$(".activity5").hide();
			$(".activity").hide();
		}else if(activity == 3){
			$(".activity"+activity).show();
			$(".activity1").hide();
			$(".activity2").hide();
			$(".activity4").hide();
			$(".activity5").hide();
			$(".activity").hide();
		}else if(activity == 4){
			$(".activity"+activity).show();
			$(".activity1").hide();
			$(".activity2").hide();
			$(".activity3").hide();
			$(".activity5").hide();
			$(".activity").hide();
		}else if(activity == 5){
			$(".activity"+activity).show();
			$(".activity1").hide();
			$(".activity2").hide();
			$(".activity3").hide();
			$(".activity4").hide();
			$(".activity").hide();
		}
	});

	$(".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=e&job_id="+job_id+hide_top_left);
	});
	$(".mileage").click(function(){	
		$("#myModalLabel").text("Mileage Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/mileage/index?page=view&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=e&job_id="+job_id+hide_top_left);
		$(".modal-footer").find(".close_button").attr("alt",job_id);
	});
	$(".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=e&job_id="+job_id+hide_top_left);
		$(".modal-footer").find(".close_button").attr("alt",job_id);
	});
	$(".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=e&job_id="+job_id+hide_top_left);
		$(".modal-footer").find(".close_button").attr("alt",job_id);
	});
	/* $(".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=e&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=e"+hide_top_left);
	});
	/* $(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
		location.reload();
	}); */
	$(".close_button").click(function(){
		var model_label_text  = $("#myModalLabel").text();
		var res = model_label_text.split(" ");
		var job_id = $(this).attr("alt");
		if(res[0]=='Appointment'){
			//alert(job_id);
			var form_data   = {'job_id':job_id};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/enquiry_management/index.php?page=ajax_actions&action=count_no_of_appointment",
				data: form_data,
				success: function(result){          
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						//alert(resultObj.total_appointment);
						//alert("This email already in system");
						$("#appointment_count"+job_id).show();
						$("#appointment_count"+job_id).html(resultObj.total_appointment);
					}else{
						$("#appointment_count"+job_id).html("");
						$("#appointment_count"+job_id).hide();
					}
				}
			});
		}else if(res[0]=='Notes'){
			//alert(job_id);
			var form_data   = {'job_id':job_id};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/enquiry_management/index.php?page=ajax_actions&action=count_no_of_notes",
				data: form_data,
				success: function(result){          
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						//alert(resultObj.total_notes);
						//alert("This email already in system");
						$("#notes_count"+job_id).show();
						$("#notes_count"+job_id).html(resultObj.total_notes);
					}else{
						$("#notes_count"+job_id).html("");
						$("#notes_count"+job_id).hide();
					}
				}
			});
		}else if(res[0]=='Reminders'){
			//alert(job_id);
			var form_data   = {'job_id':job_id};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/enquiry_management/index.php?page=ajax_actions&action=count_no_of_reminder",
				data: form_data,
				success: function(result){          
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "success"){
						//alert(resultObj.total_reminder);
						//alert("This email already in system");
						$("#reminder_count"+job_id).show();
						$("#reminder_count"+job_id).html(resultObj.total_reminder);
					}else{
						$("#reminder_count"+job_id).html("");
						$("#reminder_count"+job_id).hide();
					}
				}
			});
		}else{	
			$(".modal_iframe").attr("src","");
			location.reload();
		}
	});
	///////////////////////////// START ARCHIVING/UNARCHIVING Enquiry///////////////////////////////////////
	$("#archive_enquiry").click(function(){
		if ($(".checkbox:checked").length == 0){
			alert("Please select Enquiry");
			return false;
		}
		if(confirm("Are you sure you want archive the selected Enquiry(s).")){
			$('#form1').attr("action","index?page=view&action=archive<?=$next_page_condition.$paging;?>");
			$('#form1').submit();
		}
	});
	$("#unarchive_enquiry").click(function(){
		if ($(".checkbox:checked").length == 0){
			alert("Please select Enquiry");
			return false;
		}
		if(confirm("Are you sure you want unarchive the selected Enquiry(s).")){
			$('#form1').attr("action","index?page=view&action=unarchive<?=$next_page_condition.$paging;?>");
			$('#form1').submit();
		}
	});
	///////////////////////////// START ARCHIVING/UNARCHIVING Enquiry///////////////////////////////////////
	/*$('#check_all').on('ifChanged', function(event){
		alert("sssssssssss");
		$('.checkbox').iCheck( "toggle");
	});*/
	$('#check_all').click(function(event){
		if(event.target.checked){
			$('.checkbox').attr('checked','checked');
		}else{
			$('.checkbox').removeAttr('checked');
		}
	});
});
</script>