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

<script lang="ts" setup name="bizObject">
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { createObject, getObjectById, updateObject } from '@/api/biz/bizObjectApi'
const emit = defineEmits(['success'])

// 数据
const state = reactive({
  title: '',
  dialogVisible: false,
  loading: false,
  search: {
    keywords: '',
  },
  formData: {
    bizId: 0,
    objectId: 0,
    objectType: 1,
    objectCode: '',
    objectTitle: '',
    comment: '',
  },
  actionType: 1,
})

onMounted(() => {})

const getData = async () => {
  const { bizId, objectId } = state.formData
  state.loading = true
  const { data } = await getObjectById(bizId, objectId)
  state.loading = false
  state.formData = data
}

const formRef = ref<FormInstance>()
const formRules = reactive<FormRules>({
  objectTitle: [
    { required: true, message: '请输入对象标题', trigger: 'blur' },
  ],
  objectType: [
    { required: true, message: '请输入对象类型', trigger: 'blur' },
  ],
  objectCode: [
    { required: true, message: '请输入对像代码', trigger: 'blur' },
  ],
  comment: [
    { required: false, message: '请输入简要描述', trigger: 'blur' },
  ],
})

function onCancel() {
  state.dialogVisible = false
}

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

function resetFormData() {
  state.formData = {
    bizId: 0,
    objectId: 0,
    objectType: 0,
    objectCode: '',
    objectTitle: '',
    comment: '',
  }
}

defineExpose({
  showAddModel(bizId: number) {
    resetFormData()
    state.title = '新增业务对象'
    state.actionType = 1
    state.formData.bizId = bizId
    state.dialogVisible = true
  },

  showEditModel(bizId: number, objectId: number) {
    resetFormData()
    state.title = '编辑业务对象'
    state.actionType = 2
    state.formData.bizId = bizId
    state.formData.objectId = objectId
    state.dialogVisible = true
    getData()
  },
})
</script>

<template>
  <el-drawer v-model="state.dialogVisible" :title="state.title" size="30%">
    <el-scrollbar class="form-el-scrollbar">
      <div class="form-content">
        <el-form ref="formRef" :model="state.formData" :rules="formRules" label-position="top">
          <el-form-item label="对象名称" prop="objectTitle">
            <el-input v-model="state.formData.objectTitle" placeholder="请输入标题" />
          </el-form-item>

          <el-form-item label="类型" prop="objectType">
            <el-radio-group v-model="state.formData.objectType">
              <el-radio :label="1">
                实体对象
              </el-radio>
              <el-radio :label="2">
                视图对象
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="对象代码" prop="objectCode">
            <el-input v-model="state.formData.objectCode" placeholder="请输入代码" />
          </el-form-item>

          <el-form-item label="简要说明" prop="comment">
            <el-input v-model="state.formData.comment" type="textarea" rows="10" placeholder="请输入标题" />
          </el-form-item>
        </el-form>
      </div>
    </el-scrollbar>

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