formPage.vue 21.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
<!--
 * @Author: hookehuyr hookehuyr@gmail.com
 * @Date: 2025-01-20 10:00:00
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-06-18 11:43:03
 * @FilePath: /mlaj/src/views/teacher/formPage.vue
 * @Description: 教师作业新增表单页面
-->
<template>
    <AppLayout title="新增作业">
        <div class="bg-gradient-to-br from-green-50 via-green-100/30 to-blue-50/30 min-h-screen">
            <div class="px-4 py-6">
                <FrostedGlass class="rounded-xl overflow-hidden">
                    <div class="py-4">
                        <van-form @submit="handleSubmit">
                            <van-cell-group inset>
                                <!-- 作业名称 -->
                                <van-field
                                    v-model="formData.homework_name"
                                    name="homework_name"
                                    label="作业名称"
                                    placeholder="请输入作业名称"
                                    required
                                    :border="false"
                                    :rules="[{ required: true, message: '请输入作业名称' }]"
                                />

                                <!-- 类型 -->
                                <van-field
                                    v-model="formData.type"
                                    is-link
                                    readonly
                                    name="type"
                                    label="类型"
                                    placeholder="请选择类型"
                                    :border="false"
                                    @click="showTypePicker = true"
                                />

                                <!-- 频次 -->
                                <van-field
                                    v-model="formData.frequency"
                                    is-link
                                    readonly
                                    name="frequency"
                                    label="频次"
                                    placeholder="请选择频次"
                                    :border="false"
                                    @click="showFrequencyPicker = true"
                                />

                                <!-- 目标总数 -->
                                <van-field
                                    v-model="formData.target_count"
                                    type="number"
                                    name="target_count"
                                    label="目标总数"
                                    :border="false"
                                    placeholder="请输入目标数量"
                                />

                                <!-- 开始时间 -->
                                <van-field
                                    v-model="startTimeDisplay"
                                    is-link
                                    readonly
                                    name="start_time"
                                    label="开始时间"
                                    placeholder="请选择开始时间"
                                    :border="false"
                                    @click="showStartTimePicker = true"
                                />

                                <!-- 结束时间 -->
                                <van-field
                                    v-model="endTimeDisplay"
                                    is-link
                                    readonly
                                    name="end_time"
                                    label="结束时间"
                                    placeholder="请选择结束时间"
                                    :border="false"
                                    @click="showEndTimePicker = true"
                                />

                                <!-- 课程 -->
                                <van-field
                                    v-model="formData.course"
                                    is-link
                                    readonly
                                    name="course"
                                    label="课程"
                                    placeholder="请选择课程"
                                    :border="false"
                                    @click="showCoursePicker = true"
                                />

                                <!-- 活动 -->
                                <van-field
                                    v-model="formData.activity"
                                    is-link
                                    readonly
                                    name="activity"
                                    label="活动"
                                    placeholder="请选择活动"
                                    :border="false"
                                    @click="showActivityPicker = true"
                                />

                                <!-- 年级 -->
                                <van-field
                                    v-model="formData.grade"
                                    is-link
                                    readonly
                                    name="grade"
                                    label="年级"
                                    placeholder="请选择年级"
                                    :border="false"
                                    @click="showGradePicker = true"
                                />

                                <!-- 班级 -->
                                <van-field
                                    v-model="formData.class_name"
                                    is-link
                                    readonly
                                    name="class_name"
                                    label="班级"
                                    placeholder="请选择班级"
                                    :border="false"
                                    @click="showClassPicker = true"
                                />

                                <!-- 小组 -->
                                <van-field
                                    v-model="formData.group_name"
                                    is-link
                                    readonly
                                    name="group_name"
                                    label="小组"
                                    placeholder="请选择小组"
                                    :border="false"
                                    @click="showGroupPicker = true"
                                />
                            </van-cell-group>

                            <!-- 提交按钮 -->
                            <div style="margin: 16px;">
                                <van-button
                                    native-type="submit"
                                    type="primary"
                                    block
                                    round
                                    :loading="loading"
                                    class="bg-green-500 hover:bg-green-600 transition-colors"
                                >
                                    确认并保存
                                </van-button>
                            </div>
                        </van-form>
                    </div>
                </FrostedGlass>
            </div>
        </div>

        <!-- 类型选择器 -->
        <van-popup v-model:show="showTypePicker" position="bottom">
            <van-picker
                :columns="typeOptions"
                @confirm="onTypeConfirm"
                @cancel="showTypePicker = false"
            />
        </van-popup>

        <!-- 频次选择器 -->
        <van-popup v-model:show="showFrequencyPicker" position="bottom">
            <van-picker
                :columns="frequencyOptions"
                @confirm="onFrequencyConfirm"
                @cancel="showFrequencyPicker = false"
            />
        </van-popup>

        <!-- 开始时间选择器 -->
        <van-popup v-model:show="showStartTimePicker" position="bottom">
            <van-date-picker
                v-model="startDate"
                title="选择开始时间"
                :min-date="minDate"
                :max-date="maxDate"
                @confirm="onStartTimeConfirm"
                @cancel="showStartTimePicker = false"
            />
        </van-popup>
        
        <!-- 结束时间选择器 -->
        <van-popup v-model:show="showEndTimePicker" position="bottom">
            <van-date-picker
                v-model="endDate"
                title="选择结束时间"
                :min-date="minDate"
                :max-date="maxDate"
                @confirm="onEndTimeConfirm"
                @cancel="showEndTimePicker = false"
            />
        </van-popup>

        <!-- 课程选择器 -->
        <van-popup v-model:show="showCoursePicker" position="bottom">
            <div class="p-4">
                <van-search
                    v-model="courseSearchValue"
                    placeholder="搜索课程"
                    @search="searchCourse"
                />
                <van-list>
                    <van-cell
                        v-for="course in filteredCourses"
                        :key="course.id"
                        :title="course.name"
                        is-link
                        :border="false"
                        @click="onCourseSelect(course)"
                    />
                </van-list>
            </div>
        </van-popup>

        <!-- 活动选择器 -->
        <van-popup v-model:show="showActivityPicker" position="bottom">
            <div class="p-4">
                <van-search
                    v-model="activitySearchValue"
                    placeholder="搜索活动"
                    @search="searchActivity"
                />
                <van-list>
                    <van-cell
                        v-for="activity in filteredActivities"
                        :key="activity.id"
                        :title="activity.name"
                        is-link
                        :border="false"
                        @click="onActivitySelect(activity)"
                    />
                </van-list>
            </div>
        </van-popup>

        <!-- 年级选择器 -->
        <van-popup v-model:show="showGradePicker" position="bottom">
            <div class="p-4">
                <van-search
                    v-model="gradeSearchValue"
                    placeholder="搜索年级"
                    @search="searchGrade"
                />
                <van-list>
                    <van-cell
                        v-for="grade in filteredGrades"
                        :key="grade.id"
                        :title="grade.name"
                        is-link
                        :border="false"
                        @click="onGradeSelect(grade)"
                    />
                </van-list>
            </div>
        </van-popup>

        <!-- 班级选择器 -->
        <van-popup v-model:show="showClassPicker" position="bottom">
            <div class="p-4">
                <van-search
                    v-model="classSearchValue"
                    placeholder="搜索班级"
                    @search="searchClass"
                />
                <van-list>
                    <van-cell
                        v-for="classItem in filteredClasses"
                        :key="classItem.id"
                        :title="classItem.name"
                        is-link
                        :border="false"
                        @click="onClassSelect(classItem)"
                    />
                </van-list>
            </div>
        </van-popup>

        <!-- 小组选择器 -->
        <van-popup v-model:show="showGroupPicker" position="bottom">
            <div class="p-4">
                <van-search
                    v-model="groupSearchValue"
                    placeholder="搜索小组"
                    @search="searchGroup"
                />
                <van-list>
                    <van-cell
                        v-for="group in filteredGroups"
                        :key="group.id"
                        :title="group.name"
                        is-link
                        :border="false"
                        @click="onGroupSelect(group)"
                    />
                </van-list>
            </div>
        </van-popup>
    </AppLayout>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { showToast, DatePicker, Popup } from 'vant';
