<?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_category 	= isset($_REQUEST['search_category']) ? $_REQUEST['search_category'] : "";
if($search_category) {	
    $where	.= " AND part_number_category = '$search_category' ";
    $next_page_condition.="&search_category=$search_category";
}else{
	$where	.= " ";
}
###########################################  STRAT FILTER CONITIONS    #####################################
$search 	= isset($_REQUEST['search']) ? $_REQUEST['search'] : "";
if($search) {
	$search = trim($search);
	$where	.= " AND (project_number = '$search' ) ";
    $next_page_condition.="&search=$search";
}

$advance_search = isset($_REQUEST['advance_search']) ? $_REQUEST['advance_search'] : "";
$quick_search = isset($_REQUEST['quick_search']) ? $_REQUEST['quick_search'] : "";
if($advance_search) {
    //$quick_search=0;
    //$advance_search=1;
	//$selected_checkbox="advance_search=1";
    $quick_search_div="none";
    $advance_search_div="block";
    $next_page_condition.="&advance_search=$advance_search";
}
if($quick_search) {
   // $quick_search=1;
   // $advance_search=0;
	//$selected_checkbox="quick_search=1";
	$quick_search_div="block";
	$advance_search_div="none";
    $next_page_condition.="&quick_search=$quick_search";
}

if($advance_search){
	$search_part_number 	= isset($_REQUEST['search_part_number']) ? $_REQUEST['search_part_number'] : "";
	if($search_part_number) {
		$where	.= " AND part_number = '$search_part_number' ";
		$next_page_condition.="&search_part_number=$search_part_number";
	}
		$search_product_name 	= isset($_REQUEST['search_product_name']) ? $_REQUEST['search_product_name'] : "";
	if($search_product_name) {
		$where	.= " AND name LIKE '%$search_product_name%' ";
		$next_page_condition.="&search_product_name=$search_product_name";
	}

	
	$search_sales_item_code 	= isset($_REQUEST['search_sales_item_code']) ? $_REQUEST['search_sales_item_code'] : "";
	if($search_sales_item_code) {
		$where	.= " AND sales_item_code  LIKE '%$search_sales_item_code%' ";
		$next_page_condition.="&search_sales_item_code=$search_sales_item_code";
	}
	$search_barcode 	= isset($_REQUEST['search_barcode']) ? $_REQUEST['search_barcode'] : "";
	if($search_barcode) {
		$where	.= " AND barcode = '$search_barcode' ";
		$next_page_condition.="&search_barcode=$search_barcode";
	}
	$search_description 	= isset($_REQUEST['search_description']) ? $_REQUEST['search_description'] : "";
	if($search_description) {
		$where	.= " AND description_external = '$search_description' or description_internal = '$search_description' ";
		$next_page_condition.="&search_description=$search_description";
	}
	$is_taxable 	= isset($_REQUEST['is_taxable']) ? $_REQUEST['is_taxable'] : "";
	if($is_taxable) {
		$where	.= " AND is_taxable = '$is_taxable'";
		$next_page_condition.="&is_taxable=$is_taxable";
	}

	/* advance search end  */
}else if($quick_search){
	$search_product_q 	= isset($_REQUEST['search_product_q']) ? $_REQUEST['search_product_q'] : "";
	if($search_product_q) {
		$where	.= " AND name LIKE '%$search_product_q%' ";
		$next_page_condition.="&search_product_q=$search_product_q";
	}

	$product_template_id 	= isset($_REQUEST['product_template_id']) ? $_REQUEST['product_template_id'] : "";
	if($product_template_id) {
		$where	.= " AND product_template_id = '$product_template_id' ";
		$next_page_condition.="&product_template_id=$product_template_id";
	}
	$search_part_number_q 	= isset($_REQUEST['search_part_number_q']) ? $_REQUEST['search_part_number_q'] : "";
	if($search_part_number_q) {
		$where	.= " AND part_number = '$search_part_number_q' ";
		$next_page_condition.="&search_part_number_q=$search_part_number_q";
	}
}else{
	//$quick_search=1;
	//$quick_search_div="block";
	$advance_search_div="none";
}


$name 	= isset($_REQUEST['name']) ? $_REQUEST['name'] : "";
if($name) {
	$name = trim($name);
    $where	.= " AND (contact_first_name = '$name'  OR contact_lnitial LIKE '%$name%' OR contact_surname LIKE '%$name%')";
    $next_page_condition.="&name=$name";

}



