<template>
  <div style="height: 100%">
    <w-grid
      ref="gridRef"
      :title="$t('re.resources.designer.parameter.grid.title')"
      dense-body
      class="px-1"
      hide-bottom
      :config-button="false"
      selection="multiple"
      :checkbox-selection="false"
      :tree="false"
      :fetch-data-url="Environment.apiContextPath('/api/re/model/parameter/findByModelId?modelId=' + model.id)"
      :data-url="Environment.apiContextPath('/api/re/model/parameter')"
      :pageable="false"
      :toolbar-configure="{ noIcon: false }"
      :toolbar-actions="[
        'refresh',
        'separator',
        {
          name: 'move',
          label: $t('re.resources.designer.parameter.grid.toolbar.move'),
          icon: 'bi-share-fill',
        },
        [
          {
            extend: 'add',
            click: undefined,
          },
          {
            extend: 'add',
            name: 'constant',
            label: $t('re.resources.designer.parameter.grid.toolbar.add.constant'),
            icon: 'bi-tag',
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'CONSTANT');
            },
          },
          'separator',
          {
            extend: 'add',
            name: 'in',
            label: $t('re.resources.designer.parameter.grid.toolbar.add.in'),
            icon: 'bi-terminal',
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'IN');
            },
          },
          {
            extend: 'add',
            name: 'inOption',
            label: $t('re.resources.designer.parameter.grid.toolbar.add.inOption'),
            icon: 'bi-list-check',
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'IN_OPTION');
            },
          },
          {
            extend: 'add',
            name: 'indicator',
            label: $t('re.resources.designer.parameter.grid.toolbar.add.indicator'),
            icon: 'bi-link',
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'INDICATOR');
            },
          },
          'separator',
          {
            extend: 'add',
            name: 'intermediate',
            label: $t('re.resources.designer.parameter.grid.toolbar.add.intermediate'),
            icon: 'bi-superscript',
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'INTERMEDIATE');
            },
          },
          {
            extend: 'add',
            name: 'out',
            label: $t('re.resources.designer.parameter.grid.toolbar.add.out'),
            icon: 'bi-shuffle',
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'OUT');
            },
          },
          'separator',
          {
            extend: 'add',
            name: 'ruleResult',
            label: $t('re.resources.designer.parameter.grid.toolbar.add.ruleResult'),
            icon: 'bi-sliders',
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'RULE_RESULT');
            },
          },
          {
            extend: 'add',
            name: 'singleRuleResult',
            label: $t('re.resources.designer.parameter.grid.toolbar.add.singleRuleResult'),
            icon: 'bi-lightning',
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'SINGLE_RULE_RESULT');
            },
          },
          'separator',
          {
            name: 'importPmml',
            label: $t('re.resources.designer.parameter.grid.toolbar.add.importPmml'),
            icon: 'bi-file-earmark-text',
          },
        ],
        'separator',
        {
          extend: 'clone',
          enableIf: (arg) => {
            return arg.selected && arg.selected.parent;
          },
        },
        {
          extend: 'clone',
          name: 'deepClone',
          label: $t('deepClone'),
          icon: 'bi-copy',
          enableIf: (arg) => {
            return arg.selected && arg.selected.parent;
          },
          click: (arg) => {},
        },
        'edit',
        'remove',
        'separator',
        'view',
        'separator',
        'export',
      ]"
      :columns="[
        { width: 60, name: 'order', label: $t('order'), sortable: false, align: 'right' },
        { width: '100%', name: 'name', label: $t('name'), sortable: false },
        { width: 150, name: 'code', label: $t('code'), sortable: false },
        { width: 100, name: 'type', label: $t('type'), sortable: false, format: Formater.enum(Enums.ParameterType) },
        {
          width: 150,
          name: 'valueType',
          label: $t('re.resources.designer.parameter.grid.entity.valueType'),
          sortable: false,
          format: (value) => {
            return ValueTypeMap[value];
          },
        },
        {
          width: 80,
          name: 'valueTypeIsList',
          label: $t('re.resources.designer.parameter.grid.entity.valueTypeIsList'),
          sortable: false,
          format: Formater.yesNo(),
        },
        { width: 200, name: 'defaultValue', label: $t('defaultValue'), sortable: false },
      ]"
      :editor="{
        dialog: {
          width: '600px',
          height: '550px',
        },
        form: {
          colsNum: 1,
          fields: [
            { name: 'model', label: $t('modelId'), type: 'text', defaultValue: model.id, hidden: true },
            { name: 'code', label: $t('code'), type: 'text' },
            { name: 'name', label: $t('name'), type: 'text', required: true },
            { name: 'description', label: $t('description'), type: 'text' },
            { name: 'type', label: $t('type'), type: 'select', options: Options.enum(Enums.ParameterType) },
            {
              name: 'libCode',
              label: $t('re.resources.designer.parameter.grid.entity.libCode'),
              type: 'select',
              showIf: (arg) => {
                const type = arg.form.getFieldValue('type');
                return type === 'INDICATOR';
              },
            },
            {
              name: 'libVersion',
              label: $t('re.resources.designer.parameter.grid.entity.libVersion'),
              type: 'select',
              showIf: (arg) => {
                const type = arg.form.getFieldValue('type');
                return type === 'INDICATOR';
              },
            },
            {
              name: 'indicatorCode',
              label: $t('re.resources.designer.parameter.grid.entity.indicatorCode'),
              type: 'select',
              showIf: (arg) => {
                const type = arg.form.getFieldValue('type');
                return type === 'INDICATOR';
              },
            },
            {
              name: 'valueType',
              label: $t('re.resources.designer.parameter.grid.entity.valueType'),
              type: 'select',
              options: ValueTypeList,
              showIf: (arg) => {
                const type = arg.form.getFieldValue('type');
                if (type == 'INDICATOR' || type == 'IN_OPTION' || type == 'RULE_RESULT' || type == 'SINGLE_RULE_RESULT') {
                  return false;
                }
                return true;
              },
            },
            {
              name: 'valueTypeVersion',
              label: $t('re.resources.designer.parameter.grid.entity.valueTypeVersion'),
              type: 'select',
              options: ValueTypeList,
              showIf: (arg) => {
                const valueType = arg.form.getFieldValue('valueType');
                if (valueType) {
                  if (
                    valueType == 'java.lang.Boolean' ||
                    valueType == 'java.lang.Long' ||
                    valueType == 'java.lang.Float' ||
                    valueType == 'Float' ||
                    valueType == 'java.math.BigDecimal' ||
                    valueType == 'java.lang.String' ||
                    valueType == 'java.util.Date' ||
                    valueType == 'org.wsp.engine.rule.core.classes.ResourceAbstract' ||
                    valueType == 'org.wsp.engine.rule.core.classes.RuleResult' ||
                    valueType == 'org.wsp.engine.rule.core.classes.SingleRuleResult' ||
                    valueType.startsWith('List') ||
                    valueType.startsWith('Map')
                  ) {
                    return false;
                  } else if (Tools.isUndefinedOrNull(ValueTypeMap[valueType])) {
                    return false;
                  } else {
                    return true;
                  }
                }
                return false;
              },
            },
            {
              name: 'valueScale',
              label: $t('re.resources.designer.parameter.grid.entity.valueScale'),
              type: 'number',
              showIf: (arg) => {
                const valueType = arg.form.getFieldValue('valueType');
                const type = arg.form.getFieldValue('type');
                if (valueType == 'java.math.BigDecimal' && type != 'INDICATOR' && type != 'RULE_RESULT' && type != 'SINGLE_RULE_RESULT') {
                  return true;
                }
                return false;
              },
            },
            {
              name: 'valueRoundingMode',
              label: $t('re.resources.designer.parameter.grid.entity.valueRoundingMode'),
              type: 'select',
              options: Options.enum(Enums.RoundingMode),
              showIf: (arg) => {
                const valueType = arg.form.getFieldValue('valueType');
                const type = arg.form.getFieldValue('type');
                if (valueType == 'java.math.BigDecimal' && type != 'INDICATOR' && type != 'RULE_RESULT' && type != 'SINGLE_RULE_RESULT') {
                  return true;
                }
                return false;
              },
            },
            {
              name: 'valueTypeIsList',
              label: $t('re.resources.designer.parameter.grid.entity.valueTypeIsList'),
              type: 'checkbox',
              defaultValue: false,
              showIf: (arg) => {
                const type = arg.form.getFieldValue('type');
                if (type == 'IN_OPTION' || type == 'INDICATOR' || type == 'RULE_RESULT' || type == 'SINGLE_RULE_RESULT') {
                  return false;
                }
                return true;
              },
            },
            {
              name: 'defaultValue',
              label: $t('defaultValue'),
              type: 'text',
              showIf: (arg) => {
                const type = arg.form.getFieldValue('type');
                const valueTypeIsList = arg.form.getFieldValue('valueTypeIsList');
                if (type == 'INDICATOR' || type == 'RULE_RESULT' || type == 'SINGLE_RULE_RESULT' || valueTypeIsList) {
                  return false;
                }
                return true;
              },
            },

            { name: 'order', label: $t('order'), type: 'number' },
          ],
        },
      }"
      :viewer="{
        panel: {
          columnNum: 1,
          fields: [
            { name: 'order', label: $t('order') },
            { name: 'id', label: $t('id') },
            { name: 'code', label: $t('code') },
            { name: 'name', label: $t('name') },
            { name: 'description', label: $t('description') },
            { name: 'type', label: $t('type') },
            { name: 'valueType', label: $t('re.resources.designer.parameter.grid.entity.valueType') },
            { name: 'valueTypeVersion', label: $t('re.resources.designer.parameter.grid.entity.valueTypeVersion') },
            { name: 'valueScale', label: $t('re.resources.designer.parameter.grid.entity.valueScale') },
            { name: 'valueRoundingMode', label: $t('re.resources.designer.parameter.grid.entity.valueRoundingMode') },
            { name: 'valueTypeIsList', label: $t('re.resources.designer.parameter.grid.entity.valueTypeIsList') },
            { name: 'defaultValue', label: $t('defaultValue') },
            { name: 'libCode', label: $t('re.resources.designer.parameter.grid.entity.libCode') },
            { name: 'libVersion', label: $t('re.resources.designer.parameter.grid.entity.libVersion') },
            { name: 'indicatorCode', label: $t('re.resources.designer.parameter.grid.entity.indicatorCode') },

            { 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>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { axios, Environment, EnumTools, Formater, Options, Tools } from 'platform-core';

const props = defineProps({
  model: { 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.platform.core.enums.RoundingMode',
  'io.sc.engine.rule.core.enums.ParameterType',
  'io.sc.engine.rule.core.enums.DeployStatus',
]);
let ValueTypeMap = {};
const ValueTypeList = [];
const response = await axios.get(Environment.apiContextPath('/api/re/dictionary/getAllDictionaryMap'));
if (response && response.data) {
  ValueTypeMap = {};
  ValueTypeList.splice(0, ValueTypeList.length);
  for (const item of response.data) {
    ValueTypeMap[item.key] = item.value;
    ValueTypeList.push({ value: item.key, label: item.value });
  }
}

onMounted(() => {
  gridRef.value.refresh();
});
</script>
