<script setup lang="ts">
import { Subject } from 'rxjs';
import { FilterOutlined } from '@ant-design/icons-vue';
import { PickupGridView, IActionParam, IParam, IContext } from '@core';
import { viewState } from './{{spinalCase page.codeName}}-state';
{{#each page.ctrls as |control|}}
{{#if (eq control.controlType 'GRID') }}
import { {{control.codeName}}Grid } from '@widgets/{{spinalCase psAppDataEntity.codeName}}/{{spinalCase codeName}}-grid';
{{/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 pickupGridView = new PickupGridView(viewState(), props, emit).moduleInstall();
const { state, grid, onCtrlEvent, onQuickGroupEvent, onQuickSearchEvent } = pickupGridView;
</script>

<template>
  <AppPickupGridViewLayout :class="['app-pickup-grid-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 "GRID"}}
    <{{codeName}}Grid
      ref="grid"
      name="{{name}}"
      :context="state.context"
      :multiple="multiple"
      :rowEditState="state.rowEditState"
      :rowActiveMode="state.gridRowActiveMode"
      :showBusyIndicator="true"
      :viewParams="state.viewParams"
      :controlAction="state.{{name}}.action"
      :selectedData="selectedData"
      :viewSubject="state.viewSubject"
      :parent="pickupGridView"
      @ctrlEvent="onCtrlEvent"     
    ></{{codeName}}Grid>
    {{/eq}}
    {{/page.ctrls}}
  </AppPickupGridViewLayout>
</template>
