<template>
  <w-grid
    ref="gridRef"
    :title="$t('re.resources.designer.validator.grid.title')"
    :draggable="true"
    dense-body
    class="px-1"
    hide-bottom
    :config-button="false"
    selection="multiple"
    :checkbox-selection="true"
    :tree="false"
    :fetch-data-url="Environment.apiContextPath('/api/re/model/parameter/validator/findByParameterId?parameterId=' + parameter.id)"
    :data-url="Environment.apiContextPath('/api/re/model/parameter/validator')"
    :pageable="false"
    :toolbar-configure="{ noIcon: false }"
    :toolbar-actions="[
      'refresh',
      'separator',
      {
        extend: 'add',
        enableIf: (arg) => {
          return !readOnly;
        },
      },
      {
        extend: 'edit',
        enableIf: (arg) => {
          return !readOnly && arg.selected;
        },
      },
      {
        extend: 'remove',
        enableIf: (arg) => {
          return !readOnly && arg.selected;
        },
      },
      'separator',
      'view',
      'separator',
      'export',
    ]"
    :columns="[
      { width: 60, name: 'order', label: $t('order'), sortable: false, align: 'right' },
      { width: 100, name: 'type', label: $t('type'), sortable: false, format: Formater.enum(Enums.ValidatorType) },
      {
        width: 300,
        name: 'content',
        label: $t('re.resources.designer.validator.grid.entity.content'),
        sortable: false,
        format: (value, row) => {
          const type = row.type;
          if (type == 'EMPTY' || type == 'NOT_EMPTY' || type == 'TRUE' || type == 'FALSE' || type == 'EMAIL') {
            return '';
          } else if (type == 'INTEGER_RANGE' || type == 'DECIMAL_RANGE' || type == 'LENGTH_RANGE' || type == 'DATE_RANGE') {
            return Tools.generateIntervalRange(row.minInclude, row.minValue, row.maxValue, row.maxInclude);
          } else if (type == 'PATTERN') {
            return row.pattern;
          }
          return '';
        },
      },
      { width: '100%', name: 'tip', label: $t('re.resources.designer.validator.grid.entity.tip'), sortable: false },
    ]"
    :editor="{
      dialog: {
        width: '600px',
        height: '550px',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'parameter', label: 'parameter', type: 'text', defaultValue: parameter.id, hidden: true },
          { name: 'id', label: $t('id'), type: 'text', hidden: true },
          {
            name: 'description',
            label: $t('description'),
            type: 'text',
          },
          { name: 'type', label: $t('type'), type: 'select', options: ValueTypeAndValidatorTypeMapping[parameter.valueType] },
          {
            name: 'minValue',
            label: $t('minValue'),
            type: 'text',
            showIf: (arg) => {
              const type = arg.form.getFieldValue('type');
              if (type == 'INTEGER_RANGE' || type == 'DECIMAL_RANGE' || type == 'LENGTH_RANGE' || type == 'DATE_RANGE') {
                return true;
              }
              return false;
            },
          },
          {
            name: 'minInclude',
            label: $t('isMinValueInclude'),
            type: 'checkbox',
            showIf: (arg) => {
              const type = arg.form.getFieldValue('type');
              if (type == 'INTEGER_RANGE' || type == 'DECIMAL_RANGE' || type == 'LENGTH_RANGE' || type == 'DATE_RANGE') {
                return true;
              }
              return false;
            },
          },
          {
            name: 'maxValue',
            label: $t('maxValue'),
            type: 'text',
            showIf: (arg) => {
              const type = arg.form.getFieldValue('type');
              if (type == 'INTEGER_RANGE' || type == 'DECIMAL_RANGE' || type == 'LENGTH_RANGE' || type == 'DATE_RANGE') {
                return true;
              }
              return false;
            },
          },
          {
            name: 'maxInclude',
            label: $t('isMaxValueInclude'),
            type: 'checkbox',
            showIf: (arg) => {
              const type = arg.form.getFieldValue('type');
              if (type == 'INTEGER_RANGE' || type == 'DECIMAL_RANGE' || type == 'LENGTH_RANGE' || type == 'DATE_RANGE') {
                return true;
              }
              return false;
            },
          },
          {
            name: 'pattern',
            label: $t('RegExp'),
            type: 'text',
            showIf: (arg) => {
              const type = arg.form.getFieldValue('type');
              if (type == 'PATTERN') {
                return true;
              }
              return false;
            },
          },
          {
            name: 'tip',
            label: $t('re.resources.designer.validator.grid.entity.tip'),
            type: 'text',
          },
          { name: 'order', label: $t('order'), type: 'number', hidden: false },
        ],
      },
    }"
    :viewer="{
      panel: {
        columnNum: 1,
        fields: [
          { name: 'id', label: $t('id') },
          { name: 'order', label: $t('order') },
          { name: 'type', label: $t('type'), format: Formater.none() },
          { name: 'description', label: $t('description') },
          { name: 'tip', label: $t('re.resources.designer.validator.grid.entity.tip') },
          { name: 'minInclude', label: $t('isMinValueInclude') },
          { name: 'minValue', label: $t('minValue') },
          { name: 'maxValue', label: $t('maxValue') },
          { name: 'maxInclude', label: $t('isMaxValueInclude') },
          { name: 'pattern', label: $t('RegExp') },

          { 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, onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
import { Environment, EnumTools, Formater, Tools } from 'platform-core';

const props = defineProps({
  fetchDataUrl: { type: String, default: '' },
  dataUrl: { type: String, default: '' },
  parameter: { type: Object, default: undefined },
  readOnly: { type: Boolean, default: false },
});

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

const ValueTypeAndValidatorTypeMapping = {
  'java.lang.Boolean': [
    { value: 'NOT_EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.NOT_EMPTY') },
    { value: 'EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.EMPTY') },
    { value: 'TRUE', label: t('io.sc.engine.rule.core.enums.ValidatorType.TRUE') },
    { value: 'FALSE', label: t('io.sc.engine.rule.core.enums.ValidatorType.FALSE') },
  ],
  'java.lang.Long': [
    { value: 'NOT_EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.NOT_EMPTY') },
    { value: 'EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.EMPTY') },
    { value: 'INTEGER_RANGE', label: t('io.sc.engine.rule.core.enums.ValidatorType.INTEGER_RANGE') },
  ],
  'java.math.BigDecimal': [
    { value: 'NOT_EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.NOT_EMPTY') },
    { value: 'EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.EMPTY') },
    { value: 'DECIMAL_RANGE', label: t('io.sc.engine.rule.core.enums.ValidatorType.DECIMAL_RANGE') },
  ],
  'java.lang.String': [
    { value: 'NOT_EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.NOT_EMPTY') },
    { value: 'EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.EMPTY') },
    { value: 'LENGTH_RANGE', label: t('io.sc.engine.rule.core.enums.ValidatorType.LENGTH_RANGE') },
    { value: 'EMAIL', label: t('io.sc.engine.rule.core.enums.ValidatorType.EMAIL') },
    { value: 'PATTERN', label: t('io.sc.engine.rule.core.enums.ValidatorType.PATTERN') },
  ],
  'java.util.Date': [
    { value: 'NOT_EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.NOT_EMPTY') },
    { value: 'EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.EMPTY') },
    { value: 'DATE_RANGE', label: t('io.sc.engine.rule.core.enums.ValidatorType.DATE_RANGE') },
  ],
  'java.lang.Object': [
    { value: 'NOT_EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.NOT_EMPTY') },
    { value: 'EMPTY', label: t('io.sc.engine.rule.core.enums.ValidatorType.EMPTY') },
  ],
};

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

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

defineExpose({
  refresh,
});

const Enums = await EnumTools.fetch(['io.sc.engine.rule.core.enums.ValidatorType']);
</script>
