<?php
// Variables: $meeting (null=create, array=edit), $teamMembers, $unreadTotal
$isEdit    = !is_null($meeting ?? null);
$csrfToken = \Core\System\Csrf::token();
$action    = $isEdit ? "/admin/team/meetings/update/{$meeting['id']}" : '/admin/team/meetings/store';

// Current attendees IDs
$currentAttendeeIds = [];
if ($isEdit && !empty($meeting['attendees'])) {
    $currentAttendeeIds = array_column($meeting['attendees'], 'admin_id');
}

include __DIR__ . '/../header.php';
?>
<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

<div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
    <div>
        <h3 class="mb-1 fw-bold">
            <i class="bi bi-<?= $isEdit ? 'pencil-square' : 'calendar-plus' ?> me-2 text-primary"></i>
            <?= $isEdit ? __('team_meeting.edit','Edit Meeting') : __('team_meeting.create','Schedule Meeting') ?>
        </h3>
        <p class="text-muted mb-0 small">
            <?= $isEdit ? __('team_meeting.edit_subtitle','Update meeting details and attendees') : __('team_meeting.create_subtitle','Schedule a new team meeting') ?>
        </p>
    </div>
    <a href="/admin/team/meetings" class="btn btn-outline-secondary btn-sm">
        <i class="bi bi-arrow-left me-1"></i><?= __('sidebar.team_meetings','Meetings') ?>
    </a>
</div>

