<template>
  <div class="layout-container" v-if="render">
    #if($vue.enableCardView)
    <!-- 列表-表格 -->
    <${vue.feature}-table v-if="renderTable"
                  ref="table"
                  @open-add="() =>#if($vue.enableDrawerForm) drawer#else modal#end.openAdd()"
                  @open-update="(e) =>#if($vue.enableDrawerForm) drawer#else modal#end.openUpdate(e)" />
    <!-- 列表-卡片 -->
    <${vue.feature}-card-list v-else
                  ref="card"
                  @open-add="() =>#if($vue.enableDrawerForm) drawer#else modal#end.openAdd()"
                  @open-update="(e) =>#if($vue.enableDrawerForm) drawer#else modal#end.openUpdate(e)" />
    #else
    <!-- 列表-表格 -->
    <${vue.feature}-table ref="table"
                  @open-add="() =>#if($vue.enableDrawerForm) drawer#else modal#end.openAdd()"
                  @open-update="(e) =>#if($vue.enableDrawerForm) drawer#else modal#end.openUpdate(e)" />
    #end
    #if($vue.enableDrawerForm)
    <!-- 添加修改模态框 -->
    <${vue.feature}-form-drawer ref="drawer"
                   @added="modalAddCallback"
                   @updated="modalUpdateCallback" />
    #else
    <!-- 添加修改模态框 -->
    <${vue.feature}-form-modal ref="modal"
                   @added="modalAddCallback"
                   @updated="modalUpdateCallback" />
    #end
  </div>
</template>

<script lang="ts">
  export default {
    name: '${vue.featureEntityFirstLower}'
  };
</script>

<script lang="ts" setup>
  #if($vue.enableCardView)
  import { computed, ref, onBeforeMount } from 'vue';
  #if($dictMap.entrySet().size() > 0)
  import { useAppStore, useDictStore } from '@/store';
  import { dictKeys } from './types/const';
  #else
  import { useAppStore } from '@/store';
  #end
  #else
  import { ref, onBeforeMount } from 'vue';
  #if($dictMap.entrySet().size() > 0)
  import { useDictStore } from '@/store';
  import { dictKeys } from './types/const';
  #end
  #end
  #if($vue.enableCardView)
  import ${vue.featureEntity}CardList from './components/${vue.feature}-card-list.vue';
  #end
  import ${vue.featureEntity}Table from './components/${vue.feature}-table.vue';
  #if($vue.enableDrawerForm)
  import ${vue.featureEntity}FormDrawer from './components/${vue.feature}-form-drawer.vue';
  #else
  import ${vue.featureEntity}FormModal from './components/${vue.feature}-form-modal.vue';
  #end
  #if($vue.enableCardView)

  const appStore = useAppStore();

  // FIXME 这里需要修改一下字段名称 并且在 appStore 定义该字段
  const renderTable = computed(() => appStore.${vue.featureEntityFirstLower}View === 'table');
  #end

  const render = ref(false);
  const table = ref();
  #if($vue.enableCardView)
  const card = ref();
  #end
  #if($vue.enableDrawerForm)
  const drawer = ref();
  #else
  const modal = ref();
  #end

  // 添加回调
  const modalAddCallback = () => {
    #if($vue.enableCardView)
    if (renderTable.value) {
      table.value.addedCallback();
    } else {
      card.value.addedCallback();
    }
    #else
    table.value.addedCallback();
    #end
  };

  // 修改回调
  const modalUpdateCallback = () => {
    #if($vue.enableCardView)
    if (renderTable.value) {
      table.value.updatedCallback();
    } else {
      card.value.updatedCallback();
    }
    #else
    table.value.updatedCallback();
    #end
  };

  onBeforeMount(async () => {
    #if($dictMap.entrySet().size() > 0)
    const dictStore = useDictStore();
    await dictStore.loadKeys(dictKeys);
    #end
    render.value = true;
  });

</script>

<style lang="less" scoped>

</style>