import AppLayout from '@/components/layout/AppLayout.vue';
import FrostedGlass from '@/components/ui/FrostedGlass.vue';
import { useTitle } from '@vueuse/core';

const $route = useRoute();
const $router = useRouter();
useTitle($route.meta.title || '新增作业');

// 表单数据
const formData = ref({
    homework_name: '',
    type: '',
    frequency: '',
    target_count: '',
    start_time: new Date(),
    end_time: new Date(),
    course: '',
    activity: '',
    grade: '',
    class_name: '',
    group_name: ''
});

// 加载状态
const loading = ref(false);

// 弹窗显示状态
const showTypePicker = ref(false);
const showFrequencyPicker = ref(false);
const showStartTimePicker = ref(false);
const showEndTimePicker = ref(false);
const showCoursePicker = ref(false);
const showActivityPicker = ref(false);
const showGradePicker = ref(false);
const showClassPicker = ref(false);
const showGroupPicker = ref(false);

// 日期选择器相关
const startDate = ref(['2024', '01', '01']);
const endDate = ref(['2024', '12', '31']);
const minDate = new Date(2020, 0, 1);
const maxDate = new Date(2030, 11, 31);

// 选项数据
const typeOptions = ref([
    { text: '签到', value: 'checkin' },
    { text: '作业', value: 'homework' },
    { text: '考试', value: 'exam' },
    { text: '活动', value: 'activity' }
]);

