<template>
  <a-form v-model="computedModelValue" :config="config" :disabled="disabled" :span="24"> </a-form>
</template>

<script setup lang="ts">
import AForm from '@/components/AForm/index.vue'
import { StrAnyObj } from '@/typings'
import { FormConfigType } from '@/components/AForm/type'

const props = withDefaults(
  defineProps<{
    modelValue: StrAnyObj
    disabled: boolean
    skipConditionShow: boolean
  }>(),
  {
    disabled: false,
    skipConditionShow: true
  }
)

const emits = defineEmits(['update:modelValue'])

const computedModelValue = computed({
  get() {
    return props.modelValue
  },
  set(newValue) {
    emits('update:modelValue', newValue)
  }
})

const config: FormConfigType[] = [
  {
    type: 'input',
    prop: 'skipName',
    label: '跳转名称',
    if: () => props.skipConditionShow
  },
  {
    type: 'radio',
    prop: 'skipType',
    label: '跳转类型',
    option: [
      {
        key: 'PASS',
        label: '审批通过'
      },
      {
        key: 'REJECT',
        label: '退回'
      }
    ]
  },
  {
    type: 'input',
    prop: 'condition',
    label: '跳转条件名',
    if: () => props.skipConditionShow && computedModelValue.value.conditionType != 'spel',
    change: () => updateSkipCondition()
  },
  {
    type: 'select',
    prop: 'conditionType',
    label: '条件',
    option: [
      {
        key: 'spel',
        label: 'spel表达式'
      },
      {
        key: 'gt',
        label: '大于'
      },
      {
        key: 'ge',
        label: '大于等于'
      },
      {
        key: 'eq',
        label: '等于'
      },
      {
        key: 'ne',
        label: '不等于'
      },
      {
        key: 'lt',
        label: '小于'
      },
      {
        key: 'le',
        label: '小于等于'
      },
      {
        key: 'like',
        label: '包含'
      },
      {
        key: 'notNike',
        label: '不包含'
      }
    ],
    if: () => props.skipConditionShow,
    change: () => updateSkipCondition()
  },
  {
    type: 'input',
    prop: 'conditionValue',
    label: '条件值',
    if: () => props.skipConditionShow,
    change: () => updateSkipCondition()
  }
]

function updateSkipCondition() {
  const value = computedModelValue.value

  let skipCondition = value.conditionType + "|";
  if (!/^spel/.test(value.conditionType)) {
    skipCondition += (value.condition ? value.condition : '') + "|";
  }
  value.skipCondition = skipCondition + (value.conditionValue ? value.conditionValue : '')
}
</script>
