<template>
  <w-grid
    ref="gridRef"
    :height="200"
    :title="$t('re.resources.designer.testCase.grid.title')"
    dense-body
    class="px-1"
    hide-bottom
    :config-button="false"
    selection="multiple"
    :checkbox-selection="false"
    :tree="true"
    :tree-icon="
      (row) => {
        if (row.category === 'M') {
          return { name: 'folder', color: 'amber' };
        } else {
          return { name: 'bi-p-circle' };
        }
      }
    "
    :fetch-data-url="Environment.apiContextPath('/api/re/testCaseParameter/findByTestCase?testCaseId=' + testCase.id)"
    :data-url="Environment.apiContextPath('/api/re/testCaseParameter')"
    :pageable="false"
    :toolbar-configure="{ noIcon: false }"
    :toolbar-actions="[
      'refresh',
      'separator',
      'expand',
      'separator',
      {
        extend: 'edit',
      },
      'separator',
      {
        name: 'execute',
        label: $t('execute'),
        icon: 'bi-caret-right',
      },
      'separator',
      'view',
      'separator',
      'export',
    ]"
    :columns="[
      { width: 300, name: 'name', label: $t('name') },
      { width: 100, name: 'parameterType', label: $t('type'), format: Formater.enum(Enums.ParameterType) },
      {
        width: 100,
        name: 'valueType',
        label: $t('re.resources.designer.parameter.grid.entity.valueType'),
        format: (value) => {
          return ValueTypeMap[value];
        },
      },
      { width: 100, name: 'defaultValue', label: $t('defaultValue') },
      { width: 100, name: 'inputValue', label: $t('re.resources.designer.testCaseParameter.grid.entity.inputValue') },
      { width: 100, name: 'expectValue', label: $t('re.resources.designer.testCaseParameter.grid.entity.expectValue') },
      { width: 100, name: 'resultValue', label: $t('re.resources.designer.testCaseParameter.grid.entity.resultValue') },
      { width: 100, name: 'skipCheck', label: $t('re.resources.designer.testCaseParameter.grid.entity.skipCheck') },
      { width: 100, name: 'testResult', label: $t('re.resources.designer.testCaseParameter.grid.entity.testResult'), format: passOrNotFormater },
    ]"
    :editor="{
      dialog: {
        width: '600px',
        height: '550px',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'testCaseId', label: $t(''), type: 'text', defaultValue: testCase.id },
          { name: 'parent', label: $t(''), type: 'text' },
          { name: 'id', label: $t(''), type: 'text' },
          { name: 'category', label: $t('') },
          { name: 'code', label: $t('') },
          { name: 'name', label: $t('') },
          { name: 'skipCheck', label: $t('') },
          { name: 'source', label: $t('') },
          { name: 'type', label: $t('') },
          { name: 'parameterType', label: $t('') },
          { name: 'scoreCardVarType', label: $t('') },
          { name: 'valueType', label: $t('') },
          { name: 'defaultValue', label: $t('') },
          { name: 'inputValue', label: $t('') },
          { name: 'expectValue', label: $t('') },
          { name: 'resultValue', label: $t('') },
          { name: 'testResult', label: $t('') },
          { name: 'order', label: $t('') },
        ],
      },
    }"
    :viewer="{
      panel: {
        columnNum: 1,
        fields: [
          { 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') },
        ],
      },
    }"
    @row-click="
      (evt, row, index) => {
        emit('rowClick', evt, row, index);
      }
    "
    @before-request-data="
      () => {
        emit('beforeRequestData');
      }
    "
  ></w-grid>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { axios, Environment, EnumTools, Formater, Options, Tools } from 'platform-core';

const props = defineProps({
  testCase: { type: Object, default: undefined },
});

const emit = defineEmits<{
  (e: 'rowClick', evt: Event, row: any, index: number): void;
  (e: 'beforeRequestData', requestParams: URLSearchParams | any, callback: any): void;
}>();

const gridRef = ref();

const refresh = () => {
  gridRef.value.refresh();
};

defineExpose({
  refresh,
});

const Enums = await EnumTools.fetch(['io.sc.engine.rule.core.enums.ParameterType']);
let ValueTypeMap = {};
const response = await axios.get(Environment.apiContextPath('/api/re/dictionary/getAllDictionaryMap'));
if (response && response.data) {
  ValueTypeMap = {};
  for (const item of response.data) {
    ValueTypeMap[item.key] = item.value;
  }
}

const passOrNotFormater = (value) => {
  if (Tools.isUndefinedOrNull(value)) {
    return '';
  }
  if (value === 'PASSED') {
    return {
      componentType: 'QIcon',
      attrs: { name: 'bi-check-circle', size: '20px', color: 'green' },
    };
  } else if (value === 'UN_PASSED') {
    return {
      componentType: 'QIcon',
      attrs: { name: 'bi-x-circle', size: '20px', color: 'red' },
    };
  } else if (value === 'ERROR') {
    return {
      componentType: 'QIcon',
      attrs: { name: 'bi-x-circle', size: '20px', color: 'red' },
    };
  }
};
</script>
