<?php
//user.php
include('db_con.php');;
include('function.php');
if (isset($_SESSION['user_id'])) {
		$user_type = $_SESSION['user_type'];
		$user_id = $_SESSION['user_id'];
		$user_name = $_SESSION['user_name'];
		$branch_id = $_SESSION['branch_id'];
		$branch_name = $_SESSION['branch_name'];
		$store_id = $_SESSION['store_id'];
    $store_name = $_SESSION['store_name'];
}else{
	header('Location: login');
	die();
}
//if(!isset($_SESSION["type"]))
//{
//	header('location:login');
//}
//if($_SESSION["type"] != 'master')
//{
//	header("location:index.php");
//}
$sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'items_new'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $items_new = $statement->fetchColumn();
            $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'items_edit'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $items_edit = $statement->fetchColumn();
            $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'items_delete'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $items_delete = $statement->fetchColumn();
?>
<!DOCTYPE html>
<html>
<head>
	<title><?php  if(basename(__FILE__, '.php'=='items.php')){echo 'Item List';}?></title>
	<?php include('include/head.php'); ?>
	
</head>
<body>
	<?php include('include/header.php'); ?>
	<?php include('include/sidebar.php'); ?>
<div class="main-container">
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark"></h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Inventory</a></li>
              <li class="breadcrumb-item active">Items </li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <!-- Small boxes (Stat box) -->
     <span id="alert_action"></span>
    <div class="row">
        <div class="col-12">
          <div class="card">
            <div class="card-header">
            <div class="row">
                          <div class="col-lg-10 col-md-10 col-sm-8 col-xs-6">
                              <h3 class="panel-title">Items</h3>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-6" align="right">
                               <?php if($items_new=='ON')
                               {
                               	 echo ' <button type="button" name="add" id="add_button" data-toggle="modal" data-target="#dataModal" class="btn btn-primary "><i class="fa fa-plus"></i> Add New</button>';
                               }
                               ?>
                          
                          </div>
                        </div>
            </div>
            <!-- /.card-header -->
            <div class="card-body">
              
                        <table width="100%" id="_dataTable" class="table table-bordered table-striped table-hover">
                          <thead>
									<tr>
										<th width="10%">Image</th>
										<th width="5%">No.</th>										
										<th width="25%">Name</th>
										
										<th width="20%">Type</th>
										
										<th width="25%">Price</th>
										<th width="5%">Status</th>
										<?php if($items_edit=='ON')
										{
											echo '
										<th width="5%">Edit</th>';
										}?>
										<?php if($items_delete=='ON'){
											echo '<th width="5%">Delete</th>';
										}?>
									</tr>
								</thead>
                   			</table>
                   	 
			            </div>
			        </div>
			    </div>
			</div>
			       
        
        
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
 
  <!-- /.content-wrapper -->
 <?php include('include/footer.php'); ?>
		</div>
		</div>
	<?php include('include/script.php'); ?>
	
</body>
</html>
        <div id="dataModal" class="modal fade">
        	<div class="modal-dialog">
        		<form method="post" id="data_form">
        			<div class="modal-content">
        			<div class="modal-header">
        				
        				<?php if($items_new=='ON')
        				{
        					echo '<h4 class="modal-title"><i class="fa fa-plus"></i> Add Item</h4>';
        				}?>
						
        			</div>
        			<div class="modal-body">
        					<div class="form-group">
							<label>Enter Item Code(Barcode)</label>
							<input type="text" name="item_no" id="item_no" class="form-control" placeholder="Item Code"   />
						</div>
        				
        				<div class="form-group">
							<label>Enter Item Name</label>
							<input type="text" name="item_name" id="item_name" class="form-control" placeholder="Item Name"  required />
						</div>
						<div class="form-group">
							<label>Enter Description</label>
							<input type="text" name="description" id="description" class="form-control" placeholder="Item Description"   />
						</div>
						
						
						<div class="form-group">
						<label>Select Item Type</label>
						<br />
							<select name="item_type" id="item_type" class="form-control select2" required />
                        	<?php echo fill_itemtype($connect);?>
                         
                         
                        </select>
						</div>

						<div class="form-group">
						<label>Select Item Category</label>
						<br />
							<select name="category_id" id="category_id" class="form-control" required />
                        	<?php echo fill_itemcategory($connect);?>
                         
                         
                        </select>
						</div>

					

						<div class="form-group">
							<label>Dimension</label>
							<input type="text" name="dimension" id="dimension" class="form-control" placeholder="Dimension"  />
						</div>
												
						
						<div class="form-group">
							<label>Select Item UOM</label>
							<select name="uom" id="uom" class="form-control" placeholder="UOM" required />
                        	<?php echo fill_uom($connect);?>
                        </select>
						</div>
						
						
						<div class="form-group">
							<label>Minimum Qty</label>
							<input type="text" name="min_qty" id="min_qty" class="form-control" placeholder="Minimum Qty"  />
						</div>
						<div class="form-group">
							<label>Maximum Qty</label>
							<input type="text" name="max_qty" id="max_qty" class="form-control" placeholder="Maximum Qty"  />
						</div>

						<div class="form-group">
							<label>Location</label>
							<input type="text" name="location" id="location" class="form-control" placeholder="Location"  />
						</div>

						<div class="form-group">
							<label>HS Code</label>
							<input type="text" name="hs_code" id="hs_code" class="form-control" placeholder="HS Code" />
						</div>

						<div class="form-group">
						<label>Item Cost</label>
							<input type="text" name="item_cost" id="item_cost" class="form-control" placeholder="Item Cost" />
						</div>
					
						<div class="form-group">
						<label>Sale Price</label>
							<input type="text" name="retail_price" id="retail_price" class="form-control" placeholder="Sale Price" required />
						</div>

				
			
						<div class="form-group">
						<label>Select Item Image</label>
						     <input type="file" name="item_image" id="item_image" />
						     <span id="item_uploaded_image"></span>
						</div>
						<div class="form-group">
							<label>Status</label>
							<select name="status" class="form-control">
								<option value="Active">Active</option>
								<option value="Inactive">Inactive</option>
							</select>
						</div>
        			</div>
        			<div class="modal-footer">
        				<input type="hidden" name="item_id" id="item_id" />
        				<input type="hidden" name="btn_action" id="btn_action" />
        				<input type="submit" name="action" id="action" class="form-control btn btn-info" value="Add" />
        				<button type="button" class="form-control btn btn-secondary" data-dismiss="modal">Cancel</button>
        			</div>
        		</div>
        		</form>
        	</div>
        </div>
       
 <script>
 
  $(document).ready(function(){
    $(window).keydown(function(event){
    if(event.keyCode == 13) {
    
      event.preventDefault();
    }
  });
    $('#dated').datepicker({
      dateFormat: 'yy-mm-dd',
      changeMonth: true,
      changeYear: true
    });
  })
  
  </script>
