<?php
/**
 * Tripper Record Detail View Page
 * Dozar & Tripper Management System
 */

require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/nepali_calendar.php';

$user = getCurrentUser();
$pdo = getPDO();

// Get record ID
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if (!$id) {
    $_SESSION['error_message'] = t('msg_error', 'Invalid record ID');
    header('Location: ' . SITE_URL . '/tripper/');
    exit;
}

// Get tripper record with joins
$sql = "SELECT t.*, 
        p.project_name, 
        v.vehicle_number, 
        v.vehicle_type,
        v.vehicle_model,
        v.capacity,
        dr.full_name as driver_name,
        dr.phone_number as driver_phone,
        dr.license_number,
        df.entity_name as deployed_name,
        g.group_name,
        lc.full_name as loading_collector_name,
        uc.full_name as unloading_collector_name
        FROM tripper_records t
        LEFT JOIN projects p ON t.project_id = p.id
        LEFT JOIN vehicles v ON t.vehicle_id = v.id
        LEFT JOIN drivers dr ON t.driver_id = dr.id
        LEFT JOIN deployed_from df ON t.deployed_from_id = df.id
        LEFT JOIN groups g ON t.group_id = g.id
        LEFT JOIN users lc ON t.loading_collector_id = lc.id
        LEFT JOIN users uc ON t.unloading_collector_id = uc.id
        WHERE t.id = ?";

$stmt = $pdo->prepare($sql);
$stmt->execute([$id]);
$record = $stmt->fetch();

if (!$record) {
    $_SESSION['error_message'] = t('msg_no_records', 'Record not found');
    header('Location: ' . SITE_URL . '/tripper/');
    exit;
}

$pageTitle = t('report_tripper', 'Tripper Record Details');
?>

<?php include __DIR__ . '/../includes/header.php'; ?>

<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="fas fa-truck"></i> <?php echo t('report_tripper', 'Tripper Record Details'); ?></h2>
            <div>
                <a href="edit.php?id=<?php echo $id; ?>" class="btn btn-warning">
                    <i class="fas fa-edit"></i> <?php echo t('action_edit', 'Edit'); ?>
                </a>
                <a href="index.php" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> <?php echo t('action_back', 'Back'); ?>
                </a>
            </div>
        </div>
    </div>
</div>

<!-- Status Badge -->
<div class="alert <?php echo $record['status'] === 'completed' ? 'alert-success' : ($record['status'] === 'cancelled' ? 'alert-danger' : 'alert-warning'); ?>">
    <strong><?php echo t('label_status', 'Status'); ?>:</strong> 
    <?php 
    switch($record['status']) {
        case 'completed': echo t('status_completed', 'Completed'); break;
        case 'cancelled': echo t('status_cancelled', 'Cancelled'); break;
        default: echo t('status_pending_unloading', 'Pending Unloading');
    }
    ?>
</div>

