<script>
  $().ready(function() {
    $("#signupForm").validate({
      rules: {
          name: "required",
          username: {
             required: true,
             minlength: 2
          },
          password: {
             required: true,
             minlength: 5
          },
          confirm_password: {
             required: true,
             minlength: 5,
             equalTo: "#password"
          },
          role: "required",
          branch_id: "required",
          status: "required"
       },
    });
  });
  function showMessage(status, client_id){
    if(status == 1){
        $title      = 'Great !!';
        $message    = 'USER ADDED SUCCESSFUL';
        $type       = 'success';
    } else {
        $title      = 'Opps...';
        $message    = 'Error in adding user. Please try again';
        $type       = 'error';
    }

    swal({
        title: $title,
        text: $message,
        type: $type,
        showCancelButton: false,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "OK"
    },
    function(){
        if(status == 1){
            window.location.href = http_host + '?page=user_management/grid.php';
        } else {
            window.location.href = http_host + '?page=user_management/grid.php';    
        }
    });
  }
</script>
<?php 
  $status = '';
  $msg = '';
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'user_add') ) {
      echo '<script>window.location.href="index.php?page=no_permission.php"</script>';
      die();
  }
  // ROLE 
  $role = "SELECT * FROM `roles`";
  $role_result = mysqli_query($conn,$role);

  $bra_mst = "SELECT * FROM `branch_mst` WHERE `status` = '0'";
  $bra_res = mysqli_query($conn, $bra_mst);

  if(isset($_POST['insert'])){
    $name               = $_REQUEST['name'];
    $email              = $_REQUEST['email'];
    $mobile             = $_REQUEST['mobile'];
    $username           = $_REQUEST['username'];
    $password           = md5($_REQUEST['password']);
    $confirm_password   = md5($_REQUEST['confirm_password']);
    $branch_id          = mysqli_real_escape_string($conn, implode(",", $_POST['branch_id']));
    $status             = $_REQUEST['status'];
    $login_type         = $_REQUEST['login_type'];
    $role_id            = $_REQUEST['role_id'];;
    $created_datetime   = date('Y-m-d H:i:s');
    $created_id         = $_SESSION['user_detail']['id'];
    $updated_datetime   = date('Y-m-d H:i:s');
    $updated_id         = $_SESSION['user_detail']['id'];

    $user_query = "INSERT INTO `users`(`name`, `email`, `mobile`, `username`, `password`, `role_id`, `branch_id`, `status`, `company_id`, `login_type`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$name."','".$email."','".$mobile."','".$username."','".$password."','".$role_id."','".$branch_id."','".$status."','".$company_id."','".$login_type."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";

    $user_result = mysqli_query($conn,$user_query) or die( mysqli_error($conn) );
      
    if($user_result){ 
      echo "<script type='text/javascript'> showMessage(1, 1); </script>";
    } else {
        echo "<script type='text/javascript'> showMessage(0, 0); </script>";
    }
  }

?>
<section class="content-header">
  <div class="row">
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 bottom20">
        <h3 class="capital font15 padding5 bold">ADD NEW USER DETAILS</h3>
      </div>
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 text-right bottom20">
        <h3>
          <?php if( hasPermission($_SESSION['user_detail']['role_id'], 'user_list') ) { ?>
          <a href="index.php?page=user_management/grid.php" class="btn btn-danger capital bottom10 custome-btn">VIEW USER DETAILS</a>
          <?php } ?>
        </h3>
      </div>
  </div>
