<template>
  <el-card>
    <div v-if="lessonName" slot="header" style="text-align: center;">
      <b>{{ $t('publish.lessonName') }}： {{ lessonName }}</b>
    </div>
    <div :style="{ height: height +'px' }">
      <el-scrollbar wrap-class="scrollbar-wrapper" style="">
        <QueryListPage
          ref="queryListPage"
          :pager-config="pagerConfig"
          :query-form="queryForm"
          :query-list="queryList"
          style="height: 100%;"
        />
      </el-scrollbar>
    </div>
  </el-card>
</template>

<script>
import { getExamList, deleteExam, setExamEfficacy, setExamEffectivey } from '@/api/management/exam';
import { getPublishLessonTree } from '@/api/jmap/lesson';
import { UrlConfig } from '@/router/index';

export default {
	name: 'List',
	data() {
		return {
			BizTypeList: [],
			OrderTypeList: [],
			PayTypeList: [],
			PayStatusList: [],
			OrganizationList: [],
			EffectiveTypeList: [],
			userId: this.$store.state.user.id,
			pagerConfig: {
				pageSize: 'pageSize',
				pageIndex: 'pageNum'
			},
			lessonName: '',
			queryForm: {
				labelWidth: '160px',
				queryObject: {
					'lessonId': {
						type: this.$route.query.lessonId ? '' : 'select',
						label: this.$t('publish.lessonName'),
						config: {
							data: []
						}
					},
					'name': {
						type: 'text',
						label: this.$t('publish.paperName')
					},
					'creatorName': {
						type: 'text',
						label: this.$t('publish.creator')
					}
				},
				reset: !this.$route.query.lessonId
			},
			queryList: {
				query: this.queryFunction,
				selectCheckShow: false,
				indexShow: true,
				columns: [
					{
						title: this.$t('publish.lessonName'),
						prop: 'lessonId',
						type: 'tag',
						show: !this.$route.query.lessonId,
						columnValue: (row) => { return this.$convertField(row.lessonId, this.OrganizationList, ['id', 'name']); },
						tagType: (row) => { return ''; }
					},
					{
						title: this.$t('publish.paperName'),
						prop: 'name'
					},
					{
						title: this.$t('publish.creator'),
						prop: 'creatorName'
					},
					{
						title: this.$t('global.isTry'),
						prop: 'trial',
						type: 'tag',
						columnValue: (row) => { return this.$ConstSelect.translate(row.trial, 'Whether'); },
						tagType: (row) => {
							switch (row.trial) {
							case true: return 'success';
							case false: return 'danger';
							}
						}
					},
					{
						title: this.$t('global.duration'),
						prop: 'duration',
						type: 'tag',
						columnValue: (row) => { return this.durationField(row.duration); },
						tagType: (row) => { return ''; }
					},
					{
						title: this.$t('publish.examTime'),
						prop: 'startTime',
						type: 'formatter',
						format: 'yyyy-MM-dd hh:ss'
					},
					{
						title: this.$t('publish.fullMark'),
						prop: 'fullPoint'
					},
					{
						title: this.$t('publish.passScore'),
						prop: 'passingPoint'
					},
					{
						title: this.$t('global.status'),
						prop: 'status',
						type: 'tag',
						columnValue: (row) => { return this.$convertField(row.status, this.EffectiveTypeList, ['value', 'label']); },
						tagType: (row) => {
							switch (row.status) {
							case '1': return 'success';
							default: return 'danger';
							}
						}
					},
					{
						type: 'button',
						title: this.$t('global.operate'),
						width: '380',
						buttons: [
							{
								name: this.$t('global.fastCreate'),
								type: 'primary',
								handleClick: this.handleDistribute,
								showControl: (row) => { return row.bizType !== '02'; }
							},
							{
								name: this.$t('global.edit'),
								type: 'primary',
								showControl: (row) => { return row.creatorId == this.userId; },
								handleClick: this.handleUpdate
							},
							{
								name: this.$t('global.soldOut'),
								type: 'warning',
								handleClick: this.handleEfficacy,
								showControl: (row) => {
									return !this.$route.query.lessonId && row.status == 1;
								}
							},
							{
								name: '删除',
								type: 'warning',
								handleClick: this.handleDelete,
								showControl: (row) => {
									return this.$route.query.lessonId && row.status == 1 && row.creatorId == this.userId;
								}
							},
							{
								name: this.$t('global.putaway'),
								type: 'primary',
								handleClick: this.handleEffective,
								showControl: (row) => {
									return (this.$route.query.lessonId ? row.creatorId == this.userId : true) && row.status != 1;
								}
							},
							{
								name: this.$t('global.delete'),
								type: 'danger',
								handleClick: this.deleteList,
								showControl: (row) => { return row.creatorId == this.userId; }
							}
						]
					}
				],
				actions: [
					{ text: this.$t('global.add'), btnCode: 'employee_insert', handler: this.handleNormalAdd }
				]
			}
		};
	},
	computed: {
		height() {
			return /\/exam\//.test(`${this.$route.path}`)? this.$store.state.app.height - 92: this.$store.state.app.height - 65;
		}
	},
	created() {
		this.loadInitData();
	},
	mounted() {
		this.$store.dispatch('exam/setCourseDetail', {});
		this.$store.dispatch('exam/setRuleList', []);
	},
	methods: {
		loadInitData() {
			this.queryForm.queryObject.lessonId.config.data.length = 0;
			getPublishLessonTree({ mapId: '' }).then(response => {
				this.OrganizationList = response.data;
				this.OrganizationList.forEach(elem => {
					if (elem.id == this.$route.query.lessonId) {
						this.lessonName = elem.name;
					}
					this.queryForm.queryObject.lessonId.config.data.push({ value: elem.id, label: elem.name });
				});
			});

			this.$Dictionary.effectiveType().then(list => {
				this.$convertList(list, this.EffectiveTypeList, elem => {
					return true;
				});
			});
		},
		durationField(fieldValue) {
			if (fieldValue) {
				const time = Number(fieldValue) / 60;
				return `${time}分钟`;
			}
		},
		queryFunction(params) {
			if (this.$route.query.lessonId) {
				params.lessonId = this.$route.query.lessonId;
				params.status = '1';
			}
			return getExamList(params);
		},
		handleNormalAdd() {
			const path = `${this.$route.path.match(/(\/.*)\/examRule/)[1]}${UrlConfig.examRuleDraft}`;
			const lessonId = this.$route.query.lessonId ? this.$route.query.lessonId : 0;
			this.$router.push({ path: `${path}/add/0/${lessonId}` });
		},
		handleUpdate(index, data) {
			const path = `${this.$route.path.match(/(\/.*)\/examRule/)[1]}${UrlConfig.examRuleDraft}`;
			const lessonId = this.$route.query.lessonId ? this.$route.query.lessonId : 0;
			this.$router.push({ path: `${path}/edit/${data.id}/${lessonId}` });
		},
		handleDistribute(index, data) {
			const path = `${this.$route.path.match(/(\/.*)\/examRule/)[1]}${UrlConfig.examRuleDraft}`;
			const lessonId = this.$route.query.lessonId ? this.$route.query.lessonId : 0;
			this.$router.push({ path: `${path}/add/${data.id}/${lessonId}` });
		},
		// 删除
		deleteList(index, data) {
			this.$confirm(this.$t('publish.wellDelPaper'), this.$t('global.tips'), {
				confirmButtonText: this.$t('global.confirm'),
				cancelButtonText: this.$t('global.cancel'),
				type: 'warning'
			}).then(() => {
				deleteExam(data).then(res => {
					this.queryList.reload();
					this.$message({
						type: 'success',
						message: this.$t('publish.setSuccess')
					});
				}).catch(res => {
					if (res.code == '500009') {
						this.$message({ type: 'warning', message: this.$t('error.paperHasUseNotDel') });
					} else {
						this.$message({ type: 'error', message: this.$t('error.deleteException') });
					}
				});
			}).catch(() => { });
		},
		// 下架
		handleDelete(index, data) {
			this.$confirm(this.$t('publish.wellDelPaper'), this.$t('global.tips'), {
				confirmButtonText: this.$t('global.confirm'),
				cancelButtonText: this.$t('global.cancel'),
				type: 'warning'
			}).then(() => {
				setExamEfficacy(data).then(res => {
					this.queryList.reload();
					this.$message({
						type: 'success',
						message: this.$t('publish.setSuccess')
					});
				}).catch(res => {
					this.$message({ type: 'warning', message: `${this.$t('error.setFailed')}${res.message}` });
				});
			}).catch(() => { });
		},
		// 下架
		handleEfficacy(index, data) {
			this.$confirm(this.$t('publish.wellSoldOutPaper'), this.$t('global.tips'), {
				confirmButtonText: this.$t('global.confirm'),
				cancelButtonText: this.$t('global.cancel'),
				type: 'warning'
			}).then(() => {
				setExamEfficacy(data).then(res => {
					this.queryList.reload();
					this.$message({
						type: 'success',
						message: this.$t('publish.setSuccess')
					});
				}).catch(res => {
					this.$message({ type: 'warning', message: `${this.$t('error.setFailed')}${res.message}` });
				});
			}).catch(() => { });
		},
		// 上架
		handleEffective(index, data) {
			this.$confirm(this.$t('publish.wellPutawayPaper'), this.$t('global.tips'), {
				confirmButtonText: this.$t('global.confirm'),
				cancelButtonText: this.$t('global.cancel'),
				type: 'warning'
			}).then(() => {
				setExamEffectivey(data).then(res => {
					this.queryList.reload();
					this.$message({
						type: 'success',
						message: this.$t('publish.setSuccess')
					});
				}).catch(res => {
					this.$message({ type: 'warning', message: `${this.$t('error.setFailed')}${res.message}` });
				});
			}).catch(() => { });
		}
	}
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    /deep/ {
        .is-always-shadow {
            box-shadow: none;
            border-bottom: none;
        }
    }
</style>
