<template>
  <w-grid
    :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'),
      },
      'separator',
      {
        extend: 'clone',
        enableIf: (arg) => {
          return arg.selected && arg.selected.parent;
        },
      },
      {
        name: 'deepClone',
        label: $t('deepClone'),
        icon: 'bi-copy',
        enableIf: (arg) => {
          return arg.selected && arg.selected.parent;
        },
        click: (arg) => {},
      },
      'edit',
      'remove',
      'separator',
      {
        name: 'generateGroovySourceCode',
        label: $t('re.resources.designer.model.grid.toolbar.generateGroovySourceCode'),
        icon: 'bi-briefcase-fill',
        enableIf: (arg) => {
          return arg.selected;
        },
        click: (arg) => {},
      },
      {
        name: 'generateGroovySourceCode4TestCase',
        label: $t('re.resources.designer.model.grid.toolbar.generateGroovySourceCode4TestCase'),
        icon: 'bi-briefcase-fill',
        enableIf: (arg) => {
          return 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',
        height: '400px',
      },
      form: {
        colsNum: 1,
        fields: [
          { name: 'code', label: $t('code'), type: 'text' },
          { name: 'name', label: $t('name'), type: 'text', required: true },
          {
            name: 'category',
            label: $t('re.resources.designer.model.grid.entity.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: [],
      },
    }"
    @row-click="
      (evt, row, index) => {
        emit('rowClick', evt, row, index);
      }
    "
    @before-request-data="
      () => {
        emit('beforeRequestData');
      }
    "
  ></w-grid>
</template>
<script setup lang="ts">
import { Environment, EnumTools, Formater, Options } from 'platform-core';

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

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

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