<?php
ob_start();
require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/includes/pagination.php';

if (!canView('meetings')) {
    redirect('index.php');
}

$error = '';
$success = '';

// Handle Actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    $id = $_POST['id'] ?? 0;
    
    if ($action === 'add' || $action === 'edit') {
        if (($action === 'add' && !canAdd('meetings')) || ($action === 'edit' && !canEdit('meetings'))) {
            $error = 'ليس لديك صلاحية للقيام بهذا الإجراء';
        } else {
            $title = $_POST['title'] ?? '';
            $description = $_POST['description'] ?? '';
            $agenda = $_POST['agenda'] ?? '';
            $attendees = $_POST['attendees'] ?? '';
            $absentees = $_POST['absentees'] ?? '';
            $meeting_details = $_POST['meeting_details'] ?? '';
            $start_time = $_POST['start_time'] ?? '';
            $end_time = $_POST['end_time'] ?? '';
            $location = $_POST['location'] ?? '';
            $status = $_POST['status'] ?? 'scheduled';
            
            try {
                $db = db();
                
                if ($action === 'add') {
                    $stmt = $db->prepare("INSERT INTO meetings (title, description, agenda, attendees, absentees, meeting_details, start_time, end_time, location, status, created_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
                    $stmt->execute([$title, $description, $agenda, $attendees, $absentees, $meeting_details, $start_time, $end_time, $location, $status, $_SESSION['user_id']]);
                    $_SESSION['success'] = 'تم جدولة الاجتماع بنجاح';
                } else {
                    $stmt = $db->prepare("UPDATE meetings SET title=?, description=?, agenda=?, attendees=?, absentees=?, meeting_details=?, start_time=?, end_time=?, location=?, status=? WHERE id=?");
                    $stmt->execute([$title, $description, $agenda, $attendees, $absentees, $meeting_details, $start_time, $end_time, $location, $status, $id]);
                    $_SESSION['success'] = 'تم تحديث الاجتماع بنجاح';
                }
                redirect('meetings.php');
            } catch (PDOException $e) {
                $error = 'حدث خطأ: ' . $e->getMessage();
            }
        }
    }
}

if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
    if (!canDelete('meetings')) redirect('meetings.php');
    $id = $_GET['id'];
    $db = db();
    
    $stmt = $db->prepare("DELETE FROM meetings WHERE id = ?");
    $stmt->execute([$id]);
    
    $_SESSION['success'] = 'تم حذف الاجتماع بنجاح';
    redirect('meetings.php');
}

// Fetch Data for List
$date_from = $_GET['date_from'] ?? date('Y-m-01');
$date_to = $_GET['date_to'] ?? date('Y-m-t');
$status_filter = $_GET['status'] ?? '';
$search = $_GET['search'] ?? '';

// Base WHERE conditions
$whereConditions = ["DATE(m.start_time) BETWEEN ? AND ?"];
$params = [$date_from, $date_to];

