<template>
  <w-grid
    ref="gridRef"
    :title="$t('re.resources.designer.testCaseParameter.grid.title')"
    dense-body
    class="px-1"
    hide-bottom
    :config-button="false"
    selection="multiple"
    :checkbox-selection="false"
    :tree="false"
    :tree-icon="
      (row) => {
        if (row.category === 'M') {
          return { name: 'folder', color: 'amber' };
        } else {
          return { name: 'bi-p-circle' };
        }
      }
    "
    db-click-operation="edit"
    :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',
      {
        extend: 'edit',
      },
      'separator',
      {
        name: 'execute',
        label: $t('execute'),
        icon: 'bi-caret-right',
        click: () => {
          axios.post(Environment.apiContextPath('/api/re/testCase/executeTestCase/' + testCase.id)).then(() => {
            gridRef?.refresh();
          });
        },
      },
      'separator',
      'view',
      'separator',
      'export',
    ]"
    :columns="[
      { width: 300, name: 'name', label: $t('name') },
      { width: 100, name: 'indicatorType', label: $t('type'), format: Formater.enum(Enums.IndicatorType) },
      {
        width: 100,
        name: 'valueType',
        label: $t('re.resources.designer.parameter.grid.entity.valueType'),
        format: (value, row) => {
          return ValueTypeMap[value];
          // if (row.valueTypeVersion) {
          //   return ValueTypeMap[value] + ' (V' + row.valueTypeVersion + ')';
          // }
          // if (row.valueType == 'java.math.BigDecimal') {
          //   if (row.valueRoundingMode == 'HALF_UP') {
          //     return ValueTypeMap[value] + '(' + row.valueScale + ')';
          //   } else {
          //     return ValueTypeMap[value] + '(' + row.valueScale + ',' + Formater.enum(Enums.RoundingMode)(row.valueRoundingMode) + ')';
          //   }
          // }
          // var result = ValueTypeMap[value];
          // result = result || row.valueType;
          // if (result) {
          //   result = result.replace('<', '&lt;');
          //   result = result.replace('>', '&gt;');
          // }
          // return result;
        },
      },
      { 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',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'testCase', label: $t('testCase'), type: 'text', defaultValue: testCase.id, hidden: true },
          { name: 'id', label: $t('id'), type: 'text', hidden: true },
          { name: 'source', label: $t('source'), type: 'text', hidden: true },
          { name: 'parameterType', label: $t('parameterType'), type: 'text', hidden: true },
          { name: 'scoreCardVarType', label: $t('scoreCardVarType'), type: 'text', hidden: true },
          { name: 'indicatorType', label: $t('indicatorType'), type: 'text', hidden: true },
          {
            name: 'inputValue',
            label: $t('re.resources.designer.testCaseParameter.grid.entity.inputValue'),
            type: 'code-mirror',
            rows: 4,
            showIf: (arg) => {
              const parameterType = arg.form.getFieldValue('parameterType');
              const scoreCardVarType = arg.form.getFieldValue('scoreCardVarType');
              const indicatorType = arg.form.getFieldValue('indicatorType');
              if (
                (!Tools.isUndefinedOrNull(parameterType) && (parameterType === 'IN' || parameterType === 'IN_OPTION' || parameterType === 'INDICATOR')) ||
                (!Tools.isUndefinedOrNull(scoreCardVarType) && scoreCardVarType !== 'RESULT') ||
                (!Tools.isUndefinedOrNull(indicatorType) && indicatorType === 'INTERFACE')
              ) {
                return true;
              }
              return false;
            },
          },
          {
            name: 'expectValue',
            label: $t('re.resources.designer.testCaseParameter.grid.entity.expectValue'),
            type: 'code-mirror',
            rows: 4,
            showIf: (arg) => {
              const parameterType = arg.form.getFieldValue('parameterType');
              const scoreCardVarType = arg.form.getFieldValue('scoreCardVarType');
              const indicatorType = arg.form.getFieldValue('indicatorType');
              if (
                (!Tools.isUndefinedOrNull(parameterType) &&
                  (parameterType === 'INTERMEDIATE' || parameterType === 'OUT' || parameterType === 'RULE_RESULT' || parameterType === 'SINGLE_RULE_RESULT')) ||
                !Tools.isUndefinedOrNull(scoreCardVarType) ||
                (!Tools.isUndefinedOrNull(indicatorType) && indicatorType === 'INDICATOR')
              ) {
                return true;
              }
              return false;
            },
          },
          {
            name: 'resultValue',
            label: $t('re.resources.designer.testCaseParameter.grid.entity.resultValue'),
            type: 'code-mirror',
            rows: 4,
            lang: 'json',
          },
          { name: 'skipCheck', label: $t('re.resources.designer.testCaseParameter.grid.entity.skipCheck'), type: 'checkbox' },
        ],
      },
    }"
    :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';
import PassOrNotFormater from '@/utils/PassOrNotFormater';

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();
};

onMounted(() => {
  gridRef?.value?.refresh();
});

defineExpose({
  refresh,
});

const Enums = await EnumTools.fetch(['io.sc.engine.rule.core.enums.IndicatorType', 'io.sc.platform.core.enums.RoundingMode']);

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;
  }
}
</script>
