<?php
session_start();  
include("../../config/web_mysqlconnect.php");
include("/var/www/html/ensembler/function/Logger.php");

$userId = $_SESSION['userid'];

if(!isset($_POST['action'])) exit;

/* ======================
   BULK ASSIGN
====================== */
if($_POST['action']=="assign_bulk"){

    $queue=$_POST['queue_id'];
    $departments=$_POST['departments'];

    foreach($departments as $dept){

        $chk=$link->prepare("
            SELECT id FROM department_queue_mapping
            WHERE department_id=? AND queue_id=?
        ");
        $chk->bind_param("si",$dept,$queue);
        $chk->execute();
        $chk->store_result();

        if($chk->num_rows==0){

            $ins=$link->prepare("
                INSERT INTO department_queue_mapping
                (department_id,queue_id)
                VALUES (?,?)
            ");
            $ins->bind_param("si",$dept,$queue);
            $ins->execute();

        

           // get department name
            $dname = "";
            $dres = $link->prepare("SELECT department FROM department WHERE departmentID=?");
            $dres->bind_param("s",$dept);
            $dres->execute();
            $dres->bind_result($dname);
            $dres->fetch();
            $dres->close();

            // get queue name
            $qname = "";
            $qres = $link->prepare("SELECT queue_name FROM queue_master WHERE id=?");
            $qres->bind_param("i",$queue);
            $qres->execute();
            $qres->bind_result($qname);
            $qres->fetch();
            $qres->close();

            // AUDIT LOG
            Logger::auditLog(
                $link,
                $configdbname,
                $userId,
                "Mapped department to queue",
                "",
                "Department- $dname mapped to Queue - $qname",
                "QUEUE_MAPPING"
            );

        }
    }
}


/* ======================
   REMOVE
====================== */
if($_POST['action']=="remove"){

    $id=(int)$_POST['id'];

    // get mapping info before delete
    $info=$link->query("SELECT department_id,queue_id 
                        FROM department_queue_mapping 
                        WHERE id=$id")->fetch_assoc();

    $link->query("DELETE FROM department_queue_mapping WHERE id=$id");

    if($info){

        // department name
        $dname="";
        $dres=$link->prepare("SELECT department FROM department WHERE departmentID=?");
        $dres->bind_param("s",$info['department_id']);
        $dres->execute();
        $dres->bind_result($dname);
        $dres->fetch();
        $dres->close();

        // queue name
        $qname="";
        $qres=$link->prepare("SELECT queue_name FROM queue_master WHERE id=?");
        $qres->bind_param("i",$info['queue_id']);
        $qres->execute();
        $qres->bind_result($qname);
        $qres->fetch();
        $qres->close();

        Logger::auditLog(
            $link,
            $configdbname,
            $userId,
            "Unmapped department from queue",
            "",
            "Department - $dname removed from Queue - $qname",
            "QUEUE_MAPPING"
        );

    }
}


/* ======================
   RETURN TAGS
====================== */
if(in_array($_POST['action'],["assign_bulk","remove","get_departments"])){

    $queue=$_POST['queue_id'];

    $stmt=$link->prepare("
        SELECT m.id,d.department
        FROM department_queue_mapping m
        JOIN department d ON d.departmentID=m.department_id
        WHERE m.queue_id=?
        ORDER BY d.department
    ");
    $stmt->bind_param("i",$queue);
    $stmt->execute();
    $res=$stmt->get_result();

    if($res->num_rows==0){
        echo "<span style='color:#777'>No departments assigned</span>";
        exit;
    }

    while($r=$res->fetch_assoc()){
        echo "
        <div class='tag'>
            {$r['department']}
            <span class='removeTag' data-id='{$r['id']}'>×</span>
        </div>
        ";
    }
}
?>
