<?php $this->load->view('admin/common/header')?><link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />

<div class="dashborder-content">

<div class="container-fluid">
<div class="row">

<?php $this->load->view('admin/common/sidebar')?>

<div class="col-md-10">


<div class="row">

<div class="col-md-12">
<div class="panel panel-bd lobidrag lobipanel lobipanel-sortable" data-inner-id="6lbDiPQUBj" data-index="0">
<div class="panel-heading ui-sortable-handle">
<div class="panel-title" style="max-width: calc(100% - 180px);">

<?php  if($this->session->flashdata('err')!=''){?>
	<div class="alert alert-danger alert-dismissible">
  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
   <?= $this->session->flashdata('err');?>
</div>
<?php }if($this->session->flashdata('msg')!=''){ ?>
	<div class="alert alert-success alert-dismissible">
  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
   <?= $this->session->flashdata('msg');?>
</div>
<?php }?>
<div class="row">

<div class="col-md-6" style="padding-top: 4px;"><h4> Sub Admin</h4></div>
<div class="col-md-6" style="text-align:right;">
<form class="form-inline">


<div class="input-group" style="width:100% !important;">

<input type="text" class="form-control" placeholder="Search Here" style="font-size:12px;">
<div class="input-group-addon btn"><i class="fa fa-search"></i>
</div>
</div>


</form>

</div>

</div>
</div>
</div>
<div class="panel-body">
 <p id="error_panel">
	<?php print_r(validation_errors());?>
</p>
    <form action="<?= base_url('admin/subadmin')?>" method="post">
        <div class="form-group">
          
          <div class="row">
            <div class="col-md-6">
                <label for="name">Name:</label>
              <input type="text" class="form-control" value="<?= set_value('name')?>" name="name" id="name" placeholder="Name" required><br></div>
            <div class="col-md-6">
                <label for="NO. of Position">Email:</label>
              <input type="text" class="form-control" value="<?= set_value('email')?>" name="email" id="email" placeholder="Email"required><br></div>
          </div>
          <div class="row">
              <div class="col-md-6">
                  <label for="name">Password:</label>
                <input type="password" class="form-control" value="<?= set_value('password')?>" name="password" id="name" placeholder="Password" required><br></div>
              <div class="col-md-6">
                  <label for="NO. of Position">Phone:</label>
                <input type="text" class="form-control" id="email" value="<?= set_value('phone')?>" name="phone" placeholder="Phone:"required><br></div>
                <input type="hidden" name="role" value="2">
            </div>						 <div class="row">              <div class="col-md-8">                  <label for="name">Permission:</label>              <select class="js-example-basic-multiple" name="permission[]" multiple="multiple" required>			   <?php $data=$this->db->or_where('role',3)->or_where('role',4)->get('user')->result_array();			     foreach($data as $d){			   ?>			   <option value="<?= $d['id']?>"><?= $d['name']?></option>				 <?php }?>			   </select>                                        </div>
            </div>
          <br>
          
        <button type="submit" class="btn btn-md button2">Create Subadmin</button>
      </form> <br><br>
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>S.no.</th>
<th>Name</th>
<th>UserID</th>
<th>Email</th>
<th>Password</th>
<th>Phone</th><th>Permissions</th>
<th></th>
</tr>
</thead>

  <?php $i=1; foreach($user as $u){?>
<!-- <form action="<?= base_url('admin/subadmin_update')?>" method="post" name="form<?= $u['id']?>"> -->
  
<tr>
  
<td><?= $i; ?><input type="hidden" name="id" value="<?= $u['id']?>"></td>
<td ondblclick="editfield(this,'name',<?= $u['id']?>)"><?= $u['name']?></td>
<td ondblclick="editfield(this,'user_id',<?= $u['id']?>)"><?= $u['user_id']?></td>
<td ondblclick="editfield(this,'email',<?= $u['id']?>)"><?= $u['email']?></td>
<td ondblclick="editfield(this,'password',<?= $u['id']?>)"><?= $u['confirm_password']?></td>
<td ondblclick="editfield(this,'phone',<?= $u['id']?>)"><?= $u['phone']?></td><?php  $array=explode(',',$u['permission']);   $data=$this->db->where_in('id',$array)->get('user')->result_array();   $singleArray=[];   foreach ($data as $key => $value){        $singleArray[$key] = $value['name'];    }	$data1=implode(',',$singleArray); ?><td><?= $data1?></td>
<td id="update_but<?= $u['id']?>" style="display: none"><button type="button" class="btn btn-success" onclick="get_text(this)"  >Update</button></td>

</tr>
<!-- </form> -->
<?php $i++; }?>

</table>
<?= $links; ?>
</div>
</div>
</div>
</div>



</div>


</div>






</div>
</div>

</div>





<?php $this->load->view('admin/common/footer'); ?><script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<script type="text/javascript">$=jQuery;$(document).ready(function() {    $('.js-example-basic-multiple').select2({		placeholder: "Select SubAdmin Permission",        allowClear: true	});	 });
  function editfield(e,n,id)
  {
      var v=$(e).text();
      $(e).empty();
      $(e).append('<input name="'+n+'" type="text" value="'+v+'" class="form-control" required>');
      $('#update_but'+id).css('display', 'block');
  }

  function get_text(e)
  {
    var array = [];
    $(e).parent().parent().find('input').each(function(){
      $(this).attr('name');
      
      array.push({
        nam : $(this).attr("name"),
          vall : $(this).val()
         
    });
    });
    
      $.ajax({
      url: "<?= base_url('admin/subadmin_update')?>",
      data: { form_val : array },
      type: 'post',
      dataType: "json",
      success: function(data) {
        if(data==1)
        {
          
          alert("Successfully updated");
          window.location.href="<?= base_url('Admin/subadmin')?>";
        }
        else if(data==0)
        {
          alert("Please try After Some Time");
          window.location.href="<?= base_url('Admin/subadmin')?>";
        }
        else
        {
          alert("data");
          $('#error_panel').empty();
          $('#error_panel').append(data.error);
        }
      }
    });
  }

</script>