<template>
    <div class="app-container">
        <el-dialog title="查询" :visible.sync="query" width="50%" append-to-body>
            <el-form ref="queryParams" :model="queryParams" label-width="96px" class="search">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="姓名" prop="userName">
                            <el-input v-model="queryParams.userName" placeholder="请输入" clearable
                                @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="学年" prop="schoolYear">
                            <el-select v-model="queryParams.schoolYear" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.yearda" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="学期" prop="semester">
                            <el-select v-model="queryParams.semester" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.semester_jsdzda" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="届别" prop="year">
                            <el-select v-model="queryParams.year" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.rankda" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="年级" prop="grade">
                            <el-select v-model="queryParams.grade" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.grade_da" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="学科" prop="sub">
                            <el-select v-model="queryParams.sub" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.teaching_subjects" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="考试开始时间" prop="startTime">
                            <el-date-picker clearable v-model="queryParams.startTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择" style="width: 100%;">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="考试结束时间" prop="endTime">
                            <el-date-picker clearable v-model="queryParams.endTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择" style="width: 100%;">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="考试类型" prop="examType">
                            <el-select v-model="queryParams.examType" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.exam_typeda" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="班级" prop="className">
                            <el-input v-model="queryParams.className" placeholder="请输入班级" clearable
                                @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="班级类型" prop="classType">
                            <el-select v-model="queryParams.classType" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.class_type" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="增量分" prop="assessmentScore">
                            <el-input v-model="queryParams.assessmentScore" placeholder="请输入增量分" clearable
                                @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item class="dialog-footer" style="display: flex; justify-content: center; align-items: center">
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleQuerycx">查询</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:achievements:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:achievements:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['system:achievements:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-upload" size="mini" @click="handleImport"
                    v-hasPermi="['system:achievements:importData']">导入
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:achievements:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table :data="achievementsList" @selection-change="handleSelectionChange"
            :default-sort="{ prop: 'examTime', order: 'descending' }" :row-style="{ height: '1px' }"
            :cell-style="{ padding: '0px' }" :header-cell-style="{ height: '50px', fontSize: '12px', padding: '0 0' }">
            <el-table-column type="selection" width="30" align="center" />
            <el-table-column type="index" label="序号" width="30" align="center" sortable  />
            <el-table-column label="学年" align="center" prop="schoolYear" sortable fixed="left" width="80px"
                show-overflow-tooltip />
            <el-table-column label="学期" align="center" prop="semester" sortable fixed="left" width="80px"
                show-overflow-tooltip>
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.semester_jsdzda" :value="scope.row.semester" />
                </template>
            </el-table-column>
            <el-table-column label="考试时间" align="center" prop="examTime" width="120" sortable show-overflow-tooltip>
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.examTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="考试类型" align="center" prop="examType" sortable width="80px" show-overflow-tooltip>
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.exam_typeda" :value="scope.row.examType" />
                </template>
            </el-table-column>
            <el-table-column label="届别" align="center" prop="year" sortable show-overflow-tooltip width="50px" />
            <el-table-column label="年级" align="center" prop="grade" sortable show-overflow-tooltip width="50px">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.grade_da" :value="scope.row.grade" />
                </template>
            </el-table-column>
            <el-table-column label="姓名" align="center" prop="userName" width="70px" sortable show-overflow-tooltip />
            <el-table-column label="学科" align="center" prop="sub" width="50px" sortable show-overflow-tooltip>
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.teaching_subjects" :value="scope.row.sub" />
                </template>
            </el-table-column>
            <el-table-column label="班级" align="center" width="100px" prop="className" sortable show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="班级类型" align="center" sortable show-overflow-tooltip width="120px">
                <template slot-scope="scope">
                    <span v-if="getSelectedTypes(scope.row.classType).length > 0">
                        <span v-for="(option, index) in getSelectedTypes(scope.row.classType)" :key="index">
                            {{ option.label }}
                            <span v-if="index !== getSelectedTypes(scope.row.classType).length - 1">, </span>
                        </span>
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="增量分" align="center" prop="assessmentScore" sortable show-overflow-tooltip />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180px" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:achievements:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:achievements:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />
        <el-dialog :title="title" :visible.sync="uploadOpen" width="520px" append-to-body inline @close="cancel">
            <el-form ref="uploadForm" :model="uploadForm" :rules="rules" label-width="70px">
                <el-form-item prop="dataUrl">
                    <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
                        <i class="el-icon-upload"></i>
                        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                        <div class="el-upload__tip text-center" slot="tip">
                            <span>仅允许导入xls、xlsx格式文件。</span>
                            <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline"
                                @click="importTemplate">下载模板
                            </el-link>
                        </div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div class="mask" v-if="importing">
                <div class="mask-content">
                    <i class="el-icon-loading"></i>
                    <span>正在导入，请稍候...</span>
                </div>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="cancelFile">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 添加或修改教学成绩对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="学年" prop="schoolYear">
                            <el-select v-model="form.schoolYear" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.yearda" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="学期" prop="semester">
                            <el-select v-model="form.semester" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.semester_jsdzda" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="考试时间" prop="examTime">
                            <el-date-picker clearable v-model="form.examTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择考试时间" style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="考试类型" prop="examType">
                            <el-select v-model="form.examType" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.exam_typeda" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="届别" prop="year">
                            <el-select v-model="form.year" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.rankda" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="年级" prop="grade">
                            <el-select v-model="form.grade" clearable placeholder="请选择" style="width: 100%;">
                                <el-option v-for="dict in dict.type.grade_da" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="姓名" prop="userName">
                            <el-input v-model="form.userName" placeholder="请输入用户姓名" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="身份证号" prop="idCard">
                            <el-input v-model="form.idCard" placeholder="请输入身份证号" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="学科" prop="sub">
                            <el-select v-model="form.sub" placeholder="请选择" style="width: 100%;" clearable>
                                <el-option v-for="dict in dict.type.teaching_subjects" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="班级" prop="className">
                            <el-input v-model="form.className" placeholder="请输入班级" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="班级类型" prop="classType">
                            <el-select v-model="form.classType" multiple placeholder="请选择" clearable style="width: 100%;">
                                <el-option v-for="dict in dict.type.class_type" :key="dict.value" :label="dict.label"
                                    :value="dict.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="增量分" prop="assessmentScore">
                            <el-input v-model="form.assessmentScore" placeholder="请输入增量分" type="number" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" clearable />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listAchievements, getAchievements, delAchievements, addAchievements, updateAchievements } from "@/api/smartSchool/electronicArchives/professionalDevelopment/teachingAchievements";
