<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta charset="utf-8" />
<title>Sudila Printers</title>
<meta name="description" content="overview &amp; stats" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<!-- bootstrap & fontawesome -->
<link rel="stylesheet" href="assets/css/bootstrap.min.css" />
<link rel="stylesheet" href="assets/font-awesome/4.5.0/css/font-awesome.min.css" />
<!-- page specific plugin styles -->
<!-- text fonts -->
<link rel="stylesheet" href="assets/css/fonts.googleapis.com.css" />
<!-- ace styles -->
<link rel="stylesheet" href="assets/css/ace.min.css" class="ace-main-stylesheet" id="main-ace-style" />
<!--[if lte IE 9]>
    <link rel="stylesheet" href="assets/css/ace-part2.min.css" class="ace-main-stylesheet" />
<![endif]-->
<link rel="stylesheet" href="assets/css/ace-skins.min.css" />
<link rel="stylesheet" href="assets/css/ace-rtl.min.css" />
<!--[if lte IE 9]>
  <link rel="stylesheet" href="assets/css/ace-ie.min.css" />
<![endif]-->
<!-- inline styles related to this page -->
<!-- ace settings handler -->
<script src="assets/js/ace-extra.min.js"></script>
<!-- HTML5shiv and Respond.js for IE8 to support HTML5 elements and media queries -->
<!--[if lte IE 8]>
    <script src="assets/js/html5shiv.min.js"></script>
    <script src="assets/js/respond.min.js"></script>
<![endif]-->
</head>
<body class="no-skin">
<?php include 'common/navbar.php'; ?>
<div class="main-container ace-save-state" id="main-container">
  <?php include 'common/sidebar.php'; ?>
  <div class="main-content">
    <div class="main-content-inner">
      <div class="breadcrumbs ace-save-state" id="breadcrumbs">
        <ul class="breadcrumb">
          <li> <i class="ace-icon fa fa-home home-icon"></i> <a href="home">Home</a> </li>
          <li class="active">New Supplier </li>
        </ul>
        <!-- /.breadcrumb --> 
      </div>
      <div class="page-content">
        <div class="page-header">
          <h1> New Supplier  </h1>
        </div>
        <!-- /.page-header -->
        <div class="row">
          <div class="col-xs-12"> 
            <!-- PAGE CONTENT BEGINS -->
            <div class="widget-box">
              <div class="widget-body">
                <div class="widget-main"> <br>
                  <!-- Content -->
				  <form id="form_submit" method="post">
					  <div class="form-group">
						<label>Supplier  Name:<sup class="red">*</sup></label>
						  <input type="text" class="form-control" name="sup_name" required autocomplete="off"/>
						<span class="help-block"></span>
					  </div>
					  <div class="form-group">
						<label>Address:<sup class="red">*</sup></label>
						  <input type="text"  class="form-control" name="address" required autocomplete="off"/>
						<span class="help-block"></span>
					  </div>
					  <div class="form-group">
						<label>NIC/Br No:<sup class="red">*</sup></label>
						  <input type="text"  class="form-control" name="nic_no" required autocomplete="off"/>
						<span class="help-block"></span>
					  </div>
					  <div class="form-group">
						<label>Tel No (Mobile):<sup class="red">*</sup></label>
						  <input type="text"  class="form-control" name="mob_no" required autocomplete="off"/>
						<span class="help-block"></span>
					  </div>
					  <div class="form-group">
						<label>Tel No (Home):</label>
						  <input type="text"  class="form-control" name="tel_no" autocomplete="off"/>
						<span class="help-block"></span>
					  </div>
					  <div class="form-group">
						<label>Vendor Code:</label>
						  <input type="text"  class="form-control" name="vcode" autocomplete="off"/>
						<span class="help-block"></span>
					  </div>
					  <div class="form-group">
						<label>Note:</label>
						  <textarea type="text" rows=5 class="form-control" name="note" autocomplete="off"/></textarea>
						<span class="help-block"></span>
					  </div>
					  <br>
					  <div class="form-group">
						<button class="btn btn-primary btn-sm"> SUBMIT</button>
					  </div>
				  </form>
                  <!-- End Content --> 
                </div>
              </div>
            </div>
            <!-- /End .widget-box --> 
          </div>
        </div>
        <div class="space-10"></div>
        <div class="row">
          <div class="col-xs-12">
            <div class="widget-box">
              <div class="widget-body">
                <div class="widget-main">
                  <div class="table-header"> Supplier List </div>
                  <!-- div.table-responsive --> 
                  <!-- div.dataTables_borderWrap -->
                  <div class="table-responsive">
                    <table id="dynamic-table" class="table table-striped table-bordered table-hover">
                      <thead>
                        <tr>
                          <th>ID</th>
                          <th>Name</th>
                          <th>Address</th>
                          <th>NIC/Br No</th>
                          <th>Mobile No</th>
                          <th>Tel No</th>
                          <th>Edit/Remove</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php
						$sql = mysqli_query($conn, "SELECT * FROM supplier ORDER by name DESC");
						$x = 0;
						while ($row = mysqli_fetch_array($sql)) {
							$x++;
						?>
                        <tr>
                          <td><?php echo $x; ?>.</td>
                          <td><?php echo $row['name']; ?></td>
                          <td><?php echo $row['address']; ?></td>
                          <td><?php echo $row['nic']; ?></td>
                          <td><?php echo $row['mobile']; ?></td>
                          <td><?php echo $row['tp']; ?></td>
                          <td align="center"><div class="action-buttons">
						  <button title="Edit" class="btn btn-primary btn-sm btn_edit" data-toggle="modal" data-target="#edit_data" value="<?php echo $row['id']; ?>"> <i class="ace-icon fa fa-pencil"></i> </button>
						  <button title="Remove" class="btn btn-danger btn-sm btn_remove" value="<?php echo $row['id']; ?>"> <i class="ace-icon fa fa-trash-o"> </i> </button> </div>
						  </td>
                        </tr>
                        <?php } ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
            <!-- PAGE CONTENT ENDS --> 
          </div>
          <!-- /.col --> 
        </div>
        <!-- /.row --> 
      </div>
      <!-- /.page-content --> 
    </div>
  </div>
  <!-- /.main-content -->
  <?php include 'common/footer.php'; ?>