$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'];
}
###########################################  	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_main";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Item Management";
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')
{   $id =$_REQUEST['id'];
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = '".$_REQUEST['id']."' ");
	$product_group_id = utility::getTableFieldById("product_name","id","pro_id",$id);
	mysqli_query($conn,"DELETE FROM product_name WHERE pro_id = '".$_REQUEST['id']."' ");
	mysqli_query($conn,"DELETE FROM product_link WHERE parent_id = '".$product_group_id."' ");
	
	$_SESSION['info_message_del'] = "Item deleted sucessfully";
	header("location: ?page=view_product&pro_temp_id=".$_REQUEST['pro_temp_id'].$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  #####################################

################################################# END INSERT #################################################

?>

<style>


</style>
<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="">
                    <!----------------------- End Message ------------------------------->
                    <div class="card">
					<!----------------------- 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>
					  <?} ?>
                        <div class="card-body">
                            <!-- search form -->
                            <?php include_once('product_management_search_options.inc.php');
							
							   $path="";
								echo $path='<b><a href="index?page=view_pro_template" class="text-primary">Item Template List</a> -> <a href="index?page=view_pro_attr&pro_temp_id='.$_REQUEST["pro_temp_id"].'"class="text-primary"></a></b>';
							?><br>
                            <!-- /.search form -->
                            Total No. of Products <?=$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_product&pro_temp_id=<?=$_REQUEST['pro_temp_id'];?>" method="post" name="form1" id="form1">
                                <div class="card-footer hide">
                                    <button type="submit" name="add" class="btn btn-primary">Add Item</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>Part Number</th>
                                                <th>Item </th>
                                                <th>Template</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 class="hide"><?php echo $gsi->getProductTempateById($row['product_template_id']);?></td>
												<td class=" "> <a href='#' data-toggle='modal' class="p-1 text-info text-bold" data-target='#ShowModal1' id="" alt="<?=$row['part_number_id']?>"><?=$p_name = utility::getTableFieldById("product_name","group_number","id",$row['part_number_id']);?></a></td>
												<td><a href="index?page=view_product_details&from=view_product&id=<?php echo $row['id'].$next_page_condition.$paging;?>"class="text-primary"><?php echo ucfirst($row['name']);?></a></td>
												<td class="text-center hide"><?=$p_name = utility::getTableFieldById("product_name","group_number","id",$row['part_number']);?></td>
												<td class="">
													<?php 
														if(isset($row['product_template_id']) && $row['product_template_id'] !=""){
															echo $gsi->getProductTempateById($row['product_template_id']); 
													}?>
                                                </td>
                                                <td class=""><a href="index?page=add_product&action=edit&id=<?php echo $row['id']?>&pro_temp_id=<?php echo $row['product_template_id'].$next_page_condition.$paging;?>"><i class="fa fa-fw fa-edit text-primary"></i> </a></td>
                                                <td><a href="index?page=view_product&action=delete&id=<?php echo $row['id']?>&pro_temp_id=<?php echo $row['product_template_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="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 Products 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 </button>
								</div>
                        </div>
                        
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<div class="modal fade" id="ShowModal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalScrollableTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable" role="document" style="width:75%; max-width:80%">
	<div class="modal-content">
	  <div class="modal-header">
		<h5 class="modal-title" id="exampleModalScrollableTitle" style="float:left">Group Tree</h5>
		<button type="button" class="close" data-dismiss="modal" aria-label="Close">
		  <span aria-hidden="true">&times;</span>
		</button>
	  </div>
	  <div class="modal-body">
		<!--<h5 class="modal-title" id="exampleModalScrollableTitle">Q->Quantity, C->Cost, LC->Labour Cost, TC->Total Cost, TLC->Total Labour Cost</h5>-->
		<div id="group_tree1" style="">
		</div>
	  </div>
	  <div class="modal-footer">
		<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
		
	  </div>
	</div>
  </div>
</div>
<script type="text/javascript">
$(function() {
	$('#ShowModal1').on('show.bs.modal', function (e) {
	  create_group_tree1(e.relatedTarget.attributes.alt.value);
   })
	
   function create_group_tree1(id) {
    $.ajax({
        url: site_url + "/modules/daynmic_product_management/index.php?page=ajax_actions&action=get_group_list&id=" + id,
        method: "POST",
        dataType: "json",
        success: function(data) {
            $('#group_tree1').treeview({
                data: data,
                levels: 1,
                collapseIcon: "fa fa-angle-down",
                expandIcon: "fa fa-angle-right",
                nodeIcon: "fa fa-share-alt-square",
                showBorder: false,
                enableLinks: true,
                preventUnselect: true,
                showTags: true
            });
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert("Something went wrong");
        }
    });
	}
	
});
</script>