Files
front_base_nuxt/app/pages/attendance.vue
T
2026-11-05 18:50:57 +03:30

723 lines
34 KiB
Vue
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="space-y-8 pb-16">
<!-- Top Bar: Title, Live Date & Role Switcher -->
<div class="flex flex-col lg:flex-row lg:items-center justify-between gap-4 bg-white dark:bg-stone-900 border border-stone-200 dark:border-stone-800 rounded-3xl p-5 sm:p-6 shadow-sm">
<div class="space-y-1">
<div class="inline-flex items-center gap-2 px-3 py-1 rounded-full text-xs font-bold bg-primary-100/80 dark:bg-primary-950/60 text-primary-800 dark:text-primary-300">
<UIcon name="i-lucide-clock" class="w-3.5 h-3.5 text-primary-600" />
<span>{{ todayShamsi }}</span>
<span class="text-stone-300 dark:text-stone-700">•</span>
<span class="font-mono font-bold">{{ currentTimeStr }}</span>
</div>
<h1 class="text-2xl sm:text-3xl font-black text-stone-900 dark:text-white tracking-tight">
سامانه ثبت و نظارت حضور و غیاب طلاب
</h1>
<p class="text-xs sm:text-sm text-stone-500 dark:text-stone-400">
حوزه علمیه سفیران هدایت — ثبت بهنگام جلسات درسی، محاسبه هوشمند تاخیرات و پایش آموزشی
</p>
</div>
<div class="flex items-center gap-3">
<RoleSwitcher />
</div>
</div>
<!-- Navigation Breadcrumb & Back to Home -->
<div class="flex items-center justify-between border-b border-stone-200 dark:border-stone-800 pb-3">
<div class="flex items-center gap-2">
<NuxtLink
to="/"
class="px-3.5 py-1.5 rounded-xl text-xs sm:text-sm font-bold text-stone-700 dark:text-stone-200 hover:bg-stone-100 dark:hover:bg-stone-800 transition flex items-center gap-1.5 border border-stone-200 dark:border-stone-800"
>
<UIcon name="i-lucide-arrow-right" class="w-4 h-4 text-primary-500" />
<span>بازگشت به صفحه اصلی</span>
</NuxtLink>
<div class="h-4 w-px bg-stone-200 dark:bg-stone-800 mx-1"></div>
<span class="px-3 py-1.5 rounded-xl text-xs font-extrabold bg-primary-500/10 text-primary-600 dark:text-primary-400 border border-primary-500/20 flex items-center gap-1.5">
<UIcon name="i-lucide-clipboard-check" class="w-4 h-4" />
<span>سامانه حضور و غیاب امروز</span>
</span>
</div>
<div class="text-xs text-stone-400 font-medium hidden sm:block">
نقش فعال: <strong class="text-primary-600">{{ roleLabels[currentRole] }}</strong>
</div>
</div>
<!-- ============================================================= -->
<!-- ۱. نمای طلبه (STUDENT VIEW) -->
<!-- ============================================================= -->
<div v-if="currentRole === 'student'" class="space-y-6">
<div class="flex items-center justify-between">
<div>
<h2 class="text-lg font-extrabold text-stone-900 dark:text-white">کلاس‌های امروز شما</h2>
<p class="text-xs text-stone-500 dark:text-stone-400">برای ثبت حضور در هر کلاس روی دکمه مربوطه کلیک کنید</p>
</div>
</div>
<!-- Grid of Today Classes -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<div
v-for="c in todayStudentClasses"
:key="c.id"
class="p-6 rounded-3xl bg-white dark:bg-stone-900 border border-stone-200 dark:border-stone-800 shadow-sm flex flex-col justify-between space-y-4 hover:border-primary-500/50 transition group"
>
<div class="space-y-3">
<div class="flex items-center justify-between">
<span class="text-[11px] font-mono px-2 py-0.5 rounded bg-stone-100 dark:bg-stone-800 text-stone-600 dark:text-stone-300">
{{ c.code }}
</span>
<UiBadge :status="getStudentStatusForClass(c.id).status" :label="getStudentStatusForClass(c.id).label" />
</div>
<h3 class="text-base font-bold text-stone-900 dark:text-white leading-snug">
{{ c.title }}
</h3>
<div class="space-y-1.5 text-xs text-stone-600 dark:text-stone-300">
<div class="flex items-center gap-2">
<UIcon name="i-lucide-user" class="w-4 h-4 text-primary-500" />
<span>{{ c.teacherName }}</span>
</div>
<div class="flex items-center gap-2">
<UIcon name="i-lucide-clock" class="w-4 h-4 text-stone-400" />
<span>ساعت: <strong>{{ c.time }}</strong></span>
</div>
<div class="flex items-center gap-2">
<UIcon name="i-lucide-map-pin" class="w-4 h-4 text-stone-400" />
<span>مکان: {{ c.room }}</span>
</div>
</div>
<div v-if="c.currentSessionTopic" class="p-2.5 rounded-xl bg-stone-50 dark:bg-stone-800/60 text-[11px] text-stone-600 dark:text-stone-300 border border-stone-100 dark:border-stone-700/60">
<span class="font-bold text-primary-600">موضوع جلسه:</span> {{ c.currentSessionTopic }}
</div>
</div>
<!-- Action Button -->
<div class="pt-3 border-t border-stone-100 dark:border-stone-800">
<UButton
v-if="getStudentStatusForClass(c.id).status === 'absent'"
block
color="primary"
variant="solid"
size="md"
label="ثبت حضور در کلاس"
icon="i-lucide-check-circle-2"
@click="openStudentAttendanceModal(c)"
/>
<div v-else class="flex items-center justify-between text-xs text-stone-500">
<span class="flex items-center gap-1.5 text-emerald-600 dark:text-emerald-400 font-bold">
<UIcon name="i-lucide-check-check" class="w-4 h-4" />
حضور شما ثبت شده است
</span>
<button
class="text-xs text-primary-600 hover:underline"
@click="openStudentAttendanceModal(c)"
>
مشاهده یا ویرایش
</button>
</div>
</div>
</div>
</div>
<!-- Modal: Student Attendance Submission -->
<AppModal
v-model="isStudentModalOpen"
title="ثبت حضور در کلاس درس"
description="سامانه هوشمند حضور و غیاب حوزه علمیه سفیران هدایت"
size="md"
>
<div v-if="selectedClassForStudent" class="space-y-4 text-xs">
<!-- Class Summary Box -->
<div class="p-4 rounded-2xl bg-stone-50 dark:bg-stone-800/60 border border-stone-200 dark:border-stone-700 space-y-2">
<div class="text-sm font-bold text-stone-900 dark:text-white">{{ selectedClassForStudent.title }}</div>
<div class="grid grid-cols-2 gap-2 text-stone-600 dark:text-stone-300">
<div>استاد: <strong>{{ selectedClassForStudent.teacherName }}</strong></div>
<div>مکان: <strong>{{ selectedClassForStudent.room }}</strong></div>
<div>ساعت شروع: <strong class="font-mono text-primary-600">{{ selectedClassForStudent.startTime }}</strong></div>
<div>ساعت فعلی: <strong class="font-mono">{{ currentTimeStr }}</strong></div>
</div>
</div>
<!-- Delay Warning & Required Reason -->
<div v-if="studentDelayCalculation.isLate" class="p-4 rounded-2xl bg-amber-50 dark:bg-amber-950/40 border border-amber-300 dark:border-amber-800 space-y-3">
<div class="flex items-center gap-2 text-amber-800 dark:text-amber-200 font-bold">
<UIcon name="i-lucide-alert-triangle" class="w-5 h-5 text-amber-600" />
<span>محاسبه خودکار سیستم: {{ studentDelayCalculation.minutes }} دقیقه تاخیر نسبت به ساعت شروع</span>
</div>
<p class="text-[11px] text-amber-700 dark:text-amber-300 leading-relaxed">
با توجه به قوانین آموزشی حوزه، برای ثبت حضور با تاخیر باید **علت موجه تاخیر** خود را ثبت فرمایید:
</p>
<div>
<label class="block text-xs font-bold text-stone-800 dark:text-stone-200 mb-1">
علت تاخیر <span class="text-rose-500">* (الزامی)</span>:
</label>
<textarea
v-model="studentDelayReason"
rows="3"
placeholder="لطفاً دلیل تاخیر خود را به طور شفاف توضیح دهید (حداقل ۵ کاراکتر)..."
class="w-full px-3 py-2 text-xs rounded-xl border border-amber-300 dark:border-amber-700 bg-white dark:bg-stone-900 focus:outline-none focus:ring-2 focus:ring-amber-500"
></textarea>
</div>
</div>
<!-- Simulation Delay Toggle for Testing -->
<div class="p-3 rounded-xl border border-dashed border-stone-300 dark:border-stone-700 text-stone-500 flex items-center justify-between">
<span class="text-[11px]">شبیه‌سازی تاخیر برای تست مودال:</span>
<div class="flex items-center gap-2">
<button
type="button"
class="px-2 py-1 rounded text-[11px] font-bold"
:class="simulateLate ? 'bg-amber-500 text-white' : 'bg-stone-200 text-stone-700'"
@click="simulateLate = !simulateLate"
>
{{ simulateLate ? 'حالت با تاخیر فعال' : 'حالت سر وقت' }}
</button>
</div>
</div>
</div>
<template #footer="{ close }">
<UButton color="neutral" variant="outline" label="انصراف" @click="close" />
<UButton
color="primary"
variant="solid"
label="ثبت قطعی حضور"
icon="i-lucide-check"
:disabled="studentDelayCalculation.isLate && (!studentDelayReason || studentDelayReason.trim().length < 5)"
@click="handleStudentSubmitAttendance"
/>
</template>
</AppModal>
</div>
<!-- ============================================================= -->
<!-- ۲. نمای استاد (TEACHER VIEW) -->
<!-- ============================================================= -->
<div v-else-if="currentRole === 'teacher'" class="space-y-6">
<div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div>
<h2 class="text-lg font-extrabold text-stone-900 dark:text-white">کلاس‌های شما (ثبت و بازبینی حضور و غیاب)</h2>
<p class="text-xs text-stone-500 dark:text-stone-400">کلاس مورد نظر را انتخاب و وضعیت طلاب را مشخص نمایید</p>
</div>
<!-- Class Selector Buttons -->
<div class="flex flex-wrap gap-2">
<button
v-for="c in teacherClasses"
:key="c.id"
type="button"
class="px-3.5 py-2 rounded-xl text-xs font-bold transition flex items-center gap-2 border"
:class="selectedTeacherClassId === c.id
? 'bg-primary-500 text-white border-primary-500 shadow-md shadow-primary-500/20'
: 'bg-white dark:bg-stone-900 text-stone-700 dark:text-stone-300 border-stone-200 dark:border-stone-800 hover:border-primary-400'"
@click="selectedTeacherClassId = c.id"
>
<UIcon name="i-lucide-book" class="w-3.5 h-3.5" />
<span>{{ c.title }}</span>
</button>
</div>
</div>
<!-- Class Attendance Table & Stats Card -->
<div v-if="currentTeacherClass" class="bg-white dark:bg-stone-900 border border-stone-200 dark:border-stone-800 rounded-3xl p-6 shadow-sm space-y-6">
<!-- Summary Counters Header -->
<div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4 pb-4 border-b border-stone-100 dark:border-stone-800">
<div>
<h3 class="text-base font-bold text-stone-900 dark:text-white flex items-center gap-2">
<span>لیست طلاب کلاس: {{ currentTeacherClass.title }}</span>
<span class="text-xs font-normal text-stone-400">({{ currentTeacherClass.time }} - {{ currentTeacherClass.room }})</span>
</h3>
</div>
<!-- Quick Stats Pills -->
<div class="flex flex-wrap items-center gap-2 text-xs">
<span class="px-3 py-1 rounded-xl bg-stone-100 dark:bg-stone-800 text-stone-700 dark:text-stone-300 font-bold">
کل طلاب: {{ teacherClassStats.total }}
</span>
<span class="px-3 py-1 rounded-xl bg-emerald-100 dark:bg-emerald-950/60 text-emerald-800 dark:text-emerald-300 font-bold">
حاضر: {{ teacherClassStats.present }}
</span>
<span class="px-3 py-1 rounded-xl bg-amber-100 dark:bg-amber-950/60 text-amber-800 dark:text-amber-300 font-bold">
تاخیر: {{ teacherClassStats.late }}
</span>
<span class="px-3 py-1 rounded-xl bg-rose-100 dark:bg-rose-950/60 text-rose-800 dark:text-rose-300 font-bold">
غایب: {{ teacherClassStats.absent }}
</span>
</div>
</div>
<!-- Attendance Students Table -->
<div class="overflow-x-auto">
<table class="w-full text-right text-xs">
<thead class="bg-stone-50 dark:bg-stone-800/60 text-stone-500 font-bold border-b border-stone-200 dark:border-stone-800">
<tr>
<th class="py-3 px-4">ردیف</th>
<th class="py-3 px-4">نام و نام‌خانوادگی طلبه</th>
<th class="py-3 px-4">کد تحصیلی</th>
<th class="py-3 px-4">وضعیت حضور</th>
<th class="py-3 px-4">میزان و علت تاخیر</th>
<th class="py-3 px-4 text-center">تغییر وضعیت سریع</th>
</tr>
</thead>
<tbody class="divide-y divide-stone-100 dark:divide-stone-800/80">
<tr
v-for="(st, idx) in teacherClassStudents"
:key="st.id"
class="hover:bg-stone-50/50 dark:hover:bg-stone-800/30 transition"
:class="getTeacherRecord(st.id)?.status === 'absent' ? 'bg-rose-50/30 dark:bg-rose-950/10' : ''"
>
<td class="py-3 px-4 font-mono text-stone-400">{{ idx + 1 }}</td>
<td class="py-3 px-4 font-bold text-stone-900 dark:text-white flex items-center gap-2">
<div class="w-7 h-7 rounded-full bg-stone-200 dark:bg-stone-800 text-stone-700 dark:text-stone-300 flex items-center justify-center font-bold text-[11px]">
{{ st.name.charAt(0) }}
</div>
<span>{{ st.name }}</span>
</td>
<td class="py-3 px-4 font-mono text-stone-500">{{ st.code }}</td>
<td class="py-3 px-4">
<UiBadge
:status="getTeacherRecord(st.id)?.status || 'absent'"
:label="getStatusLabel(getTeacherRecord(st.id)?.status || 'absent')"
/>
</td>
<td class="py-3 px-4 text-stone-600 dark:text-stone-300 max-w-xs">
<div v-if="getTeacherRecord(st.id)?.status === 'late'">
<span class="font-bold text-amber-600">{{ getTeacherRecord(st.id)?.delayMinutes }} دقیقه: </span>
<span>{{ getTeacherRecord(st.id)?.delayReason || 'بدون توضیح' }}</span>
</div>
<div v-else-if="getTeacherRecord(st.id)?.status === 'excused'">
<span class="text-blue-600 font-semibold">{{ getTeacherRecord(st.id)?.delayReason || 'غیبت موجه' }}</span>
</div>
<div v-else class="text-stone-400">-</div>
</td>
<td class="py-3 px-4">
<!-- Quick status selector buttons for teacher -->
<div class="flex items-center justify-center gap-1">
<button
type="button"
class="px-2 py-1 rounded text-[11px] font-bold transition"
:class="getTeacherRecord(st.id)?.status === 'present' ? 'bg-emerald-500 text-white' : 'bg-stone-100 dark:bg-stone-800 text-stone-600 hover:bg-emerald-100'"
title="حاضر"
@click="setStudentAttendance(st.id, 'present')"
>
حاضر
</button>
<button
type="button"
class="px-2 py-1 rounded text-[11px] font-bold transition"
:class="getTeacherRecord(st.id)?.status === 'late' ? 'bg-amber-500 text-white' : 'bg-stone-100 dark:bg-stone-800 text-stone-600 hover:bg-amber-100'"
title="تاخیر"
@click="promptTeacherLate(st)"
>
تاخیر
</button>
<button
type="button"
class="px-2 py-1 rounded text-[11px] font-bold transition"
:class="getTeacherRecord(st.id)?.status === 'excused' ? 'bg-blue-500 text-white' : 'bg-stone-100 dark:bg-stone-800 text-stone-600 hover:bg-blue-100'"
title="موجه"
@click="promptTeacherExcused(st)"
>
مجاز
</button>
<button
type="button"
class="px-2 py-1 rounded text-[11px] font-bold transition"
:class="getTeacherRecord(st.id)?.status === 'absent' ? 'bg-rose-500 text-white' : 'bg-stone-100 dark:bg-stone-800 text-stone-600 hover:bg-rose-100'"
title="غایب"
@click="setStudentAttendance(st.id, 'absent')"
>
غایب
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- ۳. نمای آموزش (ADMIN MONITORING VIEW) -->
<!-- ============================================================= -->
<div v-else-if="currentRole === 'admin'" class="space-y-6">
<div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div>
<h2 class="text-lg font-extrabold text-stone-900 dark:text-white">نظارت بر کلاس‌های در حال برگزاری</h2>
<p class="text-xs text-stone-500 dark:text-stone-400">مشاهده آمار حضور و غیاب لحظه‌ای کلاس‌ها به تفکیک درس و استاد</p>
</div>
<!-- Filter bar -->
<div class="flex flex-wrap items-center gap-3 text-xs">
<input
v-model="adminSearchQuery"
type="text"
placeholder="جستجوی درس، استاد یا مدرس..."
class="px-3.5 py-2 rounded-xl border border-stone-200 dark:border-stone-800 bg-white dark:bg-stone-900 focus:outline-none focus:ring-2 focus:ring-primary-500"
/>
<select
v-model="adminTeacherFilter"
class="px-3 py-2 rounded-xl border border-stone-200 dark:border-stone-800 bg-white dark:bg-stone-900 focus:outline-none focus:ring-2 focus:ring-primary-500"
>
<option value="">همه اساتید</option>
<option v-for="t in teachers" :key="t.id" :value="t.name">{{ t.name }}</option>
</select>
</div>
</div>
<!-- Classes Grid for Admin -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<div
v-for="c in filteredAdminClasses"
:key="c.id"
class="p-6 rounded-3xl bg-white dark:bg-stone-900 border border-stone-200 dark:border-stone-800 shadow-sm hover:border-primary-500/50 transition cursor-pointer flex flex-col justify-between space-y-4"
@click="openAdminClassDetails(c)"
>
<div class="space-y-3">
<div class="flex items-center justify-between">
<span class="text-xs font-mono font-bold text-primary-600 bg-primary-50 dark:bg-primary-950 px-2 py-0.5 rounded">
{{ c.code }}
</span>
<span class="text-xs text-stone-500">{{ c.time }}</span>
</div>
<h3 class="font-bold text-base text-stone-900 dark:text-white leading-snug">
{{ c.title }}
</h3>
<div class="space-y-1 text-xs text-stone-600 dark:text-stone-300">
<div>استاد: <strong class="text-stone-800 dark:text-stone-100">{{ c.teacherName }}</strong></div>
<div>مکان: {{ c.room }}</div>
</div>
<!-- Progress bar of attendance -->
<div class="space-y-1.5 pt-2">
<div class="flex items-center justify-between text-xs">
<span>تعداد حاضرین:</span>
<span class="font-bold font-mono">
{{ getAdminClassAttendanceStats(c.id).presentCount }} از {{ c.studentCount }} نفر
</span>
</div>
<div class="w-full h-2 rounded-full bg-stone-100 dark:bg-stone-800 overflow-hidden">
<div
class="h-full bg-primary-500 rounded-full transition-all duration-300"
:style="{ width: `${getAdminClassAttendanceStats(c.id).percentage}%` }"
></div>
</div>
</div>
</div>
<div class="pt-3 border-t border-stone-100 dark:border-stone-800 flex items-center justify-between text-xs">
<span class="text-stone-400">کلیک برای مشاهده لیست طلاب</span>
<span class="text-primary-600 font-bold flex items-center gap-1">
مشاهده جزییات
<UIcon name="i-lucide-chevron-left" class="w-3.5 h-3.5" />
</span>
</div>
</div>
</div>
<!-- Admin Class Details Modal (Read-Only) -->
<AppModal
v-model="isAdminDetailsModalOpen"
:title="selectedAdminClass ? `وضعیت حضور طلاب در کلاس ${selectedAdminClass.title}` : 'جزییات کلاس'"
description="نظارت مدیریت آموزش حوزه علمیه سفیران هدایت"
size="lg"
>
<div v-if="selectedAdminClass" class="space-y-4 text-xs">
<!-- Class info cards -->
<div class="grid grid-cols-2 sm:grid-cols-4 gap-2 text-center">
<div class="p-3 rounded-xl bg-stone-50 dark:bg-stone-800">
<div class="text-stone-400 text-[11px]">کل طلاب</div>
<div class="text-lg font-bold text-stone-900 dark:text-white">{{ selectedAdminClass.studentCount }}</div>
</div>
<div class="p-3 rounded-xl bg-emerald-50 dark:bg-emerald-950/60">
<div class="text-emerald-700 text-[11px]">حاضر</div>
<div class="text-lg font-bold text-emerald-800 dark:text-emerald-300">
{{ getAdminClassAttendanceStats(selectedAdminClass.id).presentCount }}
</div>
</div>
<div class="p-3 rounded-xl bg-amber-50 dark:bg-amber-950/60">
<div class="text-amber-700 text-[11px]">با تاخیر</div>
<div class="text-lg font-bold text-amber-800 dark:text-amber-300">
{{ getAdminClassAttendanceStats(selectedAdminClass.id).lateCount }}
</div>
</div>
<div class="p-3 rounded-xl bg-rose-50 dark:bg-rose-950/60">
<div class="text-rose-700 text-[11px]">غایب</div>
<div class="text-lg font-bold text-rose-800 dark:text-rose-300">
{{ getAdminClassAttendanceStats(selectedAdminClass.id).absentCount }}
</div>
</div>
</div>
<!-- Students List (View Only) -->
<div class="overflow-x-auto max-h-72">
<table class="w-full text-right text-xs">
<thead class="bg-stone-100 dark:bg-stone-800 text-stone-500 font-bold sticky top-0">
<tr>
<th class="p-2">نام طلبه</th>
<th class="p-2">کد</th>
<th class="p-2">وضعیت</th>
<th class="p-2">توضیحات / تاخیر</th>
</tr>
</thead>
<tbody class="divide-y divide-stone-100 dark:divide-stone-800">
<tr v-for="st in getAdminClassStudents(selectedAdminClass.id)" :key="st.id">
<td class="p-2 font-bold">{{ st.name }}</td>
<td class="p-2 font-mono text-stone-500">{{ st.code }}</td>
<td class="p-2">
<UiBadge :status="st.status" :label="getStatusLabel(st.status)" />
</td>
<td class="p-2 text-stone-500">
<span v-if="st.status === 'late'">{{ st.delayMinutes }} دقیقه ({{ st.delayReason || 'بدون ثبت علت' }})</span>
<span v-else-if="st.status === 'excused'">{{ st.delayReason || 'موجه' }}</span>
<span v-else>-</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<template #footer="{ close }">
<UButton color="neutral" variant="outline" label="بستن پنجره" @click="close" />
</template>
</AppModal>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useAuth } from '~/composables/useAuth'
import { useMockData, type ClassItem, type AttendanceStatus, type Student } from '~/composables/useMockData'
import RoleSwitcher from '~/components/common/RoleSwitcher.vue'
import UiBadge from '~/components/common/UiBadge.vue'
import AppModal from '~/components/common/AppModal.vue'
const { currentRole, currentUser, roleLabels } = useAuth()
const {
classes,
students,
teachers,
attendanceRecords,
todayShamsi,
calculateDelay,
submitStudentAttendance,
updateStudentStatus
} = useMockData()
// ساعت زنده
const currentTimeStr = ref('۰۸:۱۵')
let clockInterval: any = null
onMounted(() => {
const updateClock = () => {
const now = new Date()
currentTimeStr.value = `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`
}
updateClock()
clockInterval = setInterval(updateClock, 1000)
})
onUnmounted(() => {
if (clockInterval) clearInterval(clockInterval)
})
const getStatusLabel = (st: AttendanceStatus) => {
switch (st) {
case 'present': return 'حاضر'
case 'absent': return 'غایب'
case 'late': return 'تاخیر'
case 'excused': return 'مجاز'
default: return 'غایب'
}
}
// -------------------------------------------------------------
// منطق طلبه (Student Logic)
// -------------------------------------------------------------
const todayStudentClasses = computed(() => {
return classes.value.filter(c => c.isOngoingToday)
})
const getStudentStatusForClass = (classId: string) => {
const record = attendanceRecords.value.find(
r => r.classId === classId && r.studentId === currentUser.value.id
)
if (!record || record.status === 'absent') {
return { status: 'absent' as AttendanceStatus, label: 'غایب / ثبت نشده' }
}
return { status: record.status, label: getStatusLabel(record.status) }
}
const isStudentModalOpen = ref(false)
const selectedClassForStudent = ref<ClassItem | null>(null)
const studentDelayReason = ref('')
const simulateLate = ref(true) // پیش‌فرض فعال برای مشاهده الزامی بودن علت تاخیر
const studentDelayCalculation = computed(() => {
if (!selectedClassForStudent.value) return { isLate: false, minutes: 0 }
if (simulateLate.value) {
return { isLate: true, minutes: 15 }
}
return calculateDelay(selectedClassForStudent.value.startTime)
})
const openStudentAttendanceModal = (c: ClassItem) => {
selectedClassForStudent.value = c
const existingRecord = attendanceRecords.value.find(
r => r.classId === c.id && r.studentId === currentUser.value.id
)
if (existingRecord && existingRecord.delayReason) {
studentDelayReason.value = existingRecord.delayReason
} else {
studentDelayReason.value = ''
}
isStudentModalOpen.value = true
}
const handleStudentSubmitAttendance = () => {
if (!selectedClassForStudent.value) return
const isLate = studentDelayCalculation.value.isLate
const minutes = studentDelayCalculation.value.minutes
if (isLate && (!studentDelayReason.value || studentDelayReason.value.trim().length < 5)) {
alert('لطفاً علت تاخیر را وارد فرمایید.')
return
}
submitStudentAttendance(
selectedClassForStudent.value.id,
currentUser.value.id,
isLate ? minutes : 0,
isLate ? studentDelayReason.value : undefined
)
isStudentModalOpen.value = false
alert('حضور شما با موفقیت در سامانه ثبت گردید.')
}
// -------------------------------------------------------------
// منطق استاد (Teacher Logic)
// -------------------------------------------------------------
const teacherClasses = computed(() => {
return classes.value.filter(c => c.teacherId === currentUser.value.id || c.teacherId === 't-201')
})
const selectedTeacherClassId = ref<string>('c-1')
const currentTeacherClass = computed(() => {
return classes.value.find(c => c.id === selectedTeacherClassId.value) || teacherClasses.value[0]
})
const teacherClassStudents = computed(() => {
if (!currentTeacherClass.value) return []
return students.value.filter(s => currentTeacherClass.value.studentIds.includes(s.id))
})
const getTeacherRecord = (studentId: string) => {
if (!currentTeacherClass.value) return null
return attendanceRecords.value.find(
r => r.classId === currentTeacherClass.value.id && r.studentId === studentId
)
}
const teacherClassStats = computed(() => {
const total = teacherClassStudents.value.length
let present = 0
let late = 0
let absent = 0
teacherClassStudents.value.forEach(st => {
const rec = getTeacherRecord(st.id)
if (!rec || rec.status === 'absent') absent++
else if (rec.status === 'present') present++
else if (rec.status === 'late') late++
})
return { total, present, late, absent }
})
const setStudentAttendance = (studentId: string, status: AttendanceStatus, minutes = 0, reason = '') => {
if (!currentTeacherClass.value) return
updateStudentStatus(currentTeacherClass.value.id, studentId, status, minutes, reason)
}
const promptTeacherLate = (student: Student) => {
const minutesStr = prompt(`تعداد دقایق تاخیر طلبه (${student.name}):`, '15')
if (minutesStr === null) return
const minutes = parseInt(minutesStr, 10) || 10
const reason = prompt('علت تاخیر (اختیاری):', 'تاخیر در ورود به جلسه') || ''
setStudentAttendance(student.id, 'late', minutes, reason)
}
const promptTeacherExcused = (student: Student) => {
const reason = prompt(`علت موجه بودن غیبت طلبه (${student.name}):`, 'گواهی پزشکی یا ماموریت تبلیغی') || 'موجه'
setStudentAttendance(student.id, 'excused', 0, reason)
}
// -------------------------------------------------------------
// منطق آموزش (Admin Logic)
// -------------------------------------------------------------
const adminSearchQuery = ref('')
const adminTeacherFilter = ref('')
const filteredAdminClasses = computed(() => {
return classes.value.filter(c => {
const matchesSearch = c.title.includes(adminSearchQuery.value) ||
c.teacherName.includes(adminSearchQuery.value) ||
c.room.includes(adminSearchQuery.value)
const matchesTeacher = !adminTeacherFilter.value || c.teacherName === adminTeacherFilter.value
return matchesSearch && matchesTeacher
})
})
const getAdminClassAttendanceStats = (classId: string) => {
const c = classes.value.find(item => item.id === classId)
if (!c) return { presentCount: 0, lateCount: 0, absentCount: 0, percentage: 0 }
const records = attendanceRecords.value.filter(r => r.classId === classId)
const presentCount = records.filter(r => r.status === 'present').length
const lateCount = records.filter(r => r.status === 'late').length
const absentCount = c.studentCount - (presentCount + lateCount)
const percentage = c.studentCount > 0 ? Math.round(((presentCount + lateCount) / c.studentCount) * 100) : 0
return { presentCount, lateCount, absentCount, percentage }
}
const selectedAdminClass = ref<ClassItem | null>(null)
const isAdminDetailsModalOpen = ref(false)
const openAdminClassDetails = (c: ClassItem) => {
selectedAdminClass.value = c
isAdminDetailsModalOpen.value = true
}
const getAdminClassStudents = (classId: string) => {
const c = classes.value.find(item => item.id === classId)
if (!c) return []
return students.value
.filter(s => c.studentIds.includes(s.id))
.map(s => {
const rec = attendanceRecords.value.find(r => r.classId === classId && r.studentId === s.id)
return {
id: s.id,
name: s.name,
code: s.code,
status: rec ? rec.status : ('absent' as AttendanceStatus),
delayMinutes: rec?.delayMinutes || 0,
delayReason: rec?.delayReason || ''
}
})
}
</script>