<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" draggable destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="120px" label-position="right">
			<div class="boxHeader">
				<el-row :gutter="4" justify="space-between">
					<el-col :span="11">
						<el-form-item label="费用类型" prop="login_name">
							<el-select v-model="form.login_name" placeholder="请填写登录账号" clearable style="width: 100%;">
								<el-option></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="4" justify="space-between">
					<el-col :span="11">
						<el-form-item label="快递单号" prop="login_name">
							<el-input v-model="form.login_name" placeholder="请填写快递单号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="11" :sm="11">
						<el-form-item label="快递发货日期" prop="login_name">
							<el-date-picker v-model="form.login_name" type="date" placeholder="请选择发货日期" clearable value-format="YYYY-MM-DD"></el-date-picker>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="4" justify="space-between">
					<el-col :span="11">
						<el-form-item label="发货地址" prop="login_name">
							<el-input v-model="form.login_name" placeholder="请填写发货地址" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="11">
						<el-form-item label="调拨地址" prop="login_name">
							<el-input v-model="form.login_name" placeholder="请填写调拨地址" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</div>
			<div class="boxFooter">
				<div class="title">费用项</div>
				<el-row :gutter="4" justify="space-between">
					<el-col :span="11">
						<el-form-item label="运费" prop="login_name">
							<el-input v-model="form.login_name" placeholder="请填写运费" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="11">
						<el-form-item label="备件数量" prop="login_name">
							<el-input v-model="form.login_name" placeholder="请填写备件数量" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="4" justify="space-between">
					<el-col :span="11">
						<el-form-item label="税费" prop="login_name">
							<el-input v-model="form.login_name" placeholder="请填写税费" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="11">
						<el-form-item label="备件数量" prop="login_name">
							<el-input v-model="form.login_name" placeholder="请填写备件数量"  clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="4" justify="space-between">
					<el-col :span="11">
						<el-form-item label="仓储费" prop="login_name">
							<el-input v-model="form.login_name" placeholder="请填写仓储费" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="11">
						<el-form-item label="备件数量" prop="login_name">
							<el-input v-model="form.login_name" placeholder="请填写备件数量" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</div>
		</el-form>
		<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: "confirm",
			titleMap: {
				confirm: '数据确认费用',
			},
			visible: false,
			isSave: false,
			//表单数据
			form: {
				id:"",
				login_name: "",
				name:"",
				emp_id:"",
				mobile:"",
				email:"",
				avatar: "",
				password:"",
				dept_id: "",
				role_ids: []
			},
			//验证规则
			rules: {
				avatar:[
					{required: true, 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: true, message: '请选择所属部门'}
				],
				role_ids: [
					{required: true, message: '请选择所属角色', trigger: 'change'}
				]
			},
			//所需数据选项
			groups: [],
			groupsProps: {
				value: "id",
				multiple: true,
				checkStrictly: true
			},
			depts: [],
			deptsProps: {
				value: "id",
				checkStrictly: false,
				emitPath:false
			}
		}
	},
	mounted() {
		this.getGroup()
		this.getDept()
	},
	methods: {
		//显示
		open(mode='confirm'){
			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:[]

		}
	}
}
</script>

<style>
</style>
