<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="true"
    :tree-icon="
      (row) => {
        if (row.category === 'M') {
          return { name: 'folder', color: 'amber' };
        } else {
          return { name: 'bi-p-circle' };
        }
      }
    "
    db-click-operation="edit"
    :tree-default-expand-all="true"
    :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',
        enableIf: (arg) => {
          return arg.selected && arg.selected.category !== 'M';
        },
      },
      'separator',
      {
        name: 'execute',
        label: $t('execute'),
        icon: 'bi-caret-right',
        click: () => {
          axios.post(Environment.apiContextPath('/api/re/testCase/executeTestCase/' + testCase.id), {}, { loading: true }).then(() => {
            gridRef?.refresh();
          });
        },
      },
      'separator',
      'view',
      'separator',
      'export',
    ]"
    :columns="[
      { width: 350, name: 'name', label: $t('name'), sortable: false },
      { width: 100, name: 'parameterType', label: $t('type'), format: Formater.enum(Enums.ParameterType), sortable: false },
      {
        width: 90,
        name: 'valueType',
        label: $t('re.resources.designer.parameter.grid.entity.valueType'),
        sortable: false,
        format: (value) => {
          return ValueTypeMap[value];
        },
      },
      { width: 90, name: 'defaultValue', label: $t('defaultValue'), sortable: false },
      { width: 90, name: 'inputValue', label: $t('re.resources.designer.testCaseParameter.grid.entity.inputValue'), sortable: false },
      { width: 90, name: 'expectValue', label: $t('re.resources.designer.testCaseParameter.grid.entity.expectValue'), sortable: false },
      { width: 90, name: 'resultValue', label: $t('re.resources.designer.testCaseParameter.grid.entity.resultValue'), sortable: false },
      { width: 80, name: 'skipCheck', label: $t('re.resources.designer.testCaseParameter.grid.entity.skipCheck'), sortable: false },
      {
        width: 80,
        name: 'testResult',
        label: $t('re.resources.designer.testCaseParameter.grid.entity.testResult'),
        format: PassOrNotFormater,
        sortable: false,
      },
    ]"
    :editor="{
      dialog: {
        width: '600px',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'testCase', label: $t('testCase'), type: 'w-text', defaultValue: testCase.id, hidden: true },
          { name: 'id', label: $t('id'), type: 'w-text', hidden: true },
          { name: 'source', label: $t('source'), type: 'w-text', hidden: true },
          { name: 'parameterType', label: $t('parameterType'), type: 'w-text', hidden: true },
          { name: 'scoreCardVarType', label: $t('scoreCardVarType'), type: 'w-text', hidden: true },
          { name: 'indicatorType', label: $t('indicatorType'), type: 'w-text', hidden: true },
          {
            name: 'inputValue',
            label: $t('re.resources.designer.testCaseParameter.grid.entity.inputValue'),
            type: 'w-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: 'w-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: 'w-code-mirror',
            rows: 4,
            lang: 'json',
          },
          { name: 'skipCheck', label: $t('re.resources.designer.testCaseParameter.grid.entity.skipCheck'), type: 'w-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();
};

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