<?php
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'emp_add') ) {
    echo '<script>window.location.href="index.php?page=no_permission.php"</script>';die();
  }
  if (isset($_REQUEST['eid']) && !empty($_REQUEST['eid']) && $_REQUEST['eid']!=0){
    $select_update      = "SELECT * FROM emp_mst WHERE id='".$_REQUEST['eid']."'";
    $select_update_res  = mysqli_query($conn,$select_update);
    $update_row         = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);

    $emp_code           = $update_row['code'];
    $name               = $update_row['name'];
    $mobile             = $update_row['mobile'];
    $email              = $update_row['email'];
    $b_group            = $update_row['b_group'];
    $e_contact          = $update_row['e_contact'];
    $address            = $update_row['address'];
    $file_img           = $update_row['file_img'];
    $status             = $update_row['status'];
    $id                 = $_GET['eid'];
  }

  if($_GET['eid'] == ''){
    $code = employeeCode($conn);
  }else{
    $code = $emp_code;
  }
  
?>
<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"><?php echo ($id == 0) ? 'ADD NEW EMPLOYEE' : 'UPDATE EMPLOYEE'; ?></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'], 'vendor_list') ) { ?>
          <a href="index.php?page=emp_mst/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW EMPLOYEE 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 action="emp_mst/insert.php" method="post" enctype="multipart/form-data" class="common-frm-validation" id="signupForm">
                <div class="row">
                  <div class="col-md-1">
                    <div class="form-group">
                      <label class="capital">CODE</label><span style="color: red">*</span>
                      <input type="text" size="35" id="code" name="code" class="form-control required" autocomplete="off" readonly="" value="<?php echo $code;?>" />
                    </div>
                  </div>
                  <div class="col-md-4">
                    <div class="form-group">
                      <label class="capital">EMPLOYEE name</label><span style="color: red">*</span>
                      <input type="text" size="35" id="name" name="name" class="form-control required" placeholder="EMPLOYEE NAME" autocomplete="off" onChange="CheckName(this.value);" autofocus="" value="<?php echo $name;?>" />
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">Mobile</label><span style="color: red">*</span>
                        <input type="text" name="mobile" id="mobile" class="form-control required number" placeholder="MOBILE" value="<?php echo $mobile;?>"/>
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">Emergency Mobile</label>
                        <input type="text" name="e_contact" id="e_contact" class="form-control number" placeholder="EMERGENCY MOBILE" value="<?php echo $e_contact;?>"/>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">EMAIL</label><span style="color: red">*</span>
                        <input type="text" name="email" id="email" class="form-control required" placeholder="EMAIL" value="<?php echo $email;?>"/>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">address</label><span style="color: red">*</span>
                      <textarea class="form-control required" name="address" id="address" style="height: 35px !important;"><?php echo $address;?></textarea> 
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">BLOOD GROUP</label><span style="color: red">*</span>
                        <select class="form-control select2 required" name="b_group" id="b_group" autofocus="">
                        <option></option>
                        <?php foreach ($blood_group as $blood_group) { 
                        
                          echo '<option value="'. $blood_group['title'] .'" '. ($blood_group['title'] == $b_group ? 'selected' : '') .'>' . $blood_group['title'] . '</option>';

                         } ?>
                       </select>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">IMAGE</label>
                      <input type="file" size="35" id="image" name="image" autocomplete="off" class="image" /><br/>
                      <?php
                      if($file_img !=''){ ?>
                        <a href="assets/upload/employee/<?php echo $file_img?>" class="btn btn-info" data-toggle="tooltip" data-placement="bottom" title="VIEW IMAGE...!!" target="_blank"><em class="fa fa-file-image-o"></em></a>
                      <?php } ?>
                    </div>
                  </div>
                  <div class="col-md-4">
                    <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" <?php if($status=='0'){echo "checked";}?> checked=""> <span class="label-text">ACTIVE</span>
                      </label>
                      <label class="radio-inline fontradiobuttion">
                      <input type="radio" name="status" value="1" <?php if($status=='1'){echo "checked";}?>> <span class="label-text">IN-ACTIVE</span>
                      </label>
                    </div>
                  </div>
                </div>
                <input type="hidden" value="<?php echo $id;?>" name="id" id="id">
                <hr>
                <div class="row" align="center">
                  <button id="btn" class="btn btn-primary ajax-submit capital" type="submit"> <?php echo ($id == 0) ? 'Save & List' : 'Update & save'; ?></button>
                  <a href="index.php?page=emp_mst/list.php" class="btn btn-danger capital"> Cancel</a>
                </div>
              </form>
            </div>
         </div>
      </div>
   </div>
</section>
<script type="text/javascript">
  $(document).ready(function(){
    $('#b_group').select2();
  });
  function CheckName() {
    var name      = $("#name").val();
    $.ajax({
      'type': 'POST',
      'url': 'emp_mst/check_name.php',
      'data': {
        'name': name
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 1) {
          jQuery('.submit').attr('disabled', true);
          swal("Opps !!", "SAME NAME DETECT...!!!", "info")
          setTimeout("location.reload(true);",800); 
        }else{
          jQuery('.submit').attr('enable', true);
        }
      }
    });
  };
</script>