<script type="text/javascript">
  function showMessage(status, client_id) {
    if (status == 1) {
        $title = 'Great !!';
        $message = 'ROLE ASSIGN UPDATED';
        $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=role_management/grid.php';
        } else {
            window.location.href = http_host + '?page=role_management/grid.php';
        }
    });
  }
</script>
<?php 
  $status = '';
  $msg = '';
  if (isset($_REQUEST['eid']) && !empty($_REQUEST['eid']) && $_REQUEST['eid']!=0){
    $select_update="SELECT * FROM roles WHERE id='".$_REQUEST['eid']."'";
    $select_update_res=mysqli_query($conn,$select_update);
    $update_row=mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
    $checkbox_array = explode(",", $update_row['permissions']);
  }
   
  if(isset($_POST['insert'])){
    $permissions          = implode("," , $_REQUEST['permissions']);
    $created_id          = $_SESSION['user_detail']['id'];
    $created_datetime    = date('Y-m-d H:i:s');
    $updated_id          = $_SESSION['user_detail']['id']; 
    $updated_datetime    = date('Y-m-d H:i:s');
    
    $query = "UPDATE roles SET permissions ='$permissions',created_id='$created_id',created_datetime='$created_datetime',updated_id='$updated_id',updated_datetime='$updated_datetime' WHERE id='".$_REQUEST['eid']."'";
    $result = mysqli_query($conn,$query) or die( mysqli_error($conn) );
    if($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 ROLE</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=role_management/grid.php" class="btn btn-danger capital bottom10 custome-btn">VIEW ROLE 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">
                <div class="col-md-12">
                  <div class="form-group">
                    <label>ROLE NAME</label><span style="color: red">*</span>
                    <input type="text" size="35" id="name" name="name" class="form-control" placeholder="ROLE NAME" value="<?php echo $update_row['name'];?>" readonly=""/>
                  </div>
                </div>
                <div class="col-md-12 table-responsive">
                  <table class="table table-striped table-bordered table-list">
                    <thead>
                      <tr class="hitz-tab capital">
                        <th class="text-left">MODULE</th>
                        <th class="text-center" width="5%">show</th>
                        <th class="text-center" width="5%">ADD</th>
                        <th class="text-center" width="5%">UPDATE</th>
                        <th class="text-center" width="5%">DELETE</th>
                        <th class="text-center" width="5%">VIEW</th>
                      </tr>
                    </thead>
                    <?php $keys = array_keys($permission);
                    for($i = 0; $i < count($permission); $i++) { ?>
                      <tr>
                        <td><?php echo "$keys[$i]";?></td>
                        <?php foreach($permission[$keys[$i]] as $key => $value) { ?>
                        <td class="text-center"><input type="checkbox" class="flat-red" name="permissions[]" value="<?php echo "$key"?>" <?php if(in_array("$key", $checkbox_array)){ echo " checked=\"checked\""; } ?>></td>
                        <?php } ?>
                      </tr>
                      <?php } ?>
                  </table>
                </div>
                <hr>
                <div class="row" align="center">
                  <button name="insert" class="btn btn-primary submit capital" type="submit"> UPDATE  & SAVE</button>
                  <a href="index.php?page=role_management/grid.php" class="btn btn-danger capital"> Cancel</a>
                </div>
              </form>
              
            </div>
         </div>
      </div>
   </div>
</section>
