<template>
  <w-grid
    ref="gridRef"
    dense-body
    class="px-1"
    hide-bottom
    :config-button="false"
    dnd-mode="server"
    selection="multiple"
    :checkbox-selection="true"
    db-click-operation="edit"
    :tree="false"
    :fetch-data-url="Environment.apiContextPath('/api/re/model/parameter/option/findByParameterId?parameterId=' + parameter.id)"
    :data-url="Environment.apiContextPath('/api/re/model/parameter/option')"
    :pageable="false"
    :sort-by="['order']"
    :toolbar-configure="{ noIcon: false }"
    :toolbar-actions="[
      'refresh',
      'separator',
      {
        extend: 'add',
        enableIf: (arg) => {
          return !readOnly;
        },
      },
      {
        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',
    ]"
    :columns="[
      { width: 60, name: 'order', label: $t('order'), align: 'right', showIf: false },
      { width: 100, name: 'inputValue', label: $t('re.option.grid.entity.inputValue') },
      { width: 100, name: 'value', label: $t('re.option.grid.entity.value') },
      { width: 400, name: 'title', label: $t('re.option.grid.entity.title') },
      { width: 200, name: 'description', label: $t('description') },
      {
        width: 400,
        name: 'config',
        label: $t('re.option.grid.entity.config'),
        format: (value: any, row: any) => {
          return PlaceHolder.replace(value);
        },
      },
    ]"
    :editor="{
      dialog: {
        width: '600px',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'parameter', label: 'parameter', type: 'w-text', defaultValue: parameter.id, showIf: false },
          { name: 'id', label: $t('id'), type: 'w-text', showIf: false },
          {
            name: 'inputValue',
            label: $t('re.option.grid.entity.inputValue'),
            type: 'w-text',
          },
          {
            name: 'value',
            label: $t('re.option.grid.entity.value'),
            type: 'w-text',
          },
          {
            name: 'title',
            label: $t('re.option.grid.entity.title'),
            type: 'w-text',
          },
          {
            name: 'description',
            label: $t('description'),
            type: 'w-text',
          },
          {
            name: 'config',
            label: $t('re.option.grid.entity.config'),
            type: 'w-code-mirror',
            toolbar: false,
            lang: 'java',
            rows: 4,
            lineWrap: true,
            lineBreak: false,
            placeholder: true,
            autoCompletion: autoCompletionManager.autoCompletion(),
          },
        ],
      },
    }"
    :viewer="{
      panel: {
        columnNum: 1,
        fields: [
          { name: 'id', label: $t('id') },
          { name: 'order', label: $t('order') },
          { name: 'inputValue', label: $t('re.option.grid.entity.inputValue') },
          { name: 'value', label: $t('re.option.grid.entity.value') },
          { name: 'title', label: $t('re.option.grid.entity.title') },
          { name: 'description', label: $t('description') },
          { name: 'config', label: $t('re.option.grid.entity.config') },
          ...CorporationAuditorEntityManager.getViewerFields(),
        ],
      },
    }"
    @after-editor-open="
      (args: any) => {
        autoCompletionManager.load(Environment.apiContextPath('/api/re/common/parameterAndValueType/findByParameterId/' + parameter.id));
      }
    "
  ></w-grid>
</template>
<script setup lang="ts">
import 'tailwindcss/utilities.css';
import { ref, onMounted } from 'vue';
import { Environment, Tools, Formater, CorporationAuditorEntityManager } from 'platform-core';
import { AutoCompletionManager } from '@/views/shared/AutoCompletionManager';
import { PlaceHolder } from '@/utils/PlaceHolder';

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

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

const gridRef = ref();
const autoCompletionManager = new AutoCompletionManager();

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

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

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