<!-- Record Details Card -->
<div class="card mb-4">
    <div class="card-header bg-primary text-white">
        <h5 class="mb-0"><i class="fas fa-info-circle"></i> <?php echo t('label_record_info', 'Record Information'); ?></h5>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <table class="table table-borderless">
                    <tr>
                        <td><strong><?php echo t('label_date', 'Date'); ?>:</strong></td>
                        <td><?php echo date_to_nepali($record['created_date']); ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_project', 'Project'); ?>:</strong></td>
                        <td><?php echo $record['project_name'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_deployed_from', 'Deployed From'); ?>:</strong></td>
                        <td><?php echo $record['deployed_name'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_group', 'Group'); ?>:</strong></td>
                        <td><?php echo $record['group_name'] ?? '-'; ?></td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <table class="table table-borderless">
                    <tr>
                        <td><strong><?php echo t('label_work_location', 'Work Location'); ?>:</strong></td>
                        <td><?php echo $record['work_location'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_entry_location', 'Entry Location'); ?>:</strong></td>
                        <td><?php echo $record['entry_location'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_material_type', 'Material Type'); ?>:</strong></td>
                        <td><?php echo $record['material_type'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_quantity', 'Quantity/Weight'); ?>:</strong></td>
                        <td><?php echo $record['quantity_weight'] ? number_format($record['quantity_weight'], 2) . ' ' . t('unit_ton', 'Ton') : '-'; ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Vehicle & Driver Card -->
<div class="card mb-4">
    <div class="card-header bg-info text-white">
        <h5 class="mb-0"><i class="fas fa-truck"></i> <?php echo t('label_vehicle_info', 'Vehicle & Driver Information'); ?></h5>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <h6 class="text-muted mb-3"><?php echo t('label_vehicle', 'Vehicle'); ?></h6>
                <table class="table table-borderless">
                    <tr>
                        <td><strong><?php echo t('label_vehicle_number', 'Vehicle Number'); ?>:</strong></td>
                        <td><?php echo $record['vehicle_number'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_vehicle_type', 'Vehicle Type'); ?>:</strong></td>
                        <td><?php echo $record['vehicle_type'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_vehicle_model', 'Model'); ?>:</strong></td>
                        <td><?php echo $record['vehicle_model'] ?? '-'; ?></td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <h6 class="text-muted mb-3"><?php echo t('label_driver', 'Driver'); ?></h6>
                <table class="table table-borderless">
                    <tr>
                        <td><strong><?php echo t('label_driver_name', 'Driver Name'); ?>:</strong></td>
                        <td><?php echo $record['driver_name'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_phone', 'Phone Number'); ?>:</strong></td>
                        <td><?php echo $record['driver_phone'] ?? '-'; ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Loading Details Card -->
<div class="card mb-4">
    <div class="card-header bg-success text-white">
        <h5 class="mb-0"><i class="fas fa-arrow-down"></i> <?php echo t('label_loading', 'Loading Details'); ?></h5>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <table class="table table-borderless">
                    <tr>
                        <td><strong><?php echo t('label_loading_collector', 'Loading Collector'); ?>:</strong></td>
                        <td><?php echo $record['loading_collector_name'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_loading_location', 'Loading Location'); ?>:</strong></td>
                        <td><?php echo $record['loading_location'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_loading_time', 'Loading Time'); ?>:</strong></td>
                        <td><?php echo $record['loading_time'] ? date('H:i', strtotime($record['loading_time'])) : '-'; ?></td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <table class="table table-borderless">
                    <tr>
                        <td><strong><?php echo t('label_start_odometer', 'Start Odometer'); ?>:</strong></td>
                        <td><?php echo number_format($record['loading_start_odometer'], 2); ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_remarks', 'Loading Remarks'); ?>:</strong></td>
                        <td><?php echo $record['loading_remarks'] ?? '-'; ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Unloading Details Card -->
<?php if ($record['status'] !== 'pending_unloading'): ?>
<div class="card mb-4">
    <div class="card-header bg-warning">
        <h5 class="mb-0"><i class="fas fa-arrow-up"></i> <?php echo t('label_unloading', 'Unloading Details'); ?></h5>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <table class="table table-borderless">
                    <tr>
                        <td><strong><?php echo t('label_unloading_collector', 'Unloading Collector'); ?>:</strong></td>
                        <td><?php echo $record['unloading_collector_name'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_unloading_location', 'Unloading Place'); ?>:</strong></td>
                        <td><?php echo $record['unloading_place'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_unloading_time', 'Unloading Time'); ?>:</strong></td>
                        <td><?php echo $record['unloading_time'] ? date('H:i', strtotime($record['unloading_time'])) : '-'; ?></td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <table class="table table-borderless">
                    <tr>
                        <td><strong><?php echo t('label_end_odometer', 'End Odometer'); ?>:</strong></td>
                        <td><?php echo $record['unloading_end_odometer'] ? number_format($record['unloading_end_odometer'], 2) : '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_km', 'Total KM'); ?>:</strong></td>
                        <td><?php
                            if (!$record['unloading_end_odometer']) {
                                echo '-';
                            } elseif ($record['loading_start_odometer'] <= 1 && $record['unloading_end_odometer'] <= 1) {
                                echo '<span class="badge bg-secondary">Meter N/A</span>';
                            } else {
                                $d = $record['unloading_end_odometer'] - $record['loading_start_odometer'];
                                echo $d > 0
                                    ? number_format($d, 2) . ' KM'
                                    : '<span class="badge bg-danger">Data Error</span>';
                            }
                        ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_remarks', 'Unloading Remarks'); ?>:</strong></td>
                        <td><?php echo $record['unloading_remarks'] ?? '-'; ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<?php include __DIR__ . '/../includes/footer.php'; ?>
