<?php
/**
 * User 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';

$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 . '/users/');
    exit;
}

// Get user record
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$id]);
$record = $stmt->fetch();

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

// Get assigned projects
$assignedProjects = [];
if ($record['assigned_projects']) {
    $projectIds = explode(',', $record['assigned_projects']);
    $placeholders = implode(',', array_fill(0, count($projectIds), '?'));
    $stmt = $pdo->prepare("SELECT project_name FROM projects WHERE id IN ($placeholders)");
    $stmt->execute($projectIds);
    $assignedProjects = $stmt->fetchAll(PDO::FETCH_COLUMN);
}

$pageTitle = t('nav_users', 'User 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-user"></i> <?php echo t('nav_users', 'User Details'); ?></h2>
            <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>

<!-- User 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', 'User 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('login_username', 'Username'); ?>:</strong></td>
                        <td><?php echo $record['username']; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_full_name', 'Full Name'); ?>:</strong></td>
                        <td><?php echo $record['full_name']; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_role', 'Role'); ?>:</strong></td>
                        <td>
                            <span class="badge bg-<?php echo $record['role'] == 'admin' ? 'danger' : ($record['role'] == 'viewer' ? 'info' : 'primary'); ?>">
                                <?php echo ucfirst($record['role']); ?>
                            </span>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <table class="table table-borderless">
                    <tr>
                        <td><strong><?php echo t('label_collector_access', 'Access Level'); ?>:</strong></td>
                        <td><?php echo $record['collector_access'] ? str_replace('_', ' ', ucwords($record['collector_access'], '_')) : 'All Modules'; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_status', 'Status'); ?>:</strong></td>
                        <td>
                            <span class="badge bg-<?php echo $record['is_active'] ? 'success' : 'secondary'; ?>">
                                <?php echo $record['is_active'] ? t('status_active', 'Active') : t('status_inactive', 'Inactive'); ?>
                            </span>
                        </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>
                </table>
            </div>
        </div>
        
        <?php if (!empty($assignedProjects)): ?>
        <div class="row mt-3">
            <div class="col-12">
                <strong><?php echo t('label_assigned_projects', 'Assigned Projects'); ?>:</strong>
                <ul>
                    <?php foreach ($assignedProjects as $project): ?>
                    <li><?php echo $project; ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

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