</section>
<hr class="bottom35"> 
<section class="hitz-top-header">
   <div class="row bottom40">
      <div class="col-xs-12">
         <div class="box box-success">
            <div class="box-body">
              <form class="" id="signupForm" method="post">
                <legend style="font-weight: bold;font-size: 12px !important;">BASIC INFORMATION</legend>
                <div class="row">
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">Role</label><span style="color: red">*</span><br>
                      <select class="form-control" name="role_id" id="role_id">
                        <option></option>
                        <?php while ($role_rows = mysqli_fetch_array($role_result,MYSQLI_BOTH)) { ?>
                          <option value="<?php echo $role_rows['id'];?>"><?php echo $role_rows['name'];?></option>
                        <?php } ?>
                      </select>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">Name</label><span style="color: red">*</span>
                      <input type="text" size="35" id="name" name="name" class="form-control" placeholder="NAME" autocomplete="off" />
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">Phone Number</label>
                      <input class="form-control" placeholder="PHONE NUMBER" name="mobile" type="text" id="quantity" maxlength="11" autocomplete="off">
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">E-mail Id</label>
                      <input class="form-control" name="email" placeholder="E-MAIL ID" type="text" autocomplete="off">
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">BRANCH</label><span style="color: red">*</span>
                      <select id="branch_id" name="branch_id[]" class="form-control" required="" style="width: 100% !important;" multiple="">
                       <option></option>
                       <?php while ($bra_row = mysqli_fetch_array($bra_res)){ ?>
                       <option value="<?php echo $bra_row['id']; ?>"><?php echo $bra_row['branch']; ?></option>
                       <?php } ?>
                      </select>
                    </div>
                  </div>
                </div>
                <legend style="font-weight: bold;font-size: 12px !important;">LOGIN CREDENTIALS</legend>
                <div class="row">
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">Username</label><span style="color: red">*</span>
                      <input type="text" size="35" id="username" name="username" class="form-control" placeholder="USERNAME" autocomplete="off" onChange="CheckUname(this.value);" />
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">Password</label><span style="color: red">*</span>
                      <input type="password" size="35" id="password" class="form-control" placeholder="PASSWORD" name="password" autocomplete="off">
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">Re-Type Password</label><span style="color: red">*</span>
                      <input type="password" size="35" id="confirm_password" class="form-control" placeholder="RE-TYPE PASSWORD" name="confirm_password" autocomplete="off">
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">Status</label><span style="color: red">*</span><br>
                      <label class="radio-inline fontradiobuttion">
                      <input type="radio" name="status" value="0" checked> <span class="label-text">ACTIVE</span>
                      </label>
                      <label class="radio-inline fontradiobuttion">
                      <input type="radio" name="status" value="1"> <span class="label-text">IN-ACTIVE</span>
                      </label>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">LOGIN Type</label><span style="color: red">*</span><br>
                      <label class="radio-inline fontradiobuttion">
                      <input type="radio" name="login_type" value="S" checked> <span class="label-text">SINGLE</span>
                      </label>
                      <label class="radio-inline fontradiobuttion">
                      <input type="radio" name="login_type" value="M"> <span class="label-text">MULTIPLE</span>
                      </label>
                    </div>
                  </div>
                </div>
                <input type="hidden" value="" name="id" id="id">
                <hr>
                <div class="row" align="center">
                  <button name="insert" class="btn btn-primary submit capital" type="submit"> Save & List</button>
                  <a href="index.php?page=user_management/grid.php" class="btn btn-danger capital"> Cancel</a>
                </div>
              </form>
              
            </div>
         </div>
      </div>
   </div>
</section>
<script type="text/javascript">
  $(document).ready(function() {
    $('input').iCheck({
        checkboxClass: 'icheckbox_flat',
        radioClass: 'iradio_flat'
    });
    $('#role_id').select2();
    $('#branch_id').select2();
    $("#checkAll").click(function() {
        $('input:checkbox').not(this).prop('checked', this.checked);
    });
  });
  function CheckUname() {
    var username      = $("#username").val();
    $.ajax({
      'type': 'POST',
      'url': 'user_management/check_uname.php',
      'data': {
        'username': username
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 1) {
          $('#username').val('');
          swal("Opps !!", "ALREADY CODE EXIST...!!!", "info")  
        }else{
          jQuery('.submit').attr('enable', true);
        }
      }
    });
  };
</script>