<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" draggable destroy-on-close @closed="$emit('closed')">
		<el-scrollbar max-height="500" style="padding: 0 20px;">
			<el-form class="form" :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right">
				<div class="cardBoxForm">
					<div class="comBox">
						<el-form-item label="上传头像" prop="avatar">
							<ossImgUpload @parentParams="parentParams" :url="form.avatar" />
							<div class="el-form-item-msg">请将图片拖到此处，或点击上传PNG、JPEG</div>
						</el-form-item>
					</div>
				</div>
				<div class="cardBoxForm">
					<div class="title">用户信息</div>
					<div class="comBox">
						<el-form-item label="昵称/工号" required>
							<el-row :gutter="24" justify="space-between" style="margin: 0;">
								<el-col :span="12" style="padding-left: 0;">
									<el-form-item prop="name">
										<el-input v-model="form.name" placeholder="请填写昵称" clearable></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="12" style="padding-right: 0;">
									<el-form-item prop="emp_id">
										<el-input v-model="form.emp_id" placeholder="请填写工号" clearable></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</el-form-item>
						<el-form-item label="登录账号" prop="login_name">
							<el-input v-model="form.login_name" placeholder="请填写登录账号" :disabled="mode!='add'?true:false" clearable></el-input>
							<p class="el-form-item-msg" v-if="mode == 'edit'">账号信息用于登录，系统不允许修改</p>
						</el-form-item>
						<el-form-item label="登录密码" prop="password" v-if="mode == 'add'">
							<el-input type="password" v-model="form.password" placeholder="请填写密码" clearable show-password></el-input>
						</el-form-item>
						<el-form-item label="公司名称" prop="company_full_name">
							<el-select v-model="form.company_full_name" placeholder="请填写公司名称" clearable style="width: 100%;">
								<el-option v-for="(item,index) in companyList" :key="index" :label="item.full_name" :value="item.full_name"></el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="联系电话" prop="mobile">
							<el-input v-model="form.mobile" placeholder="请填写联系电话" clearable></el-input>
						</el-form-item>
						<el-form-item label="电子邮件" prop="email">
							<el-input v-model="form.email" :disabled="mode!=='add'" placeholder="请填写电子邮件" clearable></el-input>
						</el-form-item>

					</div>
				</div>
				<div class="cardBoxForm">
					<div class="title">角色和分组</div>
					<div class="comBox">
						<el-form-item label="所属分组" prop="dept_id">
							<el-cascader v-model="form.dept_id" :options="depts" :props="deptsProps" placeholder="请选择分组" clearable style="width: 100%;"></el-cascader>
						</el-form-item>
						<el-form-item label="分配角色" prop="role_ids">
							<el-select v-model="form.role_ids" multiple filterable placeholder="请选择角色" style="width: 100%">
								<el-option v-for="item in groups" :key="item.id" :label="item.label" :value="item.id"/>
							</el-select>
						</el-form-item>
					</div>
				</div>
			</el-form>
		</el-scrollbar>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSave" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增用户',
					edit: '编辑用户',
					show: '查看'
				},
				visible: false,
				isSave: false,
				//表单数据
				form: {
					id:"",
					login_name: "",
					name:"",
					emp_id:"",
					mobile:"",
					email:"",
					avatar: "",
					password:"",
					dept_id: "",
					role_ids: [],
					company_full_name:""
				},
				//验证规则
				rules: {
					avatar:[
						{required: false, message: '请上传头像'}
					],
					login_name: [
						{required: true, message: '请输入登录账号'}
					],
					name: [
						{required: true, message: '请输入昵称'}
					],
					emp_id:[
						{required: true, message: '请输入工号'}
					],
					mobile: [
						{required: true, message: '请输入联系方式'}
					],
					email: [
						{required: true, message: '请输入邮箱地址'}
					],
					password: [
						{required: true, message: '请输入登录密码'},
						{validator: (rule, value, callback) => {
							if (this.form.password2 !== '') {
								this.$refs.dialogForm.validateField('password2');
							}
							callback();
						}}
					],
					password_cnf: [
						{required: true, message: '请再次输入密码'},
						{validator: (rule, value, callback) => {
							if (value !== this.form.password) {
								callback(new Error('两次输入密码不一致!'));
							}else{
								callback();
							}
						}}
					],
					dept_id: [
						{required: false, message: '请选择所属分组'}
					],
					role_ids: [
						{required: true, message: '请选择所属角色', trigger: 'change'}
					]
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					multiple: true,
					checkStrictly: true
				},
				depts: [],
				deptsProps: {
					value: "id",
					checkStrictly: false,
					emitPath:false
				},
				companyList:[]
			}
		},
		mounted() {
			this.getGroup()
			this.getDept()
			this.getCompanyList();
		},
		methods: {
			async getCompanyList() {
				let params = {
					field:"full_name",
					id:{
						operator:"in",
						value:[]
					}
				}
				const res = await this.$API.system.user.userCompanyField.post(params);
				if(res.code == 200){
					this.companyList = res.data;
				}
			},
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//加载树数据
			async getGroup(){
				const res = await this.$API.system.role.list.get();
				if(res.code == 200){
					this.groups = res.data.rows;
				}
			},
			async getDept(){
				const res = await this.$API.system.dept.active.post();
				if(res.code == 200){
					this.depts = res.data;
				}
			},
			parentParams(item){
				this.form.avatar = item;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSave = true;
						const res = await this.$API.system.user.add.post(this.form);
						this.isSave = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.login_name = data.login_name
				this.form.name = data.name
				this.form.emp_id = data.emp_id
				this.form.mobile = data.mobile
				this.form.email = data.email
				this.form.avatar = data.avatar
				this.form.dept_id = data.dept_id
				this.form.group = data.group
				this.form.role_ids = data.user_roles && data.user_roles.role_ids?data.user_roles.role_ids:[]
				this.form.company_full_name = data.company_info?data.company_info.full_name:''
			}
		}
	}
</script>

<style lang="scss" scoped>
.form{
	.cardBoxForm{
		border-bottom: 1px solid #f7f7f7;
		.title{
			padding: 15px 0 8px 18px;
			font-weight: bold;
		}
	}
	.cardBoxForm:last-child{
		border-bottom: 0;
	}
}
</style>
