<?php
session_start();
$where 					= " WHERE 1=1 AND is_organisation = '1' ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
} 
####################################### ARCHIVING/UNARCHIVING company #######################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'archive'){	
	foreach($_REQUEST["company_ids"] as $org_id){
		mysqli_query($conn,"UPDATE company_management SET is_archive = '1' WHERE id = '$org_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'] = "company(s) archived sucessfully";
	header("Location: index?page=view$query_string");
	exit();
}
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'unarchive'){	
	foreach($_REQUEST["company_ids"] as $org_id){
		mysqli_query($conn,"UPDATE company_management SET is_archive = '0' WHERE id = '$org_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'] = "company(s) unarchived sucessfully";
	header("Location: index?page=view$query_string");
	exit();
}
################################################## END ARCHIVING/UNARCHIVING company ###################################
###########################################  STRAT FILTER CONITIONS    #####################################

$search 	= isset($_REQUEST['search']) ? $_REQUEST['search'] : "";
if($search) {
    $where	.= " AND company_name LIKE '%$search%' ";
    $next_page_condition.="&search=$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		= "organisation_management";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Company";
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_companies = 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')
{
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = '".$_REQUEST['id']."' ");
	//include('common_delete.php');
	$_SESSION['info_message'] = "company 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_company_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'])) {
						echo '<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>';
						echo '<script>';
						echo 'Swal.fire({';
						echo '  title: "' . $_SESSION['info_message'] . '",'; 
						echo '  icon: "success",';
						echo '});';
						echo '</script>';
						unset($_SESSION['info_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('company_management_search_options.inc.php');?>
				<!-- /.search form -->
				Total No. of Companies <?=$total_no_of_companies;?>
				<div style="float:right;margin-bottom:4px;" class="hide">Export Company 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 hide">
					   <button type="submit" name="add" class="btn btn-primary">Add Company</button>
					   <? if(!$is_archive){ ?>
					   <button type="button" name="archive_company" id="archive_company" class="btn btn-warning pull-right">Archive</button>
					   <? }else{ ?>
					   <button type="button" name="unarchive_company" id="unarchive_company" 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>Company Name</th>
							<th>Edit</th>
							<th>Delete</th>
							<th class="no-sort hide" 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);
							?>
						  <tr class="activity<?=$status;?>" alt="<?=$bg_color_key;?>" style="background-color:<?=$bg_color;?>;">
							<td><?php echo ucfirst($row['company_name']);?></td>
							<td><a href="index?page=add&action=edit&id=<?=$row['id'].$next_page_condition.$paging;?>"> <i class="fa fa-fw fa-edit text-primary"></i> </a></td>
							<td><a href="index?page=view&action=delete&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 text-danger"></i> </a></td>
							<td align="center" class="hide">
								<div class="icheck-primary d-inline">
									<input class="checkbox" type="checkbox" name="company_ids[<?=$row['id']?>]" value="<?=$row['id']?>" id="primary">
									<label for="primary"></label>
								</div>
							</td>
						  </tr>
							<? } ?>
							
						</tbody>
					 </table>
					 </div>
					 <? }else{ ?>
						No company found...
					 <? } ?>
					 <div class="box-footer clearfix">
								<!-- Paging starts here  -->
								<? 
									$next_page_condition .="&page=view";
									include("paging/paging_row.inc.php") 
								?>
								<!-- Paging ends starts here  -->
							</div>
				</div>
				<div class="card-footer">
                   <button type="submit" name="add" class="btn btn-primary hide">Add company</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);
	});
	$(".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);
	});
	$(".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);
	});
	$(".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);
	});
	$(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
	});
	///////////////////////////// START ARCHIVING/UNARCHIVING company///////////////////////////////////////
	$("#archive_company").click(function(){
		if ($(".checkbox:checked").length == 0){
			alert("Please select company");
			return false;
		}
		if(confirm("Are you sure you want archive the selected company(s).")){
			$('#form1').attr("action","index?page=view&action=archive<?=$next_page_condition.$paging;?>");
			$('#form1').submit();
		}
	});
	$("#unarchive_company").click(function(){
		if ($(".checkbox:checked").length == 0){
			alert("Please select company");
			return false;
		}
		if(confirm("Are you sure you want unarchive the selected company(s).")){
			$('#form1').attr("action","index?page=view&action=unarchive<?=$next_page_condition.$paging;?>");
			$('#form1').submit();
		}
	});
	////////////////////////// START ARCHIVING/UNARCHIVING company ////////////////////////////////////
	/* $('#check_all').on('ifChanged', function(event){
		$('.checkbox').iCheck( "toggle");
	}); */
	$('#check_all').click(function(event){
		if(event.target.checked){
			$('.checkbox').attr('checked','checked');
		}else{
			$('.checkbox').removeAttr('checked');
		}
	});
});
</script>