<template>
  <w-grid
    ref="i18nGridRef"
    :title="$t('system.user.grid.title')"
    selection="multiple"
    :data-url="Environment.apiContextPath('/api/system/i18n')"
    :query-form-fields="[
      { name: 'code', label: $t('code'), type: 'text' },
      { name: 'message', label: $t('i18nMessage'), type: 'text' },
      { name: 'lang', label: $t('language'), type: 'select', options: Options.enum(LanguageEnum), queryOperator: 'equals' },
      { name: 'dataComeFrom', label: $t('dataComeFrom'), type: 'select', options: Options.enum(DataComeFromEnum), queryOperator: 'equals' },
    ]"
    :toolbar-configure="{ noIcon: false }"
    :toolbar-actions="[
      'query',
      'refresh',
      'separator',
      'add',
      'clone',
      'edit',
      'remove',
      {
        name: 'deleteAll',
        label: $t('deleteAll'),
        icon: 'bi-trash3',
        click: () => {
          DialogManager.confirm($t('system.i18n.grid.toolbar.removeAll.tip'), () => {
            axios.post(Environment.apiContextPath('/api/system/i18n/removeMessages')).then(() => {
              NotifyManager.info($t('operationSuccess'));
              i18nGridRef.refresh();
            });
          });
        },
      },
      'separator',
      {
        name: 'importAll',
        label: $t('import'),
        icon: 'bi-arrow-right-circle',
        click: () => {
          DialogManager.confirm($t('system.i18n.grid.toolbar.importAll.tip'), () => {
            axios.post(Environment.apiContextPath('/api/system/i18n/importMessages')).then(() => {
              NotifyManager.info($t('operationSuccess'));
              i18nGridRef.refresh();
            });
          });
        },
      },
      'separator',
      'view',
      'export',
    ]"
    :columns="[
      { name: 'code', label: $t('code') },
      { name: 'lang', label: $t('language'), format: Formater.enum(LanguageEnum) },
      { name: 'message', label: $t('i18nMessage') },
      { 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: 'lang', label: $t('language'), type: 'select', required: true, options: Options.enum(LanguageEnum) },
          { name: 'message', label: $t('i18nMessage'), type: 'text', required: true },
        ],
      },
    }"
    :viewer="{
      panel: {
        columnNum: 1,
        fields: [
          { name: 'id', label: $t('id') },
          { name: 'code', label: $t('code') },
          { name: 'lang', label: $t('language') },
          { name: 'message', label: $t('i18nMessage') },
          { 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') },
          { name: 'corporationCode', label: $t('corporationCode') },
        ],
      },
    }"
    @row-click="(evt, row, index) => {}"
  ></w-grid>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { axios, Environment, EnumTools, Formater, Options, DialogManager, NotifyManager } from 'platform-core';

const i18nGridRef = ref();
const DataComeFromEnum = await EnumTools.fetch('io.sc.platform.orm.api.enums.DataComeFrom');
const LanguageEnum = await EnumTools.fetch('io.sc.platform.core.enums.Language');
</script>