const frequencyOptions = ref([
    { text: '一次', value: 'once' },
    { text: '每日', value: 'daily' },
    { text: '每周', value: 'weekly' },
    { text: '每月', value: 'monthly' }
]);

// 搜索值
const courseSearchValue = ref('');
const activitySearchValue = ref('');
const gradeSearchValue = ref('');
const classSearchValue = ref('');
const groupSearchValue = ref('');

// 数据列表
const courses = ref([
    { id: 1, name: '数学课程' },
    { id: 2, name: '语文课程' },
    { id: 3, name: '英语课程' },
    { id: 4, name: '物理课程' }
]);

const activities = ref([
    { id: 1, name: '春游活动' },
    { id: 2, name: '运动会' },
    { id: 3, name: '文艺汇演' },
    { id: 4, name: '科技节' }
]);

const grades = ref([
    { id: 1, name: '一年级' },
    { id: 2, name: '二年级' },
    { id: 3, name: '三年级' },
    { id: 4, name: '四年级' }
]);

const classes = ref([
    { id: 1, name: '一班' },
    { id: 2, name: '二班' },
    { id: 3, name: '三班' },
    { id: 4, name: '四班' }
]);

const groups = ref([
    { id: 1, name: '第一小组' },
    { id: 2, name: '第二小组' },
    { id: 3, name: '第三小组' },
    { id: 4, name: '第四小组' }
]);

// 计算属性 - 时间显示格式
const startTimeDisplay = computed(() => {
    if (!formData.value.start_time) return '';
    return formatDateTime(formData.value.start_time);
});

const endTimeDisplay = computed(() => {
    if (!formData.value.end_time) return '';
    return formatDateTime(formData.value.end_time);
});

// 过滤后的数据
const filteredCourses = computed(() => {
    if (!courseSearchValue.value) return courses.value;
    return courses.value.filter(course =>
        course.name.toLowerCase().includes(courseSearchValue.value.toLowerCase())
    );
});

const filteredActivities = computed(() => {
    if (!activitySearchValue.value) return activities.value;
    return activities.value.filter(activity =>
        activity.name.toLowerCase().includes(activitySearchValue.value.toLowerCase())
    );
});

const filteredGrades = computed(() => {
    if (!gradeSearchValue.value) return grades.value;
    return grades.value.filter(grade =>
        grade.name.toLowerCase().includes(gradeSearchValue.value.toLowerCase())
    );
});

const filteredClasses = computed(() => {
    if (!classSearchValue.value) return classes.value;
    return classes.value.filter(classItem =>
        classItem.name.toLowerCase().includes(classSearchValue.value.toLowerCase())
    );
});

const filteredGroups = computed(() => {
    if (!groupSearchValue.value) return groups.value;
    return groups.value.filter(group =>
        group.name.toLowerCase().includes(groupSearchValue.value.toLowerCase())
    );
});

/**
 * 格式化日期时间
 * @param {Date} date - 日期对象
 * @returns {string} 格式化后的日期时间字符串
 */
