<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/biz/bizObjectOAMApi'
const emit = defineEmits(['success'])

const formRef = ref<FormInstance>()
const formRules = ref<FormRules>({
  oamTitle: [
    { required: true, message: '请输入标题', trigger: 'blur' },
  ],
  oamCode: [
    { required: true, message: '请输入行为代码', trigger: 'blur' },
  ],
  oamType: [
    { required: true, message: '请输入行为类型', trigger: 'blur' },
  ],
  sqlType: [
    { 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: 1,
    oamCode: '',
    sqlType: 1,
    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) {
        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 = {
    bizId: 0,
    objectId: 0,
    oamId: 0,
    oamType: 1,
    oamCode: '',
    oamTitle: '',
    sqlType: 1,
    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(bizId: number, objectId: number, oamId: number) {
    resetFormData()
    state.title = '编辑对象行为'
    state.actionType = 2
    state.formData.bizId = bizId
    state.formData.objectId = objectId
    state.formData.oamId = oamId
    state.dialogVisible = true
    getData()
  },
})
</script>

<template>
  <div>
    <el-drawer v-model="state.dialogVisible" :title="state.title" size="30%">
      <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="oamCode">
          <el-input v-model="state.formData.oamCode" placeholder="请输入行为代码" />
        </el-form-item>

        <el-form-item label="类型" prop="oamType">
          <el-radio-group v-model="state.formData.oamType">
            <el-radio :label="1">
              新增
            </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">
              分页查询
            </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 v-if="state.formData.oamType === 7" 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>

      <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>
