<script setup lang="ts">
import { Subject } from 'rxjs';
import { TreeView, IActionParam, IParam, IContext } from '@core';
import { viewState } from './{{spinalCase page.codeName}}-state';
{{#page.ctrls}}
{{#eq controlType "TREEVIEW"}}
import { {{codeName}}Tree } from '@widgets/{{spinalCase appEntity.codeName}}/{{spinalCase codeName}}-tree';
{{/eq}}
{{#if (and (eq controlType "SEARCHFORM") (eq name 'searchform'))}}
import { {{codeName}}SearchForm } from '@widgets/{{spinalCase appEntity.codeName}}/{{spinalCase codeName}}-search-form';
{{/if}}
{{#if (and (eq controlType "SEARCHFORM") (eq name 'quicksearchform'))}}
import { {{codeName}}QuickSearchForm } from '@widgets/{{spinalCase appEntity.codeName}}/{{spinalCase codeName}}-quick-search-form';
{{/if}}
{{#eq controlType "SEARCHBAR"}}
import { {{codeName}}SearchBar } from '@widgets/{{spinalCase appEntity.codeName}}/{{spinalCase codeName}}-searchBar';
{{/eq}}
{{/page.ctrls}}

// props声明和默认值处理
interface Props {
  context?: IContext;
  viewParams?: IParam;
  openType?: "ROUTE" | "MODAL" | "EMBED";
  viewSubject?: Subject<IActionParam>;
}

const props = withDefaults(defineProps<Props>(), {
  openType:'ROUTE',
  viewSubject: () => new Subject<IActionParam>()
})

// emit声明
interface ViewEmit {
  (name: "onViewEvent", value: IActionParam): void;
}

const emit = defineEmits<ViewEmit>();

// 安装功能模块，提供状态和能力方法
const { state, tree, onCtrlEvent, onToolbarEvent, onQuickGroupEvent, onQuickSearchEvent } = new TreeView(viewState, props, emit).moduleInstall();

</script>

<template>
  <AppTreeViewLayout :class="['app-tree-view', state.viewSysCss]">
    <template v-slot:caption>
      <AppIconText class="app-view__caption" size="large" :text="state.viewCaption" />
    </template>
    {{#if page.enableQuickGroup}}
    <template v-slot:quickGroupSearch>
      <AppQuickGroup v-if="state.enableQuickGroup" :quickGroupModel="state.quickGroupPSCodeList" @onQuickGroupEvent="onQuickGroupEvent" />
    </template>
    {{/if}}
    {{#page.ctrls}}
    {{#eq controlType "TOOLBAR"}}
    <template v-slot:toolbar>
      <AppToolbar
        mode="button"
        name="{{lowerCase codeName}}"
        :actionModel="state.toolbar"
        @onToolbarEvent="onToolbarEvent"/>
    </template>
    {{/eq}}
    {{#if (and (eq controlType "SEARCHFORM") (eq name 'searchform'))}}
    {{#if page.enableFilter}}
    <template v-slot:quickSearch>
      <div class='app-quick-search'>
      <a-input v-if="state.enableQuickSearch" @pressEnter="onQuickSearchEvent($event)" allowClear/>
      <a-popover trigger="click" :overlayStyle="{width: '50%'}">
        <template #content>
          <{{codeName}}SearchForm
            v-if="state.expandSearchForm"
            name="{{name}}"
            :context="state.context"
            :viewParams="state.viewParams"
            :controlAction="state.{{camelCase name}}.action"
            :viewSubject="state.viewSubject"
            @ctrlEvent="onCtrlEvent"
          ></{{codeName}}SearchForm>
        </template>
        <a-button><filter-outlined /></a-button>
      </a-popover>
      </div>
    </template>
    {{else}}
    <template v-slot:searchForm>
      <{{codeName}}SearchForm
      v-if="state.expandSearchForm"
      name="{{name}}"
      :context="state.context"
      :viewParams="state.viewParams"
      :controlAction="state.{{camelCase name}}.action"
      :viewSubject="state.viewSubject"
      @ctrlEvent="onCtrlEvent"
    ></{{codeName}}SearchForm>
    </template>
    {{/if}}
    {{/if}}
    {{#if (and (eq controlType "SEARCHFORM") (eq name 'quicksearchform'))}}
    <template v-slot:quickSearchForm>
    <{{codeName}}QuickSearchForm
      name="{{name}}"
      :context="state.context"
      :viewParams="state.viewParams"
      :controlAction="state.{{camelCase name}}.action"
      :viewSubject="state.viewSubject"
      @ctrlEvent="onCtrlEvent"
    ></{{codeName}}QuickSearchForm>
    </template>
    {{/if}}
    {{#eq controlType "SEARCHBAR"}}
    <template v-slot:searchBar>
      <{{codeName}}SearchBar
      name="{{name}}"
      :controlAction="state.{{camelCase name}}.action"
      :viewSubject="state.viewSubject"/>
    </template>
    {{/eq}}    
    {{#eq controlType "TREEVIEW"}}
    <{{codeName}}Tree
      ref="tree"
      name="{{name}}"
      :context="state.context"
      :showBusyIndicator="true"
      :viewParams="state.viewParams"
      :controlAction="state.{{name}}.action"
      :viewSubject="state.viewSubject"
      @ctrlEvent="onCtrlEvent"
    ></{{codeName}}Tree>
    {{/eq}}
    {{/page.ctrls}}
  </AppTreeViewLayout>
</template>