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

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

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

// Get related records count
$dozarCount = $pdo->prepare("SELECT COUNT(*) FROM dozar_records WHERE group_id = ?");
$dozarCount->execute([$id]);
$dozarCount = $dozarCount->fetchColumn();

$tripperCount = $pdo->prepare("SELECT COUNT(*) FROM tripper_records WHERE group_id = ?");
$tripperCount->execute([$id]);
$tripperCount = $tripperCount->fetchColumn();

$pageTitle = t('label_group', 'Group 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-users"></i> <?php echo t('label_group', 'Group 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>

<!-- Group 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', 'Group 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_group', 'Group Name'); ?>:</strong></td>
                        <td><?php echo $group['group_name']; ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php echo t('label_description', 'Description'); ?>:</strong></td>
                        <td><?php echo $group['description'] ?? '-'; ?></td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <table class="table table-borderless">
                    <tr>
                        <td><strong><?php echo t('label_status', 'Status'); ?>:</strong></td>
                        <td>
                            <span class="badge bg-<?php echo $group['is_active'] ? 'success' : 'danger'; ?>">
                                <?php echo $group['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($group['created_at'])); ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Usage Statistics -->
<div class="card mb-4">
    <div class="card-header bg-info text-white">
        <h5 class="mb-0"><i class="fas fa-chart-bar"></i> <?php echo t('report_usage_stats', 'Usage Statistics'); ?></h5>
    </div>
    <div class="card-body">
        <div class="row text-center">
            <div class="col-md-6">
                <h3><?php echo $dozarCount; ?></h3>
                <p class="text-muted"><?php echo t('nav_dozar', 'Dozar'); ?> Records</p>
            </div>
            <div class="col-md-6">
                <h3><?php echo $tripperCount; ?></h3>
                <p class="text-muted"><?php echo t('nav_tripper', 'Tripper'); ?> Records</p>
            </div>
        </div>
    </div>
</div>

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