<template>
  <div style="height: 100%; padding-bottom: 20px">
    <el-card>
      <div slot="header" style="text-align: center;">
        <b>{{ $t('global.examSystem') }}</b>
      </div>
    </el-card>
    <el-card v-loading="loading">
      <el-row style="padding: 10px;display: flex;align-items: center;justify-content: flex-end;">
        <div style="font-size: 14px;color: #000;">试卷名称：</div>
        <el-input v-model="inputName" size="small" style="width: 200px;margin-right: 50px;margin-left: 10px;" placeholder="请输入筛选的名称" @change="changeInput" />
        <el-button size="small" type="primary" @click="goToFilter">查找</el-button>
      </el-row>
      <el-table ref="filterTable" :data="filterTableData" border style="width: 100%" height="calc(100vh - 150px)">
        <el-table-column prop="name" label="试卷名称" />
        <el-table-column v-if="isGzbShow" prop="classNames" label="所属班级">
          <template slot-scope="scope">
            <el-tag v-for="(item, index) in scope.row.classNames" :key="index" type="success">{{ item }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="duration" label="考试时长（分钟）">
          <template slot-scope="scope">
            <span>{{ Number(scope.row.duration) / 60 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="fullPoint" label="总分" />
        <el-table-column prop="remarks" show-overflow-tooltip label="试卷说明" />
        <el-table-column :label="this.$t('global.operate')">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="goExamDetail(scope.row)">
              考试详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getSubSystemDetail } from '@/api/trainingPlatform';
import { UrlConfig } from '@/scripts/ConstDic';
import { getSessionStorage } from '@/utils/auth';
import { lessonCreater } from '@/router/index';
import localStore from 'storejs';

export default {
    name: 'ExamHome',
    data() {
        return {
            tableData: [],
            filterTableData: [],
            loading: false,
            project: '',
            isTeacher: false,
            userId: '',
            inputName: ''
        };
    },
    computed: {
        isGzbShow() {
            return getSessionStorage('project').startsWith('gzb');
        }
    },
    watch: {
        '$route.params.subSystem': function(newVal) {
            this.loadInitPage();
        }
    },
    mounted() {
        this.loadInitPage();
        this.project = getSessionStorage('project');
        this.isTeacher = this.$store.state.user.roles.includes(lessonCreater);
        this.userId = this.$store.state.user.id;
    },
    methods: {
        loadInitPage() {
            if (this.$route.params.subSystem) {
                getSubSystemDetail(this.$route.params.subSystem).then(resp =>{
                    if (resp.data) {
                        this.tableData = resp.data.examList;
                    } else {
                        this.tableData = [];
                    }
                    this.inputName = localStore.get(this.$route.path) || '';
                    this.goToFilter();
                }).catch((error)=>{
                    if (error.code == 30001) {
                        const url = localStore.get('orignalTrainingPlatformRoute' + this.$store.state.user.id + this.project);
                        if (url) {
                            this.$router.push(url);
                        }
                    } else {
                        this.$messageBox(this.$t('error.obtainCourseInformationFailed'));
                    }
                });
            }
        },
        goExamDetail(row) {
            const path = `${this.$route.path.match(/(\/.*)\/examHome/)[1]}${UrlConfig.examDetail}`;
            if (path.includes('device')) {
                this.$router.replace({ path: `${path}/${row.id}`, query: { subSystem: this.$route.params.subSystem, mapId: row.mapId, noPreLogout: this.$route.query.noPreLogout }});
            } else {
                this.$router.push({ path: `${path}/${row.id}`, query: { subSystem: this.$route.params.subSystem, mapId: row.mapId, noPreLogout: this.$route.query.noPreLogout }});
            }
        },
        changeInput(val) {
            localStore.set(this.$route.path, val);
        },
        goToFilter() {
            if (!this.inputName) {
                this.filterTableData = [...this.tableData];
            } else {
                this.filterTableData = [];
                this.tableData.forEach(item => {
                    if (item.name.includes(this.inputName)) {
                        this.filterTableData.push(item);
                    }
                });
            }
        }
    }
};
</script>
