
<?php
session_start();
include '../../assets/constant/config.php';

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if (isset($_POST['submit1'])) {
      extract($_POST);

  $stmt = $conn->prepare("insert into groups(name,description)values(?,?)");
    $stmt->execute([$name,$description]);
  $last_id=$conn->lastInsertId();
$id=$last_id;  
$checkItem = $_POST["checkItem"];
//print_r($_POST);
 $a = count($checkItem);  
for($i=0;$i<$a;$i++){
$stmt = $conn->prepare("insert into permission_role(permission_id,group_id)values('$checkItem[$i]','$id')");
    $stmt->execute();

       }

        $_SESSION['success'] = "success";

        header("location:../manage_role.php");
    }

    //

if (isset($_POST['update'])) {
      
      
     extract($_POST);
     $stmt = $conn->prepare("delete  from permission_role where group_id='".$_POST['id']."'");
    $stmt->execute();

    $stmt = $conn->prepare("UPDATE groups set name='$name',description='$description' where id='".$_POST['id']."'");
    $stmt->execute();

$checkItem = $_POST["checkItem"];
//print_r($_POST);
 $a = count($checkItem);  
for($i=0;$i<$a;$i++){
 $id = $_POST['id'];

         $sql="insert into permission_role(permission_id,group_id)values('$checkItem[$i]','$id')";
          $execute = $conn->query($sql);
        
       }
    
        $_SESSION['update'] = "update";
        header("location:../manage_role.php");
    }
    

if (isset($_POST['del_id'])) {

        $stmt = $conn->prepare("UPDATE `groups` SET delete_status='1' where id=? ");

        $stmt->execute([htmlspecialchars($_POST['del_id'], ENT_QUOTES, 'UTF-8')]);

        $_SESSION['delete'] = "delete";

        header("location:../manage_role.php");
    }

} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}