<template>
  <div>
    <w-grid
      ref="treeGridRef"
      :title="$t('re.resources.grid.title')"
      dense-body
      :config-button="true"
      selection="multiple"
      :checkbox-selection="true"
      :tree="true"
      :tree-icon="
        (row) => {
          if (row.type === 'FOLDER') {
            return { name: 'folder', color: 'amber' };
          } else if (row.type === 'MODEL') {
            return { name: 'bi-boxes' };
          } else if (row.type === 'SCORE_CARD') {
            return { name: 'bi-card-list' };
          } else {
            return { name: row.icon };
          }
        }
      "
      :data-url="Environment.apiContextPath('/api/re/resource')"
      :pageable="false"
      :toolbar-configure="{ noIcon: false }"
      :toolbar-actions="[
        'refresh',
        'separator',
        'expand',
        [
          {
            extend: 'add',
            click: undefined,
          },
          {
            extend: 'addTop',
            label: $t('re.resources.grid.toolbar.addTop'),
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'FOLDER');
            },
          },
          {
            extend: 'addChild',
            label: $t('re.resources.grid.toolbar.addChild'),
            enableIf: (arg) => {
              return arg.selected && arg.selected.type === 'FOLDER';
            },
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'FOLDER');
            },
          },
          'separator',
          {
            extend: 'addChild',
            name: 'addModel',
            label: $t('re.resources.grid.toolbar.addModel'),
            icon: 'bi-boxes',
            enableIf: (arg) => {
              return arg.selected && arg.selected.type === 'FOLDER';
            },
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'MODEL');
            },
          },
          {
            extend: 'addChild',
            name: 'addScoreCard',
            label: $t('re.resources.grid.toolbar.addScoreCard'),
            icon: 'bi-card-list',
            enableIf: (arg) => {
              return arg.selected && arg.selected.type === 'FOLDER';
            },
            afterClick: (arg) => {
              arg.grid.getEditorForm().setFieldValue('type', 'SCORE_CARD');
            },
          },
        ],
        [
          'clone',
          {
            extend: 'deepClone',
            name: 'deepClone',
            label: $t('deepClone'),
            icon: 'bi-copy',
            enableIf: (arg) => {
              return arg.selected && arg.selected.type !== 'FOLDER';
            },
            click: (arg) => {
              DialogManager.confirm($t('re.resources.grid.toolbar.deepClone.tip'), () => {
                axios.post(Environment.apiContextPath('/api/re/resource/deepClone/' + arg.selected.id)).then(() => {
                  treeGridRef.refresh();
                });
              });
            },
          },
          {
            extend: 'clone',
            name: 'deepCloneNew',
            label: $t('re.resources.grid.toolbar.deepCloneNew'),
            icon: 'bi-copy',
            enableIf: (arg) => {
              return arg.selected && arg.selected.type !== 'FOLDER';
            },
            click: (arg) => {
              DialogManager.confirm($t('re.resources.grid.toolbar.deepCloneNew.tip'), () => {
                axios.post(Environment.apiContextPath('/api/re/resource/deepCloneNew/' + arg.selected.id)).then(() => {
                  treeGridRef.refresh();
                });
              });
            },
          },
        ],
        'edit',
        'remove',
        'separator',
        {
          name: 'attachment',
          label: $t('attachment'),
          icon: 'bi-briefcase-fill',
          enableIf: (arg) => {
            return arg.selected && arg.selected.type !== 'FOLDER';
          },
          click: (arg) => {
            if (arg.selected) {
              foreignValueRef = arg.selected.id;
              attachmentDialogRef.open();
            }
          },
        },
        /*
      {
        name: 'compare',
        label: $t('compare'),
        icon: 'bi-code',
        enableIf: (arg) => {
          return arg.selecteds && arg.selecteds.length === 2;
        },
        click: () => {},
      },*/
        {
          name: 'design',
          label: $t('re.resources.grid.toolbar.design'),
          icon: 'bi-vector-pen',
          enableIf: (arg) => {
            return arg.selected && arg.selected.type !== 'FOLDER';
          },
          click: (arg) => {
            designerDialogRef.open(arg.selected);
          },
        },
        [
          {
            name: 'deploy',
            label: $t('deploy'),
            icon: 'bi-balloon',
            enableIf: (arg) => {
              return arg.selected && arg.selected.type !== 'FOLDER';
            },
            click: undefined,
          },
          {
            name: 'online',
            label: $t('re.resources.grid.toolbar.deploy.online'),
            icon: 'bi-arrow-up-right-circle',
            enableIf: (arg) => {
              return arg.selected && arg.selected.type !== 'FOLDER';
            },
            click: (arg) => {
              DialogManager.confirm($t('re.resources.grid.toolbar.deploy.online.tip'), () => {
                axios.post(Environment.apiContextPath('/api/re/resource/deploy/' + arg.selected.id)).then(() => {
                  treeGridRef.refresh();
                });
              });
            },
          },
          {
            name: 'offline',
            label: $t('re.resources.grid.toolbar.deploy.offline'),
            icon: 'bi-arrow-down-right-circle',
            enableIf: (arg) => {
              return arg.selected && arg.selected.type !== 'FOLDER';
            },
            click: () => {},
          },
        ],
        [
          {
            name: 'import',
            label: $t('import'),
            icon: 'file_upload',
            click: () => {},
          },
          {
            name: 'importExample',
            label: $t('re.resources.grid.toolbar.importExample'),
            icon: 'bi-cloud-arrow-up-fill',
            click: () => {},
          },
        ],
        'separator',
        'view',
        'separator',
        'export',
      ]"
      :columns="[
        { width: '100%', name: 'name', label: $t('name') },
        { width: 80, name: 'type', label: $t('type'), format: Formater.enum(Enums.ResourceType) },
        { width: 100, name: 'code', label: $t('code') },
        { width: 60, name: 'version', label: $t('version') },
        { width: 60, name: 'status', label: $t('status'), format: Formater.enum(Enums.DeployStatus) },
        { width: 120, name: 'taskName', label: $t('re.resources.grid.entity.taskName') },
        { width: 80, name: 'taskAssignee', label: $t('re.resources.grid.entity.taskAssignee') },
        { width: 80, name: 'lastModifier', label: $t('lastModifier'), sortable: false },
        { width: 90, name: 'lastModifyDate', label: $t('lastModifyDate'), sortable: false, format: Formater.dateOnly() },
        {
          width: 200,
          name: 'attachmentHtml',
          label: $t('attachment'),
          format: (value) => {
            return value;
          },
        },
      ]"
      :editor="{
        dialog: {
          width: '600px',
          height: '400px',
        },
        form: {
          colsNum: 1,
          fields: [
            { name: 'type', label: $t('type'), type: 'text', hidden: true },
            { name: 'status', label: $t('status'), type: 'text', hidden: true },
            { name: 'version', label: $t('version'), type: 'number', hidden: true },
            {
              name: 'code',
              label: $t('code'),
              type: 'text',
              showIf: (arg) => {
                const type = arg.form.getFieldValue('type');
                return type === 'MODEL' || type === 'SCORE_CARD';
              },
            },
            { name: 'name', label: $t('name'), type: 'text', required: true },
            { name: 'description', label: $t('description'), type: 'text' },
            {
              name: 'imports',
              label: $t('re.resources.grid.entity.imports'),
              type: 'code-mirror',
              lang: 'java',
              showIf: (arg) => {
                const type = arg.form.getFieldValue('type');
                return type === 'MODEL' || type === 'SCORE_CARD';
              },
            },
          ],
        },
      }"
      :viewer="{
        panel: {
          columnNum: 1,
          fields: [
            { name: 'id', label: $t('id') },
            { name: 'name', label: $t('name') },
            { name: 'type', label: $t('type'), format: Formater.none() },
            { name: 'code', label: $t('code') },
            { name: 'version', label: $t('version') },
            { name: 'status', label: $t('status'), format: Formater.none() },
            { name: 'taskName', label: $t('re.resources.grid.entity.taskName') },
            { name: 'taskAssignee', label: $t('re.resources.grid.entity.taskAssignee') },
            {
              name: 'attachmentHtml',
              label: $t('attachment'),
              format: (value) => {
                return value;
              },
            },
            { 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>
    <AttachmentDialog
      ref="attachmentDialogRef"
      :fetch-data-url="Environment.apiContextPath('/api/system/attachment/findByBussinessKey')"
      :data-url="Environment.apiContextPath('/api/system/attachment')"
      foreign-key="bussinessKey"
      :foreign-value="foreignValueRef"
    ></AttachmentDialog>
    <DesignerDialog ref="designerDialogRef"></DesignerDialog>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { axios, Environment, DialogManager, Formater, EnumTools } from 'platform-core';
import AttachmentDialog from './AttachmentDialog.vue';
import DesignerDialog from './designer/DesignerDialog.vue';

const router = useRouter();
const treeGridRef = ref();
const attachmentDialogRef = ref();
const foreignValueRef = ref('');
const designerDialogRef = ref();
const Enums = await EnumTools.fetch(['io.sc.engine.rule.core.enums.ResourceType', 'io.sc.engine.rule.core.enums.DeployStatus']);
</script>
