<template>
  <w-grid
    ref="dictionaryGridRef"
    :title="$t('system.user.grid.title')"
    selection="multiple"
    :data-url="Environment.apiContextPath('/api/system/dictionary')"
    :query-form-fields="[
      { name: 'code', label: $t('code'), type: 'text' },
      { name: 'dataComeFrom', label: $t('dataComeFrom'), type: 'select', options: Options.enum(DataComeFromEnum), queryOperator: 'equals' },
    ]"
    :toolbar-configure="{ noIcon: false }"
    :toolbar-actions="['refresh', 'separator', 'add', 'edit', 'remove', 'separator', 'view', 'export']"
    :columns="[
      { width: 200, name: 'code', label: $t('code') },
      { width: 200, name: 'value', label: $t('value') },
      { width: '100%', name: 'value', label: $t('displayValue'), format: (value) => $t(value) },
      { width: 100, name: 'order', label: $t('order'), align: 'right' },
      { width: 100, name: 'dataComeFrom', label: $t('dataComeFrom'), format: Formater.enum(DataComeFromEnum) },
      { width: 100, name: 'lastModifier', label: $t('lastModifier') },
      { width: 100, 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') },
        ],
      },
    }"
    :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'), format: Formater.enum(DataComeFromEnum) },
          { name: 'creator', label: $t('creator') },
          { name: 'createDate', label: $t('createDate') },
          { name: 'lastModifier', label: $t('lastModifier') },
          { name: 'lastModifyDate', label: $t('lastModifyDate') },
          { name: 'corporationCode', label: $t('corporationCode') },
        ],
      },
    }"
    @row-click="(evt, row, index) => {}"
  ></w-grid>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { Environment, axios, EnumTools, Formater, Options } from 'platform-core';

const dictionaryGridRef = ref();
const DataComeFromEnum = await EnumTools.fetch('io.sc.platform.orm.api.enums.DataComeFrom');

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>
