<?php
/**
 * modules/locations/view_locations.php
 */

// 1. STRICT SECURITY KICK-OUT
$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=locations&page=list_locations') . "&status=access_denied");
    exit;
}

$location_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($location_id == 0) {
    header("Location: index.php?s=" . encrypt_url('mod=locations&page=list_locations'));
    exit;
}

// 2. FETCH LOCATION DETAILS
$sql = "SELECT * FROM TBL_LOCATIONS WHERE LOCATION_ID = $location_id";
$result = $db->query($sql);
$data = mysqli_fetch_assoc($result);

if (!$data) {
    header("Location: index.php?s=" . encrypt_url('mod=locations&page=list_locations'));
    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-map mr-2"></i>Location Details</h1>
                </div>
                <div class="col-sm-6 text-right">
                    <a href="index.php?s=<?php echo encrypt_url('mod=locations&page=list_locations'); ?>" 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">Master Record Information</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-6 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Location Name</label>
                            <h5 class="font-weight-bold text-dark"><?php echo htmlspecialchars($data['LOCATION_NAME']); ?></h5>
                        </div>
                        <div class="col-md-6 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Location Code</label>
                            <h5 class="text-primary font-weight-bold"><?php echo htmlspecialchars($data['LOCATION_CODE']); ?></h5>
                        </div>
                        
                        <div class="col-md-4">
                            <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-4">
                            <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>