<template>
  <w-grid
    ref="gridRef"
    :title="$t('re.resources.designer.processor.grid.title')"
    dense-body
    hide-bottom
    :config-button="false"
    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,
        },
        {
          extend: 'add',
          name: 'arithmetic',
          label: Formater.enum(Enums.ProcessorType)('ARITHMETIC'),
          icon: 'bi-calculator',
          enableIf: (arg) => {
            return indicator.type !== 'RULE_RESULT' && indicator.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'ARITHMETIC');
          },
        },
        {
          extend: 'add',
          name: 'ternary',
          label: Formater.enum(Enums.ProcessorType)('TERNARY'),
          icon: 'bi-question',
          enableIf: (arg) => {
            return indicator.type !== 'RULE_RESULT' && indicator.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'TERNARY');
          },
        },
        {
          extend: 'add',
          name: 'whenThen',
          label: Formater.enum(Enums.ProcessorType)('WHEN_THEN'),
          icon: 'bi-sliders',
          enableIf: (arg) => {
            return indicator.type !== 'RULE_RESULT' && indicator.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'WHEN_THEN');
          },
        },
        {
          extend: 'add',
          name: 'numberRange',
          label: Formater.enum(Enums.ProcessorType)('NUMBER_RANGE'),
          icon: 'bi-justify',
          enableIf: (arg) => {
            return indicator.type !== 'RULE_RESULT' && indicator.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'NUMBER_RANGE');
          },
        },
        {
          extend: 'add',
          name: 'conditionRange',
          label: Formater.enum(Enums.ProcessorType)('CONDITION_RANGE'),
          icon: 'bi-rainbow',
          enableIf: (arg) => {
            return indicator.type !== 'RULE_RESULT' && indicator.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'CONDITION_RANGE');
          },
        },

        {
          extend: 'add',
          name: 'groovyScript',
          label: Formater.enum(Enums.ProcessorType)('GROOVY_SCRIPT'),
          icon: 'bi-code',
          enableIf: (arg) => {
            return indicator.type !== 'RULE_RESULT' && indicator.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'GROOVY_SCRIPT');
          },
        },
        {
          extend: 'add',
          name: 'sql',
          label: Formater.enum(Enums.ProcessorType)('SQL'),
          icon: 'bi-filetype-sql',
          enableIf: (arg) => {
            return indicator.type !== 'RULE_RESULT' && indicator.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'SQL');
          },
        },
      ],
      'clone',
      'edit',
      'remove',
      '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: Formater.enum(Enums.ProcessorType) },
      {
        width: '100%',
        name: 'content',
        label: $t('re.resources.designer.processor.grid.entity.content'),
        sortable: false,
        format: (value, row) => {
          const type = row.type;

          return '';
        },
      },
    ]"
    :editor="{
      dialog: {
        width: '600px',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'indicator', label: 'indicator', type: 'text', defaultValue: indicator.id, hidden: true },
          { name: 'id', label: $t('id'), type: 'text', hidden: true },
          { name: 'order', label: $t('order'), type: 'number', hidden: true },
          { name: 'type', label: $t('type'), type: 'text', hidden: true },
          { name: 'description', label: $t('description'), type: 'text' },
          {
            name: 'arithmetic',
            label: $t('re.resources.designer.processor.grid.entity.arithmetic'),
            type: 'code-mirror',
            showIf: (arg) => {
              return 'ARITHMETIC' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'ternaryCondition',
            label: $t('re.resources.designer.processor.grid.entity.ternaryCondition'),
            type: 'code-mirror',
            rows: 1,
            showIf: (arg) => {
              return 'TERNARY' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'ternaryTrue',
            label: $t('re.resources.designer.processor.grid.entity.ternaryTrue'),
            type: 'code-mirror',
            rows: 1,
            showIf: (arg) => {
              return 'TERNARY' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'ternaryFalse',
            label: $t('re.resources.designer.processor.grid.entity.ternaryFalse'),
            type: 'code-mirror',
            lang: 'groovy',
            rows: 1,
            showIf: (arg) => {
              return 'TERNARY' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'when',
            label: $t('re.resources.designer.processor.grid.entity.when'),
            type: 'code-mirror',
            lang: 'groovy',
            rows: 1,
            showIf: (arg) => {
              return 'WHEN_THEN' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'then',
            label: $t('re.resources.designer.processor.grid.entity.then'),
            type: 'code-mirror',
            lang: 'groovy',
            rows: 1,
            showIf: (arg) => {
              return 'WHEN_THEN' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'isWhenThenShorted',
            label: $t('re.resources.designer.processor.grid.entity.isWhenThenShorted'),
            type: 'checkbox',
            showIf: (arg) => {
              return 'WHEN_THEN' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'numberRange',
            label: $t('re.resources.designer.processor.grid.entity.numberRange'),
            type: 'code-mirror',
            rows: 1,
            showIf: (arg) => {
              return 'NUMBER_RANGE' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'conditionRange',
            label: $t('re.resources.designer.processor.grid.entity.conditionRange'),
            type: 'code-mirror',
            rows: 1,
            showIf: (arg) => {
              return 'CONDITION_RANGE' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'groovyScript',
            label: $t('re.resources.designer.processor.grid.entity.groovyScript'),
            type: 'code-mirror',
            rows: 1,
            showIf: (arg) => {
              return 'GROOVY_SCRIPT' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'sqlDatasourceName',
            label: $t('re.resources.designer.processor.grid.entity.sqlDatasourceName'),
            type: 'select',
            rows: 1,
            showIf: (arg) => {
              return 'SQL' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'sql',
            label: $t('re.resources.designer.processor.grid.entity.sql'),
            type: 'select',
            rows: 1,
            showIf: (arg) => {
              return 'SQL' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'sqlParameterValues',
            label: $t('re.resources.designer.processor.grid.entity.sqlParameterValues'),
            type: 'select',
            rows: 1,
            showIf: (arg) => {
              return 'SQL' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'sqlFieldMapping',
            label: $t('re.resources.designer.processor.grid.entity.sqlFieldMapping'),
            type: 'select',
            rows: 1,
            showIf: (arg) => {
              return 'SQL' === arg.form.getFieldValue('type');
            },
          },
        ],
      },
    }"
    :viewer="{
      panel: {
        columnNum: 1,
        fields: [
          { name: 'id', label: $t(''), primaryKey: true },
          { name: 'parameter', label: $t('') },
          { name: 'description', label: $t('') },
          { name: 'order', label: $t('') },
          { name: 'enable', label: $t('') },
          { name: 'type', label: $t('') },

          { name: 'arithmetic', label: $t('re.resources.designer.processor.grid.entity.arithmetic') },
          { name: 'ternaryCondition', label: $t('re.resources.designer.processor.grid.entity.ternaryCondition') },
          { name: 'ternaryTrue', label: $t('re.resources.designer.processor.grid.entity.ternaryTrue') },
          { name: 'ternaryFalse', label: $t('re.resources.designer.processor.grid.entity.ternaryFalse') },
          { name: 'when', label: $t('re.resources.designer.processor.grid.entity.when') },
          { name: 'then', label: $t('re.resources.designer.processor.grid.entity.then') },
          { name: 'isWhenThenShorted', label: $t('re.resources.designer.processor.grid.entity.isWhenThenShorted') },
          { name: 'rule', label: $t('re.resources.designer.processor.grid.entity.rule') },
          { name: 'singleRule', label: $t('re.resources.designer.processor.grid.entity.singleRule') },
          { name: 'numberRange', label: $t('re.resources.designer.processor.grid.entity.numberRange') },
          { name: 'conditionRange', label: $t('re.resources.designer.processor.grid.entity.conditionRange') },
          { name: 'groovyScript', label: $t('re.resources.designer.processor.grid.entity.groovyScript') },

          { name: 'sqlDatasourceName', label: $t('re.resources.designer.processor.grid.entity.sqlDatasourceName') },
          { name: 'sql', label: $t('re.resources.designer.processor.grid.entity.sql') },
          { name: 'sqlParameterValues', label: $t('re.resources.designer.processor.grid.entity.sqlParameterValues') },
          { name: 'sqlFieldMapping', label: $t('re.resources.designer.processor.grid.entity.sqlFieldMapping') },

          { 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() },
          { name: 'corporationCode', label: $t('corporationCode') },
        ],
      },
    }"
  ></w-grid>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { Environment, Formater, EnumTools, Options } from 'platform-core';

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

const gridRef = ref();

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

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

defineExpose({
  refresh,
});

const Enums = await EnumTools.fetch(['io.sc.engine.rule.core.enums.ProcessorType']);
</script>