const formatDateTime = (date) => {
    if (!date) return '';
    const d = new Date(date);
    const year = d.getFullYear();
    const month = String(d.getMonth() + 1).padStart(2, '0');
    const day = String(d.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
};

/**
 * 类型选择确认
 * @param {Object} option - 选中的选项
 */
const onTypeConfirm = (option) => {
    formData.value.type = option.selectedOptions[0].text;
    showTypePicker.value = false;
};

/**
 * 频次选择确认
 * @param {Object} option - 选中的选项
 */
const onFrequencyConfirm = (option) => {
    formData.value.frequency = option.selectedOptions[0].text;
    showFrequencyPicker.value = false;
};

/**
 * 开始时间确认
 */
const onStartTimeConfirm = () => {
    const [year, month, day] = startDate.value;
    formData.value.start_time = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
    showStartTimePicker.value = false;
};

/**
 * 结束时间确认
 */
const onEndTimeConfirm = () => {
    const [year, month, day] = endDate.value;
    formData.value.end_time = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
    showEndTimePicker.value = false;
};

/**
 * 课程选择
 * @param {Object} course - 选中的课程
 */
const onCourseSelect = (course) => {
    formData.value.course = course.name;
    showCoursePicker.value = false;
    courseSearchValue.value = '';
};

/**
 * 活动选择
 * @param {Object} activity - 选中的活动
 */
const onActivitySelect = (activity) => {
    formData.value.activity = activity.name;
    showActivityPicker.value = false;
    activitySearchValue.value = '';
};

/**
 * 年级选择
 * @param {Object} grade - 选中的年级
 */
const onGradeSelect = (grade) => {
    formData.value.grade = grade.name;
    showGradePicker.value = false;
    gradeSearchValue.value = '';
};

/**
 * 班级选择
 * @param {Object} classItem - 选中的班级
 */
const onClassSelect = (classItem) => {
    formData.value.class_name = classItem.name;
    showClassPicker.value = false;
    classSearchValue.value = '';
};

/**
 * 小组选择
 * @param {Object} group - 选中的小组
 */
const onGroupSelect = (group) => {
    formData.value.group_name = group.name;
    showGroupPicker.value = false;
    groupSearchValue.value = '';
};

/**
 * 搜索课程
 * @param {string} value - 搜索值
 */
const searchCourse = (value) => {
    courseSearchValue.value = value;
};

/**
 * 搜索活动
 * @param {string} value - 搜索值
 */
const searchActivity = (value) => {
    activitySearchValue.value = value;
};

/**
 * 搜索年级
 * @param {string} value - 搜索值
 */
const searchGrade = (value) => {
    gradeSearchValue.value = value;
};

/**
 * 搜索班级
 * @param {string} value - 搜索值
 */
const searchClass = (value) => {
    classSearchValue.value = value;
};

/**
 * 搜索小组
 * @param {string} value - 搜索值
 */
const searchGroup = (value) => {
    groupSearchValue.value = value;
};

/**
 * 表单提交处理
 * @param {Object} values - 表单值
 */
const handleSubmit = async (values) => {
    try {
        loading.value = true;

        // 验证必填项
        if (!formData.value.homework_name) {
            showToast('请输入作业名称');
            return;
        }

        // 这里可以调用API提交数据
        console.log('提交的表单数据:', formData.value);

        // 模拟API调用
        await new Promise(resolve => setTimeout(resolve, 1000));

        showToast('保存成功');

        // 返回上一页或跳转到列表页
        $router.back();

    } catch (error) {
        console.error('提交失败:', error);
        showToast('保存失败,请重试');
    } finally {
        loading.value = false;
    }
};

/**
 * 组件挂载时初始化数据
 */
onMounted(() => {
    // 这里可以调用API获取课程、活动、年级、班级、小组等数据
    console.log('页面初始化');
});
</script>

<style scoped>
/* 自定义样式 */
:deep(.van-field__label) {
    color: #333;
    font-weight: 500;
}

:deep(.van-field--required .van-field__label::before) {
    color: #ee0a24;
}

:deep(.van-button--primary) {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    border: none;
}

:deep(.van-popup) {
    border-radius: 16px 16px 0 0;
}

:deep(.van-picker__toolbar) {
    border-radius: 16px 16px 0 0;
}
</style>