<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: DY
 * @LastEditTime: 2023-11-25 18:09:05
 * @Description:
-->
<template>
	<div class="dialog">
		<el-form
			:model="dataForm"
			:rules="dataRule"
			ref="dataForm"
			@keyup.enter.native="dataFormSubmit()"
			label-width="80px">
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="部门名称" prop="name">
						<el-input v-model="dataForm.name" clearable placeholder="请输入部门名称" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="部门编码" prop="code">
						<el-input v-model="dataForm.code" clearable placeholder="请输入部门编码" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="上级部门" prop="parentId">
						<treeselect v-model="dataForm.parentId" :options="menuOptions" :normalizer="normalizer" :show-count="true" placeholder="选择上级部门" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="备注" prop="remark">
						<el-input
							v-model="dataForm.remark"
							placeholder="请输入备注" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</div>
</template>

<script>
import basicAdd from '../../core/mixins/basic-add';
import { createCoreDepartment, updateCoreDepartment, getCoreDepartment, getCode, getCoreDepartmentList } from "@/api/base/coreDepartment";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
	mixins: [basicAdd],
	components: { Treeselect },
	data() {
		return {
			urlOptions: {
			  isGetCode: true,
        codeURL: getCode,
				createURL: createCoreDepartment,
				updateURL: updateCoreDepartment,
				infoURL: getCoreDepartment,
			},
			dataForm: {
        id: undefined,
        code: undefined,
        name: undefined,
        parentId: undefined,
        remark: undefined,
			},
			departmentlList: [],
			menuOptions: [],
			isChoosed: false,
			dataRule: {
        code: [{ required: true, message: "部门编码不能为空", trigger: "blur" }],
        name: [{ required: true, message: "部门名称不能为空", trigger: "blur" }],
      }
		};
	},
	mounted() {
		this.getDict()
	},
	methods: {
		async getDict() {
			// 部门列表
			this.menuOptions = []
			const res = await getCoreDepartmentList();
			this.departmentlList = res.data.map(item => {
				item.parentId = item.parentId ? item.parentId : 0
				return item
			});
			const menu = { id: 0, name: '总部门', children: [] };
			menu.children = this.handleTree(this.departmentlList, "id")
			this.menuOptions.push(menu)
		},
		/** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    }
	},
};
</script>

<style scoped>
.dialog {
	min-height: 180px;
}
</style>
