<?php
/**
 * modules/mines/add_mines.php
 */

$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=mines&page=list_mines') . "&status=access_denied");
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $block_id    = (int)$_POST['block_id'];
    $category_id = (int)$_POST['category_id'];
    $mine_number = $db->escape($_POST['mine_number']);
    $status      = $db->escape($_POST['status']);
    $created_by  = (int)$_SESSION['user_id'];

    // Prevent identical mine numbers inside the same block
    $check_sql = "SELECT MINE_ID FROM TBL_MINES WHERE BLOCK_ID = $block_id AND MINE_NUMBER = '$mine_number'";
    if (mysqli_num_rows($db->query($check_sql)) > 0) {
        header("Location: index.php?s=" . encrypt_url('mod=mines&page=add_mines') . "&status=duplicate");
        exit;
    }

    $sql = "INSERT INTO TBL_MINES (BLOCK_ID, CATEGORY_ID, MINE_NUMBER, STATUS, CREATED_BY) 
            VALUES ($block_id, $category_id, '$mine_number', '$status', $created_by)";
    
    if ($db->query($sql)) {
        header("Location: index.php?s=" . encrypt_url('mod=mines&page=list_mines') . "&status=added");
        exit;
    }
}

// Fetch Blocks with Location Names for Dropdown
$blocks_sql = "SELECT b.BLOCK_ID, b.BLOCK_NAME, l.LOCATION_NAME 
               FROM TBL_BLOCKS b JOIN TBL_LOCATIONS l ON b.LOCATION_ID = l.LOCATION_ID 
               WHERE b.STATUS = 'ACTIVE' ORDER BY l.LOCATION_NAME ASC, b.BLOCK_NAME ASC";
$blocks_res = $db->query($blocks_sql);

// Fetch Categories for Dropdown
$cat_sql = "SELECT CATEGORY_ID, CATEGORY_NAME FROM TBL_CATEGORIES WHERE STATUS = 'ACTIVE' ORDER BY CATEGORY_NAME ASC";
$cat_res = $db->query($cat_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 Mine</h1>
                </div>
                <div class="col-sm-6 text-right">
                    <a href="index.php?s=<?php echo encrypt_url('mod=mines&page=list_mines'); ?>" 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 & Block <span class="text-danger">*</span></label>
                                <select name="block_id" class="form-control" required>
                                    <option value="">-- Select Block --</option>
                                    <?php while($b = mysqli_fetch_assoc($blocks_res)): ?>
                                        <option value="<?php echo $b['BLOCK_ID']; ?>">
                                            <?php echo htmlspecialchars($b['LOCATION_NAME'] . " - " . $b['BLOCK_NAME']); ?>
                                        </option>
                                    <?php endwhile; ?>
                                </select>
                            </div>

                            <div class="col-md-3 form-group">
                                <label class="text-secondary">Mine Number <span class="text-danger">*</span></label>
                                <select name="mine_number" class="form-control" required>
                                    <option value="">-- Select Mine --</option>
                                    <?php for($i=1; $i<=100; $i++): ?>
                                        <option value="Mine-<?php echo $i; ?>">Mine-<?php echo $i; ?></option>
                                    <?php endfor; ?>
                                </select>
                            </div>
                            
                            <div class="col-md-2 form-group">
                                <label class="text-secondary">Coal Category <span class="text-danger">*</span></label>
                                <select name="category_id" class="form-control" required>
                                    <option value="">-- Select --</option>
                                    <?php while($c = mysqli_fetch_assoc($cat_res)): ?>
                                        <option value="<?php echo $c['CATEGORY_ID']; ?>"><?php echo htmlspecialchars($c['CATEGORY_NAME']); ?></option>
                                    <?php endwhile; ?>
                                </select>
                            </div>

                            <div class="col-md-2 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 MINE
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </section>
</div>