<template>
  <el-dialog
    title="选择权限"
    :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: '是' },
				{ value: false, label: '否' }
			],
			pagerConfig: {
				pageSize: 'pageSize',
				pageIndex: 'pageNum'
			},
			queryForm: {
				labelWidth: '80px',
				reset: true,
				queryObject: {
					name: {
						type: 'text',
						label: '权限名称'
					},
					type: {
						type: 'select',
						label: this.$t('orderAuthor.permissionType'),
						config: {
							data: []
						}
					}
				}
			},
			queryList: {
				query: this.queryFunction,
				selectCheckShow: false,
				indexShow: true,
				columns: [
					{
						title: '权限名称',
						prop: 'name'
					},
					{
						title: '是否包权限',
						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: '创建者',
						prop: 'creatorUserName'
					},
					{
						title: '创建时间',
						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>
