<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(),
            ...groovyScript.getEditorFields(),
            ...mathFormula.getEditorFields(),
            ...numberRange.getEditorFields(),
            ...objectProperties.getEditorFields({ objectPropertiesMatcherDialogRef }),
            ...sql.getEditorFields(),
            ...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.target = 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 { Processor } from '@/views/shared/Processor';
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 arithmetic = new Arithmetic(Processor.INDICATOR);
const conditionRange = new ConditionRange(Processor.INDICATOR);
const groovyScript = new GroovyScript(Processor.INDICATOR);
const mathFormula = new MathFormula(Processor.INDICATOR);
const numberRange = new NumberRange(Processor.INDICATOR);
const objectProperties = new ObjectProperties(Processor.INDICATOR);
const sql = new Sql(Processor.INDICATOR);
const ternary = new Ternary(Processor.INDICATOR);
const whenThen = new WhenThen(Processor.INDICATOR);

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>
