<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" ></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
		},
		props:{
			orgData:{
				type:Array,
				default:[]
			}
		},
		data() {
			return {
				dialogTitle: '新增',
				dialogVisible: false,
				submitShow: true,
				formRow: configData.formRow,
				rules: configData.rules,
				vLoading: false
			}
		},
		methods: {
			openDialog(dialogTitle,orgId,id) {
				this.dialogVisible = true
				this.dialogTitle = dialogTitle
				this.formRow[14].elCol[1].options = this.orgData
				this.$nextTick(()=>{
					this.$refs.basicsForm.getField('sorts',1)
					this.$refs.basicsForm.getField('pawFrequency',1)
					this.$refs.basicsForm.getField('builtIn','1')
					this.$refs.basicsForm.getField('state','1')
					this.$refs.basicsForm.getField('sex','0')
					this.$refs.basicsForm.getField('lockState','0')
					this.$refs.basicsForm.getField('useState','1')
					this.$refs.basicsForm.getField('isLoginable','1')
					this.$refs.basicsForm.getField('isVirtual','1')
					this.$refs.basicsForm.getField('isAssigned','1')
					this.$refs.basicsForm.getField('isAdmin','1')
					this.$refs.basicsForm.getField('memberType','1')
					this.$refs.basicsForm.getField('isDeleted','1')
					this.$refs.basicsForm.getField('externalType','1')
					this.$refs.basicsForm.getField('personnelStatus','1')
					this.$refs.basicsForm.getField('department',orgId)
				})
				if(dialogTitle == '编辑'){
					this.getDetail(id)
				}
			},
			async getDetail(id){
				this.openLoading('detail')
				let res = await authApi('userService', 'user', 'queryEntity','',{id:id})
				if(res.status == '200'){
					this.$refs.basicsForm.choiceAssignment(res.attribute)
				}
			},

			handleConfirmClick() {
				async function submitForm(dialogTitle,ruleForm){
					let params = {
						...ruleForm
					}
					let res = await authApi('userService','user',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')
			},

		}
	}
</script>

<style>
</style>