<script>	
$(document).ready(function(){
	
    $('select:not(.normal)').each(function () {
                $(this).select2({
                    dropdownParent: $(this).parent()
                });
            });
	$('#add_button').click(function(){
		$('#data_form')[0].reset();
		$('.modal-title').html("<i class='fa fa-plus'></i> Add Item");
		$('#action').val("Add");
		$('#btn_action').val("Add");
		$('#uom').val(11);
	});
	var _dataTable = $('#_dataTable').DataTable({
		"processing": true,
		"serverSide": true,
		"order": [],
		"ajax":{
			url:"item_fetch.php",
			type:"POST"
		},
		"columnDefs":[
			{
				"target":[1,2,3,4],
				"orderable":false
			}
		],
		"language": {
					"info": "_START_-_END_ of _TOTAL_ entries",
					searchPlaceholder: "Search"
				},
				"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
				
				"pageLength": 25
	});
	$(document).on('click', '.update', function(){
		var item_id = $(this).attr("id");
		var btn_action = 'fetch_single';
		
		$.ajax({
			url:"item_action.php",
			method:"POST",
			data:{item_id:item_id, btn_action:btn_action},
			dataType:"json",
			success:function(data)
			{
				$('#dataModal').modal('show');
				$('#item_name').val(data.item_name);
				
				$('#item_type').val(data.type_id);
				$('#category_id').val(data.category_id);
				$('#item_no').val(data.item_no);
				$('#description').val(data.description);
				$('#dimension').val(data.dimension);
			
				$('#item_cost').val(data.item_cost);
				$('#hs_code').val(data.hs_code);
				$('#uom').val(data.uom);
				$('#retail_price').val(data.retail_price);
			//	$('#list_price').val(data.list_price);				
				$('#min_qty').val(data.min_qty);
				$('#max_qty').val(data.max_qty);
				$('#location').val(data.location);
				
				$('#status').val(data.status);						
				
				//$('#status').val(data.status);
				
				$('.modal-title').html("<i class='fa fa-pencil-square-o'></i> Edit Item");
				$('#item_id').val(item_id);
				$('#item_uploaded_image').html(data.item_image);
				$('#action').val('Edit');
				$('#btn_action').val('Edit');
				
			}
		})
	});
	$(document).on('submit', '#data_form', function(event){
		event.preventDefault();
		$('#action').attr('disabled','disabled');
		var extension = $('#item_image').val().split('.').pop().toLowerCase();
		  if(extension != '')
		  {
		   if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1)
		   {
		    alert("Invalid Image File");
		    $('#item_image').val('');
		    return false;
		   }
		  } 
		var item_data = $(this).serialize();
		$.ajax({
		    url:"item_action.php",
		    method:'POST',
		    data:new FormData(this),
		    contentType:false,
		    processData:false,
			success:function(data)
			{
				$('#data_form')[0].reset();
				$('#dataModal').modal('hide');
				$('#alert_action').fadeIn().html('<div class="alert alert-success">'+data+'</div>');
				$('#action').attr('disabled', false);
				_dataTable.ajax.reload();
			}
		})
	});
	
	
	$(document).on('click', '.delete', function(){
		var item_id = $(this).attr("id");
		var status = $(this).data('status');
		var btn_action = "delete";
		if(confirm("Are you sure you want to change status?"))
		{
			$.ajax({
				url:"item_action.php",
				method:"POST",
				data:{item_id:item_id, status:status, btn_action:btn_action},
				success:function(data)
				{
					$('#alert_action').fadeIn().html('<div class="alert alert-info">'+data+'</div>');
					_dataTable.ajax.reload();
				}
			})
		}
		else
		{
			return false;
		}
	});
});
	
</script>
<script>
	$('.select2').select2();
	</script>
<?php
//include('footer.php');
?>
