<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" size="80%" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" ref="dialogForm" label-width="150px" style="padding: 10px">
			<el-form-item label="公司名称">
				<el-input v-model="form.name" placeholder="公司名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="营业执照编号">
				<el-input v-model="form.reg_code" placeholder="营业执照编号" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系人" prop="contact">
				<sc-form-table v-model="form.contact" :addTemplate="addTemplate" >
					<el-table-column prop="name" label="姓名" width="180">
						<template #default="scope"><el-input v-model="scope.row.name" placeholder="请输入姓名"></el-input></template>
					</el-table-column>
					<el-table-column prop="name" label="性别" width="180">
						<template #default="scope"><el-input v-model="scope.row.sex" placeholder="请输入姓名"></el-input></template>
					</el-table-column>
					<el-table-column prop="name" label="电话" width="180">
						<template #default="scope"><el-input v-model="scope.row.mobile" placeholder="请输入姓名"></el-input></template>
					</el-table-column>
					<el-table-column prop="name" label="电子邮箱" width="180">
						<template #default="scope"><el-input v-model="scope.row.email" placeholder="请输入姓名"></el-input></template>
					</el-table-column>
					<el-table-column prop="name" label="职位" width="180">
						<template #default="scope"><el-input v-model="scope.row.job" placeholder="请输入姓名"></el-input></template>
					</el-table-column>
				</sc-form-table>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button type="primary" @click="submit()">确认提交</el-button>
		</template>
	</el-drawer>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				contact: [{	name: ''}]
			},
			//验证规则
			rules: {
				sort: [
					{required: true, message: '请输入排序', trigger: 'change'}
				],
				label: [
					{required: true, message: '请输入角色名称'}
				],
				alias: [
					{required: true, message: '请输入角色别名'}
				]
			},
			addTemplate: {
				id: '',
				name: '',
				email: '',
				mobile: '',
				job: '',
				sex: ''
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode='add'){ 
			this.mode = mode;
			this.visible = true;
			return this
		},
		//加载树数据
		async getGroup(){
			var res = await this.$API.user.department.list.get();
			this.groups = res.data;
		},
		//表单提交方法
		submit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = {}
					if(this.mode == 'add'){
						res = await this.$API.customer.company.add.post(this.form);
					}else{
						res = await this.$API.customer.company.edit.post(this.form);
					}
					this.isSaveing = false;
					if(res.code == 1){
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					}else{
						this.$alert(res.msg, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data){
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			Object.assign(this.form, data)
		}
	}
}
</script>