<?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="20%">Name</th>
										
										<th width="20%">Type</th>
										<th width="20%">Desc</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 Name</label>
							<input type="text" name="item_name" id="item_name" class="form-control" placeholder="Item Name"  required />
						</div>
						<div class="form-group">
							<label>Description (Urdu)</label>
							<input type="text" name="description" id="description" class="form-control" placeholder="Description (Item Name in Urdu Language)"  required />
						</div>
						
						<label>Select Item Type</label>
						<div class="form-group">
						
							<select name="item_type" id="item_type" class="form-control select2" required />
                        	<?php echo fill_itemtype($connect);?>
                         
                         
                        </select>
						</div>

						<div class="form-group">
							<label>Dimension</label>
							<input type="text" name="dimension" id="dimension" class="form-control" placeholder="Dimension" required />
						</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" required />
						</div>
						<div class="form-group">
							<label>Maximum Qty</label>
							<input type="text" name="max_qty" id="max_qty" class="form-control" placeholder="Maximum Qty" required />
						</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>PCS/CTN</label>
							<input type="text" name="ctn" id="ctn" class="form-control" placeholder="PCS/CTN" required />
						</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>List Price</label>
							<input type="text" name="list_price" id="list_price" class="form-control" placeholder="List 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(){
	$('#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);
				$('#description').val(data.description);
				$('#item_type').val(data.type_id);
				$('#dimension').val(data.dimension);
			
				$('#item_cost').val(data.item_cost);
				
				$('#uom').val(data.uom);
				$('#retail_price').val(data.retail_price);
				$('#list_price').val(data.list_price);
				$('#op_stock').val(data.op_stock);
				$('#min_qty').val(data.min_qty);
				$('#max_qty').val(data.max_qty);
				$('#location').val(data.location);
				$('#ctn').val(data.ctn);
				$('#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');
?>
