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

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { createApi, getApiById, updateApi } from '@/api/api/apiApi'
const emit = defineEmits(['success'])

const state = reactive({
  title: '',
  dialogVisible: false,
  loading: false,
  formData: {
    bizId: 0,
    objectId: 0,
    apiId: -1,
    apiTitle: '',
    apiType: 1,
    apiCode: '',
    status: 0,
    apiDesc: '',
    apiParams: '',
  },
  actionType: 1,
})

const formRef = ref<FormInstance>()
const formRules = ref<FormRules>({
  apiTitle: [
    { required: true, message: '请输入标题', trigger: 'blur' },
  ],
  apiDesc: [
    { required: false, message: '请输入描述', trigger: 'blur' },
  ],
})

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

const onSubmit = () => {
  formRef.value && formRef.value.validate(async (valid) => {
    if (valid) {
      if (state.actionType === 1) {
        await createApi(state.formData)
        state.dialogVisible = false
        emit('success')
      }
      else {
        await updateApi(state.formData)
        state.dialogVisible = false
        emit('success')
      }
    }
  })
}

const onCancel = () => {
  state.dialogVisible = false
}

function resetFormData() {
  state.formData = {
    bizId: 0,
    objectId: 0,
    apiId: -1,
    apiType: 1,
    apiTitle: '',
    apiCode: '',
    status: 0,
    apiDesc: '',
    apiParams: '',
  }
}

defineExpose({
  showAddApi(bizId: number, objectId: number) {
    resetFormData()
    state.title = '新增API'
    state.actionType = 1
    state.formData.bizId = bizId
    state.formData.objectId = objectId
    state.dialogVisible = true
  },
  showEditApi(apiId: any) {
    resetFormData()
    state.title = '编辑API'
    state.formData.apiId = apiId
    state.actionType = 2
    state.dialogVisible = true
    getData(apiId)
  },

})
</script>

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

          <el-form-item label="方法" prop="apiType">
            <el-radio-group v-model="state.formData.apiType">
              <el-radio :label="1">
                GET
              </el-radio>
              <el-radio :label="2">
                POST
              </el-radio>
              <el-radio :label="3">
                PUT
              </el-radio>
              <el-radio :label="4">
                PATCH
              </el-radio>
              <el-radio :label="5">
                DELETE
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="编码" prop="apiCode">
            <el-input v-model="state.formData.apiCode" placeholder="请输入编码" />
          </el-form-item>

          <el-form-item label="参数">
            <el-input v-model="state.formData.apiParams" placeholder="请输入参数" />
          </el-form-item>

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

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