</div>
<!-- /.main-container -->

<!-- Model -->
<div id="edit_data" class="modal fade" tabindex="-1">
	<div class="modal-dialog">
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
			<h4 class="smaller lighter blue no-margin">Edit Supplier</h4>
		</div>
		  <div class="widget-body">
			<div class="widget-main" style="margin: 15px;border: 1px solid #CCC;">
			  <!-- Content -->
			  <form id="form_update" method="post">
				  <div class="form-group">
					<label>Supplier  Name:<sup class="red">*</sup></label>
					  <input type="text" class="form-control" name="sup_name_up" id="sup_name_up" required autocomplete="off"/>
					<span class="help-block"></span>
				  </div>
				  <div class="form-group">
					<label>Address:<sup class="red">*</sup></label>
					  <input type="text"  class="form-control" name="address_up" id="address_up" required autocomplete="off"/>
					<span class="help-block"></span>
				  </div>
				  <div class="form-group">
					<label>NIC/Br No:<sup class="red">*</sup></label>
					  <input type="text"  class="form-control" name="nic_no_up" id="nic_no_up" required autocomplete="off"/>
					<span class="help-block"></span>
				  </div>
				  <div class="form-group">
					<label>Tel No (Mobile):<sup class="red">*</sup></label>
					  <input type="text"  class="form-control" name="mob_no_up" id="mob_no_up" required autocomplete="off"/>
					<span class="help-block"></span>
				  </div>
				  <div class="form-group">
					<label>Tel No (Home):</label>
					  <input type="text"  class="form-control" name="tel_no_up" id="tel_no_up" autocomplete="off"/>
					<span class="help-block"></span>
				  </div>
				  <div class="form-group">
					<label>Vendor Code:</label>
					  <input type="text"  class="form-control" name="vcode_up" id="vcode_up" autocomplete="off"/>
					<span class="help-block"></span>
				  </div>
				  <div class="form-group">
					<label>Note:</label>
					  <textarea type="text" rows=5 class="form-control" name="note_up" id="note_up" autocomplete="off"/></textarea>
					<span class="help-block"></span>
				  </div>
				  <br>
				  <input type="hidden" class="form-control" name="id_up" id="id_up" readonly autocomplete="off"/>
				  <input type="hidden" class="form-control" name="acc_no_up" id="acc_no_up" readonly autocomplete="off"/>
				  <div class="form-group">
					<button class="btn btn-danger btn-sm"> UPDATE</button>
				  </div>
			  </form>
			  <!-- End Content --> 
			</div>
		  </div>
		<div class="modal-footer">
			<button class="btn btn-xs btn-default pull-right" data-dismiss="modal"> <i class="ace-icon fa fa-times"></i> Close </button>
		</div>
	</div>
	<!-- /.modal-content --> 
	</div>
	<!-- /.modal-dialog --> 
</div>
<!-- // Model -->

<!-- basic scripts --> 
<!--[if !IE]> -->
<script src="assets/js/jquery-2.1.4.min.js"></script>
<!-- <![endif]--> 
<!--[if IE]>
<script src="assets/js/jquery-1.11.3.min.js"></script>
<![endif]--> 
<script type="text/javascript">
	if('ontouchstart' in document.documentElement) document.write("<script src='assets/js/jquery.mobile.custom.min.js'>"+"<"+"/script>");
</script> 
<script src="assets/js/bootstrap.min.js"></script> 
<!-- page specific plugin scripts --> 
<!--[if lte IE 8]>
  <script src="assets/js/excanvas.min.js"></script>
