<template>
  <el-drawer v-model="isDrawer" :size="width" :title="title" @close="handleReset">
    <el-form
      :model="formParams"
      :rules="rules"
      ref="formRef"
      label-placement="left"
      :label-width="80"
    >
      <el-form-item label="岗位编码" prop="postCode">
        <el-input
          placeholder="请输入岗位编码"
          v-model="formParams.postCode"
          :disabled="formParams.postId ? true : false"
        />
      </el-form-item>
      <el-form-item label="岗位名称" prop="postName">
        <el-input placeholder="请输入岗位名称" v-model="formParams.postName" />
      </el-form-item>
      <el-form-item label="排序" prop="orderNum">
        <el-input-number placeholder="请输入排序" v-model="formParams.orderNum" />
      </el-form-item>
      <el-form-item label="状态" prop="isEnable">
        <el-switch v-model="formParams.isEnable">
          <template #checked> 启用 </template>
          <template #unchecked> 禁用 </template>
        </el-switch>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-space>
        <el-button @click="handleReset">重置</el-button>
        <el-button type="primary" :loading="subLoading" @click="formSubmit">提交</el-button>
      </el-space>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { ElMessage } from 'element-plus';
  import type { formParamsType } from './types';

  import { addPost, editPost, postInfo } from '@/api/auth/post';

  const rules = {
    postCode: {
      required: true,
      message: '岗位编码不能为空',
      trigger: 'blur',
    },
    postName: {
      required: true,
      message: '岗位名称不能为空',
      trigger: 'blur',
    },
  };

  const emit = defineEmits(['change']);

  defineProps({
    title: {
      type: String,
      default: '添加岗位',
    },
    width: {
      type: Number,
      default: 450,
    },
  });

  const defaultValueRef = () => ({
    postId: null,
    postCode: '',
    isEnable: true,
    postName: '',
    orderNum: undefined,
  });

  const message = ElMessage;
  const formRef: any = ref(null);
  const isDrawer = ref(false);
  const subLoading = ref(false);

  const formParams = ref<formParamsType>(defaultValueRef());

  function openDrawer(postId?) {
    if (postId) {
      formParams.value.postId = postId;
      getInfo();
      return;
    }
    isDrawer.value = true;
  }

  function closeDrawer() {
    isDrawer.value = false;
  }

  function formSubmit() {
    formRef.value.validate((valid) => {
      if (!valid) {
        return message.error('请填写完整信息');
      }
      const msg = formParams.value.postId ? '编辑成功' : '添加成功';
      if (formParams.value.postId) {
        editPost(formParams.value).then((_) => {
          message.success(msg);
          emit('change');
          handleReset();
          closeDrawer();
        });
      } else {
        addPost(formParams.value).then((_) => {
          message.success(msg);
          emit('change');
          handleReset();
          closeDrawer();
        });
      }
    });
  }

  function handleReset() {
    formRef.value.resetFields();
    formParams.value = Object.assign(formParams.value, defaultValueRef());
  }

  function getInfo() {
    postInfo({ id: formParams.value.postId }).then((res) => {
      const params = {
        postId: res.postId,
        postCode: res.postCode,
        isEnable: res.isEnable,
        postName: res.postName,
        orderNum: res.orderNum,
      };
      formParams.value = Object.assign(formParams.value, params);
      isDrawer.value = true;
    });
  }

  defineExpose({
    openDrawer,
    closeDrawer,
  });
</script>
