<template>
  <w-grid
    ref="gridRef"
    :title="$t('re.resources.designer.processor.grid.title')"
    dense-body
    class="px-1"
    hide-bottom
    :config-button="false"
    selection="multiple"
    :checkbox-selection="false"
    :tree="false"
    :fetch-data-url="Environment.apiContextPath('/api/re/model/parameter/processor/findByParameterId?parameterId=' + parameter.id)"
    :data-url="Environment.apiContextPath('/api/re/model/parameter/processor')"
    :pageable="false"
    :toolbar-configure="{ noIcon: false }"
    :toolbar-actions="[
      'refresh',
      'separator',
      [
        {
          extend: 'add',
          click: undefined,
        },
        {
          extend: 'add',
          name: 'optionValue',
          label: Formater.enum(Enums.ProcessorType)('OPTION_VALUE'),
          icon: 'bi-card-list',
          enableIf: (arg) => {
            return parameter.type !== 'RULE_RESULT' && parameter.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'OPTION_VALUE');
          },
        },
        {
          extend: 'add',
          name: 'arithmetic',
          label: Formater.enum(Enums.ProcessorType)('ARITHMETIC'),
          icon: 'bi-calculator',
          enableIf: (arg) => {
            return parameter.type !== 'RULE_RESULT' && parameter.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 parameter.type !== 'RULE_RESULT' && parameter.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 parameter.type !== 'RULE_RESULT' && parameter.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 parameter.type !== 'RULE_RESULT' && parameter.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 parameter.type !== 'RULE_RESULT' && parameter.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'CONDITION_RANGE');
          },
        },
        {
          extend: 'add',
          name: 'decisionTable2c',
          label: Formater.enum(Enums.ProcessorType)('DECISION_TABLE_2C'),
          icon: 'bi-grid-3x2',
          enableIf: (arg) => {
            return parameter.type !== 'RULE_RESULT' && parameter.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'DECISION_TABLE_2C');
          },
        },
        {
          extend: 'add',
          name: 'decisionTable',
          label: Formater.enum(Enums.ProcessorType)('DECISION_TABLE'),
          icon: 'bi-grid-3x3',
          enableIf: (arg) => {
            return parameter.type !== 'RULE_RESULT' && parameter.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'DECISION_TABLE');
          },
        },
        {
          extend: 'add',
          name: 'decisionTree',
          label: Formater.enum(Enums.ProcessorType)('DECISION_TREE'),
          icon: 'bi-tree',
          enableIf: (arg) => {
            return parameter.type !== 'RULE_RESULT' && parameter.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'DECISION_TREE');
          },
        },
        {
          extend: 'add',
          name: 'executionFlow',
          label: Formater.enum(Enums.ProcessorType)('EXECUTION_FLOW'),
          icon: 'bi-bounding-box-circles',
          enableIf: (arg) => {
            return parameter.type !== 'RULE_RESULT' && parameter.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'EXECUTION_FLOW');
          },
        },
        {
          extend: 'add',
          name: 'pmml',
          label: Formater.enum(Enums.ProcessorType)('PMML'),
          icon: 'bi-filetype-xml',
          enableIf: (arg) => {
            return parameter.type !== 'RULE_RESULT' && parameter.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'PMML');
          },
        },
        {
          extend: 'add',
          name: 'groovyScript',
          label: Formater.enum(Enums.ProcessorType)('GROOVY_SCRIPT'),
          icon: 'bi-code',
          enableIf: (arg) => {
            return parameter.type !== 'RULE_RESULT' && parameter.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 parameter.type !== 'RULE_RESULT' && parameter.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'SQL');
          },
        },
        {
          extend: 'add',
          name: 'rule',
          label: Formater.enum(Enums.ProcessorType)('RULE'),
          icon: 'bi-shadows',
          enableIf: (arg) => {
            return parameter.type !== 'RULE_RESULT' && parameter.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'RULE');
          },
        },
        {
          extend: 'add',
          name: 'singleRule',
          label: Formater.enum(Enums.ProcessorType)('SINGLE_RULE'),
          icon: 'bi-noise-reduction',
          enableIf: (arg) => {
            return parameter.type !== 'RULE_RESULT' && parameter.type !== 'SINGLE_RULE_RESULT';
          },
          afterClick: (arg) => {
            arg.grid.getEditorForm().setFieldValue('type', 'SINGLE_RULE');
          },
        },
      ],
      'edit',
      'remove',
      'separator',
      'view',
      'separator',
      'export',
    ]"
    :columns="[
      { width: 60, name: 'order', label: $t('order'), sortable: false, align: 'right' },
      { width: 100, name: 'type', label: $t('type'), sortable: false, format: Formater.enum(Enums.ProcessorType) },
      { width: 100, name: 'enable', label: $t('isEnable'), sortable: false },
      {
        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',
        height: '400px',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'parameter', label: 'parameter', type: 'text', defaultValue: parameter.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: 'enable', label: $t('enable'), type: 'checkbox', defaultValue: true },
          {
            name: 'optionCode',
            label: $t('re.resources.designer.processor.grid.entity.optionCode'),
            type: 'select',
            options: [],
            showIf: (arg) => {
              return 'OPTION_VALUE' === arg.form.getFieldValue('type');
            },
          },
          {
            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',
            rows: 1,
            showIf: (arg) => {
              return 'TERNARY' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'when',
            label: $t('re.resources.designer.processor.grid.entity.when'),
            type: 'code-mirror',
            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',
            rows: 1,
            showIf: (arg) => {
              return 'WHEN_THEN' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'isWhenThenShorted',
            label: $t('re.resources.designer.processor.grid.entity.isWhenThenShorted'),
            type: 'code-mirror',
            rows: 1,
            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: 'decisionTable2C',
            label: $t('re.resources.designer.processor.grid.entity.decisionTable2C'),
            type: 'code-mirror',
            rows: 1,
            showIf: (arg) => {
              return 'DECISION_TABLE_2C' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'decisionTable',
            label: $t('re.resources.designer.processor.grid.entity.decisionTable'),
            type: 'code-mirror',
            rows: 1,
            showIf: (arg) => {
              return 'DECISION_TABLE' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'decisionTree',
            label: $t('re.resources.designer.processor.grid.entity.decisionTree'),
            type: 'code-mirror',
            rows: 1,
            showIf: (arg) => {
              return 'DECISION_TREE' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'executionFlow',
            label: $t('re.resources.designer.processor.grid.entity.executionFlow'),
            type: 'code-mirror',
            rows: 1,
            showIf: (arg) => {
              return 'EXECUTION_FLOW' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'pmml',
            label: $t('re.resources.designer.processor.grid.entity.pmml'),
            type: 'code-mirror',
            rows: 1,
            showIf: (arg) => {
              return 'PMML' === 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: 'optionCode', label: $t('re.resources.designer.processor.grid.entity.optionCode') },
          { 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: 'decisionTable2C', label: $t('re.resources.designer.processor.grid.entity.decisionTable2C') },
          { name: 'decisionTable', label: $t('re.resources.designer.processor.grid.entity.decisionTable') },
          { 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({
  fetchDataUrl: { type: String, default: '' },
  dataUrl: { type: String, default: '' },
  parameter: { type: Object, default: undefined },
});

const emit = defineEmits<{
  (e: 'rowClick', evt: Event, row: any, index: number): void;
  (e: 'beforeRequestData', requestParams: URLSearchParams | any, callback: any): void;
}>();
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>
