<template>
  <div>
    <QueryListPage ref="queryListPage" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList" />
    <publish-exam-edit ref="edit" type="EDIT" @reloadTable="reloadTable" />
  </div>
</template>

<script>
import { getPublishExam, delPublishExam } from '@/api/management/userexam';
import PublishExamEdit from './edit';

export default {
    name: 'PublishExam',
    components: {
        PublishExamEdit
    },
    data() {
        return {
            examResultList: [],
            pagerConfig: {
                pageSize: 'pageSize',
                pageIndex: 'pageNum'
            },
            queryForm: {
                labelWidth: '150px',
                reset: true,
                queryObject: {
                    examName: {
                        type: 'text',
                        label: this.$t('system.examName')
                    },
                    userName: {
                        type: 'text',
                        label: this.$t('system.examUser')
                    },
                    result: {
                        type: 'select',
                        label: this.$t('system.examResult'),
                        config: {
                            data: this.$ConstSelect.examResultList
                        }
                    }
                }

            },
            queryList: {
                query: getPublishExam,
                selectCheckShow: false,
                indexShow: true,
                columns: [
                    {
                        title: this.$t('system.examName'),
                        prop: 'examName'
                    },
                    {
                        title: this.$t('system.examResult'),
                        prop: 'result',
                        type: 'tag',
                        columnValue: (row) => { return this.$convertField(row.result, this.$ConstSelect.examResultList, ['value', 'label']); },
                        tagType: (row) => {
                            switch (row.result) {
                            case '01': return 'warning';
                            case '02': return 'success';
                            case '03': return 'danger';
                            case '04': return 'danger';
                            }
                        }
                    },
                    {
                        title: this.$t('system.examScore'),
                        prop: 'score'
                    },
                    {
                        title: this.$t('system.examUser'),
                        prop: 'userNickname'
                    },
                    {
                        title: this.$t('global.mobile'),
                        prop: 'userMobile'
                    },
                    {
                        type: 'button',
                        title: this.$t('global.operate'),
                        width: '250',
                        buttons: [
                            {
                                name: this.$t('global.edit'),
                                handleClick: this.edit
                            },
                            {
                                name: this.$t('global.delete'),
                                handleClick: this.handleDelete,
                                type: 'danger'
                            }
                        ]
                    }
                ]
            },

            currentModel: {}
        };
    },
    methods: {
        // 编辑
        edit(index, row) {
            this.$refs.edit.show(row);
        },
        // 删除
        handleDelete(index, row) {
            this.$confirm(this.$t('system.wellDelExamResult'), this.$t('global.tips'), {
                confirmButtonText: this.$t('global.confirm'),
                cancelButtonText: this.$t('global.cancel'),
                type: 'warning'
            }).then(() => {
                delPublishExam(row.id).then(response => {
                    this.$message.success(this.$t('system.deleteSuccess'));
                    this.reloadTable();
                }).catch(() => {
                    this.reloadTable();
                    this.$messageBox(this.$t('error.deleteFailed'));
                });
            });
        },
        reloadTable() {
            this.queryList.reload();
        }
    }
};
</script>
