<template>
  <w-grid
    ref="treeGridRef"
    :title="$t('re.resources.designer.model.grid.title')"
    dense-body
    class="px-1"
    hide-bottom
    :config-button="false"
    selection="multiple"
    :checkbox-selection="false"
    :tree="true"
    :tree-icon="
      (row) => {
        return { name: 'folder', color: 'amber' };
      }
    "
    :fetch-data-url="Environment.apiContextPath('/api/re/model/findModelWithSubModelsByResourceId?resourceId=' + resource.id)"
    :data-url="Environment.apiContextPath('/api/re/model')"
    :pageable="false"
    :toolbar-configure="{ noIcon: false }"
    :toolbar-actions="[
      'refresh',
      'separator',
      'expand',
      {
        extend: 'addChild',
        label: $t('re.resources.designer.model.grid.toolbar.addChild'),
        enableIf: (arg) => {
          return !readOnly;
        },
      },
      'separator',
      {
        extend: 'clone',
        enableIf: (arg) => {
          return !readOnly && arg.selected && arg.selected.parent;
        },
      },
      {
        name: 'deepClone',
        label: $t('deepClone'),
        icon: 'bi-copy',
        enableIf: (arg) => {
          return !readOnly && arg.selected && arg.selected.parent;
        },
        click: (arg) => {
          DialogManager.confirm($t('re.resources.designer.model.grid.toolbar.deepClone.tip'), () => {
            axios.post(Environment.apiContextPath('/api/re/model/deepClone/' + arg.selected.id)).then(() => {
              treeGridRef.refresh();
            });
          });
        },
      },
      'separator',
      {
        extend: 'edit',
        enableIf: (arg) => {
          return !readOnly && arg.selected;
        },
      },
      {
        extend: 'remove',
        enableIf: (arg) => {
          return !readOnly && arg.selected;
        },
      },
      'separator',
      {
        name: 'generateGroovySourceCode',
        label: $t('re.resources.designer.model.grid.toolbar.generateGroovySourceCode'),
        icon: 'bi-code',
        enableIf: (arg) => {
          return !readOnly && arg.selected;
        },
        click: (arg) => {},
      },
      {
        name: 'generateGroovySourceCode4TestCase',
        label: $t('re.resources.designer.model.grid.toolbar.generateGroovySourceCode4TestCase'),
        icon: 'bi-code',
        enableIf: (arg) => {
          return !readOnly && arg.selected;
        },
        click: (arg) => {},
      },
      'separator',
      'view',
      'separator',
      'export',
    ]"
    :columns="[
      { width: '100%', name: 'name', label: $t('name'), sortable: false },
      {
        width: 80,
        name: 'category',
        label: $t('category'),
        sortable: false,
        format: Formater.enum(Enums.ModelCategory),
      },
      {
        width: 80,
        name: 'executeMode',
        label: $t('re.resources.designer.model.grid.entity.executeMode'),
        align: 'center',
        sortable: false,
        format: (value) => {
          if (value === 'TOP_DOWN') {
            return {
              componentType: 'QIcon',
              attrs: { name: 'bi-arrow-down', size: '20px' },
            };
          }
          return '';
        },
      },
    ]"
    :editor="{
      dialog: {
        width: '600px',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'code', label: $t('code'), type: 'text' },
          { name: 'name', label: $t('name'), type: 'text', required: true },
          {
            name: 'category',
            label: $t('category'),
            type: 'select',
            options: Options.enum(Enums.ModelCategory),
            defaultValue: 'OTHER',
          },
          {
            name: 'executeMode',
            label: $t('re.resources.designer.model.grid.entity.executeMode'),
            type: 'select',
            options: Options.enum(Enums.ExecuteMode),
            defaultValue: 'DOWN_TOP',
          },
          { name: 'description', label: $t('description'), type: 'text' },
          { name: 'enable', label: $t('isEnable'), type: 'checkbox', defaultValue: true },
        ],
      },
    }"
    :viewer="{
      panel: {
        columnNum: 1,
        fields: [
          { name: 'order', label: $t('order') },
          { name: 'id', label: $t('id') },
          { name: 'parent', label: $t('parent') },
          { name: 'resource', label: $t('resource') },
          { name: 'code', label: $t('code') },
          { name: 'name', label: $t('name') },
          { name: 'description', label: $t('description') },
          { name: 'enable', label: $t('enable') },
          { name: 'category', label: $t('category'), format: Formater.none() },
          { name: 'executeMode', label: $t('re.resources.designer.model.grid.entity.executeMode'), format: Formater.none() },

          { 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() },
        ],
      },
    }"
    v-bind="attrs"
  ></w-grid>
</template>
<script setup lang="ts">
import { ref, useAttrs } from 'vue';
import { axios, Environment, EnumTools, Formater, Options, DialogManager } from 'platform-core';

const attrs = useAttrs();

const props = defineProps({
  resource: { type: Object, default: undefined },
  readOnly: { type: Boolean, default: false },
});

const treeGridRef = ref();

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