<script setup lang="ts">
import { ref } from "vue";
import { AddPostCommand, UpdatePostCommand } from "@/api/system/post";
import { useUserStoreHook } from "@/store/modules/user";
import { FormInstance, FormRules } from "element-plus";

interface Props {
  formInline: AddPostCommand & Partial<UpdatePostCommand>;
}

const props = withDefaults(defineProps<Props>(), {
  formInline: () => ({
    postId: 0,
    postCode: "",
    postName: "",
    postSort: 1,
    remark: "",
    status: ""
  })
});
const formData = ref(props.formInline);

const statusList = useUserStoreHook().dictionaryMap["common.status"];

const rules: FormRules = {
  postName: [
    {
      required: true,
      message: "岗位名称不能为空"
    }
  ],
  postCode: [
    {
      required: true,
      message: "岗位编码不能为空"
    }
  ],
  postSort: [
    {
      required: true,
      message: "岗位序号不能为空"
    }
  ]
};
const formRef = ref<FormInstance>();

function getFormRuleRef() {
  return formRef.value;
}

defineExpose({ getFormRuleRef });
</script>

<template>
  <el-form :model="formData" label-width="120px" :rules="rules" ref="formRef">
    <el-form-item prop="postName" label="岗位名称" required inline-message>
      <el-input v-model="formData.postName" />
    </el-form-item>
    <el-form-item prop="postCode" label="岗位编码" required>
      <el-input v-model="formData.postCode" />
    </el-form-item>
    <el-form-item prop="postSort" label="岗位顺序" required>
      <el-input-number :min="1" v-model="formData.postSort" />
    </el-form-item>
    <el-form-item prop="status" label="岗位状态">
      <el-radio-group v-model="formData.status">
        <el-radio
          v-for="item in Object.keys(statusList)"
          :key="item"
          :label="statusList[item].value"
          >{{ statusList[item].label }}</el-radio
        >
      </el-radio-group>
    </el-form-item>
    <el-form-item prop="remark" label="备注" style="margin-bottom: 0">
      <el-input type="textarea" v-model="formData.remark" />
    </el-form-item>
  </el-form>
</template>
