<template>
  <div class="page-box" style="position: relative;">
    <el-form
      ref="formTourProjectGroupPurchaseRebateRef"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshFormTourProjectGroupPurchaseRebate()" @reset="resetFormTourProjectGroupPurchaseRebate">
        <el-form-item label="返利变化的原因">
          <el-select
            class="filter-item"
            v-model="formFilter.reasonFilter"
            placeholder=""
            :clearable="true"
            :filterable="true"
            @visible-change="reasonFilterWidget.onVisibleChange"
          >
            <el-option
              v-for="item in reasonFilterWidgetDropdownList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="返利的金额">
          <input-number-range
            class="filter-item"
            v-model="formFilter.priceFilter"
            startPlaceholder=""
            endPlaceholder=""
            :step="1"
          />
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      ref="tourProjectGroupPurchaseRebate"
      class="page-table"
      :data="tourProjectGroupPurchaseRebateWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((tourProjectGroupPurchaseRebateWidgetCurrentPage - 1) * tourProjectGroupPurchaseRebateWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="false"
      @sort-change="tourProjectGroupPurchaseRebateWidget.onSortChange"
      @refresh="tourProjectGroupPurchaseRebateWidget.refreshTable()"
    >
      <vxe-column title="序号" type="seq" :index="tourProjectGroupPurchaseRebateWidget.getTableIndex" :width="80" />
      <vxe-column title="用户昵称" field="tourUser.showName" />
      <vxe-column title="拼团的标题" field="tourProjectGroupPurchase.title" />
      <vxe-column title="拼团的状态" field="tourProjectGroupPurchase.stateDictMap.name" />
      <vxe-column title="拼团是否成功" field="tourProjectGroupPurchase.successDictMap.name" />
      <vxe-column title="返利的金额(+为增加;-为减少)" field="price" />
      <vxe-column title="返利金额的单位" field="priceUnit" />
      <vxe-column title="返利变化的原因" field="reasonDictMap.name" />
      <!-- <vxe-column title="返利变化的关联用户" field="reasonRelationUserid" /> -->
      <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="tourProjectGroupPurchaseRebateWidgetTotalCount"
            :current-page="tourProjectGroupPurchaseRebateWidgetCurrentPage"
            :page-size="tourProjectGroupPurchaseRebateWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="tourProjectGroupPurchaseRebateWidget.onCurrentPageChange"
            @size-change="tourProjectGroupPurchaseRebateWidget.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: 'formTourProjectGroupPurchaseRebate',
};
</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 { TourProjectGroupPurchaseData } from '@/api/generated/tourProjectGroupPurchaseController';
import { TourOrderData } from '@/api/generated/tourOrderController';
import { TourProjectGroupPurchaseRebateData } from '@/api/generated/tourProjectGroupPurchaseRebateController';
import { TourUserData } from '@/api/generated/tourUserController';
import { TourProjectGroupPurchaseController, TourOrderController, TourProjectGroupPurchaseRebateController, TourUserController } from '@/api/generated';

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({
  // 返利变化的原因
  reasonFilter: undefined,
  // 返利的金额
  priceFilter: [],
});
const formFilterCopy = reactive({
  // 返利变化的原因
  reasonFilter: undefined,
  // 返利的金额
  priceFilter: [],
});

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

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

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadTourProjectGroupPurchaseRebateWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    tourProjectGroupPurchaseRebateDtoFilter: {
      priceStart: Array.isArray(formFilter.priceFilter) ? formFilter.priceFilter[0] : formFilter.priceFilter,
      priceEnd: Array.isArray(formFilter.priceFilter) ? formFilter.priceFilter[1] : formFilter.priceFilter,
      reason: formFilter.reasonFilter,
    }
  };
  return new Promise((resolve, reject) => {
    TourProjectGroupPurchaseRebateController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadTourProjectGroupPurchaseRebateVerify = () => {
  formFilterCopy.reasonFilter = formFilter.reasonFilter;
  formFilterCopy.priceFilter = formFilter.priceFilter;
  return true;
};
// 表格组件表格组件参数
const tourProjectGroupPurchaseRebateOptions: TableOptions<TourProjectGroupPurchaseRebateData> = {
  loadTableData: loadTourProjectGroupPurchaseRebateWidgetData,
  verifyTableParameter: loadTourProjectGroupPurchaseRebateVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const tourProjectGroupPurchaseRebate = ref();
const tourProjectGroupPurchaseRebateWidget = useTable(tourProjectGroupPurchaseRebateOptions);
const {
  dataList: tourProjectGroupPurchaseRebateWidgetDataList,
  currentPage: tourProjectGroupPurchaseRebateWidgetCurrentPage,
  pageSize: tourProjectGroupPurchaseRebateWidgetPageSize,
  totalCount: tourProjectGroupPurchaseRebateWidgetTotalCount,
} = tourProjectGroupPurchaseRebateWidget;
/**
 * 返利变化的原因下拉数据获取函数
 */
const loadReasonFilterDropdownList = (): Promise<ListData<DictData>> => {
  return Promise.resolve({
    dataList: StaticDict.GroupPurchaseRebate.getList(),
  });
};
// 返利变化的原因配置参数
const reasonFilterOptions: DropdownOptions<DictData> = {
  loadData: loadReasonFilterDropdownList,
  isTree: false,
};
// 返利变化的原因下拉组件
const reasonFilterWidget = useDropdown(reasonFilterOptions);
const { dropdownList: reasonFilterWidgetDropdownList } = reasonFilterWidget
const refreshFormTourProjectGroupPurchaseRebate = () => {
  // 刷新段落
  tourProjectGroupPurchaseRebateWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormTourProjectGroupPurchaseRebate = () => {
  formFilter.reasonFilter = undefined;
  formFilterCopy.reasonFilter = undefined;
  formFilter.priceFilter = undefined;
  formFilterCopy.priceFilter = undefined;
  refreshFormTourProjectGroupPurchaseRebate();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormTourProjectGroupPurchaseRebate();
};
const formInit = () => {
  reasonFilterWidget.onVisibleChange(true).catch(e => {console.error(e)});
  refreshFormTourProjectGroupPurchaseRebate();
};

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

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