<template>
  <div style="height: 100%">
    <w-grid
      ref="gridRef"
      :title="$t('re.resources.designer.processor.grid.title')"
      dnd-mode="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;
            },
          },
          ...processorManager.getToolbarAction(),
        ],
        {
          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) => {
            return processorManager.format(value, row);
          },
        },
      ]"
      :editor="{
        dialog: {
          width: '80%',
        },
        form: {
          colsNum: 12,
          fields: [
            { colSpan: 12, name: 'indicator', label: 'indicator', type: 'w-text', defaultValue: indicator.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 },
            ...processorManager.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') },
            ...processorManager.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) => {
          processorManager.beforeEditorDataSubmit(args);
        }
      "
      @after-editor-open="
        (args) => {
          processorManager.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, Tools } from 'platform-core';
import { ProcessorManager } from '@/views/shared/ProcessorManager';
import { Processor } from '@/views/shared/Processor';
import { PlaceHolder } from '@/utils/PlaceHolder';
import ObjectPropertiesMatcherDialog from '@/views/shared/ObjectPropertiesMatcherDialog.vue';

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

const gridRef = ref();
const objectPropertiesMatcherDialogRef = ref();
const processorManagerContext = {
  target: props.indicator,
  objectPropertiesMatcherDialogRef: objectPropertiesMatcherDialogRef,
};
const processorManager = new ProcessorManager(Processor.INDICATOR, processorManagerContext);

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

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

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