<route lang="yaml">
meta:
  enabled: false
</route>

<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { createUser, getUserById, updateUser } from '@/api/system/userApi'
const emit = defineEmits(['success'])

const formRef = ref<FormInstance>()
const formRules = ref<FormRules>({
  accountName: [
    { required: true, message: '请选择帐号名称', trigger: 'blur' },
  ],
  accountRealName: [
    { required: false, message: '请选择姓名', trigger: 'blur' },
  ],
  accountPhone: [
    { required: false, message: '请输入手机号', trigger: 'blur' },
  ],
  accountStaffNo: [
    { required: false, message: '请输入工号', trigger: 'blur' },
  ],
  positionId: [
    { required: true, message: '请选择岗位', trigger: 'blur' },
  ],
})

const state = reactive({
  title: '',
  dialogVisible: false,
  loading: false,
  viewData: {
    isFixed: 1,
  },
  formData: {
    gmId: 0,
    gmpId: 0,
    accountId: 0, // 帐号ID
    groupId: 0, // 所属部门
    groupName: '',
    accountName: '', // 帐号名称
    password: '888888', // 密码
    accountRealName: '', // 姓名
    accountStaffNo: '', // 工号
    accountPhone: '', // 手机号
    positionId: 0, // 岗位,
    accountIntro: '',
    isFixed: 0,
  },
  actionType: 1,
})

const getData = async (groupId: number, accountId: number) => {
  state.loading = true
  const { data } = await getUserById(groupId, accountId)
  state.loading = false
  state.formData = data
}

function onSubmit() {
  formRef.value && formRef.value.validate(async (valid) => {
    if (valid) {
      if (state.actionType === 1) {
        const { msg } = await createUser(state.formData)
        ElMessage.success(msg)
        state.dialogVisible = false
        emit('success')
      }
      else {
        const { msg } = await updateUser(state.formData)
        ElMessage.success(msg)
        state.dialogVisible = false
        emit('success')
      }
    }
  })
}

function onCancel() {
  state.dialogVisible = false
}

function resetFormData() {
  state.formData = {
    gmId: 0,
    gmpId: 0,
    accountId: 0, // 帐号ID
    groupId: 0, // 所属部门
    groupName: '',
    accountName: '', // 帐号名称
    password: '888888', // 密码
    accountRealName: '', // 姓名
    accountStaffNo: '', // 工号
    accountPhone: '', // 手机号
    positionId: 0, // 岗位,
    accountIntro: '',
    isFixed: 0,
  }
}

defineExpose({
  showAddModel() {
    resetFormData()
    state.title = '新增账号'
    state.actionType = 1
    state.dialogVisible = true
  },
  showEditModel(groupId: number, accountId: number) {
    resetFormData()
    state.title = '编辑账号'
    state.actionType = 2
    state.dialogVisible = true
    getData(groupId, accountId)
  },

})
onMounted(() => {})
</script>

<template>
  <el-drawer v-model="state.dialogVisible" :title="state.title" size="30%">
    <el-form ref="formRef" :model="state.formData" :rules="formRules" label-width="120px" label-suffix="：">
      <el-form-item label="账号" prop="accountName">
        <el-input v-model="state.formData.accountName" placeholder="输入账号" />
      </el-form-item>

      <el-form-item label="所在部门" prop="groupId">
        <group-selector v-model="state.formData.groupId" />
      </el-form-item>

      <el-form-item label="密码">
        <span style="color: #eee">初始化密码为 888888</span>
      </el-form-item>

      <el-form-item label="姓名" prop="accountRealName">
        <el-input v-model="state.formData.accountRealName" placeholder="输入姓名" />
      </el-form-item>

      <el-form-item label="工号" prop="accountStaffNo">
        <el-input v-model="state.formData.accountStaffNo" max="11" placeholder="输入工号" />
      </el-form-item>

      <el-form-item v-show="state.formData.isFixed !== 1" label="岗位" prop="positionId">
        <position-selector v-model="state.formData.positionId" />
      </el-form-item>

      <el-form-item label="手机号">
        <el-input v-model="state.formData.accountPhone" max="11" placeholder="输入手机号码" />
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="state.formData.accountIntro" type="textarea" rows="6" max="127" placeholder="备注" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button size="large" @click="onCancel">
        取消
      </el-button>
      <el-button type="primary" size="large" @click="onSubmit">
        保存
      </el-button>
    </template>
  </el-drawer>
</template>
