<template>
  <!-- 添加或修改项目信息对话框 -->
  <el-dialog title="部门信息" v-model="visible" width="600px" append-to-body draggable>
    <div class="form-container">
      <el-form ref="deptRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级单位" prop="parentId">
              <el-tree-select
                v-model="form.parentId"
                :data="deptOptions"
                :props="{
                  value: 'deptId',
                  label: 'deptName',
                  children: 'children'
                }"
                value-key="deptId"
                placeholder="选择上级单位"
                check-strictly
                :render-after-expand="false"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="form.type === '1' ? '企业名称' : '部门名称'" prop="deptName">
              <el-input v-model="form.deptName" :placeholder="form.type === '1' ? '请输入企业名称' : '请输入部门名称'" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="orderNum">
              <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <template #footer>
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { listDept, getDept, saveDept, listDeptExcludeChild } from '@/api/system/dept'
const { proxy } = getCurrentInstance()
/** 父组件传参 */
const props = defineProps({
  getList: {
    type: Function,
    default: () => {}
  }
})
const { getList } = toRefs(props)
/** 字典数组区 */
/** 表单抽屉 页变量 */
const visible = ref(false)
const roleOptions = ref([])
const postOptions = ref([])
const initPassword = ref(undefined)
const deptOptions = ref([])
const disabled = ref(false)
const webHost = import.meta.env.VITE_APP_BASE_API
const { company_dept } = proxy.useDict('company_dept')
const data = reactive({
  form: {},
  rules: {}
})
const { form, rules } = toRefs(data)
/***********************  方法区  ****************************/
/** 打开抽屉 */
function open(row) {
  reset()
  visible.value = true
  listDept().then((response) => {
    deptOptions.value = deptOptions.value.concat(proxy.handleTree(response.data, 'deptId'))
  })
  if (row) {
    getDept(row.id).then((response) => {
      form.value = response.data
      form.value.parentId = row.parentId
      form.value.type = '0'
      disabled.value = true
    })
  } else {
    form.value.type = '1'
    disabled.value = true
  }
}

/** 取消按钮 */
function handleCancel() {
  visible.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    deptId: null,
    companyId: null,
    parentId: null,
    code: null,
    name: null,
    remark: null,
    sort: null,
    createTime: null,
    creatorId: null,
    updateTime: null,
    updaterId: null,
    deleted: null,
    version: null
  }
  disabled.value = false
  deptOptions.value = []
  proxy.resetForm('deptRef')
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['deptRef'].validate((valid) => {
    if (valid) {
      saveDept(form.value).then((response) => {
        proxy.$modal.msgSuccess('新增成功')
        visible.value = false
        getList.value()
      })
    }
  })
}

/** 暴露给父组件的方法 */
defineExpose({
  open
})
</script>
