<?php
session_start();
require_once realpath(dirname(__DIR__, 3) . '/services/Model.php');
$obj = new Model();

$userid = $_SESSION['userid'] ?? null;
$zone_id = (int)($_POST['zone_id'] ?? 0);

if ($zone_id <= 0) {
    echo json_encode(['ok' => false, 'msg' => 'Invalid zone id']);
    exit;
}

$result = $obj->rawSqlSingle(
    "SELECT COUNT(*) AS total FROM re_tbl_agent WHERE zone = '$zone_id' AND entry_by = '$userid'"
);

if ($result['total'] > 0) {
    echo json_encode(['ok' => false, 'msg' => 'Zone is used by customers']);
    exit;
}

$obj->rawSql("DELETE FROM re_tbl_zone WHERE zone_id = '$zone_id'");

echo json_encode(['ok' => true, 'msg' => 'Zone deleted']);
