<template>
  <el-dialog
    class="org-model"
    v-model="state"
    width="1112"
    :destroy-on-close="true"
    :align-center="true"
    @closed="onClosed"
  >
    <div class="title">
      <div class="text">{{ popName }}用户</div>
      <Icon @click="goClose" class="close" icon="ep:close" />
    </div>

    <div :class="['content', { disabled: type === 'check' }]">
      <el-form ref="formRef" :model="params" :rules :show-message="false">
        <div class="block-container road-setting">
          <el-form-item prop="deptId" label="所属组织：">
            <el-tree-select
              v-model="params.deptId"
              :data="deptList"
              :props="defaultProps"
              check-strictly
              node-key="id"
              placeholder="请选择归属机构"
            />
          </el-form-item>

          <el-form-item prop="nickname" label="姓名：">
            <el-input v-model="params.nickname" placeholder="请输入..." />
          </el-form-item>

          <el-form-item prop="username" label="用户名：">
            <el-input v-model="params.username" placeholder="请输入..." />
          </el-form-item>

          <el-form-item prop="password" label="密码：" v-if="type == 'new'">
            <el-input v-model="params.password" show-password placeholder="请输入..." />
          </el-form-item>

          <el-form-item prop="password2" label="确认密码：" v-if="type == 'new'">
            <el-input v-model="params.password2" show-password placeholder="请输入..." />
          </el-form-item>

          <el-form-item prop="mobile" label="手机号码：">
            <el-input v-model="params.mobile" placeholder="请输入..." />
          </el-form-item>

          <el-form-item prop="userType" label="用户类型：">
            <el-select :teleported="false" v-model="params.userType" placeholder="请选择...">
              <el-option
                v-for="(item, i) in getIntDictOptions(DICT_TYPE.USER_TYPE_DATA) || []"
                :key="i"
                :value="item.value"
                :label="item.label"
              />
              <template #empty> 没有数据 </template>
            </el-select>
          </el-form-item>

          <el-form-item prop="roleIds" label="角色：">
            <el-select
              :teleported="false"
              multiple
              v-model="params.roleIds"
              placeholder="请选择..."
            >
              <el-option
                v-for="(item, i) in roleList"
                :key="i"
                :value="item.id"
                :label="item.name"
              />
              <template #empty> 没有数据 </template>
            </el-select>
          </el-form-item>

          <el-form-item prop="status" label="账户状态：">
            <el-select :teleported="false" v-model="params.status" placeholder="请选择...">
              <el-option :value="0" label="正常" />
              <el-option :value="1" label="关闭" />
              <template #empty> 没有数据 </template>
            </el-select>
          </el-form-item>

          <el-form-item prop="remark" label="备注：">
            <el-input type="textarea" rows="2" v-model="params.remark" placeholder="请输入..." />
          </el-form-item>
        </div>
      </el-form>
    </div>

    <template #footer>
      <div class="footer">
        <el-button v-if="type === 'check'" type="primary" @click="goClose">关闭</el-button>
        <template v-else>
          <el-button @click="goClose">取消</el-button>
          <el-button @click="onSubmit" type="primary">确定</el-button>
        </template>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { defaultProps, handleTree } from '@/utils/tree'
import {
  getSimpleDeptList,
  getroleList,
  userCreate,
  getDetails,
  updateUser
} from '@/api/system/user'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
const emits = defineEmits(['update'])

const state = ref(false)
const type = ref<any>(null)
const params = reactive({})
const deptList = ref<any>([])
const roleList = ref<any>([])

const formRef = ref(null)

const rules = reactive<any>({
  deptId: [{ required: true }],
  nickname: [{ required: true }],
  username: [{ required: true }],
  password: [{ required: true }],
  password2: [{ required: true }],
  userType: [{ required: true }],
  roleIds: [{ required: true }],
  status: [{ required: true }]
})

const popName = computed(() => {
  return {
    check: '查看',
    edit: '编辑',
    new: '新增'
  }[type.value]
})

const diaOpen = async (xtype, e?) => {
  if (e) {
    const res = await getDetails({ id: e.id })
    Object.keys(res).forEach((key) => {
      if (key == 'userType') {
        params.userType = Number(res.userType)
      } else {
        params[key] = res[key]
      }
    })
  }
  type.value = xtype
  state.value = true
}

const goClose = () => {
  state.value = false
}

const onClosed = () => {
  // 数据清除
  params.deptId = ''
  params.nickname = ''
  params.username = ''
  params.password = ''
  params.password2 = ''
  params.userType = ''
  params.roleIds = ''
  params.status = ''
  params.mobile = ''
  params.remark = ''
  params.id = ''
}

// 提交
const onSubmit = async () => {
  const base = await new Promise((resolve) => {
    formRef.value?.validate((e) => {
      resolve(e)
    })
  })
  if (!base) {
    return ElMessage.error('请完成数据填写！')
  }

  if (params.password != params.password2) {
    return ElMessage.error('请确认密码是否一致！')
  }

  if (type.value === 'new') {
    await userCreate(params)
  } else {
    delete params.password
    await updateUser(params)
  }

  ElMessage.success(type.value === 'edit' ? '修改成功！' : '创建成功！')
  state.value = false
  emits('update')
}
//获取组织列表
const getDeptlist = async () => {
  const res = await getSimpleDeptList(params)
  deptList.value = handleTree(res)
}
const getRolelist = async () => {
  const res = await getroleList()
  roleList.value = res
}
defineExpose({
  diaOpen,
  getDeptlist,
  getRolelist
})
</script>

<style lang="scss" scoped>
$base_url: '@/assets/images/home/';

.title {
  height: 48px;
  display: flex;
  align-content: center;
  align-items: center;
  color: white;
  padding: 0 20px;
  font-size: 18px;
  font-weight: 600;
  justify-content: space-between;
  .close {
    cursor: pointer;
  }
}

.content {
  padding: 0 16px;
  // height: 600px;

  .block-container {
    padding-right: 24px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    margin-top: 20px;
  }

  &.disabled {
    :deep(.base-block) {
      pointer-events: none;
    }
    :deep(.road-setting) {
      pointer-events: none;
    }
    :deep(.el-input) {
      background: rgba($color: #ffffff, $alpha: 0.2);
    }
  }
}

.footer {
  // background: rgba(69, 116, 182, 0.36);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 20px 24px;
  color: white;
}
</style>

<style lang="scss">
.org-model {
  padding: 0;
  box-sizing: border-box;
  // border: 1px solid rgba(255, 255, 255, 0.5);
  background: #0d3561;
  border-radius: 4px;

  .el-dialog__header {
    display: none;
  }
  .el-dialog__footer {
    padding-top: 0;
  }
  .def-btn {
    background: #1270ff;
    border: 1px solid rgba(155, 193, 255, 0.74);
    height: 28px;
    padding: 8px 10px;
    color: white;
    &:hover {
      border: 1px solid rgba(255, 255, 255, 0.8);
    }
  }
}
</style>
