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

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { createObjectOAM, getObjectOAMById, updateObjectOAM } from '@/api/model/modelObjectOAMApi'
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: {
    modelId: 0,
    objectId: 0,
    oamId: 0,
    oamTitle: '',
    oamType: 0,
    sqlType: 0,
    sqlStatement: '',
    sqlSelect: '',
    sqlFrom: '',
    sqlWhere: '',
    sqlOrder: '',
    oamDesc: '',
  },
  actionType: 1,
})

onMounted(() => {})

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

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

function onCancel() {
  state.dialogVisible = false
}

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

defineExpose({
  showAddModel(modelId: number, objectId: number) {
    resetFormData()
    state.title = '新增对象行为'
    state.actionType = 1
    state.formData.modelId = modelId
    state.formData.objectId = objectId
    state.dialogVisible = true
  },
  showEditModel(modelId: number, objectId: number, oamId: number) {
    resetFormData()
    state.title = '编辑对象行为'
    state.formData.modelId = modelId
    state.formData.objectId = objectId
    state.formData.oamId = oamId
    state.actionType = 2
    state.dialogVisible = true
    getData()
  },

})
</script>

<template>
  <div>
    <el-drawer v-model="state.dialogVisible" :title="state.title" size="45%">
      <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="3" placeholder="请输入SQL" />
        </el-form-item>

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

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

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

        <el-form-item label="Order" prop="sqlOrder">
          <el-input v-model="state.formData.sqlOrder" type="textarea" rows="3" 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>

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