<?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-12 grid-margin stretch-card">
              <div class="card">
                	<div class="card-body">
					<div class="form-group text-left" style="color:#a4dcec;">
							<h5></h5>
							<?php 
                        if(!empty($edit_data['name'])) {
                            ?>
                            <h5>EDIT USER</h5>
                            <?php 
                        }
                        else{

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

                        </div>
                  	<form class="forms-sample text-left" id="add_user_form">                    	
                    	<div class="row">
                    		<div class="col-md-6 col-lg-6"  >
                    				<input type="hidden" name="old_password" value="<?php echo @$edit_data['password']; ?>">
                    				<input type="hidden" name="user_id" value="<?php echo @$edit_data['user_id']; ?>">

                    				<div class="form-group">
                    			  		<label for="qr_string">Name</label>
                    			  		<input type="text" value="<?php echo @$edit_data['name']; ?>" class="form-control" name="name" id="name" placeholder="Name">
                    				</div>

                    				<div class="form-group">
                    			  		<label for="qr_string">Email</label>
                    			  		<input type="text" value="<?php echo @$edit_data['email']; ?>" class="form-control" name="email" id="email" placeholder="Email">
                    				</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_data['status'] == '1') { ?> selected <?php }
                    				    	?>
                    				    	>Active</option>
                    				    	<option value="0" 
                    				    	<?php 
                    				    	if(@$edit_data['status'] == '0') { ?> selected <?php }
                    				    	?>>Suspended</option>

                    				    	<option value="2"                     				    	
                    				    	<?php 
                    				    	if(@$edit_data['status'] == '2') { ?> selected <?php }
                    				    	?>>Not Registered</option>
                    				  </select>
                    				</div>
                    		</div>

            		    	<div class="col-md-6 col-lg-6">                    		
            					<div class="form-group">
            				  		<label for="qr_string">Password</label>
            				  		<input type="password" value="" class="form-control" name="password" id="password" placeholder="Password">
            					</div>

            					<div class="form-group">
            				  		<label for="qr_string">Mobile Number</label>
            				  		<input type="text" value="<?php echo @$edit_data['mobile_number']; ?>" class="form-control" name="mobile_number" maxlength="10" id="mobile_number" placeholder="Mobile Number">
            					</div>

            					<div class="form-group">
            					  <label for="exampleFormControlSelect2">Verified</label>
            					  	<select class="form-control" name="is_verify" id="is_verify" id="exampleFormControlSelect2">
            					    	<option value="1" 
            					    	<?php 
            					    	if(@$edit_data['is_verify'] == '1') { ?> selected <?php }
            					    	?>
            					    	>Yes</option>
            					    	<option value="0" 
            					    	<?php 
            					    	if(@$edit_data['is_verify'] == '0') { ?> selected <?php }
            					    	?>>No</option>
            					  </select>
            					</div>
            		    	</div>
                    	</div>
                    	<input type="submit" class="btn btn-theme-black mr-2" id="submit" value="<?php echo @$button; ?>"></input>
                  	</form>
                </div>
              </div>
            </div>                        
      </div>
    </div>
<?php $this->load->view('admin/footer'); ?>
<script type="text/javascript">

	/* Form Validate  */		
	$("#add_user_form").validate({
		rules: {
			name: {
			    required: true,
			},
			<?php 
			if(empty(@$edit_data['user_id'])) {
				?>
					password: {
					    required: true,
					},
				<?php 
			}
			?>
			email: {
			    required: true,
			    email:true
			},
			mobile_number: {
			    required: true,
			    number:true,
			    maxlength:10,
			},			
	    },
	    messages: {
	        name: {
			    required: "Name required !",
			},
			password: {
			    required: "Password required !",
			},
			email: {
			    required: "Email required !",
			    email: "Please enter valid email required !",
			},
			mobile_number: {
			    required: "Mobile number required !",
			    number:"Enter valid mobile number !",
			    maxlength:"Enter valid mobile number !",
			},
	    },
	    errorElement : 'span',
	    errorClass: 'validate-error-custom',
	});

	$("#submit").on("click",function(){
		event.preventDefault();
	    let myForm = document.getElementById('add_user_form');
        let formData = new FormData(myForm);

		if($("#add_user_form").valid()) {
			$.ajax({
			 	url:"<?php echo base_url('admin/user/user/add_user'); ?>",
			 	type:'POST',
				data:formData,
				cache:false,
	            contentType: false,
	            processData: false,
			 	success:function(response) {
			        var response = JSON.parse(response);
			        if(response.result == 'failure'){
						toastr.error(response.message);			        	
			        } else {
			        	document.getElementById("add_user_form").reset();
						toastr.success(response.message);	
						const myTimeout = setTimeout(redirect, 1000);
			        }
			 	}
			});
		}
	});		

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

</script>