<?php   include_once('config/db.php');  include_once('config/check.php'); 

$sysrights  = $conn->query("SELECT  *  FROM sys_module   where  active=1   order by displayno asc    ")->fetchAll();

if (filter_has_var(INPUT_POST, "submit")) 
 try{
         $role  =     $_POST['role'];   
         $module  =     $_POST['module'];   
         $perm  =      $_POST['perm'];   
         $number =    count(@$_POST["perm"]); 

        for($i=0; $i<$number; $i++)  
          {  
            $sql = 'INSERT INTO sys_rights (role,page ) VALUES(:role,:page )'; 
            $conn->prepare($sql)->execute(['role'=> $role, 'page' => $perm[$i]   ]); 
          }
          
         header("location:sys_rights"); 

   } catch(Exception $e) { echo 'Message: ' .$e->getMessage();}




?>

<!DOCTYPE html>
<html>
<head>
<?php include_once('inc/link.php');   ?>  
</head>
<body  <?php include_once('inc/nav.php');   ?> >
<div class="wrapper">

  <!-- Main Header -->
    <header class="main-header">

    <!-- Logo -->
       <?php include_once('inc/brand.php');   ?>

    <!-- Header Navbar -->
    <?php include_once('inc/navbar.php');   ?>

  </header>
 
  <!-- Left side column. contains the logo and sidebar -->
    <aside class="main-sidebar">

    <!-- sidebar: style can be found in sidebar.less -->
     <?php //include_once('inc/menu.php');   ?>
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        
        <section class="content-header">
          <h1>   <i class="fa fa-user-plus"></i> Assign System Rights    </h1>
            <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> </a></li>
            <li><a href="#"> </a></li>
            <li class="active"> </li>
            <!-- <li><a href="bppartnerl.php" class="btn btn-default btn-sm"> <i class="fa fa-bars"></i> View All Business rights  </a></li> -->
            </ol>
        </section>
        <section class="content container-fluid">

<div class="row">
  <!-- /.col -->
   <div class="col-md-12">
    <div class="nav-tabs-custom">
     
          <form class="form-horizontal"  action="" method="post" style="" enctype="multipart/form-data" >
          <div class="row">
                
                <div class="col-sm-12 ">                   
                       
                                    
                              <div class="col-md-4">  
                                  <label > Select role  </label>                                                                       
                                      <select class="form-control  js-example-basic-single  "  required="" name="role" style="width:100%" >
                                              <?php $stmt = $conn->prepare("SELECT * FROM sys_roles ");
                                                    $stmt->execute(); foreach ($stmt as $row) {?>
                                                        <option> </option>
                                                      <option value="<?php echo $row['id']; ?>"><?php
                                                          echo $row['name']; ?></option> <?php } ?>
                                      </select> 
                              </div>  
                              <div class="col-md-8">
                                <label > Select module and proceed  to assign permission   </label>
                                   <ul class="menu" data-widget="tree"> 
                                      <?php        foreach($sysrights  as $rights)   { ?>
                  
                                    <li class="treeview list">
                                       <a href=""> <i class="fa fa-angle-right "></i>        <?php echo $rights["name"]; ?>     </a> 
                                         
                                        <ul class="treeview-menu">
                                        <?php                  
                                            $stmt1 = $conn->query(" SELECT * FROM  sys_page a where a.module= '".$rights["id"]."'  and a.active=1  ");
                                            foreach($stmt1  as $PM)
                                                    {  ?>

                                                    <input type="hidden" class="form-control "   name="module" value="<?php echo  $PM['module']; ?>"   readonly="true">                                    
                                                     <input type="checkbox" name="perm[]" value="<?php echo  $PM['id']; ?>" /> <?php echo  $PM['name']; ?>    
                                                    
                                            <?php } ?>        
                                        </ul>
                                    </li>  <?php } ?>
                                 </ul>
                              </div>
                               
  
                            <!-- submit button -->
                          <div class="form-group">
                                   <div class="col-sm-offset-4 col-sm-12">                               
                                   <button type="submit" style="  text-align: center;" name="submit" class="btn btn-success ">  <i class="fa fa-user-plus "></i>  Add a new client  </button>
                                  </div>
                            </div>
                 </div>
              </div>
          
      </form>     
  
    </div>
    <!-- /.nav-tabs-custom -->
    </div>
  </div>
  <!-- /.col -->

</section>

      
    </div>
  <!-- /.content-wrapper -->
  
    <!-- Main Footer -->
    <footer class="main-footer">
        <?php  include_once('inc/footer.php'); ?>
    </footer>
  
</div>
                         


<!-- REQUIRED JS SCRIPTS -->
<?php include_once('inc/linkjs.php') ?>

</body>
</html>

