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

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { createObjectOAMRule, getObjectOAMRuleById, updateObjectOAMRule } from '@/api/biz/bizObjectOAMRuleApi'
import RuleSelector from '@/views/biz/oam/components/RuleSelector.vue'
import ObjectAttrSelector from '@/views/biz/rule/components/ObjectAttrSelector.vue'
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,
    orId: 0,
    ruleId: 0,
    targetAttrId: '',
    ruleCode: '',
  },
  viewData: {
    objectTitle: '',
    ruleTitle: '',
  },
  actionType: 1,
})

onMounted(() => {})

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

function onSubmit() {
  formRef.value && formRef.value.validate(async (valid) => {
    if (valid) {
      if (state.actionType === 1) {
        const { msg } = await createObjectOAMRule(state.formData)
        ElMessage.success(msg)
        state.dialogVisible = false
        emit('success')
      }
      else {
        const { msg } = await updateObjectOAMRule(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,
    orId: 0,
    ruleId: 0,
    targetAttrId: '',
    ruleCode: '',
  }
}

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

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

<template>
  <div>
    <el-drawer v-model="state.dialogVisible" :title="state.title" size="30%">
      <div v-loading="state.loading">
        <el-form ref="formRef" :model="state.formData" :rules="formRules" label-width="120px" label-suffix="：">
          <el-form-item label="目标属性" prop="ruleCode">
            <ObjectAttrSelector
              v-model="state.formData.targetAttrId"
              :biz-id="state.formData.bizId"
              :object-id="state.formData.objectId"
              placeholder="请输入行为代码"
            />
          </el-form-item>

          <el-form-item label="业务规则" prop="ruleId">
            <RuleSelector v-model="state.formData.ruleId" />
          </el-form-item>

          <el-form-item label="规则代码" prop="ruleCode">
            <el-input v-model="state.formData.ruleCode" 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-drawer>
  </div>
</template>
