<?php
/**
 * modules/rates/view_rates.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=rates&page=list_rates') . "&status=access_denied");
    exit;
}

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

$sql = "SELECT r.*, c.CATEGORY_NAME, t.TYPE_NAME 
        FROM TBL_RATES r
        JOIN TBL_CATEGORIES c ON r.CATEGORY_ID = c.CATEGORY_ID
        JOIN TBL_COAL_TYPES t ON r.TYPE_ID = t.TYPE_ID
        WHERE r.RATE_ID = $rate_id";
$result = $db->query($sql);
$data = mysqli_fetch_assoc($result);

if (!$data) {
    header("Location: index.php?s=" . encrypt_url('mod=rates&page=list_rates'));
    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-receipt mr-2"></i>Rate Details</h1>
                </div>
                <div class="col-sm-6 text-right">
                    <a href="index.php?s=<?php echo encrypt_url('mod=rates&page=list_rates'); ?>" 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">Pricing Matrix Entry</h3>
                    <div class="card-tools">
                        <span class="badge <?php echo ($data['STATUS'] == 'ACTIVE') ? 'badge-success' : ($data['STATUS'] == 'HISTORICAL' ? 'badge-warning' : '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;">Coal Parameters</label>
                            <h5 class="text-dark font-weight-bold"><?php echo htmlspecialchars($data['CATEGORY_NAME']); ?></h5>
                            <span class="badge badge-info shadow-sm px-2 py-1 mt-1"><?php echo htmlspecialchars($data['TYPE_NAME']); ?></span>
                        </div>
                        <div class="col-md-4 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Assigned Rate</label>
                            <h2 class="text-success font-weight-bold mb-0">Rs <?php echo number_format($data['RATE_PER_TON'], 2); ?></h2>
                            <small class="text-muted">Per Metric Ton</small>
                        </div>
                        <div class="col-md-4 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Active Timeline</label>
                            <h5 class="text-dark font-weight-bold">
                                <?php echo date('d-M-Y', strtotime($data['EFFECTIVE_FROM'])); ?> 
                                to 
                                <?php echo $data['EFFECTIVE_TO'] ? date('d-M-Y', strtotime($data['EFFECTIVE_TO'])) : 'Present'; ?>
                            </h5>
                        </div>
                        
                        <div class="col-md-6 mt-3 border-top pt-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 border-top pt-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>