<![endif]-->
<script src="assets/js/jquery.dataTables.min.js"></script>
<script src="assets/js/jquery.dataTables.bootstrap.min.js"></script>
<!-- ace scripts -->
<script src="assets/js/ace-elements.min.js"></script>
<script src="assets/js/ace.min.js"></script>
<script src="assets/jquery.validate.min.js"></script>
<link rel="stylesheet" href="bootstrap-sweetalert/lib/sweet-alert.css" />
<script src="bootstrap-sweetalert/lib/sweet-alert.min.js"></script>
<!-- inline scripts related to this page --> 
<script type="text/javascript">
jQuery(function ($) {
	//initiate dataTables plugin
	var myTable =
	$('#dynamic-table')
	//.wrap("<div class='dataTables_borderWrap' />")   //if you are applying horizontal scrolling (sScrollX)
	.DataTable({
		bAutoWidth: false,
		"aaSorting": [],
	});
});

$('document').ready(function()
{
   $("#form_submit").validate({
	 errorPlacement : function(error, element) {
		$(element).closest('.form-group').find('.help-block').html(error.html());
	 },
	 highlight : function(element) {
		$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
	 },
	 unhighlight: function(element, errorClass, validClass) {
		$(element).closest('.form-group').removeClass('has-error');
		$(element).closest('.form-group').find('.help-block').html('');
	 },
		  submitHandler: submitForm
	});
	function submitForm(){
	   $.ajax({
			  url: 'connection/supplier_conn',
			  type: 'POST',
			  data: $('#form_submit').serialize(),
			  //dataType: 'json'
		 })
		 .done(function(data){
			  if ( data === 'success' ) {
				swal({
				  title: "",
				  text: "Successfully Added!",
				  type: "success",
				  confirmButtonText: "OK"
					},
					function (isConfirm) {
				  if (isConfirm) {
					  window.location.href = "add_supplier";
				  }
				});
			  
			  } else if ( data === 'data exist' ) {
				  swal("", "Supplier Name Already Exist. Please Try Again!", "warning");
			  
			  } else if ( data === 'data exist1' ) {
				  swal("", "NIC/Br No Already Exist. Please Try Again!", "warning");
			  
			  } else {
				  swal("", "Something went wrong! Please Try Again!", "warning");
			  }
		 })
		 .fail(function(){
			  swal("", "Something went wrong! Please Try Again!", "warning");
		 });
	}
});

/******************* Edit **********************/

$(".btn_edit").click(function() {
  var id = $(this).val();
  $("#id_up").val(id);
  
  $.post( "connection/supplier_conn", { get_dataset: "data", id : id}, function( data ) {
	$("#sup_name_up").val(data.name);
	$("#nic_no_up").val(data.nic);
	$("#mob_no_up").val(data.mobile);
	$("#tel_no_up").val(data.tp);
	$("#note_up").val(data.note);
	$("#address_up").val(data.address);
	$("#vcode_up").val(data.vender_code);
	$("#acc_no_up").val(data.acc_no);
  }, "json");
  
});

$("#form_update").validate({
 errorPlacement : function(error, element) {
	$(element).closest('.form-group').find('.help-block').html(error.html());
 },
 highlight : function(element) {
	$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
 },
 unhighlight: function(element, errorClass, validClass) {
	$(element).closest('.form-group').removeClass('has-error');
	$(element).closest('.form-group').find('.help-block').html('');
 },
	  submitHandler: editForm
});
function editForm(){
   $.ajax({
		  url: 'connection/supplier_conn',
		  type: 'POST',
		  data: $('#form_update').serialize(),
		  //dataType: 'json'
	 })
	 .done(function(data){
		  if ( data === 'success' ) {
			swal({
			  title: "",
			  text: "Successfully Updated!",
			  type: "success",
			  confirmButtonText: "OK"
				},
				function (isConfirm) {
			  if (isConfirm) {
				  window.location.href = "add_supplier";
			  }
			});
		  
		  } else if ( data === 'data exist' ) {
			  swal("", "Supplier Name Already Exist. Please Try Again!", "warning");
		  
		  } else if ( data === 'data exist1' ) {
			  swal("", "NIC/Br No Already Exist. Please Try Again!", "warning");
		  
		  } else {
			  swal("", "Something went wrong! Please Try Again!", "warning");
		  }
	 })
	 .fail(function(){
		  swal("", "Something went wrong! Please Try Again!", "warning");
	 });
}

/******************* Remove **********************/

$(".btn_remove").click(function(){
  var row_id = $(this).val();
	swal({
	  title: "",
	  text: "Are you sure you want to delete this?",
	  type: "warning",
	  showCancelButton: true,
	  confirmButtonClass: "btn-danger",
	  confirmButtonText: "Yes, delete it!",
	  cancelButtonText: "No, cancel!",
	  closeOnConfirm: false,
	  closeOnCancel: true
	},
	function(isConfirm) {
	  if (isConfirm) {
		$.post( "connection/supplier_conn", {remove_data: "data", id : row_id}, function( data ) {
			if (data.msgType === 1) {
			  swal({
				title: "",
				text: "Successfully Deleted!",
				type: "success",
				confirmButtonText: "OK"
				  },
				  function (isConfirm) {
				if (isConfirm) {
					window.location.href = "add_supplier";
				}
			  });
			
			} else if (data.msgType === 3) {
			  swal("", "Data Already Exist!", "warning");
			
			} else{
			  swal("", "Something went wrong! Please Try Again!", "warning");
			}
		}, "json");
	  }
	});
});
</script>
</body>
</html>