<?php
session_start();
$where 					= " WHERE 1=1 ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
} 
$item_id = utility::app_crypt($_REQUEST['item_id'],'d');
if($_REQUEST['id'] != $item_id){
	//die("Invaild URL");
}

###########################################  STRAT FILTER CONDITIONS    #####################################

$part_number_input = isset($_REQUEST['part_number']) ? $_REQUEST['part_number'] : '';
$part_number = mysqli_real_escape_string($conn, $part_number_input);

	if ($part_number_input) {
		$where .= " AND part_number LIKE '%$part_number%'";
		$next_page_condition .= "&part_number=" . urlencode($part_number_input);
	}


###########################################  	END  FILTER CONITIONS 	#####################################
###########################################  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		= "item_management";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Item 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_records = 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".$next_page_condition.$paging);
	exit();
}
#################################  END DELETE    #########################################
?>

<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">
						<!-- search form -->
						<?php include_once('item_management_search_options.inc.php');?>
						<!-- /.search form -->
						Total No. of Items: <?=$total_no_of_records;?>
						
							<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 Item</button>
							   <?/* if(!$is_archive){ ?>
								<button type="button" name="archive_cause" id="archive_cause" class="btn btn-warning pull-right" style="float:right;">Archive</button>
								<? }else{ ?>
								<button type="button" name="unarchive_cause" id="unarchive_cause" class="btn btn-warning pull-right" style="float: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>Part Number</th>
									<th>Product Description</th>
									<th>Product Group</th>
									<th>BOM</th>
									<th>Edit</th>
									<th>Delete</th>
								  </tr>
								</thead>
								<tbody>
									<?
										while($row =mysqli_fetch_array($sql)){ 
										$row=utility::validate_crypt_data($row);
									?>
								  <tr>
								    <td nowrap>
										<a href="index?page=view_item_details&from=view_item&item_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging;?>" class="text-primary"><?php echo ucfirst($row["part_number"]);?>
										</a>
									</td>
									
									<td><?php echo $row["product_description"];?></td>
									<td><?php echo $row["product_group"];?></td>
									<td class=" "> <a href='#' data-toggle='modal' class="p-1 text-info text-bold" data-target='#ShowModal1' id="" alt="<?=$row['part_number_id']?>">View</a></td>
									
									<td><a href="index?page=add&action=edit&item_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&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&item_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 text-danger"></i> </a></td>
									
								  </tr>
									<? } ?>
									
								</tbody>
							 </table>
							 </div>
							 <? }else{ ?>
								No Item 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 hide">
						   <button type="submit" name="add" class="btn btn-primary">Add Item</button>
						</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">Bill Of Materils 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/item_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");
        }
    });
	}
	
});
$(function () {
	$(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
	});
	///////////////////////////// START ARCHIVING/UNARCHIVING cause///////////////////////////////////////
	$("#archive_cause").click(function(){
		if ($(".checkbox:checked").length == 0){
			alert("Please select cause");
			return false;
		}
		if(confirm("Are you sure you want archive the selected cause(s).")){
			$('#form1').attr("action","index?page=view&action=archive<?=$next_page_condition.$paging;?>");
			$('#form1').submit();
		}
	});
	$("#unarchive_cause").click(function(){
		if ($(".checkbox:checked").length == 0){
			alert("Please select cause");
			return false;
		}
		if(confirm("Are you sure you want unarchive the selected cause(s).")){
			$('#form1').attr("action","index?page=view&action=unarchive<?=$next_page_condition.$paging;?>");
			$('#form1').submit();
		}
	});
	///////////////////////////// START ARCHIVING/UNARCHIVING cause///////////////////////////////////////
	$('#check_all').click(function(){
        if($("#check_all:checked").length == 1){
            $('.checkbox').prop( "checked", true );
        }else{
            $('.checkbox').prop( "checked", false );
        }
    });
});
</script>