<?php
// includes/functions.php - توابع کمکی

header('Content-Type: text/html; charset=UTF-8');

function persianNumber($number) {
    $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
    $english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    return str_replace($english, $persian, $number);
}

function formatPrice($amount) {
    return number_format($amount) . ' تومان';
}

function getShebaStatus($isConfirmed) {
    if ($isConfirmed) {
        return '<span class="badge-success"><i class="fas fa-check-circle"></i> تأیید شده</span>';
    }
    return '<span class="badge-warning"><i class="fas fa-clock"></i> در انتظار تأیید</span>';
}

function getStatusBadge($status) {
    if ($status) {
        return '<span class="badge-success">فعال</span>';
    }
    return '<span class="badge-danger">غیرفعال</span>';
}

function getTermName($termId) {
    if (empty($termId)) return '-';
    $term = fetchOne("SELECT name FROM terms WHERE id = ?", [$termId]);
    return $term ? $term['name'] : '-';
}

function getTotalTeachers() {
    return fetchCount("SELECT id FROM users WHERE role_id = (SELECT id FROM roles WHERE level = 6)");
}

function getTeachersWithSheba() {
    return fetchCount("SELECT id FROM users WHERE role_id = (SELECT id FROM roles WHERE level = 6) AND sheba_number IS NOT NULL AND sheba_number != '' AND is_sheba_confirmed = 1");
}

function getTeachersWithoutSheba() {
    return fetchCount("SELECT id FROM users WHERE role_id = (SELECT id FROM roles WHERE level = 6) AND (sheba_number IS NULL OR sheba_number = '')");
}

function getPendingSheba() {
    return fetchCount("SELECT id FROM users WHERE role_id = (SELECT id FROM roles WHERE level = 6) AND sheba_number IS NOT NULL AND sheba_number != '' AND is_sheba_confirmed = 0");
}

function getRecentLogins($limit = 10) {
    return fetchAll(
        "SELECT u.*, r.name as role_name 
         FROM users u 
         LEFT JOIN roles r ON u.role_id = r.id 
         WHERE u.last_login IS NOT NULL 
         ORDER BY u.last_login DESC 
         LIMIT ?",
        [$limit]
    );
}

function getAllTeachers($limit = null, $offset = 0) {
    $sql = "SELECT u.*, r.name as role_name 
            FROM users u 
            LEFT JOIN roles r ON u.role_id = r.id 
            WHERE u.role_id = (SELECT id FROM roles WHERE level = 6)";
    if ($limit) {
        $sql .= " LIMIT $offset, $limit";
    }
    return fetchAll($sql);
}

function getTeacherCourses($teacherId) {
    return fetchAll(
        "SELECT * FROM courses WHERE teacher_id = ? ORDER BY created_at DESC",
        [$teacherId]
    );
}

function getTeacherTransactions($teacherId) {
    return fetchAll(
        "SELECT * FROM transactions WHERE teacher_id = ? ORDER BY payment_date DESC",
        [$teacherId]
    );
}

function getAllRoles() {
    return fetchAll("SELECT * FROM roles ORDER BY level ASC");
}

function getRolePermissions($roleId) {
    return fetchAll(
        "SELECT * FROM permissions WHERE role_id = ? ORDER BY permission_name",
        [$roleId]
    );
}

function getAllTerms() {
    return fetchAll("SELECT * FROM terms ORDER BY id DESC");
}

function getActiveTerm() {
    return fetchOne("SELECT * FROM terms WHERE is_active = 1 LIMIT 1");
}

function updateTeacherSheba($teacherId, $shebaNumber) {
    return updateData(
        'users',
        ['sheba_number' => $shebaNumber, 'is_sheba_confirmed' => 0],
        'id = :id',
        ['id' => $teacherId]
    );
}

function confirmTeacherSheba($teacherId) {
    return updateData(
        'users',
        ['is_sheba_confirmed' => 1],
        'id = :id',
        ['id' => $teacherId]
    );
}

function updateTeacher($teacherId, $data) {
    return updateData('users', $data, 'id = :id', ['id' => $teacherId]);
}

function deleteTeacher($teacherId) {
    return updateData('users', ['is_active' => 0], 'id = :id', ['id' => $teacherId]);
}

function createCourse($data) {
    return insertData('courses', $data);
}

function updateCourse($courseId, $data) {
    return updateData('courses', $data, 'id = :id', ['id' => $courseId]);
}

function deleteCourse($courseId) {
    return deleteData('courses', 'id = :id', ['id' => $courseId]);
}

function createTransaction($data) {
    return insertData('transactions', $data);
}

function updateTransaction($transactionId, $data) {
    return updateData('transactions', $data, 'id = :id', ['id' => $transactionId]);
}

function getTicketStats() {
    return [
        'total' => fetchCount("SELECT id FROM tickets"),
        'new' => fetchCount("SELECT id FROM tickets WHERE status = 'new'"),
        'replied' => fetchCount("SELECT id FROM tickets WHERE status = 'replied'"),
        'closed' => fetchCount("SELECT id FROM tickets WHERE status = 'closed'"),
    ];
}
?>