<template>
<div dir="rtl">
<div v-if="request.doc">
<h1>طلب تعليمات التحقق: {{ request.doc.name }}</h1>
<!-- عرض معلومات الطلب الرئيسي -->
<div>
<label><strong>العميل:</strong></label>
<span>{{ request.doc.customer }}</span>
</div>
<div>
<label><strong>تاريخ الطلب:</strong></label>
<span>{{ formatDate(request.doc.order_date) }}</span>
</div>
<!-- جدول المعلومات التعليمية الفرعية -->
<h2>المعلومات التعليمية</h2>
<table>
<thead>
<tr>
<th>الشركة</th>
<th>مستمر</th>
<th>البلد</th>
<th>من تاريخ</th>
<th>الهاتف</th>
<th>نوع التوظيف</th>
<th>إجراءات</th>
</tr>
</thead>
<tbody>
<tr v-for="(education, index) in request.doc.education_information" :key="index">
<td>
<input
v-model="education.company"
placeholder="اسم الشركة"
@input="updateEducation(index, 'company', education.company)"
/>
</td>
<td>
<input
type="checkbox"
v-model="education.continuous"
@change="updateEducation(index, 'continuous', education.continuous)"
/>
</td>
<td>
<select
v-model="education.country"
@change="updateEducation(index, 'country', education.country)"
>
<option value="">اختر بلد</option>
<option v-for="country in countries" :key="country.value" :value="country.value">
{{ country.label }}
</option>
</select>
</td>
<td>
<input
type="date"
v-model="education.from_date"
@change="updateEducation(index, 'from_date', education.from_date)"
/>
</td>
<td>
<input
type="tel"
v-model="education.phone"
placeholder="الهاتف"
@input="updateEducation(index, 'phone', education.phone)"
pattern="[0-9]{10}"
/>
</td>
<td>
<select
v-model="education.type_of_employment"
@change="updateEducation(index, 'type_of_employment', education.type_of_employment)"
>
<option value="">اختر نوع التوظيف</option>
<option value="Full-time">دوام كامل</option>
<option value="Part-time">دوام جزئي</option>
<option value="Contract">عقد</option>
<option value="Internship">تدريب</option>
</select>
</td>
<td>
<Button @click="removeEducation(index)" type="danger">
حذف
</Button>
</td>
</tr>
</tbody>
</table>
<!-- أزرار الإضافة والحفظ -->
<Button @click="addEducation" type="primary">
إضافة سجل تعليمي
</Button>
<Button @click="saveRequest" type="success" style="margin-left: 10px;">
حفظ الطلب
</Button>
</div>
<div v-else>
جاري التحميل...
</div>
</div>
</template>
<script setup>
import { createDocumentResource, Button, frappe } from 'frappe-ui'
import { ref, watch } from 'vue'
// إنشاء مورد الوثيقة للـ DocType الرئيسي (Verification Instructions Request)
const request = createDocumentResource({
doctype: 'Verification Instructions Request',
// name: 'VIR-2024-26-09-000007', // إلغاء التعليق لإنشاء طلب جديد
})
// تعريف قائمة البلدان
const countries = ref([
{ label: 'مصر', value: 'EG' },
{ label: 'اليمن', value: 'YE' },
{ label: 'الإمارات', value: 'AE' },
{ label: 'قطر', value: 'QA' },
// أضف المزيد من البلدان حسب الحاجة
])
// تأكد من تهيئة education_information كصفيف
watch(() => request.doc, (newDoc) => {
if (newDoc && !newDoc.education_information) {
newDoc.education_information = []
}
}, { immediate: true })
// دالة لإضافة سجل تعليمي جديد إلى الجدول الفرعي
const addEducation = () => {
const newEducation = {
company: '',
continuous: false,
country: '',
from_date: '',
phone: '',
type_of_employment: '',
}
request.doc.education_information.push(newEducation)
}
// دالة لحذف سجل تعليمي بناءً على الفهرس
const removeEducation = (index) => {
request.doc.education_information.splice(index, 1)
}
// دالة لتحديث بيانات سجل تعليمي معين
const updateEducation = (index, field, value) => {
request.doc.education_information[index][field] = value
}
// دالة لحفظ التغييرات في الطلب
const saveRequest = () => {
const errors = []
request.doc.education_information.forEach((education, index) => {
if (!education.company) {
errors.push(`اسم الشركة مطلوب في السجل رقم ${index + 1}.`)
}
if (!education.country) {
errors.push(`البلد مطلوب في السجل رقم ${index + 1}.`)
}
if (!education.from_date) {
errors.push(`تاريخ البدء مطلوب في السجل رقم ${index + 1}.`)
}
if (!education.type_of_employment) {
errors.push(`نوع التوظيف مطلوب في السجل رقم ${index + 1}.`)
}
// تحقق إضافي لنمط الهاتف
const phonePattern = /^[0-9]{10}$/
if (education.phone && !phonePattern.test(education.phone)) {
errors.push(`رقم الهاتف غير صالح في السجل رقم ${index + 1}.`)
}
})
if (errors.length > 0) {
frappe.show_alert({
message: errors.join('<br>'),
indicator: 'red'
})
return
}
// حفظ التغييرات باستخدام setValue.submit
request.setValue.submit({
education_information: request.doc.education_information,
// إضافة حقول أخرى إذا لزم الأمر
})
.then(() => {
frappe.show_alert({
message: 'تم حفظ الطلب بنجاح!',
indicator: 'green'
})
})
.catch((error) => {
frappe.show_alert({
message: `حدث خطأ أثناء حفظ الطلب: ${error.message}`,
indicator: 'red'
})
})
}
// دالة لتنسيق التاريخ
const formatDate = (dateStr) => {
if (!dateStr) return ''
const date = new Date(dateStr)
return date.toLocaleDateString('ar-EG')
}
</script>
<style scoped>
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: center;
}
th {
background-color: #f2f2f2;
}
input, select {
width: 100%;
padding: 4px;
box-sizing: border-box;
}
button {
margin: 5px;
}
</style>