<template>
  <w-grid
    ref="gridRef"
    :title="$t('re.resources.designer.option.grid.title')"
    dense-body
    class="px-1"
    hide-bottom
    :config-button="false"
    selection="multiple"
    :checkbox-selection="true"
    :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"
    :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',
      'separator',
      'export',
    ]"
    :columns="[
      { width: 60, name: 'order', label: $t('order'), sortable: false, align: 'right' },
      { width: 100, name: 'inputValue', label: $t('re.resources.designer.option.grid.entity.inputValue'), sortable: false },
      { width: 100, name: 'value', label: $t('re.resources.designer.option.grid.entity.value'), sortable: false },
      { width: 400, name: 'title', label: $t('re.resources.designer.option.grid.entity.title'), sortable: false },
      { width: '100%', name: 'description', label: $t('description'), sortable: false },
    ]"
    :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: 'inputValue',
            label: $t('re.resources.designer.option.grid.entity.inputValue'),
            type: 'text',
          },
          {
            name: 'value',
            label: $t('re.resources.designer.option.grid.entity.value'),
            type: 'text',
          },
          {
            name: 'title',
            label: $t('re.resources.designer.option.grid.entity.title'),
            type: 'text',
          },
          {
            name: 'description',
            label: $t('description'),
            type: 'text',
          },
          { name: 'order', label: $t('order'), type: 'number', hidden: false },
        ],
      },
    }"
    :viewer="{
      panel: {
        columnNum: 1,
        fields: [
          { name: 'id', label: $t('id') },
          { name: 'order', label: $t('order') },
          { name: 'inputValue', label: $t('re.resources.designer.option.grid.entity.inputValue') },
          { name: 'value', label: $t('re.resources.designer.option.grid.entity.value') },
          { name: 'title', label: $t('re.resources.designer.option.grid.entity.title') },
          { name: 'description', label: $t('description') },

          { 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 } from 'platform-core';

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 refresh = () => {
  gridRef.value.refresh();
};

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

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