<template>
  <div>
    <query-list-page ref="user" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList" />
  </div>
</template>

<script>
import { UrlConfig } from '@/scripts/ConstDic';
import { listQuestionPage, deleteQuestion } from '@/api/questionBank.js';
import { convertSheetToList } from '@/utils/runPlan';
import XLSX from 'xlsx';
import { importQuestionBand } from '@/api/race';

export default {
    data() {
        return {
            pagerConfig: {
                pageSize: 'pageSize',
                pageIndex: 'pageNum'
            },
            queryForm: {
                reset: true,
                labelWidth: '80px',
                leftSpan: 18,
                queryObject: {
                }
            },
            queryList: {
                query: this.handlePreDataPaging,
                selectCheckShow: false,
                indexShow: true,
                columns: [
                    {
                        title: '题 目',
                        prop: 'topic'
                    },
                    {
                        title: '类 型',
                        prop: 'type',
                        type: 'tag',
                        width: '120',
                        columnValue: (row) => { return this.$ConstSelect.translate(row.type, 'QuestionTypeList'); },
                        tagType: (row) => {
                            return '';
                        }
                    },
                    {
                        title: '答 案',
                        prop: 'answer',
                        type: 'tagMore',
                        width: '200',
                        columnValue: (row) => { return this.answerTags(row); },
                        tagType: (row) => {
                            return '';
                        }
                    },
                    {
                        type: 'button',
                        title: '操 作',
                        width: '320',
                        buttons: [
                            {
                                name: '编辑',
                                handleClick: this.edit
                            },
                            {
                                name: '删 除',
                                handleClick: this.doDelete,
                                type: 'danger'
                            }
                        ]
                    }
                ]
            }
        };
    },
    computed: {
        userId() {
            return this.$store.state.user.id;
        }
    },
    methods: {
        edit(index, row) {
            this.$router.push({path: `${UrlConfig.bank.questionUpdate}/${row.id}`});
        },
        handlePreDataPaging(param) {

        },
        doDelete(index, row) {
            this.$confirm('删除试题，是否继续？', '提 示', {
                confirmButtonText: '确 定',
                cancelButtonText: '取 消',
                type: 'warning'
            }).then(() => {
                deleteQuestion(row.id).then(resp => {
                    this.reloadTable();
                }).catch(error => {
                    this.$message.error(`删除试题失败: ${error.message}`);
                });
            }).catch( () => { });
        },
        answerTags(row) {
            const answer = [];
            row.optionList.forEach((el, i) => {
                switch (row.type) {
                case 'select':
                    if (el.correct) {
                        answer.push(this.$asc2chart(i + 65));
                    }
                    break;
                case 'judge':
                    if (el.correct) {
                        answer.push(el.content);
                    }
                    break;
                case 'multi':
                    if (el.correct) {
                        answer.push(this.$asc2chart(i + 65));
                    }
                    break;
                case 'fill':
                    answer.push(el.content);
                    break;
                case 'answer':
                    answer.push(el.content);
                    break;
                }
            });
            return answer;
        },
        reloadTable() {
            this.queryList.reload();
        }
    }
};
</script>
