<template>
  <div style="height: 100%">
    <w-grid
      ref="gridRef"
      :title="$t('re.resources.designer.processor.grid.title')"
      draggable="server"
      dense
      hide-bottom
      db-click-operation="edit"
      :config-button="true"
      selection="multiple"
      :checkbox-selection="false"
      :tree="false"
      :fetch-data-url="Environment.apiContextPath('/api/re/indicator/processor?indicator=' + indicator.id)"
      :data-url="Environment.apiContextPath('/api/re/indicator/processor')"
      :pageable="false"
      :toolbar-configure="{ noIcon: false }"
      :toolbar-actions="[
        'refresh',
        'separator',
        [
          {
            extend: 'add',
            click: undefined,
            enableIf: (arg) => {
              return !readOnly;
            },
          },
          ObjectProperties.getToolbarAction(indicator),
          MathFormula.getToolbarAction(indicator),
          Arithmetic.getToolbarAction(indicator),
          Ternary.getToolbarAction(indicator),
          WhenThen.getToolbarAction(indicator),
          NumberRange.getToolbarAction(indicator),
          ConditionRange.getToolbarAction(indicator),
          GroovyScript.getToolbarAction(indicator),
          Sql.getToolbarAction(indicator),
        ],
        {
          extend: 'clone',
          enableIf: (arg) => {
            return !readOnly && arg.selected;
          },
        },
        {
          extend: 'edit',
          enableIf: (arg) => {
            return !readOnly && arg.selected;
          },
        },
        {
          extend: 'remove',
          enableIf: (arg) => {
            return !readOnly && arg.selected;
          },
        },
        'separator',
        'view',
        'separator',
        'export',
      ]"
      :columns="[
        { width: 60, name: 'order', label: $t('order'), sortable: false, align: 'right' },
        {
          width: 120,
          name: 'type',
          label: $t('type'),
          sortable: false,
          format: (value, row) => {
            return $t('io.sc.engine.rule.core.enums.ProcessorType.' + value);
          },
        },
        {
          width: '100%',
          name: 'content',
          label: $t('re.resources.designer.processor.grid.entity.content'),
          sortable: false,
          format: (value, row) => {
            switch (row.type) {
              case 'ARITHMETIC':
                return Arithmetic.format(row);
              case 'CONDITION_RANGE':
                return ConditionRange.format(row);
              case 'GROOVY_SCRIPT':
                return GroovyScript.format(row);
              case 'MATH_FORMULA':
                return MathFormula.format(row);
              case 'NUMBER_RANGE':
                return NumberRange.format(row);
              case 'OBJECT_PROPERTIES':
                return ObjectProperties.format(row);
              case 'SQL':
                return Sql.format(row);
              case 'TERNARY':
                return Ternary.format(row);
              case 'WHEN_THEN':
                return WhenThen.format(row);
            }
            return '';
          },
        },
      ]"
      :editor="{
        dialog: {
          width: '80%',
        },
        form: {
          colsNum: 5,
          fields: [
            { colSpan: 5, name: 'indicator', label: 'indicator', type: 'w-text', defaultValue: indicator.id, showIf: false },
            { colSpan: 5, name: 'id', label: $t('id'), type: 'w-text', showIf: false },
            { colSpan: 5, name: 'order', label: $t('order'), type: 'w-number', showIf: false },
            { colSpan: 5, name: 'type', label: $t('type'), type: 'w-text', showIf: false },

            ...Arithmetic.getEditorFields(),
            ...ConditionRange.getEditorFields({ gridRef }),
            ...GroovyScript.getEditorFields(),
            ...MathFormula.getEditorFields(),
            ...NumberRange.getEditorFields({ gridRef }),
            ...ObjectProperties.getEditorFields({ gridRef, objectPropertiesMatcherDialogRef }),
            ...Sql.getEditorFields({ gridRef }),
            ...Ternary.getEditorFields(),
            ...WhenThen.getEditorFields(),
          ],
        },
      }"
      :viewer="{
        panel: {
          columnNum: 1,
          fields: [
            { name: 'order', label: $t('order') },
            { name: 'id', label: $t('id'), primaryKey: true },
            { name: 'indicator', label: $t('indicator') },
            { name: 'description', label: $t('description') },
            { name: 'enable', label: $t('enable') },
            { name: 'type', label: $t('type') },

            ...Arithmetic.getViewerFields(),
            ...ConditionRange.getViewerFields(),
            ...GroovyScript.getViewerFields(),
            ...MathFormula.getViewerFields(),
            ...NumberRange.getViewerFields(),
            ...ObjectProperties.getViewerFields(),
            ...Sql.getViewerFields(),
            ...Ternary.getViewerFields(),
            ...WhenThen.getViewerFields(),

            { name: 'dataComeFrom', label: $t('dataComeFrom') },
            { name: 'creator', label: $t('creator') },
            { name: 'createDate', label: $t('createDate') },
            { name: 'lastModifier', label: $t('lastModifier') },
            { name: 'lastModifyDate', label: $t('lastModifyDate'), format: Formater.none() },
          ],
        },
      }"
      @before-editor-data-submit="
        (args) => {
          switch (args.data.type) {
            case 'ARITHMETIC':
              return Arithmetic.beforeEditorDataSubmit(args);
            case 'CONDITION_RANGE':
              return ConditionRange.beforeEditorDataSubmit(args);
            case 'GROOVY_SCRIPT':
              return GroovyScript.beforeEditorDataSubmit(args);
            case 'MATH_FORMULA':
              return MathFormula.beforeEditorDataSubmit(args);
            case 'NUMBER_RANGE':
              return NumberRange.beforeEditorDataSubmit(args);
            case 'OBJECT_PROPERTIES':
              return ObjectProperties.beforeEditorDataSubmit(args);
            case 'SQL':
              return Sql.beforeEditorDataSubmit(args);
            case 'TERNARY':
              return Ternary.beforeEditorDataSubmit(args);
            case 'WHEN_THEN':
              return WhenThen.beforeEditorDataSubmit(args);
          }
        }
      "
      @after-editor-open="
        (args) => {
          args.type = 'indicator';
          args.indicator = indicator;
          switch (args.grid.getEditorForm().getFieldValue('type')) {
            case 'ARITHMETIC':
              return Arithmetic.afterEditorOpen(args);
            case 'CONDITION_RANGE':
              return ConditionRange.afterEditorOpen(args);
            case 'GROOVY_SCRIPT':
              return GroovyScript.afterEditorOpen(args);
            case 'MATH_FORMULA':
              return MathFormula.afterEditorOpen(args);
            case 'NUMBER_RANGE':
              return NumberRange.afterEditorOpen(args);
            case 'OBJECT_PROPERTIES':
              return ObjectProperties.afterEditorOpen(args);
            case 'SQL':
              return Sql.afterEditorOpen(args);
            case 'TERNARY':
              return Ternary.afterEditorOpen(args);
            case 'WHEN_THEN':
              return WhenThen.afterEditorOpen(args);
          }
        }
      "
    ></w-grid>
    <ObjectPropertiesMatcherDialog
      ref="objectPropertiesMatcherDialogRef"
      target-type="indicator"
      :target="indicator"
      @after-matched="
        (objectProperties) => {
          const grid = gridRef.getEditorForm().getFieldComponent('objectProperties');
          grid.setLocalData(objectProperties);
        }
      "
    ></ObjectPropertiesMatcherDialog>
  </div>
</template>
<script setup lang="ts">
import 'tailwindcss/utilities.css';
import { ref, onMounted } from 'vue';
import { axios, Environment, Formater } from 'platform-core';
import { PlaceHolder } from '@/utils/PlaceHolder';
import ObjectPropertiesMatcherDialog from '@/views/shared/ObjectPropertiesMatcherDialog.vue';
import { Arithmetic, ConditionRange, GroovyScript, MathFormula, NumberRange, ObjectProperties, Sql, Ternary, WhenThen } from '@/views/shared/processors';

const props = defineProps({
  indicator: { type: Object, default: undefined },
  readOnly: { type: Boolean, default: false },
});

const gridRef = ref();
const objectPropertiesMatcherDialogRef = ref();

const refresh = () => {
  gridRef?.value?.refresh();
};

onMounted(() => {
  gridRef?.value?.refresh();
});

defineExpose({
  refresh,
});
</script>
