<template>
	<base-right-dialog ref="baseRightDialog" :footerShow="true" :dialogVisible.sync="dialogVisible" :appendBody="true"
		:title="dialogTitle + ' 角色'" :submitShow="submitShow" @handleClose="handleDialogClose"
		@handleConfirmClick="handleConfirmClick">
		<base-form ref="basicsForm" :formRow="formRow" :isFunBtn="false" :rules="rules" class="dialog_form"
			:spanWidth="`120px`" :loading="vLoading" @elDialogClick="elDialogClick"></base-form>
	</base-right-dialog>
</template>

<script>
	import baseRightDialog from "@/components/base/baseRightDialog";
	import baseForm from "@/components/base/baseNewForm/index.vue";
	import configData from "./configData";
	import {
		authApi
	} from '@/api/apis/auth'
	export default {
		components: {
			baseRightDialog,
			baseForm
		},
		data() {
			return {
				dialogTitle: '新增',
				dialogVisible: false,
				submitShow: true,
				formRow: configData.formRow,
				rules: configData.rules,
				vLoading: false
			}
		},
		methods: {
			openDialog(dialogTitle,id) {
				this.dialogVisible = true
				this.dialogTitle = dialogTitle
				this.formRow[0].elCol[1].disabled = false
				this.getMenuData()
				this.$nextTick(()=>{
					this.$refs.basicsForm.getField('sorts',1)
					this.$refs.basicsForm.getField('isEnable','1')
					this.$refs.basicsForm.getField('externalType','0')
					this.$refs.basicsForm.getField('isDeleted','0')
					this.$refs.basicsForm.getField('frontAuthorization','1')
					this.$refs.basicsForm.getField('personSelectionInterface','1')
					this.$refs.basicsForm.getField('isBenchmark','1')
				})
				if(dialogTitle == '编辑'){
					this.formRow[0].elCol[1].disabled = true
					this.getDetail(id)
				}
			},
			async getDetail(id){
				this.openLoading('detail')
				let res = await authApi('roleService', 'role', 'queryEntity','',{id:id})
				if(res.status == '200'){
					this.$refs.basicsForm.choiceAssignment(res.attribute)
				}
			},
			// 获得树状图
			async getMenuData() {
				let res = await authApi('orgService', 'org', 'queryTree')
				if(res.status == '200'){
					this.formRow[1].elCol[1].options = res.attribute
				}
			},
			handleConfirmClick() {
				async function submitForm(dialogTitle,ruleForm){
					let params = {
						...ruleForm
					}
					let res = await authApi('roleService','role',dialogTitle == '新增'?'addEntity':'updateEntity','',params)
					return res
				}
				this.$refs.basicsForm.$refs['ruleForm'].validate((valid) => {
					if(!valid){
						return
					}else{
						submitForm(this.dialogTitle,this.$refs.basicsForm.ruleForm).then(res=>{
							if(res.status == '200'){
								this.$vmNews('保存成功', 'success')
								this.handleDialogClose()
							}
						})
					}
				})
			},
			handleDialogClose() {
				this.dialogVisible = false
				this.$emit('handleDialogClose')
			},
			elDialogClick() {},

		}
	}
</script>

<style>
</style>
