<template>
  <div class="page-box" style="position: relative;">
    <el-form
      ref="formTourProjectGroupPurchaseBannerInfoRef"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshFormTourProjectGroupPurchaseBannerInfo()" @reset="resetFormTourProjectGroupPurchaseBannerInfo">
        <el-form-item label="banner名称">
          <el-input
            class="filter-item"
            v-model="formFilter.bannerNameFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select
            class="filter-item"
            v-model="formFilter.enableFilter"
            placeholder=""
            :clearable="true"
            :filterable="true"
            @visible-change="enableFilterWidget.onVisibleChange"
          >
            <el-option
              v-for="item in enableFilterWidgetDropdownList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="banner类型">
          <el-select
            class="filter-item"
            v-model="formFilter.typeFilter"
            placeholder=""
            :clearable="true"
            :filterable="true"
            @visible-change="typeFilterWidget.onVisibleChange"
          >
            <el-option
              v-for="item in typeFilterWidgetDropdownList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="选择旅游项目">
          <SelectBookTourismProject
            class="filter-item"
            v-model="formFilter.projectIdFilter"
            placeholder=""
            :required="false"
            :disabled="false"
            :props="{
              label: 'projectTitle',
              value: 'id'
            }"
          />
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      ref="tourProjectGroupPurchaseBanner"
      class="page-table"
      :data="tourProjectGroupPurchaseBannerWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((tourProjectGroupPurchaseBannerWidgetCurrentPage - 1) * tourProjectGroupPurchaseBannerWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="true"
      @sort-change="tourProjectGroupPurchaseBannerWidget.onSortChange"
      @refresh="tourProjectGroupPurchaseBannerWidget.refreshTable()"
    >
      <template #operator>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          :disabled="!checkPermCodeExist('formTourProjectGroupPurchaseBannerInfo:formTourProjectGroupPurchaseBannerInfo:add')"
          @click="onAddClick()"
          >
          新增
        </el-button>
      </template>
      <vxe-column title="序号" type="seq" :index="tourProjectGroupPurchaseBannerWidget.getTableIndex" :width="80" />
      <vxe-column title="banner名称" field="bannerName" />
      <vxe-column title="旅游项目标题" field="tourismProject.projectTitle" />
      <vxe-column title="是否启用" field="enableDictMap.name" />
      <vxe-column title="图片URL">
        <template v-slot="scope">
          <upload-file-list
            :file-list="
              parseUploadData(scope.row.imgUrl, {
                id: scope.row.id,
                fieldName: 'imgUrl',
                asImage: true
              })
            "
            type="card"
            direction="horizontal"
            :readonly="true"
          />
        </template>
      </vxe-column>
      <vxe-column title="banner排序" field="showOrder" />
      <vxe-column title="banner类型" field="typeDictMap.name" />
      <vxe-column title="操作" fixed="right">
        <template v-slot="scope">
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onEditClick(scope.row)"
            :disabled="!checkPermCodeExist('formTourProjectGroupPurchaseBannerInfo:formTourProjectGroupPurchaseBannerInfo:edit')"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onDeleteClick(scope.row)"
            :disabled="!checkPermCodeExist('formTourProjectGroupPurchaseBannerInfo:formTourProjectGroupPurchaseBannerInfo:delete')"
          >
            删除
          </el-button>
        </template>
      </vxe-column>
      <template slot="empty">
        <div class="table-empty unified-font">
          <img src="@/assets/img/empty.png">
          <span>暂无数据</span>
        </div>
      </template>
      <!-- 分页 -->
      <template #pagination>
        <el-row type="flex" justify="end" style="margin-top: 10px;">
          <el-pagination
            :total="tourProjectGroupPurchaseBannerWidgetTotalCount"
            :current-page="tourProjectGroupPurchaseBannerWidgetCurrentPage"
            :page-size="tourProjectGroupPurchaseBannerWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="tourProjectGroupPurchaseBannerWidget.onCurrentPageChange"
            @size-change="tourProjectGroupPurchaseBannerWidget.onPageSizeChange">
          </el-pagination>
        </el-row>
      </template>
    </table-box>
    <label v-if="subPage" class="page-close-box" @click="onCancel()">
      <img src="@/assets/img/back2.png" alt="">
    </label>
  </div>
