<?php
// teacher/test-ticket.php - تست ارسال تیکت

session_start();
require_once 'config/database.php';
require_once 'includes/db.php';
require_once 'includes/auth.php';
require_once 'includes/functions.php';

if (!isLoggedIn()) {
    die('لطفاً وارد شوید.');
}

$user = getCurrentUser();
$teachers = getAllTeachers();
$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $receiver_id = $_POST['receiver_id'] ?? 0;
    $subject = $_POST['subject'] ?? '';
    $body = $_POST['body'] ?? '';
    
    if ($receiver_id > 0 && !empty($subject) && !empty($body)) {
        $data = [
            'sender_id' => $user['id'],
            'receiver_id' => $receiver_id,
            'receiver_role_id' => 6,
            'subject' => $subject,
            'body' => $body,
            'status' => 'new'
        ];
        $result = insertData('tickets', $data);
        if ($result) {
            $message = '<div style="color:green;padding:15px;border:2px solid green;border-radius:8px;">✅ تیکت با موفقیت ارسال شد! (ID: ' . $result . ')</div>';
        } else {
            $message = '<div style="color:red;padding:15px;border:2px solid red;border-radius:8px;">❌ خطا در ارسال تیکت</div>';
        }
    } else {
        $message = '<div style="color:red;padding:15px;border:2px solid red;border-radius:8px;">❌ اطلاعات ناقص است.</div>';
    }
}
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>تست ارسال تیکت</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-6">
    <div class="max-w-lg mx-auto bg-white p-6 rounded-2xl shadow-lg">
        <h2 class="text-2xl font-bold text-center mb-4">🧪 تست ارسال تیکت</h2>
        <p class="text-gray-600 text-center mb-4">کاربر: <?= htmlspecialchars($user['name'] . ' ' . $user['family']) ?></p>
        
        <?= $message ?>
        
        <form method="POST" class="space-y-4">
            <div>
                <label class="block font-bold mb-1">ارسال به (مدرس):</label>
                <select name="receiver_id" class="w-full p-2 border rounded-lg" required>
                    <option value="">انتخاب مدرس...</option>
                    <?php foreach ($teachers as $teacher): ?>
                    <option value="<?= $teacher['id'] ?>">
                        <?= htmlspecialchars($teacher['name'] . ' ' . $teacher['family']) ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            
            <div>
                <label class="block font-bold mb-1">موضوع:</label>
                <input type="text" name="subject" class="w-full p-2 border rounded-lg" required>
            </div>
            
            <div>
                <label class="block font-bold mb-1">متن:</label>
                <textarea name="body" rows="4" class="w-full p-2 border rounded-lg" required></textarea>
            </div>
            
            <button type="submit" class="w-full bg-blue-600 text-white py-2 rounded-lg hover:bg-blue-700">
                ارسال تیکت
            </button>
        </form>
        
        <div class="mt-4 text-center text-sm text-gray-500">
            <a href="tickets.php" class="text-blue-600 hover:underline">↩ بازگشت به صفحه تیکت‌ها</a>
        </div>
    </div>
</body>
</html>