<template>
  <w-grid
    ref="announcementGridRef"
    :title="$t('license.keygen.grid.title')"
    :config-button="true"
    selection="multiple"
    :checkbox-selection="true"
    :data-url="Environment.apiContextPath('/api/license/keygen')"
    :sort-by="['-lastModifyDate']"
    :query-form-cols-num="2"
    :query-form-fields="[{ name: 'consumerName', label: $t('license.keygen.grid.entity.consumerName'), type: 'w-text' }]"
    :toolbar-configure="{ noIcon: false }"
    :toolbar-actions="[
      'query',
      'refresh',
      'separator',
      'add',
      'clone',
      'edit',
      'remove',
      'separator',
      {
        name: 'keygen',
        label: $t('license.keygen.grid.toolbar.keygen'),
        icon: 'download',
        enableIf: (arg) => {
          return arg.selected;
        },
        click: (arg) => {
          Downloader.get(Environment.apiContextPath('/api/license/keygen/generateLicenseFile/' + arg.selected.id), { loading: true });
        },
      },
      'separator',
      'view',
      'separator',
      'export',
    ]"
    :columns="[
      { width: 150, name: 'productName', label: $t('license.keygen.grid.entity.productName') },
      { width: '*', name: 'consumerName', label: $t('license.keygen.grid.entity.consumerName') },
      {
        width: 140,
        name: 'macAddresses',
        label: $t('license.keygen.grid.entity.macAddressesOnly'),
        format: (value, row) => {
          if (value) {
            let str = '';
            const splits: string[] = value.split(',');
            splits.forEach((split, index) => {
              if (index === splits.length) {
                str += split;
              } else {
                str += split + '<br>';
              }
            });
            return str;
          }
          return value;
        },
      },
      {
        width: 100,
        name: 'type',
        label: $t('license.keygen.grid.entity.type'),
        format: Formater.enum(LicenseTypeEnum),
      },
      { width: 80, name: 'term', label: $t('license.keygen.grid.entity.term'), align: 'right' },
      { width: 140, name: 'issuedDate', label: $t('license.keygen.grid.entity.issuedDate') },
      { width: 140, name: 'startDate', label: $t('license.keygen.grid.entity.startDate') },
      { width: 140, name: 'expiredDate', label: $t('license.keygen.grid.entity.expiredDate') },
    ]"
    :editor="{
      dialog: {
        width: '800px',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'consumerName', label: $t('license.keygen.grid.entity.consumerName'), type: 'w-text' },
          { name: 'description', label: $t('license.keygen.grid.entity.description'), type: 'w-textarea' },
          { name: 'type', label: $t('license.keygen.grid.entity.type'), type: 'w-select', options: Options.enum(LicenseTypeEnum) },
          {
            name: 'term',
            label: $t('license.keygen.grid.entity.term'),
            type: 'w-number',
            showIf: (arg) => {
              return 'TERM' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'expiredDate',
            label: $t('license.keygen.grid.entity.expiredDate'),
            type: 'w-date',
            showIf: (arg) => {
              return 'EXPIRED' === arg.form.getFieldValue('type');
            },
          },
          {
            name: 'macAddresses',
            label: $t('license.keygen.grid.entity.macAddresses'),
            type: 'w-text',
          },
        ],
      },
    }"
    :viewer="{
      panel: {
        columnNum: 1,
        fields: [
          { name: 'id', label: $t('id') },
          { name: 'productName', label: $t('license.keygen.grid.entity.productName') },
          { name: 'consumerName', label: $t('license.keygen.grid.entity.consumerName') },
          { name: 'description', label: $t('license.keygen.grid.entity.description') },
          { name: 'type', label: $t('license.keygen.grid.entity.type') },
          { name: 'issuedDate', label: $t('license.keygen.grid.entity.issuedDate') },
          { name: 'startDate', label: $t('license.keygen.grid.entity.startDate') },
          { name: 'expiredDate', label: $t('license.keygen.grid.entity.expiredDate') },
          { name: 'macAddresses', label: $t('license.keygen.grid.entity.macAddresses') },
          { name: 'creator', label: $t('creator') },
          { name: 'createDate', label: $t('createDate') },
          { name: 'lastModifier', label: $t('lastModifier') },
          { name: 'lastModifyDate', label: $t('lastModifyDate'), format: Formater.none() },
        ],
      },
    }"
  ></w-grid>
</template>
<script setup lang="ts">
import 'tailwindcss/utilities.css';
import { Environment, EnumTools, Options, Formater, Downloader } from 'platform-core';

const LicenseTypeEnum = await EnumTools.fetch('io.sc.platform.core.enums.LicenseType');
</script>
