<?php
// messages-content.php - محتوای پیام‌ها
$roles = getAllRoles();
$teachers = getAllTeachers();
?>

<?php if (isset($message)): ?>
<div class="alert alert-<?= strpos($message, 'خطا') !== false ? 'danger' : 'success' ?> fade-in">
    <i class="fas <?= strpos($message, 'خطا') !== false ? 'fa-exclamation-circle' : 'fa-check-circle' ?>"></i>
    <?= $message ?>
</div>
<?php endif; ?>

<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
    <div class="bg-gray-50 rounded-xl p-6">
        <h4 class="font-bold text-gray-700 mb-4 flex items-center gap-2">
            <i class="fas fa-paper-plane text-blue-500"></i>
            ارسال پیام جدید
        </h4>
        <form method="POST" action="">
            <div class="form-group">
                <label>ارسال به:</label>
                <select name="receiver_type" id="receiverType" class="w-full px-3 py-2 border border-gray-300 rounded-lg" onchange="toggleReceiver()">
                    <option value="role">یک نقش</option>
                    <option value="user">یک نفر خاص</option>
                </select>
            </div>
            
            <div class="form-group" id="roleSelect">
                <label>نقش گیرنده</label>
                <select name="receiver_role_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg">
                    <option value="">انتخاب نقش...</option>
                    <?php foreach ($roles as $role): ?>
                    <option value="<?= $role['id'] ?>"><?= htmlspecialchars($role['name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            
            <div class="form-group" id="userSelect" style="display:none;">
                <label>کاربر گیرنده</label>
                <select name="receiver_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg">
                    <option value="">انتخاب کاربر...</option>
                    <?php foreach ($teachers as $teacher): ?>
                    <option value="<?= $teacher['id'] ?>"><?= htmlspecialchars($teacher['name'] . ' ' . $teacher['family']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            
            <div class="form-group">
                <label>موضوع</label>
                <input type="text" name="subject" placeholder="موضوع پیام" required>
            </div>
            
            <div class="form-group">
                <label>متن پیام</label>
                <textarea name="body" rows="4" placeholder="متن پیام را وارد کنید..." required></textarea>
            </div>
            
            <button type="submit" class="btn btn-primary w-full">
                <i class="fas fa-send"></i> ارسال پیام
            </button>
        </form>
    </div>
    
    <div>
        <h4 class="font-bold text-gray-700 mb-4 flex items-center gap-2">
            <i class="fas fa-inbox text-purple-500"></i>
            پیام‌های دریافتی
        </h4>
        
        <?php if (empty($messages)): ?>
        <p class="text-gray-400 text-center py-8">هیچ پیامی وجود ندارد</p>
        <?php else: ?>
        <div class="space-y-3 max-h-[500px] overflow-y-auto">
            <?php foreach ($messages as $msg): ?>
            <div class="bg-white rounded-xl p-4 shadow-sm border <?= $msg['is_read'] ? 'border-gray-200' : 'border-blue-300' ?>">
                <div class="flex justify-between items-start">
                    <div>
                        <div class="font-medium text-gray-800">
                            <?= htmlspecialchars($msg['subject']) ?>
                        </div>
                        <div class="text-sm text-gray-500">
                            از: <?= htmlspecialchars($msg['sender_name'] . ' ' . $msg['sender_family']) ?>
                        </div>
                        <div class="text-sm text-gray-500 mt-1">
                            <?= htmlspecialchars($msg['body']) ?>
                        </div>
                    </div>
                    <div class="text-left text-xs text-gray-400">
                        <?= jalaliDate($msg['created_at']) ?>
                        <?php if (!$msg['is_read']): ?>
                        <span class="badge-info">جدید</span>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>
</div>

<script>
function toggleReceiver() {
    var type = document.getElementById('receiverType').value;
    document.getElementById('roleSelect').style.display = (type == 'role') ? 'block' : 'none';
    document.getElementById('userSelect').style.display = (type == 'user') ? 'block' : 'none';
}
</script>