import { checkIdcard } from "@/utils/utilLibrary/validate";
import { getToken } from '@/utils/auth'

export default {
    name: "Achievements",
    dicts: ["teaching_subjects", 'semester_jsdzda', 'exam_typeda', 'grade_da', 'class_type', 'yearda', 'rankda', 'audit_states'],
    data() {
        return {
            importing: false,
            uploadOpen: false,
            upload: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的用户数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getToken() },
                // 上传的地址
                url:
                    process.env.VUE_APP_BASE_API + "/system/achievements/importData",
            },
            uploadForm: {

            },
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 教学成绩表格数据
            achievementsList: [

            ],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            query: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userName: "",
                schoolYear: "",
                semester: "",
                year: "",
                grade: "",
                sub: "",
                startTime: "",
                endTime: "",
                examType: "",
                className: "",
                classType: "",
                assessmentScore: "",
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                schoolYear: [
                    { required: true, message: "学年不能为空", trigger: "change" }
                ],
                semester: [
                    { required: true, message: "学期不能为空", trigger: "change" }
                ],
                examTime: [
                    { required: true, message: "考试时间不能为空", trigger: "blur" }
                ],
                examType: [
                    { required: true, message: "考试类型不能为空", trigger: "change" }
                ],
                year: [
                    { required: true, message: "届别不能为空", trigger: "change" }
                ],
                grade: [
                    { required: true, message: "年级不能为空", trigger: "change" }
                ],
                userId: [
                    { required: true, message: "用户id不能为空", trigger: "blur" }
                ],
                userName: [
                    { required: true, message: "用户姓名不能为空", trigger: "blur" }
                ],
                idCard: [{ required: true, validator: checkIdcard, trigger: "blur" }],
                sub: [
                    { required: true, message: "学科不能为空", trigger: "change" }
                ],
                className: [
                    { required: true, message: "班级不能为空", trigger: "blur" }
                ],
                classType: [
                    { required: true, message: "班级类型不能为空", trigger: "change" }
                ],
                assessmentScore: [
                    { required: true, message: "增量分不能为空", trigger: "blur" }
                ],

            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询教学成绩列表 */
        getList() {
            this.loading = true;
            listAchievements(this.queryParams).then(response => {
                this.achievementsList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                schoolYear: null,
                semester: null,
                examTime: null,
                examType: null,
                year: null,
                grade: null,
                userId: null,
                userName: null,
                idCard: null,
                sub: null,
                className: null,
                classType: null,
                assessmentScore: null,
                remark: null,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuerycx() {
            this.query = true;
            this.resetcx();
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit()
        },
        //下载模版
        importTemplate() {
            this.download(
                "/system/achievements/importTemplate",
                {},
                `教学成绩_${Date.now()}.xlsx`
            );
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
            this.query = false;
        },
        getSelectedTypes(classType) {
            const selectedTypes = classType.split(','); // 将逗号分隔的字符串转换为数组
            return this.dict.type.class_type.filter(option => selectedTypes.includes(option.value.toString()));
        },
        /** 导入按钮操作 */
        handleImport(row) {
            this.reset();
            this.title = "导入"
            this.uploadOpen = true
        },
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
            this.importing = true;
            // console.log(event, file, fileList);
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.uploadOpen = false
            this.upload.isUploading = false
            this.importing = false;
            this.$refs.upload.clearFiles()
            this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + '</div>', '导入结果', { dangerouslyUseHTMLString: true })
            this.getList()
        },
        cancelFile() {
            this.uploadOpen = false
            this.upload.isUploading = false
            this.$refs.upload.clearFiles()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                userName: "",
                schoolYear: "",
                semester: "",
                year: "",
                grade: "",
                sub: "",
                startTime: "",
                endTime: "",
                examType: "",
                className: "",
                classType: "",
                assessmentScore: "",
            },
                this.resetForm("queryParams");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加教学成绩";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            this.open = true;
            const id = row.id || this.ids
            getAchievements(id).then(response => {
                this.form = response.data;
                this.form.classType = this.form.classType.split(',');
                this.title = "修改教学成绩";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                const classTypeStr = this.form.classType.join(',');
                console.log(classTypeStr); // 输出转换后的字符串
                if (valid) {
                    if (this.form.id != null) {
                        updateAchievements({ ...this.form, classType: classTypeStr }).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addAchievements({ ...this.form, classType: classTypeStr }).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除教学成绩编号为"' + ids + '"的数据项？').then(function () {
                return delAchievements(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('/system/achievements/export', {
                ...this.queryParams
            }, `教学成绩${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
<style lang="less" scoped>
::v-deep .el-form-item {
    margin-bottom: 0px;
}

::v-deep .el-input--medium .el-input__inner {
    height: 26px;
    line-height: 26px;
}

::v-deep .el-dialog__body {
    padding: 10px 20px
}

::v-deep .el-dialog:not(.is-fullscreen) {
    margin-top: 4vh !important;
}

.search ::v-deep .el-form-item__content {
    width: 200px
}

.mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
}

.mask-content {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #1a74be;
    font-size: 15px;
}

::v-deep .el-table .el-table__cell {
    padding: 0;
}

::v-deep .el-table .cell {
    padding: 0;
}

::v-deep .el-table th.el-table__cell>.cell {
    padding: 0;

}
</style>