<?php require_once('../auth/auth.php'); ?>
<?php
if(isset($_GET['getuid'])){
  $getuid= $_GET['getuid'];
  $Getuser_query= mysqli_query($conn,"SELECT `uid`,`us_coid`, `us_bid`, `us_brid`, `us_em_id`, `us_ugid`, `username`, `password`, `us_sid`, `us_date`, `us_language`, `us_status`,branch.branch_name,business.bid_name,em_employee.em_acname,user_g.group_name  FROM user 
   INNER JOIN business ON (user.us_bid = business.bid)
   INNER JOIN branch ON (user.us_brid = branch.brid)
   INNER JOIN em_employee ON (user.us_em_id = em_employee.em_id)
   INNER JOIN user_g ON (user.us_ugid = user_g.ugid)
   WHERE  user.uid='$getuid' LIMIT 1 ") or die (mysqli_error($conn));

  $rowuid= mysqli_fetch_assoc($Getuser_query);
  $us_bid=$rowuid['us_bid'];
  $us_brid=$rowuid['us_brid'];
  $us_em_id=$rowuid['us_em_id'];
  $g_per_id=$rowuid['us_ugid'];
  $username=$rowuid['username'];
  $password=$rowuid['password'];
   }
   ?>
<section class="content">
    <div class="container-fluid">
      <div class="row">
         <div class="col-md-2"> </div>
         <div class="col-md-8">
            <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
               <div class="card-header" style="padding: 5px 8px;">
                  <h3 class="card-title"><?php echo language(243); ?></h3>
                  <div class="card-tools"> <button type="button" class="btn btn-tool" data-card-widget="remove"> <i class="fas fa-times"></i>  </button> </div>
               </div>
               <form id="user_form" action="#" method="post" autocomplete="off">
                  <?php if(isset($_GET['getuid'])){  ?>
                  <input type="hidden" name="newuser" id="newuser" value="<?php echo @$getuid ;?>"/>
                  <?php }else{?>
                  <input type="hidden" name="newuser" id="newuser" value="0"/>
                  <?php } ?>
                  <div class="card-body">
                     <div class="col-md-12">
                        <div class="row">
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label> <?php echo language(231); ?>   <b class="text-danger"> *</b></label>
                                 <select name="busid" id="bbid" class="form-control" >
                                    <option value="<?php echo @$us_bid ;?>"> <?php echo @$rowuid['bid_name'] ;?></option>
                                    <?php 
                                       $business_query= mysqli_query($conn,"SELECT * FROM business  WHERE coid='$coid'   ") or die (mysqli_error($conn));
                                       while($rob= mysqli_fetch_assoc($business_query)){
                                           $getsetbrid=$rob['bid'];
                                        echo '<option value='.$rob["bid"].'>'.$rob["bid_name"]."-".$rot["bid_gsm"].' </option>';
                                       }?> 
                                 </select>
                              </div>
                           </div>
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label>  <?php echo language(232); ?> <b class="text-danger"> *</b></label>
                                 <select  name="bra_id" id="viewid"  class="form-control" >
                                     <option value="<?php echo @$us_brid ;?>"> <?php echo @$rowuid['branch_name'] ;?></option>
                                 </select>
                              </div>
                           </div>
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label><?php echo language(239); ?><b class="text-danger"> *</b></label>
                                 <select  name="em_id"  class="form-control" >
                                    <option value="<?php echo @$us_em_id ;?>"> <?php echo @$rowuid['em_acname'] ;?></option>
                                    <?php 
                                      $emdd_query= mysqli_query($conn,"SELECT *,em_designation.desig_name FROM em_employee INNER JOIN em_designation ON (em_employee.em_dis_id=em_designation.em_desid)  WHERE em_coid='$coid'  ") or die (mysqli_error($conn));
                                      while($roem= mysqli_fetch_assoc($emdd_query)){
                                        echo '<option value='.$roem["em_id"].'>'.$roem["em_acname"].' ('.$roem["desig_name"].')</option>';
                                      }
                                       ?> 
                                 </select>
                              </div>
                           </div>
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label><?php echo language(240); ?>  <b class="text-danger"> *</b></label>
                                 <select name="g_per_id"  class="form-control" >
                                    <option value="<?php echo @$g_per_id ;?>"> <?php echo @$rowuid['group_name'] ;?></option>
                                    <?php 
                                       $em_Groupquery= mysqli_query($conn,"SELECT * FROM user_g  WHERE coid='$coid' AND group_name!='Administrator'  ") or die (mysqli_error($conn));
                                       while($row_user_g= mysqli_fetch_assoc($em_Groupquery)){
                                        echo '<option value='.$row_user_g["ugid"].'>'.$row_user_g["group_name"].' </option>';
                                       }
                                       ?> 
                                 </select>
                              </div>
                           </div>
                             <div class="col-md-4">
                              <div class="form-group">
                                 <label><?php echo language(241); ?>  <span id="user-availability-status"></span> <b class="text-danger"> *</b></label>
                                 <input type="text" name="username"  id="username"  onBlur="checkeuserAvailability()" value="<?php echo @$username ?>" class="form-control form-control-sm inputselect" style="text-align:center;" required="true">
                              </div>
                           </div>
                           <div class="col-md-4">
                              <div class="form-group">
                                 <label><?php echo language(242); ?>  <b class="text-danger"> *</b></label>
                                 <input type="text" name="password"  value="<?php echo @$password ?>" class="form-control form-control-sm inputselect" style="text-align:center;" required="true">
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
                  <center>
                     <div class="card-footer" style="padding: 8px 5px;">
                        <button type="submit" id="save" class="btn btn-success btn-xs"><?php if (@$_GET['getuid']) {echo language(25);}else{echo language(23);}?></button>
                        <button type="reset" class="btn btn-secondary btn-xs"><?php echo language(24);?></button>
                     </div>
                  </center>
               </form>
               <div class="card-body p-1">
                  <tr>
                     <th rowspan="3"  class="text-center" >
                        <input type="text" id="search" class="form-control form-control-sm search"  placeholder="searching" required="true" style="border-radius: ;border:1px solid primary;width:100%; text-align:center; padding:; ">
                     </th>
                  </tr>
						<table class="table table-sm table-head-fixed text-nowrap table-hover table-bordered " style="overflow-x:auto;">
                     <thead>
                        <tr class="sticky-top">
                           <th class="text-center" style="width:5%;"><?php echo language(15);?></th>
                           <th class="text-center"><?php echo language(26); ?></th>
                           <th class="text-center"><?php echo language(244); ?></th>
                           <th class="text-center"><?php echo language(245); ?></th>
                           <th class="text-center"><?php echo language(238); ?></th>
                           <th class="text-center"><?php echo language(241);?> </th>
                           <th class="text-center"><?php echo language(242);?></th>
                           <th class="text-center"><?php echo language(131);?></th>
                           <th class="text-center"><?php echo language(90);?>  </th>
                        </tr>
                     </thead>
                     <tbody id="colview">
                        <?php
                           $user_query= mysqli_query($conn,"SELECT `uid`,`us_coid`, `us_bid`, `us_brid`, `us_em_id`, `us_ugid`, `username`, `password`, `us_sid`, `us_date`, `us_language`, `us_status`,branch.branch_name,business.bid_name,em_employee.em_acname,user_g.group_name  FROM user 
                             INNER JOIN business ON (user.us_bid = business.bid)
                             INNER JOIN branch ON (user.us_brid = branch.brid)
                             INNER JOIN em_employee ON (user.us_em_id = em_employee.em_id)
                             INNER JOIN user_g ON (user.us_ugid = user_g.ugid)
                           
                             WHERE  user.us_coid='$coid'") or die (mysqli_error($conn));
                           $i=0;
                           while ($rowus= mysqli_fetch_array ($user_query) ){
                            $branchid  =$rowus['us_bid'];
                            $us_status  =$rowus['us_status'];
                           
                           
                            ?>
                        <tr class="btn-outline-dark tr-hover delete_op<?php echo $rowus['uid'];?>"  onclick="pagecall('../hr_manage/user_add.php?getuid='+'<?php echo $rowus['uid'];?>','#hrhome')">
                           <td class="text-center"><?php echo ++$i ?></td>
                           <td class="text-center"> <?php echo $rowus['bid_name'] ;?> </td>
                           <td class="text-center"> <?php echo $rowus['branch_name'] ;?> </td>
                           <td class="text-center"> <?php echo $rowus['em_acname'] ;?> </td>
                           <td class="text-center"> <?php echo $rowus['group_name'] ;?> </td>
                           <td class="text-center"><?php echo $rowus['username'] ;?>  </td>
                           <td class="text-center"><?php echo $rowus['password'];?></td>
                           <td class="text-center"><?php echo $rowus['us_date'];?></td>
                           <td class="text-center"> <?php if ($us_status ==1) { echo "Active";}else if($us_status==0){echo "Deactive"; }?></td>
                        </tr>
                        <?php } ?>
                     </tbody>
                  </table>
               </div>
            </div>
         </div>
         <div class="col-md-2"> </div>
      </div>
    </div>
 </section>
<script>
   $("select").select2();
   $(document).ready(function() {
   $('#user_form').on('submit', function(e){
     $('#save').prop('disabled', true);
     $('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...');
     e.preventDefault();  
     $.post("../hr_manage/user_db.php",$(this).serialize(),function(data){
      $('#user_form')[0].reset();
      $('#save').prop('disabled', false);
      $('#save').html(' Save');
      $.post('../hr_manage/user_add.php?running='+1,{},function(res){
       $('#hrhome').html(res);
       toastr.success(Showing, '<?php echo "Data Load Succefully " ?>');
     }); 
    });
   });
   
   
   
   $('.search').on('keyup',function(){
    var searchTerm = $(this).val().toLowerCase();
    $('.searchtable tr').each(function(){
      var lineStr = $(this).text().toLowerCase();
      if(lineStr.indexOf(searchTerm) === -1){
        $(this).hide();
      }else{
        $(this).show();
      }
    });
   });
   
   $('th').each(function (col) {
    $(this).hover(
      function () {
        $(this).addClass('focus');
      },
      function () {
        $(this).removeClass('focus');
      }
      );
    $(this).click(function () {
      if ($(this).is('.asc')) {
        $(this).removeClass('asc');
        $(this).addClass('desc selected');
        sortOrder = -1;
      } else {
        $(this).addClass('asc selected');
        $(this).removeClass('desc');
        sortOrder = 1;
      }
      $(this).siblings().removeClass('asc selected');
      $(this).siblings().removeClass('desc selected');
      var arrData = $('table').find('tbody >tr:has(td)').get();
      arrData.sort(function (a, b) {
        var val1 = $(a).children('td').eq(col).text().toUpperCase();
        var val2 = $(b).children('td').eq(col).text().toUpperCase();
        if ($.isNumeric(val1) && $.isNumeric(val2))
          return sortOrder == 1 ? val1 - val2 : val2 - val1;
        else
          return (val1 < val2) ? -sortOrder : (val1 > val2) ? sortOrder : 0;
      });
      $.each(arrData, function (index, row) {
        $('tbody').append(row);
      });
    });
   });
   });
</script>
<script>
   $('select').select2();
   $(document).ready(function() {
     $('#bbid').change(function(e) { 
      var bbid = $(this).val();
       $.post("../customer/branch_show.php",{bbid:bbid},function(data){
         $('#viewid').html(data);
       });
   });
   });  
</script>
<script type="text/javascript">
   function checkeuserAvailability() {
      jQuery.ajax({
         url: "../duplicate_check/number_check.php",
         data:'username='+$("#username").val(),
         type: "POST",
         success:function(data){
         $("#user-availability-status").html(data);
         },
         error:function (){}
      });
   }
</script>