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

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

$sql = "SELECT w.*, c.CONTRACTOR_NAME, c.CONTRACTOR_CODE, m.MINE_NUMBER, b.BLOCK_NAME, l.LOCATION_NAME, t.TYPE_NAME 
        FROM TBL_WEIGHBRIDGE_TICKETS w
        LEFT JOIN TBL_CONTRACTORS c ON w.CONTRACTOR_ID = c.CONTRACTOR_ID
        LEFT JOIN TBL_MINES m ON w.MINE_ID = m.MINE_ID
        LEFT JOIN TBL_BLOCKS b ON m.BLOCK_ID = b.BLOCK_ID
        LEFT JOIN TBL_LOCATIONS l ON b.LOCATION_ID = l.LOCATION_ID
        LEFT JOIN TBL_COAL_TYPES t ON w.TYPE_ID = t.TYPE_ID
        WHERE w.TICKET_ID = $id";
$result = $db->query($sql);
$data = mysqli_fetch_assoc($result);

if (!$data) {
    header("Location: index.php?s=" . encrypt_url('mod=token&page=list_token'));
    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-search mr-2"></i>Token Overview</h1>
                </div>
                <div class="col-sm-6 text-right">
                    <a href="index.php?s=<?php echo encrypt_url('mod=token&page=print_token&id='.$id); ?>" target="_blank" class="btn btn-warning shadow-sm font-weight-bold mr-2">
                        <i class="fas fa-print mr-1"></i> Re-Print Slip
                    </a>
                    <a href="index.php?s=<?php echo encrypt_url('mod=token&page=list_token'); ?>" 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 border-bottom-0 pb-0">
                    <div class="d-flex justify-content-between align-items-center">
                        <h3 class="card-title font-weight-bold text-dark" style="font-size: 1.5rem;"><?php echo htmlspecialchars($data['TICKET_NO']); ?></h3>
                        <div>
                            <?php if ($data['STATUS'] == 'FIRST_WEIGHT'): ?>
                                <span class="badge badge-warning px-3 py-2 shadow-sm" style="font-size: 14px;"><i class="fas fa-spinner fa-spin mr-1"></i> IN TRANSIT</span>
                            <?php elseif ($data['STATUS'] == 'COMPLETED'): ?>
                                <span class="badge badge-success px-3 py-2 shadow-sm" style="font-size: 14px;"><i class="fas fa-check-double mr-1"></i> DISPATCHED</span>
                            <?php else: ?>
                                <span class="badge badge-danger px-3 py-2 shadow-sm" style="font-size: 14px;"><i class="fas fa-times mr-1"></i> CANCELLED</span>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
                
                <div class="card-body">
                    <div class="row bg-light rounded p-3 mb-4 border">
                        <div class="col-md-3">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Daily Serial No.</label>
                            <h5 class="font-weight-bold text-dark"><?php echo str_pad($data['DAILY_SERIAL'], 3, '0', STR_PAD_LEFT); ?></h5>
                        </div>
                        <div class="col-md-3">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Vehicle Number</label>
                            <h5 class="text-primary font-weight-bold"><i class="fas fa-truck mr-2"></i><?php echo htmlspecialchars($data['VEHICLE_NO']); ?></h5>
                        </div>
                        <div class="col-md-3">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Expected Tons</label>
                            <h5 class="font-weight-bold"><?php echo $data['EXPECTED_TONS'] ? $data['EXPECTED_TONS'] : 'N/A'; ?></h5>
                        </div>
                        <div class="col-md-3">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Print Count</label>
                            <h5 class="font-weight-bold text-danger"><?php echo $data['PRINT_COUNT']; ?> Times</h5>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Assigned Contractor</label>
                            <h5 class="text-dark font-weight-bold"><?php echo htmlspecialchars($data['CONTRACTOR_CODE'] . " - " . $data['CONTRACTOR_NAME']); ?></h5>
                        </div>
                        <div class="col-md-6 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Target Location & Mine</label>
                            <h5 class="text-dark font-weight-bold"><?php echo htmlspecialchars($data['LOCATION_NAME'] . " > " . $data['BLOCK_NAME'] . " > " . $data['MINE_NUMBER']); ?></h5>
                        </div>
                        
                        <div class="col-md-3 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Coal Type</label>
                            <span class="badge badge-info shadow-sm px-2 py-1" style="font-size:14px;"><?php echo htmlspecialchars($data['TYPE_NAME']); ?></span>
                        </div>
                        <div class="col-md-3 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Driver Name</label>
                            <h5 class="text-dark"><?php echo htmlspecialchars($data['DRIVER_NAME']); ?></h5>
                        </div>
                    </div>

                    <div class="row mt-3 pt-4 border-top">
                        <div class="col-md-4 text-center">
                            <h6 class="text-muted text-uppercase mb-2">Tare Weight (In)</h6>
                            <h2 class="font-weight-bold text-dark"><?php echo number_format($data['FIRST_WEIGHT'], 2); ?> <small>Ton</small></h2>
                            <small class="text-muted"><?php echo date('d-M-Y h:i A', strtotime($data['FIRST_WEIGHT_TIME'])); ?></small>
                        </div>
                        <div class="col-md-4 text-center border-left border-right">
                            <h6 class="text-muted text-uppercase mb-2">Gross Weight (Out)</h6>
                            <h2 class="font-weight-bold <?php echo $data['SECOND_WEIGHT'] ? 'text-dark' : 'text-muted'; ?>">
                                <?php echo $data['SECOND_WEIGHT'] ? number_format($data['SECOND_WEIGHT'], 2) : 'PENDING'; ?> <small>Ton</small>
                            </h2>
                            <small class="text-muted"><?php echo $data['SECOND_WEIGHT_TIME'] ? date('d-M-Y h:i A', strtotime($data['SECOND_WEIGHT_TIME'])) : 'Waiting...'; ?></small>
                        </div>
                        <div class="col-md-4 text-center bg-light rounded pb-2 pt-2 border">
                            <h6 class="text-primary text-uppercase font-weight-bold mb-2">Net Dispatched</h6>
                            <h2 class="font-weight-bold <?php echo $data['NET_WEIGHT'] ? 'text-success' : 'text-muted'; ?>">
                                <?php echo $data['NET_WEIGHT'] ? number_format($data['NET_WEIGHT'], 2) : 'PENDING'; ?> <small>Ton</small>
                            </h2>
                        </div>
                    </div>
                    
                </div>
            </div>
        </div>
    </section>
</div>