if ($status_filter) {
    $whereConditions[] = "m.status = ?";
    $params[] = $status_filter;
}
if ($search) {
    $whereConditions[] = "(m.title LIKE ? OR m.description LIKE ? OR m.location LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

$whereClause = implode(' AND ', $whereConditions);

// Pagination
$page = $_GET['page'] ?? 1;
$perPage = 10;

// Count Total Items
$countSql = "SELECT COUNT(*) FROM meetings m WHERE $whereClause";
$countStmt = db()->prepare($countSql);
$countStmt->execute($params);
$totalMeetings = $countStmt->fetchColumn();

$pagination = getPagination($page, $totalMeetings, $perPage);

// Fetch Items with Limit
$sql = "SELECT m.*, u.username as created_by_name 
        FROM meetings m 
        LEFT JOIN users u ON m.created_by = u.id 
        WHERE $whereClause
        ORDER BY m.start_time ASC
        LIMIT ? OFFSET ?";

// Add LIMIT and OFFSET to params
$params[] = $pagination['limit'];
$params[] = $pagination['offset'];

$stmt = db()->prepare($sql);

// Bind params manually because limit/offset must be integers
// But wait, $params is mixed string/int.
// PDO::execute($params) treats all as strings which is fine for limit/offset in MySQL usually,
// but strictly speaking, LIMIT/OFFSET should be ints.
// Let's bind all params.
foreach ($params as $k => $v) {
    // 1-based index
    $type = is_int($v) ? PDO::PARAM_INT : PDO::PARAM_STR;
    $stmt->bindValue($k + 1, $v, $type);
}
$stmt->execute();
$meetings = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (isset($_SESSION['success'])) {
    $success = $_SESSION['success'];
    unset($_SESSION['success']);
}
?>

<!-- Summernote Lite CSS -->
<link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.css" rel="stylesheet">
<style>
    /* Custom Styles for Summernote Integration */
    .note-editor .note-toolbar {
        background: #f8f9fa;
    }
    .note-editable {
        background: #fff;
        min-height: 200px;
    }
</style>

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">إدارة الاجتماعات</h1>
    <?php if (canAdd('meetings')): ?>
    <button type="button" class="btn btn-primary shadow-sm" onclick="openModal('add')">
        <i class="fas fa-plus"></i> جدولة اجتماع جديد
    </button>
    <?php endif; ?>
</div>

<?php if ($success): ?>
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <?= $success ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
<?php endif; ?>

<?php if ($error): ?>
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <?= $error ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
<?php endif; ?>

<!-- Filters -->
<div class="card shadow-sm border-0 mb-4">
    <div class="card-body bg-light">
        <form method="GET" class="row g-3">
            <div class="col-md-3">
                <label class="form-label">من تاريخ</label>
                <input type="date" name="date_from" class="form-control" value="<?= $date_from ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">إلى تاريخ</label>
                <input type="date" name="date_to" class="form-control" value="<?= $date_to ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">الحالة</label>
                <select name="status" class="form-select">
                    <option value="">الكل</option>
                    <option value="scheduled" <?= $status_filter == 'scheduled' ? 'selected' : '' ?>>مجدول</option>
                    <option value="completed" <?= $status_filter == 'completed' ? 'selected' : '' ?>>منتهي</option>
                    <option value="cancelled" <?= $status_filter == 'cancelled' ? 'selected' : '' ?>>ملغي</option>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label">بحث</label>
                <div class="input-group">
                    <input type="text" name="search" class="form-control" placeholder="عنوان، وصف، مكان..." value="<?= htmlspecialchars($search) ?>">
                    <button class="btn btn-primary" type="submit"><i class="fas fa-search"></i></button>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- Table -->
<div class="card shadow-sm border-0">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="bg-light">
                    <tr>
                        <th class="ps-4">العنوان</th>
                        <th>التاريخ والوقت</th>
                        <th>المكان</th>
                        <th>المنظم</th>
                        <th>الحالة</th>
                        <th class="text-secondary text-nowrap" style="font-size: 0.85rem;"><i class="fas fa-user-plus me-1"></i>أضيف بواسطة</th>
                        <th class="text-secondary text-nowrap" style="font-size: 0.85rem;"><i class="fas fa-user-edit me-1"></i>عُدل بواسطة</th>
                        <th class="text-center">الإجراءات</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($meetings)): ?>
                        <tr>
                            <td colspan="8" class="text-center py-5 text-muted">لا توجد اجتماعات مطابقة</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($meetings as $meeting): ?>
                            <?php
                                $status_class = match($meeting['status']) {
                                    'scheduled' => 'bg-primary',
                                    'completed' => 'bg-success',
                                    'cancelled' => 'bg-danger',
                                    default => 'bg-secondary'
                                };
                                $status_text = match($meeting['status']) {
                                    'scheduled' => 'مجدول',
                                    'completed' => 'منتهي',
                                    'cancelled' => 'ملغي',
                                    default => $meeting['status']
                                };
                                // Strip tags for preview, but keep it clean
                                $agenda_preview = strip_tags($meeting['agenda'] ?? '');
                            ?>
                            <tr>
                                <td class="ps-4 fw-bold">
                                    <?= htmlspecialchars($meeting['title']) ?>
                                    <?php if ($agenda_preview): ?>
                                        <div class="small text-muted fw-normal text-truncate" style="max-width: 250px;"><?= htmlspecialchars($agenda_preview) ?></div>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <div><i class="fas fa-calendar-alt text-muted me-1"></i> <?= date('Y-m-d', strtotime($meeting['start_time'])) ?></div>
                                    <div class="small text-muted"><i class="fas fa-clock me-1"></i> <?= date('H:i', strtotime($meeting['start_time'])) ?> - <?= date('H:i', strtotime($meeting['end_time'])) ?></div>
                                </td>
                                <td>
                                    <?php if ($meeting['location']): ?>
                                        <i class="fas fa-map-marker-alt text-danger me-1"></i> <?= htmlspecialchars($meeting['location']) ?>
                                    <?php else: ?>
                                        -
                                    <?php endif; ?>
                                </td>
                                <td><?= htmlspecialchars($meeting['created_by_name']) ?></td>
                                <td><span class="badge <?= $status_class ?>"><?= $status_text ?></span></td>
                                <td><small class="text-muted"><i class="fas fa-user text-primary opacity-50 me-1"></i><?= htmlspecialchars(getAuditUserName($meeting['created_by'] ?? null)) ?></small></td>
                            <td><small class="text-muted"><i class="fas fa-user-edit text-warning opacity-50 me-1"></i><?= htmlspecialchars(getAuditUserName($meeting['updated_by'] ?? null)) ?></small></td>
                            <td class="text-center">
                                    <?php if (canEdit('meetings')): ?>
                                        <button class="btn btn-sm btn-outline-primary" onclick='openModal("edit", <?= json_encode($meeting, JSON_HEX_APOS | JSON_HEX_QUOT) ?>)'>
                                            <i class="fas fa-edit"></i>
                                        </button>
                                    <?php endif; ?>
                                    <a href="print_meeting.php?id=<?= $meeting['id'] ?>" target="_blank" class="btn btn-sm btn-outline-secondary" title="طباعة">
                                        <i class="fas fa-print"></i>
                                    </a>
                                    <?php if (canDelete('meetings')): ?>
                                        <a href="javascript:void(0)" onclick="confirmDelete(<?= $meeting['id'] ?>)" class="btn btn-sm btn-outline-danger">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    <div class="card-footer bg-white">
        <?= renderPagination($pagination['current_page'], $pagination['total_pages']) ?>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="meetingModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title" id="modalTitle">جدولة اجتماع جديد</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="POST" id="meetingForm">
                <div class="modal-body">
                    <input type="hidden" name="action" id="modalAction" value="add">
                    <input type="hidden" name="id" id="modalId" value="0">
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold">عنوان الاجتماع</label>
                        <input type="text" name="title" id="modalTitleInput" class="form-control" required placeholder="مثال: اجتماع الفريق الأسبوعي">
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">وقت البدء</label>
                            <input type="datetime-local" name="start_time" id="modalStartTime" class="form-control" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">وقت الانتهاء</label>
                            <input type="datetime-local" name="end_time" id="modalEndTime" class="form-control" required>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold">الموقع / الرابط</label>
                        <input type="text" name="location" id="modalLocation" class="form-control" placeholder="غرفة الاجتماعات، Zoom، Google Meet...">
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold">جدول الأعمال</label>
                        <textarea name="agenda" id="agenda" class="summernote" placeholder="أدخل بنود جدول الأعمال والمواضيع المطروحة للنقاش هنا..."></textarea>
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">الحضور</label>
                            <textarea name="attendees" id="modalAttendees" class="form-control" rows="2" placeholder="أسماء الحضور..."></textarea>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold">الغياب</label>
                            <textarea name="absentees" id="modalAbsentees" class="form-control" rows="2" placeholder="أسماء الغائبين..."></textarea>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-bold">تفاصيل الاجتماع / المحضر</label>
                        <textarea name="meeting_details" id="meeting_details" class="summernote" placeholder="أدخل تفاصيل محضر الاجتماع، القرارات المتخذة، والنقاط التي تمت مناقشتها..."></textarea>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold"> القرارات والإلتزامات</label>
                        <textarea name="description" id="description" class="summernote" placeholder="القرارات والإلتزامات..."></textarea>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold">الحالة</label>
                        <select name="status" id="modalStatus" class="form-select">
                            <option value="scheduled">مجدول</option>
                            <option value="completed">منتهي</option>
                            <option value="cancelled">ملغي</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
                    <button type="submit" class="btn btn-primary">حفظ</button>
                </div>
            </form>
        </div>
    </div>
