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

<script lang="ts" setup name="Oc">
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { createOc, getOcById, updateOc } from '@/api/system/ocApi'
import OcCatSelector from '@/views/customer/components/OcCatSelector.vue'
const emit = defineEmits(['success'])

const formRef = ref<FormInstance>()
const formRules = ref<FormRules>({
  ocName: [
    { required: true, message: '请输入标题', trigger: 'blur' },
  ],
  ocTypeId: [
    { required: true, message: '请输入类型', trigger: 'blur' },
  ],
  ocCatId: [
    { required: true, message: '请输入分类', trigger: 'blur' },
  ],
})

const state = reactive({
  title: '',
  dialogVisible: false,
  loading: false,
  formData: {
    ocId: 0,
    ocName: '',
    ocTypeId: 0,
    ocCatId: 0,
    ocRemark: '',
  },
  actionType: 1,
})

onMounted(() => {})

const getData = async (ocId: number) => {
  state.loading = true
  const { data } = await getOcById(ocId)
  state.loading = false
  state.formData = data
}

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

function onCancel() {
  state.dialogVisible = false
}

function resetFormData() {
  state.formData = {
    ocId: 0,
    ocName: '',
    ocTypeId: 0,
    ocCatId: 0,
    ocRemark: '',
  }
}

defineExpose({
  showAddModel(ocTypeId: number){
    resetFormData()
    state.title = '新增岗位'
    state.actionType = 1
    state.formData.ocTypeId = ocTypeId
    state.dialogVisible = true
  },
  showEditModel(ocId: number) {
    resetFormData()
    state.title = '编辑岗位'
    state.actionType = 2
    state.dialogVisible = true
    getData(ocId)
  },
})
</script>

<template>
  <el-drawer v-model="state.dialogVisible" :title="state.title" width="600px" :close-on-click-modal="false" append-to-body destroy-on-close>
    <el-form ref="formRef" :model="state.formData" :rules="formRules" label-width="120px" label-suffix="：">
      <el-form-item label="单位名称" prop="ocName">
        <el-input v-model="state.formData.positionName" placeholder="请输入标题" />
      </el-form-item>

      <el-form-item label="分类" prop="ocName">
        <OcCatSelector v-model="state.formData.ocCatId" :oc-type-id="state.formData.ocTypeId" placeholder="请输入标题" />
      </el-form-item>

      <el-form-item label="备注" prop="ocRemark">
        <el-input v-model="state.formData.ocRemark" type="textarea" rows="10" 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>
