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

$p = $GLOBALS['my_privs'] ?? ['can_view' => 0];
if (in_array(1, $_SESSION['all_role_ids'] ?? [])) { $p['can_view'] = 1; }

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

$mine_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($mine_id == 0) {
    header("Location: index.php?s=" . encrypt_url('mod=mines&page=list_mines'));
    exit;
}

$sql = "SELECT m.*, b.BLOCK_NAME, l.LOCATION_NAME, c.CATEGORY_NAME 
        FROM TBL_MINES m 
        JOIN TBL_BLOCKS b ON m.BLOCK_ID = b.BLOCK_ID 
        JOIN TBL_LOCATIONS l ON b.LOCATION_ID = l.LOCATION_ID 
        JOIN TBL_CATEGORIES c ON m.CATEGORY_ID = c.CATEGORY_ID
        WHERE m.MINE_ID = $mine_id";
$result = $db->query($sql);
$data = mysqli_fetch_assoc($result);

if (!$data) {
    header("Location: index.php?s=" . encrypt_url('mod=mines&page=list_mines'));
    exit;
}
?>

<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-mountain mr-2"></i>Mine Details</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">
            <div class="card card-outline card-primary shadow-sm" style="border-top: 4px solid #007bff;">
                <div class="card-header bg-white">
                    <h3 class="card-title font-weight-bold text-dark">Hierarchy & Assignments</h3>
                    <div class="card-tools">
                        <span class="badge <?php echo ($data['STATUS'] == 'ACTIVE') ? 'badge-success' : 'badge-danger'; ?> px-3 py-1" style="font-size: 14px;">
                            <?php echo $data['STATUS']; ?>
                        </span>
                    </div>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Physical Location</label>
                            <h5 class="text-danger font-weight-bold"><i class="fas fa-map-marker-alt mr-2"></i><?php echo htmlspecialchars($data['LOCATION_NAME']); ?></h5>
                            <span class="badge badge-light border mt-1"><i class="fas fa-cube mr-1 text-secondary"></i><?php echo htmlspecialchars($data['BLOCK_NAME']); ?></span>
                        </div>
                        <div class="col-md-4 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Mine Identifier</label>
                            <h5 class="text-dark font-weight-bold"><i class="fas fa-mountain mr-2 text-primary"></i><?php echo htmlspecialchars($data['MINE_NUMBER']); ?></h5>
                        </div>
                        <div class="col-md-4 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Assigned Category</label>
                            <br><span class="badge badge-info shadow-sm px-3 py-2" style="font-size: 14px;"><?php echo htmlspecialchars($data['CATEGORY_NAME']); ?></span>
                        </div>
                        
                        <div class="col-md-6 mt-3">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Record Created</label>
                            <p class="font-weight-bold"><?php echo date('d-M-Y h:i A', strtotime($data['CREATED_DATE'])); ?></p>
                        </div>
                        <div class="col-md-6 mt-3">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Last Updated</label>
                            <p class="font-weight-bold"><?php echo $data['UPDATED_DATE'] ? date('d-M-Y h:i A', strtotime($data['UPDATED_DATE'])) : 'Never Updated'; ?></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>