<template>
  <w-grid
    ref="dictionaryGridRef"
    :title="$t('system.dictionary.grid.title')"
    :config-button="true"
    selection="multiple"
    :checkbox-selection="true"
    :data-url="Environment.apiContextPath('/api/system/dictionary')"
    :query-form-fields="[{ name: 'code', label: $t('code'), type: 'text' }]"
    :toolbar-configure="{ noIcon: false }"
    :toolbar-actions="['refresh', 'separator', 'add', 'clone', 'edit', 'remove', 'separator', 'view', 'separator', 'export']"
    :columns="[
      { width: 200, name: 'code', label: $t('code') },
      { width: 200, name: 'value', label: $t('value') },
      { width: '100%', name: 'value', label: $t('displayValue'), sortable: false, format: (value) => $t(value) },
      { width: 100, name: 'order', label: $t('order'), align: 'right' },
      { width: 100, name: 'lastModifier', label: $t('lastModifier') },
      { width: 150, name: 'lastModifyDate', label: $t('lastModifyDate'), format: Formater.dateOnly() },
    ]"
    :editor="{
      dialog: {
        width: '600px',
        height: '300px',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'code', label: $t('code'), type: 'text', required: true },
          { name: 'value', label: $t('value'), type: 'text', required: true },
          { name: 'order', label: $t('order'), type: 'text', required: true },
        ],
      },
    }"
    :viewer="{
      panel: {
        columnNum: 1,
        fields: [
          { name: 'id', label: $t('id') },
          { name: 'code', label: $t('code') },
          { name: 'value', label: $t('value') },
          { name: 'dataComeFrom', label: $t('dataComeFrom') },
          { name: 'creator', label: $t('creator') },
          { name: 'createDate', label: $t('createDate') },
          { name: 'lastModifier', label: $t('lastModifier'), format: Formater.none() },
          {
            name: 'lastModifyDate',
            label: $t('lastModifyDate'),
            format: (value) => {
              return value;
            },
          },
          { name: 'corporationCode', label: $t('corporationCode') },
        ],
      },
    }"
    @row-click="(evt, row, index) => {}"
  ></w-grid>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { Environment, axios, Formater } from 'platform-core';

const dictionaryGridRef = ref();

const rowDragDropAfter = () => {
  const rows = dictionaryGridRef.value.getRowsFun();
  for (let i = 0; i < rows.length; i++) {
    rows[i].order = i;
  }
  axios.post(Environment.apiContextPath('/api/system/dictionary/updateDictionariesOrder'), rows).then(() => {
    //dictionaryGridRef.value.refreshTable();
  });
};
</script>
