<template>
  <div class="page-box" style="position: relative;">
    <el-form
      ref="formTourBusinessBannerRef"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshFormTourBusinessBanner()" @reset="resetFormTourBusinessBanner">
        <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>
      </filter-box>
    </el-form>
    <table-box
      ref="formTourBusinessBannerTable"
      class="page-table"
      :data="formTourBusinessBannerTableWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((formTourBusinessBannerTableWidgetCurrentPage - 1) * formTourBusinessBannerTableWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="true"
      @sort-change="formTourBusinessBannerTableWidget.onSortChange"
      @refresh="formTourBusinessBannerTableWidget.refreshTable()"
    >
      <template #operator>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          :disabled="!checkPermCodeExist('formTourBusinessBanner:formTourBusinessBanner:addTourBusinessBanner')"
          @click="onAddTourBusinessBannerClick()"
          >
          新建
        </el-button>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          :disabled="!checkPermCodeExist('formTourBusinessBanner:formTourBusinessBanner:exportTourBusinessBanner')"
          @click="onExportTourBusinessBannerClick()"
          >
          导出
        </el-button>
        <el-upload
          class="btn-import"
          :auto-upload="false"
          action=""
          :show-file-list="false"
          accept=".xls,.xlsx"
          style="display: inline-block;"
          :disabled="!checkPermCodeExist('formTourBusinessBanner:formTourBusinessBanner:importTourBusinessBanner')"
          :on-change="onImportTourBusinessBannerClick"
        >
          <template #trigger>
            <el-button
              type="primary"
              :size="layoutStore.defaultFormItemSize"
              :disabled="!checkPermCodeExist('formTourBusinessBanner:formTourBusinessBanner:importTourBusinessBanner')"
            >
              导入
            </el-button>
          </template>
        </el-upload>
      </template>
      <vxe-column title="序号" type="seq" :index="formTourBusinessBannerTableWidget.getTableIndex" :width="80" />
      <vxe-column title="banner名称" field="bannerName" />
      <vxe-column title="业务名称" field="businessTypeDictMap.name" />
      <vxe-column title="是否启用" field="enableDictMap.name" />
      <vxe-column title="是否主banner" field="mainBannerDictMap.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="跳转链接" field="bannerUrl" />
      <vxe-column title="banner排序" field="showOrder" />
      <vxe-column title="操作" fixed="right">
        <template v-slot="scope">
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onEditTourBusinessBannerClick(scope.row)"
            :disabled="!checkPermCodeExist('formTourBusinessBanner:formTourBusinessBanner:editTourBusinessBanner')"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onDeleteTourBusinessBannerClick(scope.row)"
            :disabled="!checkPermCodeExist('formTourBusinessBanner:formTourBusinessBanner:deleteTourBusinessBanner')"
          >
            删除
          </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="formTourBusinessBannerTableWidgetTotalCount"
            :current-page="formTourBusinessBannerTableWidgetCurrentPage"
            :page-size="formTourBusinessBannerTableWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="formTourBusinessBannerTableWidget.onCurrentPageChange"
            @size-change="formTourBusinessBannerTableWidget.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: 'formTourBusinessBanner',
};
</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 { TourBusinessBannerData } from '@/api/generated/tourBusinessBannerController';
import { TourBusinessBannerController } from '@/api/generated';
import FormEditTourBusinessBanner from '@/pages/TourBusinessBanner/formEditTourBusinessBanner.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,
});
const formFilterCopy = reactive({
  // banner名称
  bannerNameFilter: undefined,
});

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

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

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadFormTourBusinessBannerTableWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    tourBusinessBannerDtoFilter: {
      bannerName: formFilter.bannerNameFilter,
    }
  };
  return new Promise((resolve, reject) => {
    TourBusinessBannerController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadFormTourBusinessBannerTableVerify = () => {
  formFilterCopy.bannerNameFilter = formFilter.bannerNameFilter;
  return true;
};
/**
 * 新建
 */
const onAddTourBusinessBannerClick = (row?: TourBusinessBannerData) => {
  let params: ANY_OBJECT = {
  };

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

  Dialog
    .show('编辑', FormEditTourBusinessBanner, { area: '900px' }, { ...params, subPage: true })
    .then(res => {
      formTourBusinessBannerTableWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
/**
 * 导出
 */
const onExportTourBusinessBannerClick = (row?: TourBusinessBannerData) => {
  let params: ANY_OBJECT = {
  };

  TourBusinessBannerController.export(params, '表格组件.xlsx').then(res => {
    ElMessage.success('导出成功');
  }).catch(e => {
    ElMessage.error(e.errorMessage);
  });
};
/**
 * 导入
 */
const onImportTourBusinessBannerClick = (file) => {
  let params: ANY_OBJECT = {
    importFile: file.raw,
    // 是否忽略表头
    skipHeader: false
  };

  TourBusinessBannerController.import(params).then(res => {
    ElMessage.success('导入成功');
    formTourBusinessBannerTableWidget.refreshTable();
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
/**
 * 删除
 */
const onDeleteTourBusinessBannerClick = (row?: TourBusinessBannerData) => {
  let params: ANY_OBJECT = {
    id: row?.id,
  };

  ElMessageBox.confirm('是否删除此记录？').then(res => {
    TourBusinessBannerController.delete(params).then(res => {
      ElMessage.success('删除成功');
      formTourBusinessBannerTableWidget.refreshTable(false, 1);
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
// 表格组件表格组件参数
const formTourBusinessBannerTableOptions: TableOptions<TourBusinessBannerData> = {
  loadTableData: loadFormTourBusinessBannerTableWidgetData,
  verifyTableParameter: loadFormTourBusinessBannerTableVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const formTourBusinessBannerTable = ref();
const formTourBusinessBannerTableWidget = useTable(formTourBusinessBannerTableOptions);
const {
  dataList: formTourBusinessBannerTableWidgetDataList,
  currentPage: formTourBusinessBannerTableWidgetCurrentPage,
  pageSize: formTourBusinessBannerTableWidgetPageSize,
  totalCount: formTourBusinessBannerTableWidgetTotalCount,
} = formTourBusinessBannerTableWidget;
const refreshFormTourBusinessBanner = () => {
  // 刷新段落
  formTourBusinessBannerTableWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormTourBusinessBanner = () => {
  formFilter.bannerNameFilter = undefined;
  formFilterCopy.bannerNameFilter = undefined;
  refreshFormTourBusinessBanner();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormTourBusinessBanner();
};
const formInit = () => {
  refreshFormTourBusinessBanner();
};

onMounted(() => {
  formInit();
});
</script>