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

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { createObjectOAM, getObjectOAMById, updateObjectOAM } from '@/api/biz/bizObjectOAMApi'
const emit = defineEmits(['success'])

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

const state = reactive({
  title: '',
  dialogVisible: false,
  loading: false,
  formData: {
    bizId: 0,
    objectId: 0,
    oamId: 0,
    oamTitle: '',
    oamType: 0,
    sqlType: 0,
    sqlStatement: '',
    sqlSelect: '',
    sqlFrom: '',
    sqlWhere: '',
    sqlOrder: '',
    sqlCount: '',
    oamDesc: '',
  },
  actionType: 1,
})

onMounted(() => {})

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

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

function onCancel() {
  state.dialogVisible = false
}

function resetFormData() {
  state.formData = {
    bizId: 0,
    objectId: 0,
    oamId: 0,
    oamType: 0,
    oamTitle: '',
    sqlType: 0,
    sqlStatement: '',
    sqlSelect: '',
    sqlFrom: '',
    sqlWhere: '',
    sqlOrder: '',
    sqlCount: '',
    oamDesc: '',
  }
}

defineExpose({
  showAddModel(bizId: number, objectId: number) {
    resetFormData()
    state.title = '新增对象行为'
    state.actionType = 1
    state.formData.bizId = bizId
    state.formData.objectId = objectId
    state.dialogVisible = true
  },
  showEditModel(modelId: number, objectId: number, oamId: number) {
    resetFormData()
    state.title = '编辑对象行为'
    state.formData.bizId = modelId
    state.formData.objectId = objectId
    state.formData.oamId = oamId
    state.actionType = 2
    state.dialogVisible = true
    getData()
  },
})
</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="oamTitle">
            <el-input v-model="state.formData.oamTitle" placeholder="请输入标题" />
          </el-form-item>

          <el-form-item label="类型" prop="oamType">
            <el-radio-group v-model="state.formData.oamType">
              <el-radio :label="1">
                基于Pk查询
              </el-radio>
              <el-radio :label="2">
                多笔查询
              </el-radio>
              <el-radio :label="3">
                分页查询
              </el-radio>
              <el-radio :label="4">
                新增
              </el-radio>
              <el-radio :label="5">
                修改
              </el-radio>
              <el-radio :label="6">
                删除
              </el-radio>
              <el-radio :label="7">
                非Pk单笔查询
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="类型" prop="sqlType">
            <el-radio-group v-model="state.formData.sqlType">
              <el-radio :label="1">
                DQL(数据查询)
              </el-radio>
              <el-radio :label="2">
                DML(数据操纵)
              </el-radio>
              <el-radio :label="3">
                DDL(数据定义)
              </el-radio>
              <el-radio :label="4">
                DCL(数据控制)
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="SQL" prop="sqlStatement">
            <el-input v-model="state.formData.sqlStatement" type="textarea" rows="5" placeholder="请输入SQL" />
          </el-form-item>

          <el-form-item label="Count" prop="sqlCount">
            <el-input v-model="state.formData.sqlCount" type="textarea" rows="5" placeholder="请输入SQL" />
          </el-form-item>

          <el-form-item label="简要说明" prop="oamDesc">
            <el-input v-model="state.formData.oamDesc" type="textarea" rows="3" 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>
