<template>
  <div style="height: 100%">
    <w-grid
      ref="gridRef"
      :title="$t('re.processor.grid.title')"
      dnd-mode="server"
      dense-body
      class="px-1"
      hide-bottom
      :config-button="false"
      selection="multiple"
      db-click-operation="customEdit"
      :checkbox-selection="true"
      :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',
            enableIf: (args: any) => {
              return !readOnly;
            },
            click: undefined,
          },
          ...processorManager.getToolbarAction(),
        ],
        'clone',
        {
          extend: 'edit',
          name: 'customEdit',
          enableIf: (args: any) => {
            return args.selected;
          },
          click: (args: any) => {
            const type = args.selected?.type;
            if ('DECISION_TREE' === type) {
              decisionTreeDialogRef.open(parameter.id, args.selected?.id);
            } else if ('EXECUTION_FLOW' === type) {
              executionFlowDialogRef.open(parameter.id, args.selected?.id);
            } else {
              args._click(args);
            }
          },
        },
        {
          extend: 'remove',
          enableIf: (args: any) => {
            return !readOnly && args.selected;
          },
        },
        'separator',
        {
          name: 'enable',
          label: (args: any) => {
            if (args.selected?.enable) {
              return $t('disable');
            }
            return $t('enable');
          },
          icon: (args: any) => {
            if (args.selected?.enable) {
              return 'bi-x';
            }
            return 'bi-check';
          },
          enableIf: (args: any) => {
            return !readOnly && args.selected;
          },
          click: (args: any) => {
            const data = args.selected;
            data.enable = !data.enable;
            axios.put(Environment.apiContextPath('/api/re/model/parameter/processor/' + data.id), data).then((response) => {
              gridRef.updateLocalData(response.data);
            });
          },
        },
        'separator',
        'view',
        'separator',
        'export',
      ]"
      :columns="[
        { width: 80, name: 'enable', label: $t('isEnable'), align: 'center', sortable: false, format: Formater.enableTag() },
        {
          width: 150,
          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.processor.grid.entity.content'),
          sortable: false,
          title: () => {},
          format: (value: any, row: any) => {
            return processorManager.format(value, row);
          },
        },
      ]"
      :editor="{
        dialog: {
          width: '50%',
        },
        form: {
          colsNum: 12,
          fields: [
            { colSpan: 12, name: 'parameter', label: 'parameter', type: 'w-text', defaultValue: parameter.id, showIf: false },
            { colSpan: 12, name: 'id', label: $t('id'), type: 'w-text', showIf: false },
            { colSpan: 12, name: 'order', label: $t('order'), type: 'w-number', showIf: false },
            { colSpan: 12, name: 'type', label: $t('type'), type: 'w-text', showIf: false },
            { colSpan: 12, name: 'description', label: $t('description'), type: 'w-text', showIf: false },
            { colSpan: 12, name: 'enable', label: $t('enable'), type: 'w-checkbox', defaultValue: true, showIf: false },
            ...processorManager.getEditorFields(),
          ],
        },
      }"
      :viewer="{
        panel: {
          columnNum: 1,
          fields: [
            { name: 'parameter', label: $t('parameter') },
            { name: 'type', label: $t('type') },
            { name: 'id', label: $t('id'), primaryKey: true },
            { name: 'description', label: $t('description') },
            { name: 'enable', label: $t('enable') },
            { name: 'order', label: $t('order') },
            ...processorManager.getViewerFields(),
            ...CorporationAuditorEntityManager.getViewerFields(),
          ],
        },
      }"
      @before-editor-data-submit="
        (args) => {
          processorManager.beforeEditorDataSubmit(args);
        }
      "
      @after-editor-open="
        (args) => {
          processorManager.afterEditorOpen(args);
        }
      "
    ></w-grid>
    <DecisionTreeDialog ref="decisionTreeDialogRef"></DecisionTreeDialog>
    <ExecutionFlowDialog ref="executionFlowDialogRef"></ExecutionFlowDialog>
    <ObjectPropertiesMatcherDialog
      ref="objectPropertiesMatcherDialogRef"
      :target-type="Processor.PARAMETER"
      :target="parameter"
      @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 { $t, axios, Environment, Formater, CorporationAuditorEntityManager } from 'platform-core';
import { ProcessorManager } from '@/views/shared/ProcessorManager';
import { Processor } from '@/views/shared/Processor';
import { PlaceHolder } from '@/utils/PlaceHolder';
import DecisionTreeDialog from './DecisionTreeDialog.vue';
import ExecutionFlowDialog from './ExecutionFlowDialog.vue';
import ObjectPropertiesMatcherDialog from '@/views/shared/ObjectPropertiesMatcherDialog.vue';

const props = defineProps({
  fetchDataUrl: { type: String, default: '' },
  dataUrl: { type: String, default: '' },
  parameter: { type: Object, default: undefined },
  readOnly: { type: Boolean, default: false },
});

const gridRef = ref();
const decisionTreeDialogRef = ref();
const executionFlowDialogRef = ref();
const objectPropertiesMatcherDialogRef = ref();
const processorManagerContext = {
  target: props.parameter,
  objectPropertiesMatcherDialogRef: objectPropertiesMatcherDialogRef,
};
const processorManager = new ProcessorManager(Processor.PARAMETER, processorManagerContext);

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

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

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