<?php
session_start();
$where 					= " WHERE 1=1  ";
$next_page_condition 	= "";
$form_action 	= "insert";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
} 
####################################### ARCHIVING/UNARCHIVING CUSTOMER #######################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'archive'){	
	foreach($_REQUEST["warehouse_ids"] as $customer_id){
		mysqli_query($conn,"UPDATE warehouses SET is_archive = '1' WHERE id = '$customer_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'] = "warehouse(s) archived sucessfully";
	header("Location: index?page=view_warehouse$query_string");
	exit();
}
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'unarchive'){	
	foreach($_REQUEST["warehouse_ids"] as $customer_id){
		mysqli_query($conn,"UPDATE warehouses SET is_archive = '0' WHERE id = '$customer_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'] = "warehouse(s) unarchived sucessfully";
	header("Location: index?page=view_warehouse$query_string");
	exit();
}
################################################## END ARCHIVING/UNARCHIVING ORGANISATION ###################################
###########################################  STRAT FILTER CONITIONS    #####################################
$search 	= isset($_REQUEST['search']) ? $_REQUEST['search'] : "";
if($search) {
	$where	.= " AND product_template_name LIKE '%$search%' ";
	$next_page_condition.="&search=$search";
}

$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	.= " ";
}
/*$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";
}*/
$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 created_on 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 created_on >= '$from_date' ";
	$next_page_condition.="&from_date=".$_REQUEST['from_date'];
}else if($to_date){
	$where.= " AND created_on <= '$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?page=view_pro_template"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "product_template";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Item Template";
include("../admin/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_organisations = 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']."' ");
	mysqli_query($conn,"DELETE FROM product_attributes WHERE pro_template_id = '".$_REQUEST['id']."' ");
	mysqli_query($conn,"DELETE FROM product_main WHERE product_template_id = '".$_REQUEST['id']."' ");
	mysqli_query($conn,"DELETE FROM product_attributes_value WHERE pro_template_id = '".$_REQUEST['id']."' ");
	$_SESSION['info_message_del'] = "Item Template deleted sucessfully";
	header("location: ?page=view_pro_template".$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_organisation_details_to_csv.inc.php");
}
###########################################  End Export  #####################################

################################################## START INSERT ##################################################
	if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){
	utility::addslashesOnAllInput(array("query_string","admin_id","engineers_id","ware_id")); 
	$warehouse_id 					= $_REQUEST['warehouse_id']; 
	$engineers_id 					= $_REQUEST['engineers_id']; 
	
	$updated_by				= $_SESSION['user_id'];
	$created_by 				=$_SESSION['admin_user_id'];
	$created_on					= date("Y-m-d H:i:s");	
	mysqli_query($conn,"DELETE FROM warehouse_engineers WHERE warehouse_id = '".$warehouse_id."' ");
	
  /* 
		 if(count($_REQUEST['engineers_id'])>0){	 
			foreach($_REQUEST['engineers_id'] as $key => $engineers_id){
				$engineers_id                   = $_REQUEST['engineers_id'][$key];		

				if($engineers_id!=""){
					 $sql1= "INSERT INTO warehouse_engineers (warehouse_id	,engineers_id,created_on ,created_by,updated_on,updated_by) VALUES('$warehouse_id','$engineers_id','$created_on','$created_by','$updated_on','$updated_by')";
					mysqli_query($conn,$sql1);
					
				}
			}
		} */
		/* if(count($_REQUEST['engineers_id'])>0){ */	
		
			foreach($_REQUEST['engineers_id'] as $key => $engineers_id){
				$engineers_id                   = $_REQUEST['engineers_id'][$key];	
				$ware_id                   = $_REQUEST['ware_id'][$key];
				if(mysqli_num_rows(mysqli_query($conn,"SELECT * FROM warehouse_engineers WHERE id = '$ware_id' ")) > 0){
					  $sql1= "UPDATE warehouse_engineers SET engineers_id = '$engineers_id' ,updated_by = '$updated_by' WHERE id = '$ware_id' ";
				}else{
					$sql1= "INSERT INTO warehouse_engineers (warehouse_id,engineers_id,created_on,created_by,updated_on,updated_by) VALUES('$warehouse_id','$engineers_id','$created_on','$created_by','$updated_on','$updated_by')";
				}
				mysqli_query($conn,$sql1);
			}
		
		$_SESSION['info_message'] = "Engineer Added sucessfully";
		header("Location: $file_name");
		exit();
	}
	
################################################# END INSERT #################################################

?>
<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="">
                    <!----------------------- Message ------------------------------->
                     <?php if(isset($_SESSION['info_message'])) { ?>
						<script>
							$(document).ready(function(){
								Toastify({
									text: "<?php echo $_SESSION['info_message']; ?>",duration: 4000,close: true,gravity: "top",position: 'right',backgroundColor: "#28a745",style: {borderBottom: "3px solid white",},
								}).showToast();
								<?php unset($_SESSION['info_message']); ?>
							});
						</script>
					<?}?>
					<?php if(isset($_SESSION['info_message_del'])) { ?>
						<script>
							$(document).ready(function(){
								Toastify({
									text: "<?php echo $_SESSION['info_message_del']; ?>",duration: 4000, close: true,gravity: "top",position: 'right', backgroundColor: "#E24D4C",style: {borderBottom: "3px solid white"},
								}).showToast();
								<?php unset($_SESSION['info_message_del']); ?>
							});
						</script>
					<?} ?>
                    <!----------------------- End Message ------------------------------->
                    <div class="card">
                        <div class="card-body">
                            <!-- search form -->
                            <?php include_once('pro_template_management_search_options.inc.php');?>
                            <!-- /.search form -->
                            Total No. of Item Templates: <?=$total_no_of_organisations;?>
                            <div class='hide' style="float:right;margin-bottom:4px;">Export warehouse 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_pro_template" method="post" name="form1" id="form1">
                                <div class="card-footer">
                                    <button type="submit" name="add" class="btn btn-primary">Create Item Template</button>
									<button type="button" class="btn btn-success add_engineer hide"
													id="add_engineer" value="<?php echo $row['id'];?>">
													<a href="index?page=view_product&quic=1"class="text-light text-bold " target=""> Item List</a></button>
                                    <? if(!$is_archive){ ?>
                                    <button type="button" name="archive_warehouse" id="archive_warehouse"
                                        class="btn btn-warning pull-right hide">Archive</button>
                                    <? }else{ ?>
                                    <button type="button" name="unarchive_warehouse" id="unarchive_warehouse"
                                        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 style="width:500px;">Item Template Name</th>
                                               
                                                <th> Add Item</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<?php echo $status;?>" alt="<?php echo $bg_color_key;?>"
                                                style="background-color:<?=$bg_color;?>;">
                                                <td>
													<a href="index?page=view_pro_attr&pro_temp_id=<?php echo $row['id'].$next_page_condition.$paging;?>"class="text-primary">
													<?php 
														if(isset($row['product_template_name']) && $row['product_template_name'] !=""){
															echo ucfirst($row['product_template_name']); 
													}?></a>
                                                </td>
												<td class="hide"><?php echo ucfirst($row['product_template_name']);?></td>
												
											    <td>
													<button type="button" class="btn btn-success add_engineer"
													id="add_engineer" value="<?php echo $row['id'];?>">
													<a href="index?page=add_product&pro_temp_id=<?php echo $row['id'].$next_page_condition.$paging;?>"class="text-light text-bold" target=""> Add Item</a></button>
												</td>
                                                <td><a href="index?page=add_pro_template&action=edit&id=<?php echo $row['id'].$next_page_condition.$paging;?>"><i class="fa fa-fw fa-edit text-primary"></i> </a></td>
                                                <td  class=""><a href="index?page=view_pro_template&action=delete&id=<?php echo $row['id'].$next_page_condition.$paging; ?>"onclick=" javascript:return confirm('Are you sure! You want to delete all products used this template');"><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="warehouse_ids[<?php echo $row['id']?>]"
                                                            value="<?php echo $row['id']?>" id="primary">
                                                        <label for="primary"></label>
                                                    </div>
                                                </td>
                                            </tr>
                                            <? } ?>

                                        </tbody>
                                    </table>
                                </div>
                                <? }else{ ?>
                                No Item Template found...
                                <? } ?>
                                <div class="box-footer clearfix">
                                    <!-- Paging starts here  -->
                                    <? 
									$next_page_condition .="&page=view_warehouse";
									include("paging/paging_row.inc.php") 
								?>
                                    <!-- Paging ends starts here  -->
                                </div>
								<div class="card-footer hide">
									<button type="submit" name="add" class="btn btn-primary ms-2">Add Item Template</button>
								</div>
                        </div>
                        
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<script type="text/javascript">
$(function() {
	
	$('#search').on('change',function(){
		
	});
});
</script>