<div class="row justify-content-center">
    <div class="col-lg-7">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white border-bottom py-2">
                <span class="fw-semibold small">
                    <i class="bi bi-<?= $isEdit ? 'pencil' : 'plus-circle' ?> me-2 text-primary"></i>
                    <?= $isEdit ? __('team_meeting.edit','Edit Meeting') : __('team_meeting.create','Schedule Meeting') ?>
                </span>
            </div>
            <div class="card-body">
                    <form method="POST" action="<?= $action ?>">
                        <input type="hidden" name="_csrf" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES) ?>">

                        <div class="mb-3">
                            <label class="form-label small fw-semibold"><?= __('team_meeting.title','Title') ?> <span class="text-danger">*</span></label>
                            <input type="text" name="title" class="form-control form-control-sm"
                                   value="<?= htmlspecialchars($meeting['title'] ?? '', ENT_QUOTES) ?>" required maxlength="255">
                        </div>

                        <div class="row g-3 mb-3">
                            <div class="col-md-6">
                                <label class="form-label small fw-semibold"><?= __('team_meeting.type','Type') ?></label>
                                <select name="meeting_type" class="form-select form-select-sm" id="meetingTypeSelect">
                                    <?php foreach (['standup','review','training','planning','group_chat','other'] as $t): ?>
                                    <option value="<?= $t ?>" <?= ($meeting['meeting_type'] ?? 'other') === $t ? 'selected' : '' ?>>
                                        <?= __('team_meeting.type_'.$t, ucfirst(str_replace('_',' ',$t))) ?>
                                    </option>
                                    <?php endforeach; ?>
                                </select>
                                <div id="groupChatNote" class="small text-success mt-1 <?= ($meeting['meeting_type'] ?? '') === 'group_chat' ? '' : 'd-none' ?>">
                                    <i class="bi bi-info-circle me-1"></i><?= __('team_meeting.group_chat_note','Attendees will have a shared group chat') ?>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label small fw-semibold"><?= __('team_meeting.duration','Duration (minutes)') ?></label>
                                <input type="number" name="duration_minutes" class="form-control form-control-sm"
                                       value="<?= (int)($meeting['duration_minutes'] ?? 60) ?>" min="5" max="480">
                            </div>
                        </div>

                        <div class="row g-3 mb-3">
                            <div class="col-md-6">
                                <label class="form-label small fw-semibold"><?= __('team_meeting.scheduled','Scheduled At') ?> <span class="text-danger">*</span></label>
                                <input type="datetime-local" name="scheduled_at" class="form-control form-control-sm"
                                       value="<?= !empty($meeting['scheduled_at']) ? app_format_datetime_input($meeting['scheduled_at']) : '' ?>" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label small fw-semibold"><?= __('team_meeting.location','Location') ?></label>
                                <input type="text" name="location" class="form-control form-control-sm"
                                       value="<?= htmlspecialchars($meeting['location'] ?? '', ENT_QUOTES) ?>" maxlength="255"
                                       placeholder="<?= htmlspecialchars(__('team_meeting.location_placeholder','e.g. Meeting Room, Zoom link...'), ENT_QUOTES) ?>">
                            </div>
                        </div>

                        <?php if ($isEdit): ?>
                        <div class="mb-3">
                            <label class="form-label small fw-semibold"><?= __('team_meeting.status','Status') ?></label>
                            <select name="status" class="form-select form-select-sm">
                                <?php foreach (['upcoming','completed','cancelled'] as $s): ?>
                                <option value="<?= $s ?>" <?= ($meeting['status'] ?? 'upcoming') === $s ? 'selected' : '' ?>>
                                    <?= __('team_meeting.status_'.$s, ucfirst($s)) ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <?php endif; ?>

                        <div class="mb-3">
                            <label class="form-label small fw-semibold"><?= __('team_meeting.description','Description') ?></label>
                            <textarea name="description" class="form-control form-control-sm" rows="3"><?= htmlspecialchars($meeting['description'] ?? '', ENT_QUOTES) ?></textarea>
                        </div>

                        <!-- Reminder -->
                        <div class="mb-3">
                            <label class="form-label small fw-semibold">
                                <i class="bi bi-bell me-1 text-warning"></i>
                                <?= __('team_meeting.reminder_minutes','Reminder Before (minutes)') ?>
                            </label>
                            <input type="number" name="reminder_minutes" class="form-control form-control-sm"
                                   value="<?= (int)($meeting['reminder_minutes'] ?? 0) ?>" min="0" max="1440">
                            <div class="small text-muted mt-1">
                                <i class="bi bi-info-circle me-1"></i><?= __('team_meeting.reminder_note','Set to 0 to disable. Sends email and WhatsApp to attendees who have phone numbers.') ?>
                            </div>
                        </div>

                        <!-- Attendees -->
                        <div class="mb-3">
                            <label class="form-label small fw-semibold"><?= __('team_meeting.attendees','Attendees') ?></label>
                            <div class="border rounded p-2" style="max-height:180px;overflow-y:auto">
                                <?php foreach ($teamMembers as $member): ?>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" name="attendees[]"
                                           value="<?= $member['id'] ?>"
                                           id="att_<?= $member['id'] ?>"
                                           <?= in_array((string)$member['id'], array_map('strval', $currentAttendeeIds)) ? 'checked' : '' ?>>
                                    <label class="form-check-label small" for="att_<?= $member['id'] ?>">
                                        <?= htmlspecialchars($member['username']) ?>
                                        <span class="text-muted ms-1">(<?= __('team.role_'.$member['role'], $member['role']) ?>)</span>
                                    </label>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        </div>

                        <div class="d-flex gap-2 justify-content-end">
                            <a href="/admin/team/meetings" class="btn btn-sm btn-outline-secondary">
                                <?= __('team_comm.cancel','Cancel') ?>
                            </a>
                            <button type="submit" class="btn btn-sm btn-primary">
                                <i class="bi bi-check2 me-1"></i>
                                <?= $isEdit ? __('team_meeting.update','Update Meeting') : __('team_meeting.save','Schedule Meeting') ?>
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

</div>
</section>
</div>

<script>
document.getElementById('meetingTypeSelect').addEventListener('change', function () {
    const note = document.getElementById('groupChatNote');
    if (this.value === 'group_chat') note.classList.remove('d-none');
    else note.classList.add('d-none');
});
</script>
<?php include __DIR__ . '/../footer.php'; ?>
