<?php
header('Content-Type: application/json');
header('X-Content-Type-Options: nosniff');

session_start();
if (!isset($_SESSION['userid']) || empty($_SESSION['key'])) {
    echo json_encode(['success' => false, 'message' => 'غیر مجاز رسائی']);
    exit;
}

require_once('../libs/classes/DbPathsArray.php');
require_once('../libs/classes/App_DB.php');

$db = new App_DB(DBU::$dba_user);

$response = ['success' => false, 'message' => '', 'data' => []];

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    try {
        $sql = "SELECT 
                    s.darja,
                    s.shoba_sno,
                    s.promotionDate,
                    s.dateEnd,
                    d.darja AS darjaName,
                    sh.shoba AS shobaName
                FROM stddarjaat s
                JOIN darjaat d ON s.darja = d.derjaCode
                JOIN shobajaat sh ON s.shoba_sno = sh.sno
                GROUP BY 
                    s.darja, s.shoba_sno, s.promotionDate, s.dateEnd, d.darja, sh.shoba
                ORDER BY 
                    s.shoba_sno ASC,
                    s.darja ASC";

        $data = $db->getRecordSet($sql);
        $response['success'] = true;
        $response['data'] = $data;
    } catch (Exception $e) {
        $response['message'] = 'ڈیٹابیس غلطی: ' . $e->getMessage();
    }
}

echo json_encode($response);
