<template>
  <w-grid
    ref="gridRef"
    :title="$t('engine.st.testCase.grid.title')"
    dense
    :checkbox-selection="true"
    :tree="false"
    :data-url="Environment.apiContextPath('/api/st/testCase')"
    :pageable="false"
    :sort-by="['lastModifyDate']"
    :toolbar-actions="[
      'refresh',
      'separator',
      'add',
      'clone',
      'edit',
      'remove',
      'separator',
      {
        name: 'test',
        label: $t('test'),
        icon: 'bi-caret-right',
        enableIf: (arg) => {
          return arg.selected;
        },
        click: (arg) => {
          if (arg.selected) {
            axios.post(Environment.apiContextPath('/api/st/testCase/test?testCaseId=' + arg.selected.id)).then(() => {
              NotifyManager.success();
              emit('afterTest');
            });
          }
        },
      },
      'view',
      'separator',
      'export',
    ]"
    :columns="[
      { width: 160, name: 'name', label: $t('name') },
      {
        width: 150,
        name: 'model',
        label: $t('engine.st.testCase.grid.entity.model'),
        format: (value, row) => {
          return modelMap[value];
        },
      },
    ]"
    :editor="{
      dialog: {
        width: '500px',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'name', label: $t('name'), type: 'text', required: true },
          { name: 'model', label: $t('engine.st.testCase.grid.entity.model'), type: 'select', options: modelOptionsRef, required: true },
        ],
      },
    }"
    :viewer="{
      panel: {
        columnNum: 1,
        fields: [
          { name: 'id', label: $t('id') },
          { name: 'name', label: $t('name') },
          { name: 'model', label: $t('model') },
          { 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 } from 'vue';
import { Environment, Formater, axios, NotifyManager } from 'platform-core';

const emit = defineEmits<{
  (e: 'afterTest'): void;
}>();

const gridRef = ref();
const modelMap = {};
const modelOptionsRef = ref([]);

axios.get(Environment.apiContextPath('/api/st/model?pageable=false')).then((response) => {
  const options = [];
  for (const item of response.data.content) {
    modelMap[item.id] = item.name;
    options.push({
      value: item.id,
      label: item.name,
    });
  }
  modelOptionsRef.value = options;
});
</script>