</div>

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

<!-- jQuery (required for Summernote) -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<!-- Summernote Lite JS -->
<script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.js"></script>

<script>
$(document).ready(function() {
    // Initialize Summernote
    $('.summernote').each(function() {
        $(this).summernote({
            placeholder: $(this).attr('placeholder') || 'أدخل التفاصيل هنا...',
            tabsize: 2,
            height: 200,
            toolbar: [
              ['style', ['style']],
              ['font', ['bold', 'underline', 'clear', 'strikethrough', 'superscript', 'subscript']],
              ['fontname', ['fontname']],
              ['color', ['color']],
              ['para', ['ul', 'ol', 'paragraph']],
              ['table', ['table']],
              ['insert', ['link', 'picture', 'video', 'hr']],
              ['view', ['fullscreen', 'codeview', 'help']]
            ],
            // Ensure RTL support if needed (Summernote usually auto-detects or needs plugin, but 'dir=rtl' on body often helps)
        });
    });
});

let meetingModal;

function openModal(action, data = null) {
    if (!meetingModal) {
        meetingModal = new bootstrap.Modal(document.getElementById('meetingModal'));
    }
    
    document.getElementById('modalAction').value = action;
    const title = document.getElementById('modalTitle');
    
    // Reset Summernote content first
    $('.summernote').summernote('code', '');

    if (action === 'add') {
        title.textContent = 'جدولة اجتماع جديد';
        document.getElementById('modalId').value = 0;
        document.getElementById('modalTitleInput').value = '';
        document.getElementById('modalAttendees').value = '';
        document.getElementById('modalAbsentees').value = '';
        document.getElementById('modalLocation').value = '';
        
        // Default start time: Next hour, :00
        const now = new Date();
        now.setMinutes(0, 0, 0);
        now.setHours(now.getHours() + 1);
        
        const formatDateTime = (date) => {
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            return `${year}-${month}-${day}T${hours}:${minutes}`;
        };
        
        document.getElementById('modalStartTime').value = formatDateTime(now);
        
        const end = new Date(now);
        end.setHours(end.getHours() + 1);
        document.getElementById('modalEndTime').value = formatDateTime(end);
        
        document.getElementById('modalStatus').value = 'scheduled';
    } else {
        title.textContent = 'تعديل الاجتماع';
        document.getElementById('modalId').value = data.id;
        document.getElementById('modalTitleInput').value = data.title;
        document.getElementById('modalAttendees').value = data.attendees || '';
        document.getElementById('modalAbsentees').value = data.absentees || '';
        document.getElementById('modalLocation').value = data.location || '';
        document.getElementById('modalStartTime').value = data.start_time.replace(' ', 'T').slice(0, 16);
        document.getElementById('modalEndTime').value = data.end_time.replace(' ', 'T').slice(0, 16);
        document.getElementById('modalStatus').value = data.status;

        // Set Summernote content
        $('#agenda').summernote('code', data.agenda || '');
        $('#meeting_details').summernote('code', data.meeting_details || '');
        $('#description').summernote('code', data.description || '');
    }
    
    meetingModal.show();
}

function confirmDelete(id) {
    if (confirm('هل أنت متأكد من حذف هذا الاجتماع؟')) {
        window.location.href = 'meetings.php?action=delete&id=' + id;
    }
}
</script>