<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="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-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-row>
                <el-row>
                    <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-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-row>
                <el-row>
                    <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-form-item>
                    <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:teamaward: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:teamaward: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:teamaward:remove']">删除</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:teamaward:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table :row-style="{ height: '1px' }" :cell-style="{ padding: '0px' }"
            :header-cell-style="{ height: '50px', fontSize: '12px', padding: '0 0' }" v-loading="loading" :data="awardList"
            @selection-change="handleSelectionChange" :default-sort="{ prop: 'schoolYear', order: 'descending' }">
            <el-table-column type="selection" width="30" align="center" />
            <el-table-column type="index" label="序号" width="30" align="center" sortable fixed="left" />
            <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="year" sortable show-overflow-tooltip width="60px">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.rankda" :value="scope.row.year" />
                </template>
            </el-table-column>
            <el-table-column label="年级" align="center" prop="grade" sortable show-overflow-tooltip width="70px">
                <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="sub" sortable show-overflow-tooltip width="70px">
                <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" prop="awardSituation" sortable show-overflow-tooltip
                width="160px" />
            <el-table-column label="团队成员贡献情况" align="center" prop="teamMembersContribution" sortable width="160px"
                show-overflow-tooltip />
            <el-table-column label="备注" align="center" prop="remark" sortable show-overflow-tooltip />
            <el-table-column label="审核状态" align="center" prop="auditState" sortable width="80" fixed="right">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.audit_states" :value="scope.row.auditState" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:teamaward:edit']"
                        v-if="scope.row.auditState === '0' || scope.row.auditState === '3'">修改</el-button>
                    <el-button size="mini" type="text" @click="handleLook(scope.row)" v-hasPermi="['system:teamaward:look']"
                        v-if="scope.row.auditState === '0' || scope.row.auditState === '1' || scope.row.auditState === '3' || scope.row.auditState === '2'">查看</el-button>
                    <el-button size="mini" type="text" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:teamaward:remove']"
                        v-if="scope.row.auditState === '0' || scope.row.auditState === '3'">删除</el-button>
                    <el-button size="mini" type="text" @click="handleExamine(scope.row)"
                        v-hasPermi="['system:teamaward:sub']" v-if="scope.row.auditState === '0'">提交</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="open" width="900px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="140px" :disabled="isEdit">
                <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="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="sub">
                            <el-select v-model="form.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-col :span="12">
                        <el-form-item label="获奖情况" prop="awardSituation">
                            <el-input v-model="form.awardSituation" placeholder="请输入获奖情况" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="团队成员贡献情况" prop="teamMembersContribution">
                            <el-input v-model="form.teamMembersContribution" type="textarea" placeholder="请输入内容"
                                clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button v-if="isEdit === false" type="primary" @click="submitForm">确定</el-button>
                <el-button @click="cancel">{{ isEdit ? '取消' : '关闭' }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listAward, listAwardgr, getAward, delAward, addAward, updateAward, audit } from "@/api/smartSchool/electronicArchives/teamAward";
export default {
    name: "Award",
    dicts: ["teaching_subjects", 'semester_jsdzda', 'exam_typeda', 'grade_da', 'class_type', 'yearda', 'rankda', 'audit_states'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 团队获奖表格数据
            awardList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            isEdit: false,
            query: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                schoolYear: null,
                semester: null,
                year: null,
                grade: null,
                sub: null,
                awardSituation: null,
                teamMembersContribution: null,
                auditState: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                schoolYear: [
                    { required: true, message: "学年不能为空", trigger: "blur" }
                ],
                semester: [
                    { required: true, message: "学期不能为空", trigger: "blur" }
                ],
                year: [
                    { required: true, message: "届别不能为空", trigger: "blur" }
                ],
                grade: [
                    { required: true, message: "年级不能为空", trigger: "blur" }
                ],
                sub: [
                    { required: true, message: "学科不能为空", trigger: "blur" }
                ],
                awardSituation: [
                    { required: true, message: "获奖情况不能为空", trigger: "blur" }
                ],
                teamMembersContribution: [
                    { required: true, message: "团队成员贡献情况不能为空", trigger: "blur" }
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询团队获奖列表 */
        getList() {
            this.loading = true;
            const createBy = this.$store.state.user.userId;
            const params = Object.assign({}, this.queryParams, { createBy }); // 将 userId 添加到 queryParams 中
            listAwardgr(params).then(response => {
                this.awardList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 查看按钮操作
        handleLook(row) {
            this.open = true;
            this.isEdit = true;
            const id = row.id || this.ids
            getAward(id).then(response => {
                this.form = response.data;
                // this.form.classType = this.form.classType.split(',');
                this.title = "查看团队获奖";
            });
        },
        handleExamine(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm("是否提交该信息？")
                .then(function () {
                    const params = {
                        id: row.id,
                        auditState: '1'
                    };
                    return audit(params);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("提交成功");
                })
                .catch((err) => {
                    console.error(err);
                });
        }, handleExamine(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm("是否提交该信息？")
                .then(function () {
                    const params = {
                        id: row.id,
                        auditState: '1'
                    };
                    return audit(params);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("提交成功");
                })
                .catch((err) => {
                    console.error(err);
                });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                schoolYear: null,
                semester: null,
                year: null,
                grade: null,
                sub: null,
                awardSituation: null,
                teamMembersContribution: null,
                remark: null,
                auditState: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                delFlag: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuerycx() {
            this.query = true;
            this.resetcx();
        },
        handleQuery() {
            this.query = true;
            this.getList();
            this.query = false;
        },
        /** 重置按钮操作 */
        resetQuery() {
            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.isEdit = false;
            this.form.userId = this.$store.state.user.userId;
            this.title = "添加团队获奖";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            this.isEdit = false;
            getAward(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改团队获奖";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.form.auditState = '0';
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateAward(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addAward(this.form).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 delAward(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            const userId = this.$store.state.user.userId;
            this.download('/school/award/export', {
                ...this.queryParams,
                userId: userId
            }, `团队获奖_${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
}

::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>