<?php
/**
 * modules/blocks/add_blocks.php
 */

// 1. STRICT SECURITY KICK-OUT
$p = $GLOBALS['my_privs'] ?? ['can_add' => 0];
if (in_array(1, $_SESSION['all_role_ids'] ?? [])) {
    $p['can_add'] = 1;
}

if ($p['can_add'] == 0) {
    header("Location: index.php?s=" . encrypt_url('mod=blocks&page=list_blocks') . "&status=access_denied");
    exit;
}

// 2. FORM POST HANDLER
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $location_id = (int)$_POST['location_id'];
    $block_name  = $db->escape($_POST['block_name']);
    $status      = $db->escape($_POST['status']);
    $created_by  = (int)$_SESSION['user_id'];

    // Duplicate Check: Same Block Name inside the Same Location
    $check_sql = "SELECT BLOCK_ID FROM TBL_BLOCKS WHERE LOCATION_ID = $location_id AND BLOCK_NAME = '$block_name'";
    if (mysqli_num_rows($db->query($check_sql)) > 0) {
        header("Location: index.php?s=" . encrypt_url('mod=blocks&page=add_blocks') . "&status=duplicate");
        exit;
    }

    $sql = "INSERT INTO TBL_BLOCKS (LOCATION_ID, BLOCK_NAME, STATUS, CREATED_BY) 
            VALUES ($location_id, '$block_name', '$status', $created_by)";
    
    if ($db->query($sql)) {
        header("Location: index.php?s=" . encrypt_url('mod=blocks&page=list_blocks') . "&status=added");
        exit;
    }
}

// Fetch Active Locations for Dropdown
$loc_sql = "SELECT LOCATION_ID, LOCATION_NAME FROM TBL_LOCATIONS WHERE STATUS = 'ACTIVE' ORDER BY LOCATION_NAME ASC";
$loc_res = $db->query($loc_sql);
?>

<div class="p-3">
    <section class="content-header px-0">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="font-weight-bold text-primary"><i class="fas fa-plus-circle mr-2"></i>Register New Block</h1>
                </div>
                <div class="col-sm-6 text-right">
                    <a href="index.php?s=<?php echo encrypt_url('mod=blocks&page=list_blocks'); ?>" class="btn btn-default shadow-sm border">
                        <i class="fas fa-arrow-left mr-1"></i> Back to List
                    </a>
                </div>
            </div>
        </div>
    </section>

    <section class="content px-0">
        <div class="container-fluid">
            <form action="" method="POST">
                <div class="card card-outline card-primary shadow-sm" style="border-top: 4px solid #007bff;">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-5 form-group">
                                <label class="text-secondary">Parent Location <span class="text-danger">*</span></label>
                                <select name="location_id" class="form-control" required>
                                    <option value="">-- Select Location --</option>
                                    <?php while($l = mysqli_fetch_assoc($loc_res)): ?>
                                        <option value="<?php echo $l['LOCATION_ID']; ?>"><?php echo htmlspecialchars($l['LOCATION_NAME']); ?></option>
                                    <?php endwhile; ?>
                                </select>
                            </div>
                            
                            <div class="col-md-4 form-group">
                                <label class="text-secondary">Block Identifier <span class="text-danger">*</span></label>
                                <select name="block_name" class="form-control" required>
                                    <option value="">-- Select Block --</option>
                                    <!-- Dynamically generate Block-1 to Block-50 -->
                                    <?php for($i=1; $i<=50; $i++): ?>
                                        <option value="BLOCK-<?php echo $i; ?>">BLOCK-<?php echo $i; ?></option>
                                    <?php endfor; ?>
                                </select>
                            </div>

                            <div class="col-md-3 form-group">
                                <label class="text-secondary">Status</label>
                                <select name="status" class="form-control">
                                    <option value="ACTIVE">ACTIVE</option>
                                    <option value="INACTIVE">INACTIVE</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="card-footer bg-white text-right p-3">
                        <button type="submit" class="btn btn-primary px-5 font-weight-bold shadow-sm">
                            <i class="fas fa-save mr-2"></i> SAVE BLOCK
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </section>
</div>