<?php
header('Content-Type: application/json');
require_once('../libs/classes/DbPathsArray.php');
require_once('../libs/classes/App_DB.php');

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

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents('php://input'), true);

    if (!$data) {
        $response['message'] = 'غلط ڈیٹا فارمیٹ';
        echo json_encode($response);
        exit;
    }

    $attSno = $data['attSno'] ?? '';
    $status = $data['status'] ?? '';

    if (empty($attSno) || empty($status)) {
        $response['message'] = 'تمام معلومات فراہم کریں';
        echo json_encode($response);
        exit;
    }

    try {
        $sql = "UPDATE attendence SET stdStatus = :status WHERE sno = :sno";
        $db->dbQuery($sql, [
            'status' => $status,
            'sno' => $attSno
        ]);

        $response['success'] = true;
        $response['message'] = 'حاضری کامیابی کے ساتھ تبدیل ہو گئی';
    } catch (Exception $e) {
        $response['message'] = 'ڈیٹابیس کی خرابی: ' . $e->getMessage();
    }
}

echo json_encode($response);
