<template>
  <el-dialog
    v-dialogDrag
    :title="this.$t('orderAuthor.selectPermission')"
    :visible.sync="show"
    top="20px"
    width="90%"
    :before-do-close="doClose"
    :close-on-click-modal="false"
  >
    <QueryListPage ref="queryListPage" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList" />
  </el-dialog>
</template>

<script>
import { getLessonPermissonPageList } from '@/api/management/author';

export default {
	name: 'AddGoods',
	props: {
		trainings: {
			type: Array,
			default() {
				return [];
			}
		},
		detail: {
			type: Object,
			default() {
				return null;
			}
		}
	},
	data() {
		return {
			show: false,
			PermissionTypeList: [],
			ruleLists: [
				{ value: true, label: this.$t('global.yes') },
				{ value: false, label: this.$t('global.no') }
			],
			pagerConfig: {
				pageSize: 'pageSize',
				pageIndex: 'pageNum'
			},
			queryForm: {
				labelWidth: '80px',
				reset: true,
				queryObject: {
					name: {
						type: 'text',
						label: this.$t('orderAuthor.permissionName')
					},
					type: {
						type: 'select',
						label: this.$t('orderAuthor.permissionType'),
						config: {
							data: []
						}
					}
				}
			},
			queryList: {
				query: this.queryFunction,
				selectCheckShow: false,
				indexShow: true,
				columns: [
					{
						title: this.$t('orderAuthor.permissionName'),
						prop: 'name'
					},
					{
						title: this.$t('orderAuthor.isPackage'),
						prop: 'isPackage',
						type: 'tag',
						columnValue: (row) => { return this.$convertField(row.isPackage, this.ruleLists, ['value', 'label']); },
						tagType: (row) => { return ''; }
					},
					{
						title: this.$t('orderAuthor.permissionType'),
						prop: 'type',
						type: 'tag',
						columnValue: (row) => { return this.$convertField(row.type, this.PermissionTypeList, ['value', 'label']); },
						tagType: (row) => { return ''; }
					},
					{
						title: this.$t('orderAuthor.founder'),
						prop: 'creatorUserName'
					},
					{
						title: this.$t('orderAuthor.creationTime'),
						prop: 'createTime'
					},
					{
						type: 'button',
						title: this.$t('global.operate'),
						width: '250',
						buttons: [
							{
								name: this.$t('orderAuthor.select'),
								type: 'primary',
								handleClick: this.handleAdd
							}
						]
					}
				],
				actions: [
				]
			},

			currentModel: {}
		};
	},
	created() {
		this.loadInitData();
	},
	methods: {
		doShow() {
			this.show = true;
			this.reloadTable();
		},
		doClose() {
			this.show = false;
		},
		async queryFunction(params) {
			return getLessonPermissonPageList(params);
		},
		async loadInitData() {
			this.$Dictionary.permissionType().then(list => {
				list.forEach(elem => {
					this.queryForm.queryObject.type.config.data.push({ value: elem.code, label: elem.name });
				});
				this.$convertList(list, this.PermissionTypeList, elem => {
					return true;
				});
			});
		},

		handleAdd(index, row) {
			this.$emit('selectGoods', row);
			this.doClose();
		},

		reloadTable() {
			if (this.queryList && this.queryList.reload) {
				this.queryList.reload();
			}
		}
	}
};
</script>
