<template>
  <div>
    <el-dialog v-model="state.showDialog" destroy-on-close :title="title" draggable width="769px">
      <el-form ref="formRef" :model="form" size="default" label-width="80px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="姓名" prop="name" :rules="[{ required: true, message: '请输入姓名', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.name" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item
              label="手机号"
              prop="mobile"
              :rules="[
                { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
                { validator: testMobile, trigger: ['blur', 'change'] },
              ]"
            >
              <el-input v-model="form.mobile" autocomplete="off" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="部门" prop="orgIds" :rules="[{ required: true, message: '请选择部门', trigger: ['change'] }]">
              <el-tree-select
                ref="orgTreeSelectRef"
                v-model="form.orgIds"
                placeholder="请选择部门"
                :data="state.orgTreeData"
                node-key="id"
                :props="{ label: 'name' }"
                check-strictly
                default-expand-all
                render-after-expand
                fit-input-width
                clearable
                multiple
                collapse-tags
                collapse-tags-tooltip
                class="w100"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="主属部门" prop="orgId" :rules="[{ required: true, message: '请选择主属部门', trigger: ['change'] }]">
              <el-select v-model="form.orgId" placeholder="请选择主属部门" class="w100">
                <el-option v-for="item in state.orgs" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="用户名" prop="userName" :rules="[{ required: true, message: '请输入用户名', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.userName" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col v-if="!(form.id > 0)" :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
            <el-form-item label="密码" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: ['blur', 'change'] }]">
              <el-input key="password" v-model="form.password" show-password autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="直属主管" prop="managerUserId">
              <my-select-user v-model="form.managerUserId" :name="form.managerUserName" clearable></my-select-user>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="角色" prop="roles">
              <el-tree-select
                v-model="form.roleIds"
                placeholder="请选择角色"
                :data="state.roleTreeData"
                node-key="id"
                :props="{ label: 'name' }"
                default-expand-all
                render-after-expand
                fit-input-width
                clearable
                multiple
                collapse-tags
                collapse-tags-tooltip
                class="w100"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="邮箱" prop="email" :rules="[{ validator: testEmail, trigger: ['blur', 'change'] }]">
              <el-input v-model="form.email" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSure" size="default" :loading="state.sureLoading">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, toRefs, getCurrentInstance, ref, watch, defineAsyncComponent } from 'vue'
import { UserAddInput, UserUpdateInput, OrgListOutput, RoleGetListOutput } from '/@/api/admin/data-contracts'
import { User as UserApi } from '/@/api/admin/User'
import { Org as OrgApi } from '/@/api/admin/Org'
import { Role as roleApi } from '/@/api/admin/Role'
import { listToTree, treeToList } from '/@/utils/tree'
import { cloneDeep } from 'lodash-es'
import { testMobile, testEmail } from '/@/utils/test'
import eventBus from '/@/utils/mitt'

// 引入组件
const MySelectUser = defineAsyncComponent(() => import('./my-select-user.vue'))

defineProps({
  title: {
    type: String,
    default: '',
  },
})

const { proxy } = getCurrentInstance() as any

const orgTreeSelectRef = ref()
const formRef = ref()
const state = reactive({
  showDialog: false,
  sureLoading: false,
  form: {
    orgIds: [] as any,
    roleIds: [] as any,
  } as UserAddInput & UserUpdateInput,
  orgs: [] as any,
  orgTreeData: [] as OrgListOutput[],
  roleTreeData: [] as RoleGetListOutput[],
})
const { form } = toRefs(state)

watch(
  () => state.form.orgIds,
  (value) => {
    if (value && value.length > 0) {
      let orgs = [] as any
      treeToList(cloneDeep(state.orgTreeData)).forEach((a: any) => {
        if (value.some((b) => a.id === b)) {
          orgs.push(a)
        }
      })
      state.orgs = orgs
    } else {
      state.orgs = []
    }
  },
  {
    immediate: true,
  }
)

watch(
  () => state.orgs,
  () => {
    if (state.orgs?.some((a: any) => a.id === state.form.orgId)) {
      return
    }
    state.form.orgId = state.orgs && state.orgs.length > 0 ? state.orgs[0].id : undefined
  },
  {
    immediate: true,
    deep: true,
  }
)

const getOrgs = async () => {
  const res = await new OrgApi().getList()
  if (res?.success && res.data && res.data.length > 0) {
    state.orgTreeData = listToTree(res.data)
  } else {
    state.orgTreeData = []
  }
}

const getRoles = async () => {
  const res = await new roleApi().getList()
  if (res?.success && res.data && res.data.length > 0) {
    state.roleTreeData = listToTree(res.data)
  } else {
    state.roleTreeData = []
  }
}

// 打开对话框
const open = async (row: any = {}) => {
  proxy.$modal.loading()

  await getOrgs()
  await getRoles()

  if (row.id > 0) {
    const res = await new UserApi().get({ id: row.id }).catch(() => {
      proxy.$modal.closeLoading()
    })

    if (res?.success) {
      state.form = res.data as UserAddInput & UserUpdateInput
    }
  } else {
    state.form = {
      orgIds: [] as any,
      roleIds: [] as any,
      staff: {},
    } as UserAddInput & UserUpdateInput
  }

  proxy.$modal.closeLoading()
  state.showDialog = true
}

// 取消
const onCancel = () => {
  state.showDialog = false
}

// 确定
const onSure = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (!valid) return

    state.sureLoading = true
    let res = {} as any
    if (state.form.id != undefined && state.form.id > 0) {
      res = await new UserApi().update(state.form, { showSuccessMessage: true }).catch(() => {
        state.sureLoading = false
      })
    } else {
      res = await new UserApi().add(state.form, { showSuccessMessage: true }).catch(() => {
        state.sureLoading = false
      })
    }
    state.sureLoading = false

    if (res?.success) {
      eventBus.emit('refreshUser')
      state.showDialog = false
    }
  })
}

defineExpose({
  open,
})
</script>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  name: 'admin/user/form',
})
</script>
