<?php $this->load->view('admin/header'); ?>
<?php $this->load->view('admin/sidebar'); ?>
    
  <div class="main-panel">

    <div class="content-wrapper">
      <div class="row">            
        <div class="col-md-6 grid-margin stretch-card">
              <div class="card">
                	<div class="card-body">
					<div class="form-group" style="color:#a4dcec;">
					<?php 
                        if(!empty($edit_lock['qr_string'])) {
                            ?>
                            <h5>EDIT LOCK</h5>
                            <?php 
                        }
                        else{

                        
                        ?>
							<h5>ADD LOCK</h5>
                            <?php    
                        }
                            ?>

                        </div>
                  	<form class="forms-sample text-left" id="insert_qr">
                    	<div class="form-group">
                      		<label for="qr_string">QR String</label>
                      		<input type="text" value="<?php echo @$edit_lock['qr_string']; ?>" class="form-control" name="qr_string" id="qr_string" placeholder="QR String">
                    	</div>
                    	<div class="form-group">
                    	  <label for="exampleFormControlSelect2">Status</label>
                    	  	<select class="form-control" name="status" id="status" id="exampleFormControlSelect2">
                    	    	<option value="1" 
                    	    	<?php 
                    	    	if(@$edit_lock['status'] == '1') { ?> selected <?php }
                    	    	?>
                    	    	>Active</option>
                    	    	<option value="0" 
                    	    	<?php 
                    	    	if(@$edit_lock['status'] == '0') { ?> selected <?php }
                    	    	?>>Suspended</option>
                    	  </select>
                    	</div>
                    	<button type="submit" class="btn btn-theme-black mr-2" id="submit">Submit</button>
                  	</form>
                </div>
              </div>
            </div>                        
      </div>
    </div>
<?php $this->load->view('admin/footer'); ?>
<script type="text/javascript">

	/* Form Validate  */		
	// $("#insert_qr").validate({
	// 	rules: {
	// 		qr_string: {
	// 		    required: true,
	// 		},			
	//     },
	//     messages: {
	//         qr_string: {
	// 		    required: "QR string required.",
	// 		},			
	//     },
	//     errorElement : 'span',
	//     errorClass: 'validate-error-custom',
	// });

	$("#submit").on("click",function(){
		event.preventDefault();
		if($("#qr_string").val()) {
			$.ajax({
			 	url:"<?php echo base_url('admin/lock/lock/add_lock'); ?>",
			 	type:'POST',
				data:{qr_string:$("#qr_string").val(),status:$("#status").val(),edit_id:"<?php echo @$edit_lock['qr_id']; ?>" },
			 	success:function(response) {
			        var response = JSON.parse(response);
			        if(response.result == 'failure'){
						toastr.error(response.message);			        	
			        } else {
			        	document.getElementById("insert_qr").reset();
						toastr.success(response.message);	
						const myTimeout = setTimeout(redirect, 1000);
			        }
			 	}
			});
		} else {
			toastr.error('QR string required.');				
		}
	});		

	function redirect() {
	  	window.location.href = "<?php echo base_url('admin/lock/manage');?>";
	}

</script>