<script setup lang="ts">
import { Subject } from 'rxjs';
import { FilterOutlined } from '@ant-design/icons-vue';
import { PickupTreeView, IActionParam, IParam, IContext } from '@core';
import { viewState } from './{{spinalCase page.codeName}}-state';
{{#each page.ctrls as |control|}}
{{#if (eq control.controlType 'TREEVIEW') }}
import { {{control.codeName}}Tree } from '@widgets/{{spinalCase psAppDataEntity.codeName}}/{{spinalCase codeName}}-tree';
{{/if}}
{{#if (and (eq controlType "SEARCHFORM") (eq name 'searchform'))}}
import { {{codeName}}SearchForm } from '@widgets/{{spinalCase psAppDataEntity.codeName}}/{{spinalCase codeName}}-search-form';
{{/if}}
{{#if (and (eq controlType "SEARCHFORM") (eq name 'quicksearchform'))}}
import { {{codeName}}QuickSearchForm } from '@widgets/{{spinalCase psAppDataEntity.codeName}}/{{spinalCase codeName}}-quick-search-form';
{{/if}}     
{{/each}}


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

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

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

const emit = defineEmits<ViewEmit>();

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

<template>
  <AppPickupTreeViewLayout :class="['app-pickup-tree-view', state.viewSysCss]">
    <template v-slot:header-left>
      <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" :context="state.context" :viewParams="state.viewParams" @onQuickGroupEvent="onQuickGroupEvent" />
    </template>
    {{/if}}    
    {{#page.ctrls}}
    {{#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 "TREEVIEW"}}
    <{{codeName}}Tree
      ref="tree"
      name="{{name}}"
      :multiple="multiple"
      :context="state.context"
      :showBusyIndicator="true"
      :viewParams="state.viewParams"
      :controlAction="state.{{name}}.action"
      :viewSubject="state.viewSubject"
      :parent="pickupTreeView"
      :selectedData="selectedData"
      @ctrlEvent="onCtrlEvent"
    ></{{codeName}}Tree>
    {{/eq}}
    {{/page.ctrls}}
  </AppPickupTreeViewLayout>
</template>
