<?php
session_start();
$where 					= " WHERE 1=1  ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
} 
$warehouse 	= isset($_REQUEST['warehouse']) ? $_REQUEST['warehouse'] : "";
if($warehouse) {
    $where	.= " AND parent_id ='$warehouse' ";
    $next_page_condition.="&warehouse=$warehouse";
}
####################################### ARCHIVING/UNARCHIVING CUSTOMER #######################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'archive'){	
	foreach($_REQUEST["bay_ids"] as $customer_id){
		mysqli_query($conn,"UPDATE bays 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'] = "bay(s) archived sucessfully";
	header("Location: index?page=view_bay$query_string");
	exit();
}
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'unarchive'){	
	foreach($_REQUEST["bay_ids"] as $customer_id){
		mysqli_query($conn,"UPDATE bays 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'] = "bay(s) unarchived sucessfully";
	header("Location: index?page=view_bay$query_string");
	exit();
}
################################################## END ARCHIVING/UNARCHIVING ORGANISATION ###################################
###########################################  STRAT FILTER CONITIONS    #####################################
$search 	= isset($_REQUEST['search']) ? $_REQUEST['search'] : "";
if($search) {
	$where	.= " AND bay_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	.= "AND pro_template_id ='".$_REQUEST['pro_temp_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 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"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "product_attributes";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Item Template Attributes ";
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_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']."' ");
	//include('common_delete.php');
	$_SESSION['info_message'] = "Item Attributes  deleted sucessfully";
	header("location: $file_name?page=view_bay".$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  #####################################
?>

<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 ------------------------------->
                    <? 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">
                            <?php
								$path="";
								echo $path='<b><a href="index?page=view_pro_template" class="text-primary">Item Template List</a> -> </b>';
								
								?>
                            <div class='hide' style="float:right;margin-bottom:4px;">Export Item Attributes  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_attr&pro_temp_id=<?=$_REQUEST['pro_temp_id'];?>" method="post" name="form1" id="form1">
                                <div class="card-footer ">
								<? if($result<1){?>
									<button type="submit" name="add" class="btn btn-primary">Add Item Attributes</button>
								<?} 
								if(!$is_archive){ ?>
                                    <button type="button" name="archive_bay" id="archive_bay"
                                        class="btn btn-warning pull-right hide">Archive</button>
                                    <? }else{ ?>
                                    <button type="button" name="unarchive_bay" id="unarchive_bay"
                                        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>Item Template Name</th>
                                                <th>Added Attributes Name</th>
                                                <th>Edit</th>
                                                <th class="hide">Delete</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 nowrap><a href="index?page=view_product&pro_temp_id=<?php echo $row['pro_template_id'].$next_page_condition.$paging;?>" class="text-primary">
                                                        <?php 
															if(isset($row['pro_template_id']) && $row['pro_template_id'] !=""){
																echo  $gsi->getProductTempateById($row['pro_template_id']); 
															}									
														?>
                                                    </a>
                                                </td>
                                               <td>
												<?php
													$attributes = array();
													mysqli_data_seek($sql, 0); // Reset the pointer to the beginning
													while ($sub_row = mysqli_fetch_array($sql)) {
														if ($sub_row['pro_template_id'] == $row['pro_template_id']) {
															$attributes[] = ucwords($sub_row['attribute_name']);
														}
													}
													echo implode(", ", $attributes);
													?>
												</td>
                                                <td><a
                                                        href="index?page=add_pro_attr&action=edit&pro_temp_id=<?php echo $row['pro_template_id']?>&id=<?php echo $row['id'].$next_page_condition.$paging;?>">
                                                        <i class="fa fa-fw fa-edit text-primary"></i> </a></td>
                                                <td  class="hide"><a href="index?page=view_bay&action=delete&id=<?php echo $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>
                                             </tr>
                                            <? } ?>

                                        </tbody>
                                    </table>
                                </div>
                                <? }else{ ?>
                                No Item Attributes  found...
                                <? } ?>
                                <div class="box-footer clearfix">
                                    <!-- Paging starts here  -->
                                    <? 
									$next_page_condition .="&page=view_bay";
									include("paging/paging_row.inc.php") 
								?>
                                    <!-- Paging ends starts here  -->
                                </div>
                        </div>
                        <div class="card-footer">
                            <input type='hidden' name="warehouse" value="<?=$_REQUEST['warehouse'];?>">
                        </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<!-- Modal -->
<div class="modal fade" id="myModalAjaxforCustomer" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <div class="col-lg-12">
                    <div class="row">
                        <div class="col-lg-8">
                            <h4 class="modal-title" id="myModalLabelAjaxforCustomer">Modal title</h4>
                        </div>
                        <div class="col-lg-4">
                            <button type="button" class="close close_button2" data-dismiss="modal"
                                aria-hidden="true">&times;</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-body4 modal_iframe2">
                <section class="content">
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="alert alert-info alert-dismissable repair_add_update_message"
                                style="display:none;">
                                <button type="button" class="close" data-dismiss="alert"
                                    aria-hidden="true">&times;</button>
                            </div>
                            <div class="box box-primary">
                                <div class="box-body">

                                </div>
                            </div>
                        </div>
                    </div>
                </section>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default close_button2" data-dismiss="modal">Close</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

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

});
</script>