<?php
/**
 * Dozar 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 . '/dozar/');
    exit;
}

// Get dozar record with joins
$sql = "SELECT d.*, 
        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,
        u.full_name as created_by_name
        FROM dozar_records d
        LEFT JOIN projects p ON d.project_id = p.id
        LEFT JOIN vehicles v ON d.vehicle_id = v.id
        LEFT JOIN drivers dr ON d.driver_id = dr.id
        LEFT JOIN deployed_from df ON d.deployed_from_id = df.id
        LEFT JOIN groups g ON d.group_id = g.id
        LEFT JOIN users u ON d.created_by = u.id
        WHERE d.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 . '/dozar/');
    exit;
}

$pageTitle = t('report_dozar', 'Dozar 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-monster"></i> <?php echo t('report_dozar', 'Dozar 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>

<!-- 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_collector', 'Collector'); ?>:</strong></td>
                        <td><?php echo $record['collector_name'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_created_by', 'Created By'); ?>:</strong></td>
                        <td><?php echo $record['created_by_name'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_created_date', 'Created'); ?>:</strong></td>
                        <td><?php echo date('Y-m-d H:i', strtotime($record['created_at'])); ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_updated_date', 'Last Updated'); ?>:</strong></td>
                        <td><?php echo date('Y-m-d H:i', strtotime($record['updated_at'])); ?></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>
                    <tr>
                        <td><strong><?php echo t('label_capacity', 'Capacity'); ?>:</strong></td>
                        <td><?php echo $record['capacity'] ?? '-'; ?></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>
                    <tr>
                        <td><strong><?php echo t('label_license', 'License Number'); ?>:</strong></td>
                        <td><?php echo $record['license_number'] ?? '-'; ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Work Details Card -->
<div class="card mb-4">
    <div class="card-header bg-success text-white">
        <h5 class="mb-0"><i class="fas fa-briefcase"></i> <?php echo t('label_work_details', 'Work 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_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_address', 'Collector Address'); ?>:</strong></td>
                        <td><?php echo $record['collector_entered_address'] ?? '-'; ?></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['start_odometer'], 2); ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_end_odometer', 'End Odometer'); ?>:</strong></td>
                        <td><?php echo $record['end_odometer'] ? number_format($record['end_odometer'], 2) : '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_work_length', 'Work Length (Hours)'); ?>:</strong></td>
                        <td><?php echo number_format($record['work_length_hours'], 2); ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Time Details Card -->
<div class="card mb-4">
    <div class="card-header bg-warning">
        <h5 class="mb-0"><i class="fas fa-clock"></i> <?php echo t('label_time_details', 'Time 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_start_time', 'Start Time'); ?>:</strong></td>
                        <td><?php echo $record['start_time'] ? date('H:i', strtotime($record['start_time'])) : '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_end_time', 'End Time'); ?>:</strong></td>
                        <td><?php echo $record['end_time'] ? date('H:i', strtotime($record['end_time'])) : '-'; ?></td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <table class="table table-borderless">
                    <tr>
                        <td><strong><?php echo t('label_remarks', 'Remarks'); ?>:</strong></td>
                        <td><?php echo $record['remarks'] ?? '-'; ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- GPS Location Card -->
<?php if ($record['gps_latitude'] || $record['gps_longitude']): ?>
<div class="card mb-4">
    <div class="card-header bg-dark text-white">
        <h5 class="mb-0"><i class="fas fa-map-marker-alt"></i> <?php echo t('label_gps_location', 'GPS Location'); ?></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_latitude', 'Latitude'); ?>:</strong></td>
                        <td><?php echo $record['gps_latitude']; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_longitude', 'Longitude'); ?>:</strong></td>
                        <td><?php echo $record['gps_longitude']; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_address', 'Address'); ?>:</strong></td>
                        <td><?php echo $record['gps_address'] ?? '-'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_captured_at', 'Captured At'); ?>:</strong></td>
                        <td><?php echo $record['gps_captured_at'] ? date('Y-m-d H:i', strtotime($record['gps_captured_at'])) : '-'; ?></td>
                    </tr>
                </table>
            </div>
            <?php if ($record['gps_latitude'] && $record['gps_longitude']): ?>
            <div class="col-md-6">
                <div id="map" style="height: 250px; border-radius: 8px;"></div>
                <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
                <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
                <script>
                    var map = L.map('map').setView([<?php echo $record['gps_latitude']; ?>, <?php echo $record['gps_longitude']; ?>], 15);
                    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                        attribution: '© OpenStreetMap'
                    }).addTo(map);
                    L.marker([<?php echo $record['gps_latitude']; ?>, <?php echo $record['gps_longitude']; ?>]).addTo(map)
                        .bindPopup('<?php echo addslashes($record['work_location'] ?? 'Work Location'); ?>')
                        .openPopup();
                </script>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Photo Card -->
<?php if ($record['photo_path']): ?>
<div class="card mb-4">
    <div class="card-header bg-secondary text-white">
        <h5 class="mb-0"><i class="fas fa-camera"></i> <?php echo t('label_photo', 'Photo'); ?></h5>
    </div>
    <div class="card-body text-center">
        <img src="<?php echo SITE_URL . '/' . $record['photo_path']; ?>" alt="Record Photo" class="img-fluid" style="max-height: 400px;">
    </div>
</div>
<?php endif; ?>

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