<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

club_module_boot();

$clubRecord = find_club_or_abort((int) ($_GET['id'] ?? 0));
$staffList = db_fetch_all('SELECT id, full_name, role, status FROM club_staff WHERE club_id = :club_id ORDER BY created_at DESC', ['club_id' => $clubRecord['id']]);
$pageTitle = 'View Club';

require dirname(__DIR__, 2) . '/includes/header.php';
require dirname(__DIR__, 2) . '/includes/sidebar.php';
require dirname(__DIR__, 2) . '/includes/topbar.php';
require dirname(__DIR__, 2) . '/includes/flash.php';
?>
<section class="panel-card mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Club profile</p>
            <h2 class="h4 mb-0"><?php echo e($clubRecord['name']); ?></h2>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/clubs/edit.php?id=' . (int) $clubRecord['id'])); ?>">Edit</a>
            <?php if (!is_club_admin()): ?>
                <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/clubs/delete.php?id=' . (int) $clubRecord['id'])); ?>">Delete</a>
            <?php endif; ?>
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/players/index.php?club_id=' . (int) $clubRecord['id'])); ?>">Manage players</a>
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/staff/index.php?club_id=' . (int) $clubRecord['id'])); ?>">Manage staff</a>
            <?php if (can('manage_finance')): ?>
                <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/finance/index.php?club_id=' . (int) $clubRecord['id'])); ?>">Club finance</a>
            <?php endif; ?>
        </div>
    </div>
    <div class="row g-3">
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-shield-check"></i>
                <div>
                    <h4>Short name</h4>
                    <p><?php echo e($clubRecord['short_name']); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-calendar3"></i>
                <div>
                    <h4>Founded</h4>
                    <p><?php echo e($clubRecord['founded_year'] ?: 'Not set'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-building"></i>
                <div>
                    <h4>Stadium</h4>
                    <p><?php echo e($clubRecord['stadium_name'] ?: 'Not set'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-person"></i>
                <div>
                    <h4>Contact person</h4>
                    <p><?php echo e($clubRecord['contact_person'] ?: 'Not set'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-telephone"></i>
                <div>
                    <h4>Phone</h4>
                    <p><?php echo e($clubRecord['phone'] ?: 'Not set'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-envelope"></i>
                <div>
                    <h4>Email</h4>
                    <p><?php echo e($clubRecord['email'] ?: 'Not set'); ?></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-patch-check"></i>
                <div>
                    <h4>License</h4>
                    <p><span class="badge <?php echo e(club_badge_class($clubRecord['license_status'])); ?>"><?php echo e(ucfirst($clubRecord['license_status'])); ?></span></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-cash-stack"></i>
                <div>
                    <h4>Payment</h4>
                    <p><span class="badge <?php echo e(club_badge_class($clubRecord['payment_status'])); ?>"><?php echo e(ucfirst($clubRecord['payment_status'])); ?></span></p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="feature-tile"><i class="bi bi-flag"></i>
                <div>
                    <h4>Status</h4>
                    <p><span class="badge <?php echo e(club_badge_class($clubRecord['status'])); ?>"><?php echo e(ucfirst($clubRecord['status'])); ?></span></p>
                </div>
            </div>
        </div>
    </div>
</section>

<?php if (!is_club_admin() && can('approve_clubs')): ?>
    <section class="panel-card mb-4">
        <div class="panel-card-header mb-3">
            <div>
                <p class="eyebrow mb-1">Licensing workflow</p>
                <h3 class="h5 mb-0">Approval controls</h3>
            </div>
        </div>
        <div class="d-flex flex-wrap gap-2">
            <?php foreach (club_license_options() as $licenseStatus): ?>
                <form method="post" action="<?php echo e(base_url('app/modules/clubs/actions.php')); ?>">
                    <?php echo csrf_input(); ?>
                    <input type="hidden" name="action" value="set_license_status">
                    <input type="hidden" name="id" value="<?php echo e((string) $clubRecord['id']); ?>">
                    <input type="hidden" name="license_status" value="<?php echo e($licenseStatus); ?>">
                    <button class="btn btn-outline-jlfc" type="submit"><?php echo e(ucfirst($licenseStatus)); ?></button>
                </form>
            <?php endforeach; ?>
        </div>
        <div class="mt-3 small text-muted">Fixture eligibility: <?php echo club_is_fixture_eligible($clubRecord) ? 'eligible for scheduling' : 'not eligible for scheduling'; ?>.</div>
    </section>
<?php endif; ?>

<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Club staff</p>
            <h3 class="h5 mb-0">Registered staff members</h3>
        </div>
    </div>
    <?php if ($staffList === []): ?>
        <p class="text-muted mb-0">No staff records added yet.</p>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Role</th>
                        <th>Status</th>
                        <th class="text-end">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($staffList as $staff): ?>
                        <tr>
                            <td><?php echo e($staff['full_name']); ?></td>
                            <td><?php echo e(ucwords(str_replace('_', ' ', $staff['role']))); ?></td>
                            <td><?php echo e(ucfirst($staff['status'])); ?></td>
                            <td class="text-end"><a class="btn btn-outline-secondary btn-sm" href="<?php echo e(base_url('app/modules/staff/view.php?id=' . (int) $staff['id'])); ?>">View</a></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>