<template>
  <div style="height: 100%">
    <w-grid
      ref="gridRef"
      :title="$t('re.lib.grid.title')"
      dense-body
      hide-bottom
      :config-button="true"
      selection="multiple"
      :checkbox-selection="false"
      :tree="false"
      :fetch-data-url="Environment.apiContextPath('api/re/indicator?lib=' + lib.id)"
      :data-url="Environment.apiContextPath('api/re/indicator')"
      :pageable="false"
      :toolbar-configure="{ noIcon: false }"
      :toolbar-actions="[
        'query',
        'refresh',
        'separator',
        [
          {
            name: 'addGroup',
            label: $t('re.lib.grid.toolbar.addGroup'),
            icon: 'add',
            click: undefined,
          },
          {
            extend: 'add',
            name: 'addInterface',
            label: $t('re.lib.grid.toolbar.addTop'),
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'FOLDER');
            },
          },
          {
            extend: 'add',
            name: 'addIndicator',
            label: $t('re.lib.grid.toolbar.addChild'),
            enableIf: (arg) => {
              return arg.selected && arg.selected.type === 'FOLDER';
            },
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'FOLDER');
            },
          },
        ],
        [
          {
            name: 'cloneGroup',
            label: $t('re.lib.grid.toolbar.cloneGroup'),
            icon: 'content_copy',
            click: undefined,
            enableIf: (arg) => {
              return arg.selected;
            },
          },
          'clone',
          {
            extend: 'deepClone',
            name: 'deepClone',
            label: $t('re.lib.grid.toolbar.deepClone'),
            icon: 'bi-copy',
            enableIf: (arg) => {
              return arg.selected && arg.selected.type !== 'FOLDER';
            },
            click: (arg) => {
              DialogManager.confirm($t('re.lib.grid.toolbar.deepClone.tip'), () => {
                axios.post(Environment.apiContextPath('/api/re/lib/deepClone/' + arg.selected.id)).then(() => {
                  libTreeGridRef.refresh();
                });
              });
            },
          },
        ],
        'separator',
        'edit',
        'remove',
        'separator',
        'view',
        'separator',
        'export',
      ]"
      :query-form-fields="[
        { name: 'code', label: $t('code'), type: 'text', clearable: true },
        { name: 'name', label: $t('name'), type: 'text', clearable: true },
      ]"
      :columns="[
        { width: 60, name: 'order', label: $t('order'), align: 'right' },
        { width: 250, name: 'name', label: $t('name') },
        { width: 100, name: 'type', label: $t('type'), format: Formater.enum(Enums.IndicatorType) },
        {
          width: 100,
          name: 'valueType',
          label: $t('valueType'),
          format: (value, row) => {
            return ValueTypeMap[value];
          },
        },
        { width: 80, name: 'valueTypeIsList', label: $t('re.resources.designer.parameter.grid.entity.valueTypeIsList'), format: Formater.checkTag() },
        { width: '100%', name: 'defaultValue', label: $t('defaultValue') },
      ]"
      :editor="{
        dialog: {
          width: '600px',
        },
        form: {
          colsNum: 1,
          fields: [],
        },
      }"
      :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') },
          ],
        },
      }"
      v-bind="attrs"
    ></w-grid>
    <ImportDialog ref="importDialogRef"></ImportDialog>
  </div>
</template>
<script setup lang="ts">
import { ref, useAttrs, onMounted } from 'vue';
import { axios, Environment, EnumTools, Formater, DialogManager } from 'platform-core';
import ImportDialog from './ImportDialog.vue';

const attrs = useAttrs();

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

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