</template>

<script lang="ts">
export default {
  name: 'formTourProjectGroupPurchaseBannerInfo',
};
</script>

<script setup lang="ts">
import { VxeColumn, VxeTable } from 'vxe-table';
import { ANY_OBJECT } from '@/types/generic';
import { DictData, DictionaryBase } from '@/common/staticDict/types';
import { ElMessage, ElMessageBox, UploadFile } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import { useCommon } from '@/common/hooks/useCommon';
import { useLayoutStore, useStaticDictStore } from '@/store';
import { useDownload } from '@/common/hooks/useDownload';
import { useDropdown } from '@/common/hooks/useDropdown';
import { DropdownOptions, ListData } from '@/common/types/list';
import { useTable } from '@/common/hooks/useTable';
import { TableOptions } from '@/common/types/pagination';
import { useUpload } from '@/common/hooks/useUpload';
import { useUploadWidget } from '@/common/hooks/useUploadWidget';
import { DictionaryController } from '@/api/system';
import { treeDataTranslate, findItemFromList, findTreeNodePath, findTreeNode, stringCase } from '@/common/utils';
import { TourismProjectData } from '@/api/generated/tourismProjectController';
import { TourProjectGroupPurchaseBannerData } from '@/api/generated/tourProjectGroupPurchaseBannerController';
import { TourismProjectController, TourProjectGroupPurchaseBannerController } from '@/api/generated';
import EditTourProjectGroupPurchaseBanner from '@/pages/tourProjectGroupPurchaseBanner/editTourProjectGroupPurchaseBanner.vue';
import SelectBookTourismProject from '@/pages/tourTourismBookInfo/selectBookTourismProject/index.vue';

const router = useRouter();
const route = useRoute();
const layoutStore = useLayoutStore();
const { downloadFile } = useDownload();
const { getUploadHeaders, getUploadActionUrl, fileListToJson, parseUploadData, getPictureList } = useUpload();
const { 
  Delete,
  Search,
  Edit,
  Plus,
  Refresh,
  Picture,
  Dialog,
  mainContextHeight,
  clientHeight,
  checkPermCodeExist,
  parseParams,
  parseArrayParams,
  formatDateByStatsType,
  getDateRangeFilter,
} = useCommon();
// 静态字典
const { staticDict: StaticDict } = useStaticDictStore();

const props = withDefaults(
  defineProps<{
    subPage?: number | string | boolean;
  }>(),
  {
    subPage: 0,
  },
);

const formFilter = reactive({
  // banner名称
  bannerNameFilter: undefined,
  // 是否启用
  enableFilter: undefined,
  // banner类型
  typeFilter: undefined,
  // 旅游项目id
  projectIdFilter: undefined,
});
const formFilterCopy = reactive({
  // banner名称
  bannerNameFilter: undefined,
  // 是否启用
  enableFilter: undefined,
  // banner类型
  typeFilter: undefined,
  // 旅游项目id
  projectIdFilter: undefined,
});

const onCancel = () => {
  router.go(-1);
  layoutStore.removeCachePage(route.fullPath as string);
  route.meta.refreshParentCachedPage = true;
};

