<?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();
}
$title = 'Customers';
$sql = "SELECT IFNULL(doc_status,'ON') FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'customer'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $customer = $statement->fetchColumn();
            
            $sql = "SELECT IFNULL(doc_status,'ON') FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'customer_new'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $customer_new = $statement->fetchColumn();
            $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'customer_edit'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $customer_edit = $statement->fetchColumn();
$sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'customer_delete'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $customer_delete = $statement->fetchColumn();
           
           
if($customer=='OFF'){session_destroy();header('Location: login');}
//if(!isset($_SESSION["type"]))
//{
//	header('location:login');
//}
//if($_SESSION["type"] != 'master')
//{
//	header("location:index.php");
//}
?>
<!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="#">Receivable</a></li>
              <li class="breadcrumb-item active">Customers </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">Customers</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>Name</th>
										
										
										
										<th>Phone</th>
										<th>Sales Man</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="cust_form">
        			<div class="modal-content">
        			<div class="modal-header">
        				
						<h4 class="modal-title"><i class="fa fa-plus"></i> Add Customer</h4>
        			</div>
        			<div class="modal-body">
        				
        				<div class="form-group">
							
							<input type="text" name="cust_name" id="cust_name" class="form-control" placeholder="Customer Name" required />
						</div>
						<div class="form-group">
							
							<input type="text" name="address" id="address" class="form-control"  placeholder="Address" />
						</div>

								<div class="form-group">
							
							<input type="text" name="area" id="area" class="form-control"  placeholder="Area" />
						</div>
						
						
						<div class="form-group">
							
							<input type="text" name="city" id="city" class="form-control"  placeholder="City" />
						</div>
						
						
						<div class="form-group">
							
							<input type="text" name="phone" id="phone" class="form-control"  placeholder="Phone" />
						</div>
						<div class="form-group">
						
							<input type="text" name="email" id="email" class="form-control"  placeholder="E-Mail" />
						</div>
						<div class="form-group">
							
							<input type="text" name="web" id="web" class="form-control"  placeholder="Website" />
						</div>
						<div class="form-group">
							
							<input type="text" name="op_balance" id="op_balance" class="form-control"  placeholder="Opening Balance" />
						</div>
						
					
						<div class="form-group">
							
							<input type="text" name="sale_target" id="sale_target" class="form-control"  placeholder="Sale Target" />
						</div>
						<div class="form-group">
						
							<input type="text" name="cr_limit" id="cr_limit" class="form-control"  placeholder="Credit Limit" />
						</div>
						<div class="form-group">
						
							<input type="text" name="due_days" id="due_days" class="form-control"  placeholder="Due Days" />
						</div>
						
						<div class="form-group">
							
							<select name="sm_id" id="sm_id" class="form-control" />
                        
                         <?php echo fill_sm($connect);?>
                        </select>
						</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="acc_no" id="acc_no" />
        				
        				<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-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(){
		$('#cust_form')[0].reset();
		$('.modal-title').html("<i class='fa fa-plus'></i> Add Customer");
		$('#action').val("Add");
		$('#btn_action').val("Add");
	});
	var _dataTable = $('#_dataTable').DataTable({
		"processing": true,
		"serverSide": true,
		"order": [],
		"ajax":{
			url:"customer_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
	});
	function ModifyEnterKeyPressAsTab() {
    if (window.event && window.event.keyCode == 13) {
        window.event.keyCode = 9;
    }
}
	$(document).on('submit', '#cust_form', function(event){
		event.preventDefault();
		$('#action').attr('disabled','disabled');
		var cust_data = $(this).serialize();
		$.ajax({
			url:"customer_action.php",
			method:"POST",
			data:cust_data,
			success:function(data)
			{
				$('#cust_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', '.update', function(){
		var acc_no = $(this).attr("id");
		var btn_action = 'fetch_single';
		
		$.ajax({
			url:"customer_action.php",
			method:"POST",
			data:{acc_no:acc_no, btn_action:btn_action},
			dataType:"json",
			success:function(data)
			{
				$('#dataModal').modal('show');
				
				$('#cust_name').val(data.acc_name);
				$('#address').val(data.address);
				$('#city').val(data.city);
				$('#area').val(data.area);
			//	$('#country').val(data.country);
				$('#phone').val(data.phone);
				$('#email').val(data.email);
				$('#web').val(data.web);
				$('#op_balance').val(data.op_balance);
				$('#sale_target').val(data.sale_target);
				$('#cr_limit').val(data.cr_limit);
				$('#due_days').val(data.due_days);
				$('#sm_id').val(data.sm_id);
				
				$('.modal-title').html("<i class='fa fa-pencil-square-o'></i> Edit Customer");
				$('#acc_no').val(acc_no);
				$('#action').val('Edit');
				$('#btn_action').val('Edit');
				
			}
		})
	});
	$(document).on('click', '.delete', function(){
		var acc_no = $(this).attr("id");
		var status = $(this).data('status');
		var btn_action = "delete";
		if(confirm("Are you sure you want to change status?"))
		{
			$.ajax({
				url:"customer_action.php",
				method:"POST",
				data:{acc_no:acc_no, 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>
<?php
//include('footer.php');
?>
