<?php
include('db_con.php');
include('db_coni.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();
}
?>
<!DOCTYPE html>
<html>
<head>
	<title><?php echo basename(__FILE__, '.php'); ?></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="#">GL</a></li>
              <li class="breadcrumb-item active">Accounts </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">Item Types</h3>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-6" align="right">
                               <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>ID.</th>
										
										<th>Item Type</th>
										
										<th>Status</th>
										<th>Edit</th>
										<th>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">
        				
						<h4 class="modal-title"><i class="fa fa-plus"></i> Add New</h4>
        			</div>
        			<div class="modal-body">
        			
        				<div class="form-group">
							<label>Type Name</label>
							<input type="text" name="type_name" id="type_name" class="form-control" required />
						</div>
						
						
						<div class="form-group">
							<label>Status</label>
							<select name="status" id="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="type_id" id="type_id" />
        				<input type="hidden" name="btn_action" id="btn_action" />
        				
        				<input type="submit" name="action" id="action" class="btn btn-info" value="Add" />
        				<button type="button" class="btn btn-default" data-dismiss="modal">Close</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 Type");
		$('#action').val("Add");
		$('#btn_action').val("Add");
	});
	var _dataTable = $('#_dataTable').DataTable({
		"processing": true,
		"serverSide": true,
		"order": [],
		"ajax":{
			url:"item_type_fetch.php",
			type:"POST"
		},
		"columnDefs":[
			{
				"target":[2,3,4],
				"orderable":false
			}
		],
		"pageLength": 25
	});
	$(document).on('submit', '#data_form', function(event){
		event.preventDefault();
		$('#action').attr('disabled','disabled');
		var _data = $(this).serialize();
		$.ajax({
			url:"item_type_action.php",
			method:"POST",
			data:_data,
			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();
				//window.location = "item_type.php";
			}
		})
	});
	$(document).on('click', '.update', function(){
		var type_id = $(this).attr("id");
		var btn_action = 'fetch_single';
		$.ajax({
			url:"item_type_action.php",
			method:"POST",
			data:{type_id:type_id, btn_action:btn_action},
			dataType:"json",
			success:function(data)
			{
				$('#dataModal').modal('show');	
						
				$('#type_name').val(data.type_name);
				
				
				
				
				$('#status').val(data.status);
				
				$('.modal-title').html("<i class='fa fa-pencil-square-o'></i> Edit Item Type");
				$('#type_id').val(type_id);
				$('#action').val('Edit');
				$('#btn_action').val('Edit');
				
			}
		})
	});
	$(document).on('click', '.delete', function(){
		var type_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_type_action.php",
				method:"POST",
				data:{type_id:type_id, status:status, btn_action:btn_action},
				success:function(data)
				{
					$('#alert_action').fadeIn().html('<div class="alert alert-info">'+data+'</div>');
					_dataTable.ajax.reload();
					window.location="item_type.php";
				}
			})
		}
		else
		{
			return false;
		}
	});
});
	  
</script>
<?php
//include('footer.php');
?>