const onResume = () => {
  refreshFormTourProjectGroupPurchaseBannerInfo();
};

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadTourProjectGroupPurchaseBannerWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    tourProjectGroupPurchaseBannerDtoFilter: {
      bannerName: formFilter.bannerNameFilter,
      enable: formFilter.enableFilter,
      type: formFilter.typeFilter,
      projectId: formFilter.projectIdFilter,
    }
  };
  return new Promise((resolve, reject) => {
    TourProjectGroupPurchaseBannerController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadTourProjectGroupPurchaseBannerVerify = () => {
  formFilterCopy.bannerNameFilter = formFilter.bannerNameFilter;
  formFilterCopy.enableFilter = formFilter.enableFilter;
  formFilterCopy.typeFilter = formFilter.typeFilter;
  formFilterCopy.projectIdFilter = formFilter.projectIdFilter;
  return true;
};
/**
 * 新增
 */
const onAddClick = (row?: TourProjectGroupPurchaseBannerData) => {
  let params: ANY_OBJECT = {
  };

  Dialog
    .show('新增', EditTourProjectGroupPurchaseBanner, { area: '900px' }, { ...params, subPage: true })
    .then(res => {
      tourProjectGroupPurchaseBannerWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
/**
 * 编辑
 */
const onEditClick = (row?: TourProjectGroupPurchaseBannerData) => {
  let params: ANY_OBJECT = {
    id: row?.id,
  };

  Dialog
    .show('编辑', EditTourProjectGroupPurchaseBanner, { area: '900px' }, { ...params, subPage: true })
    .then(res => {
      tourProjectGroupPurchaseBannerWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
/**
 * 删除
 */
const onDeleteClick = (row?: TourProjectGroupPurchaseBannerData) => {
  let params: ANY_OBJECT = {
    id: row?.id,
  };

  ElMessageBox.confirm('是否删除该条数据?').then(res => {
    TourProjectGroupPurchaseBannerController.delete(params).then(res => {
      ElMessage.success('删除成功');
      tourProjectGroupPurchaseBannerWidget.refreshTable(false, 1);
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
// 表格组件表格组件参数
const tourProjectGroupPurchaseBannerOptions: TableOptions<TourProjectGroupPurchaseBannerData> = {
  loadTableData: loadTourProjectGroupPurchaseBannerWidgetData,
  verifyTableParameter: loadTourProjectGroupPurchaseBannerVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const tourProjectGroupPurchaseBanner = ref();
const tourProjectGroupPurchaseBannerWidget = useTable(tourProjectGroupPurchaseBannerOptions);
const {
  dataList: tourProjectGroupPurchaseBannerWidgetDataList,
  currentPage: tourProjectGroupPurchaseBannerWidgetCurrentPage,
  pageSize: tourProjectGroupPurchaseBannerWidgetPageSize,
  totalCount: tourProjectGroupPurchaseBannerWidgetTotalCount,
} = tourProjectGroupPurchaseBannerWidget;
/**
 * 是否启用下拉数据获取函数
 */
const loadEnableFilterDropdownList = (): Promise<ListData<DictData>> => {
  return Promise.resolve({
    dataList: StaticDict.Enable.getList(),
  });
};
// 是否启用配置参数
const enableFilterOptions: DropdownOptions<DictData> = {
  loadData: loadEnableFilterDropdownList,
  isTree: false,
};
// 是否启用下拉组件
const enableFilterWidget = useDropdown(enableFilterOptions);
const { dropdownList: enableFilterWidgetDropdownList } = enableFilterWidget
/**
 * banner类型下拉数据获取函数
 */
const loadTypeFilterDropdownList = (): Promise<ListData<DictData>> => {
  return Promise.resolve({
    dataList: StaticDict.BannerType.getList(),
  });
};
// banner类型配置参数
const typeFilterOptions: DropdownOptions<DictData> = {
  loadData: loadTypeFilterDropdownList,
  isTree: false,
};
// banner类型下拉组件
const typeFilterWidget = useDropdown(typeFilterOptions);
const { dropdownList: typeFilterWidgetDropdownList } = typeFilterWidget
const refreshFormTourProjectGroupPurchaseBannerInfo = () => {
  // 刷新段落
  tourProjectGroupPurchaseBannerWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormTourProjectGroupPurchaseBannerInfo = () => {
  formFilter.bannerNameFilter = undefined;
  formFilterCopy.bannerNameFilter = undefined;
  formFilter.enableFilter = undefined;
  formFilterCopy.enableFilter = undefined;
  formFilter.typeFilter = undefined;
  formFilterCopy.typeFilter = undefined;
  formFilter.projectIdFilter = undefined;
  formFilterCopy.projectIdFilter = undefined;
  refreshFormTourProjectGroupPurchaseBannerInfo();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormTourProjectGroupPurchaseBannerInfo();
};
const formInit = () => {
  enableFilterWidget.onVisibleChange(true).catch(e => {console.error(e)});
  typeFilterWidget.onVisibleChange(true).catch(e => {console.error(e)});
  refreshFormTourProjectGroupPurchaseBannerInfo();
};

onMounted(() => {
  formInit();
});

onActivated(() => {
  onResume();
});
</script>