<template>
  <div class="pr-1" style="height: 100%">
    <w-grid
      ref="treeGridRef"
      :title="$t('re.lib.grid.title')"
      dense-body
      db-click-operation="edit"
      :config-button="true"
      :checkbox-selection="false"
      :tree="true"
      :tree-icon="
        (row) => {
          if (row.type === 'FOLDER') {
            return { name: 'folder', color: 'amber' };
          } else {
            return { name: 'bi-card-list' };
          }
        }
      "
      :data-url="Environment.apiContextPath('/api/re/lib')"
      :sort-by="['type', 'namec', '-version']"
      :pageable="false"
      :toolbar-configure="{ noIcon: false }"
      :toolbar-actions="[
        'refresh',
        'separator',
        'expand',
        [
          {
            name: 'addGroup',
            label: $t('re.lib.grid.toolbar.addGroup'),
            icon: 'add',
            click: undefined,
          },
          {
            extend: 'addTop',
            name: 'addTop',
            label: $t('re.lib.grid.toolbar.addTop'),
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'FOLDER');
            },
          },
          {
            extend: 'addChild',
            name: 'addChild',
            label: $t('re.lib.grid.toolbar.addChild'),
            enableIf: (arg) => {
              return arg.selected && arg.selected.type === 'FOLDER';
            },
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'FOLDER');
            },
          },
          'separator',
          {
            name: 'addLib',
            extend: 'addChild',
            label: $t('re.lib.grid.toolbar.addLib'),
            icon: 'bi-card-list',
            enableIf: (arg) => {
              return arg.selected && arg.selected.type === 'FOLDER';
            },
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'INDICATOR');
            },
          },
          'separator',
          '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(() => {
                  treeGridRef.refresh();
                });
              });
            },
          },
          {
            extend: 'clone',
            name: 'deepCloneNew',
            label: $t('re.lib.grid.toolbar.deepCloneNew'),
            icon: 'bi-copy',
            enableIf: (arg) => {
              return arg.selected && arg.selected.type !== 'FOLDER';
            },
            click: (arg) => {
              DialogManager.confirm($t('re.lib.grid.toolbar.deepCloneNew.tip'), () => {
                axios.post(Environment.apiContextPath('/api/re/lib/deepCloneNew/' + arg.selected.id)).then(() => {
                  treeGridRef.refresh();
                });
              });
            },
          },
        ],
        'separator',
        'edit',
        'remove',
        'separator',
        {
          name: 'generateGroovy',
          label: $t('re.lib.grid.toolbar.generateGroovy'),
          icon: 'bi-code',
          enableIf: (arg) => {
            return arg.selected && arg.selected.type === 'INDICATOR';
          },
          click: (arg) => {
            axios.post(Environment.apiContextPath('/api/re/resource/generateGroovySourceCode/' + arg.selected.id), {}, { loading: true }).then((response) => {
              sourceCodeDialogRef.open(response.data.source);
            });
          },
        },
        'separator',
        {
          name: 'deploy',
          label: $t('re.lib.grid.toolbar.deploy'),
          icon: 'bi-balloon',
          enableIf: (arg) => {
            return arg.selected && arg.selected.type !== 'FOLDER' && arg.selected.status === 'SKETCH';
          },
          click: (arg) => {
            DialogManager.confirm($t('re.lib.grid.toolbar.deploy.tip'), () => {
              axios.post(Environment.apiContextPath('/api/re/lib/deploy/' + arg.selected.id)).then((response) => {
                treeGridRef.refresh();
              });
            });
          },
        },
        'separator',
        [
          {
            name: 'importGroup',
            label: $t('re.lib.grid.toolbar.importGroup'),
            icon: 'file_upload',
            click: undefined,
          },
          {
            name: 'import',
            label: $t('re.lib.grid.toolbar.import'),
            icon: 'file_upload',
            click: () => {
              importDialogRef.open();
            },
          },
          {
            name: 'importSample',
            label: $t('re.lib.grid.toolbar.importSample'),
            icon: 'bi-database-up',
            click: () => {
              importSampleDialogRef.open();
            },
          },
        ],
        'separator',
        'view',
        {
          name: 'viewBloodRelationship',
          label: $t('re.resources.grid.toolbar.viewBloodRelationship'),
          icon: 'hub',
          enableIf: (args) => {
            return args.selected && args.selected.type !== 'FOLDER';
          },
          click: (args) => {
            bloodRelationshipDialogRef.open();
          },
        },
        'separator',
        {
          extend: 'export',
          enableIf: (arg) => {
            return arg.selected;
          },
          click: (arg) => {
            Downloader.get(Environment.apiContextPath('/api/re/lib/export/' + arg.selected.id));
          },
        },
      ]"
      :columns="[
        {
          width: '100%',
          name: 'namec',
          label: $t('name'),
          format: (value: any, row: any) => {
            return row.name;
          },
        },
        { width: 135, name: 'code', label: $t('code') },
        {
          width: 60,
          name: 'type',
          label: $t('type'),
          format: (value) => {
            if (value !== 'FOLDER') {
              return EngineEnums.LibType.formater(value);
            }
          },
        },
        { width: 60, name: 'version', label: $t('version'), align: 'right' },
        {
          width: 60,
          name: 'status',
          label: $t('status'),
          align: 'center',
          format: EngineEnums.DeployStatus.formater,
        },
      ]"
      :editor="{
        dialog: {
          width: '600px',
        },
        form: {
          colsNum: 1,
          fields: [
            { name: 'parent', label: $t('parent'), type: 'w-text', showIf: false },
            { name: 'type', label: $t('type'), type: 'w-text', showIf: false },
            {
              name: 'code',
              label: $t('code'),
              type: 'w-text',
              showIf: (arg) => {
                return false;
                // const type = arg.form.getFieldValue('type');
                // return type !== 'FOLDER';
              },
            },
            { name: 'name', label: $t('name'), type: 'w-text', requiredIf: true },
            { name: 'description', label: $t('description'), type: 'w-text' },
          ],
        },
      }"
      :viewer="{
        panel: {
          columnNum: 1,
          fields: [
            { name: 'type', label: $t('type'), format: Formater.none() },
            { name: 'parent', label: $t('parent') },
            { name: 'id', label: $t('id') },
            { name: 'code', label: $t('code') },
            { name: 'name', label: $t('name') },
            { name: 'description', label: $t('description') },
            ...CorporationAuditorEntityManager.getViewerFields(),
          ],
        },
      }"
      v-bind="attrs"
    ></w-grid>
    <ImportDialog
      ref="importDialogRef"
      @after-imported="
        () => {
          treeGridRef.refresh();
        }
      "
    ></ImportDialog>
    <ImportSampleDialog ref="importSampleDialogRef"></ImportSampleDialog>
    <SourceCodeDialog ref="sourceCodeDialogRef"></SourceCodeDialog>
    <BloodRelationshipDialog ref="bloodRelationshipDialogRef"></BloodRelationshipDialog>
  </div>
</template>
<script setup lang="ts">
import 'tailwindcss/utilities.css';
import { ref, useAttrs } from 'vue';
import { axios, Environment, Formater, EnumTools, DialogManager, Downloader, CorporationAuditorEntityManager } from 'platform-core';
import ImportDialog from './ImportDialog.vue';
import ImportSampleDialog from './ImportSampleDialog.vue';
import SourceCodeDialog from '@/views/shared/SourceCodeDialog.vue';
import BloodRelationshipDialog from '@/views/shared/BloodRelationshipDialog.vue';
import { EngineEnums } from '@/views/shared/enums/EngineEnums';

const attrs = useAttrs();

const treeGridRef = ref();
const sourceCodeDialogRef = ref();
const importDialogRef = ref();
const importSampleDialogRef = ref();
const bloodRelationshipDialogRef = ref();

await EngineEnums.init();
</script>
