<template>
  <div class="page-box" style="position: relative;">
    <el-form
      ref="formTourProjectGroupPurchaseApplyRef"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshFormTourProjectGroupPurchaseApply()" @reset="resetFormTourProjectGroupPurchaseApply">
        <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>
        <el-form-item label="是否处理">
          <el-select
            class="filter-item"
            v-model="formFilter.isHandleFilter"
            placeholder=""
            :clearable="true"
            :filterable="true"
            @visible-change="isHandleFilterWidget.onVisibleChange"
          >
            <el-option
              v-for="item in isHandleFilterWidgetDropdownList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      ref="tourProjectGroupPurchaseApply"
      class="page-table"
      :data="tourProjectGroupPurchaseApplyWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((tourProjectGroupPurchaseApplyWidgetCurrentPage - 1) * tourProjectGroupPurchaseApplyWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="false"
      @sort-change="tourProjectGroupPurchaseApplyWidget.onSortChange"
      @refresh="tourProjectGroupPurchaseApplyWidget.refreshTable()"
    >
      <vxe-column title="序号" type="seq" :index="tourProjectGroupPurchaseApplyWidget.getTableIndex" :width="80" />
      <vxe-column title="项目标题" field="tourismProject.projectTitle" />
      <vxe-column title="项目状态" field="tourismProject.enableDictMap.name" />
      <vxe-column title="用户昵称" field="tourUser.showName" />
      <vxe-column title="用户联系方式" field="tourUser.mobile" />
      <vxe-column title="申请时间" field="applyTime">
        <template v-slot="scope">
          <span>{{formatDateByStatsType(scope.row.applyTime, 'day')}}</span>
        </template>
      </vxe-column>
      <vxe-column title="是否处理" field="isHandleDictMap.name" />
      <vxe-column title="备注" field="remark" />
      <vxe-column title="操作" fixed="right">
        <template v-slot="scope">
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onEditClick(scope.row)"
            :disabled="!checkPermCodeExist('formTourProjectGroupPurchaseApply:formTourProjectGroupPurchaseApply:edit')"
          >
            编辑状态
          </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="tourProjectGroupPurchaseApplyWidgetTotalCount"
            :current-page="tourProjectGroupPurchaseApplyWidgetCurrentPage"
            :page-size="tourProjectGroupPurchaseApplyWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="tourProjectGroupPurchaseApplyWidget.onCurrentPageChange"
            @size-change="tourProjectGroupPurchaseApplyWidget.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: 'formTourProjectGroupPurchaseApply',
};
</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 { TourProjectGroupPurchaseApplyData } from '@/api/generated/tourProjectGroupPurchaseApplyController';
import { TourUserData } from '@/api/generated/tourUserController';
import { TourismProjectController, TourProjectGroupPurchaseApplyController, TourUserController } from '@/api/generated';
import EditTourProjectGroupPurchaseState from '@/pages/tourProjectGroupPurchaseApply/editTourProjectGroupPurchaseState.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({
  // 选择旅游项目
  projectIdFilter: undefined,
  // 是否处理
  isHandleFilter: undefined,
});
const formFilterCopy = reactive({
  // 选择旅游项目
  projectIdFilter: undefined,
  // 是否处理
  isHandleFilter: undefined,
});

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

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

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadTourProjectGroupPurchaseApplyWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    tourProjectGroupPurchaseApplyDtoFilter: {
      projectId: formFilter.projectIdFilter,
      isHandle: formFilter.isHandleFilter,
    }
  };
  return new Promise((resolve, reject) => {
    TourProjectGroupPurchaseApplyController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadTourProjectGroupPurchaseApplyVerify = () => {
  formFilterCopy.projectIdFilter = formFilter.projectIdFilter;
  formFilterCopy.isHandleFilter = formFilter.isHandleFilter;
  return true;
};
/**
 * 编辑状态
 */
const onEditClick = (row?: TourProjectGroupPurchaseApplyData) => {
  let params: ANY_OBJECT = {
    id: row?.id,
  };

  Dialog
    .show('编辑状态', EditTourProjectGroupPurchaseState, { area: ['900px', '700px'] }, { ...params, subPage: true })
    .then(res => {
      tourProjectGroupPurchaseApplyWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
// 表格组件表格组件参数
const tourProjectGroupPurchaseApplyOptions: TableOptions<TourProjectGroupPurchaseApplyData> = {
  loadTableData: loadTourProjectGroupPurchaseApplyWidgetData,
  verifyTableParameter: loadTourProjectGroupPurchaseApplyVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const tourProjectGroupPurchaseApply = ref();
const tourProjectGroupPurchaseApplyWidget = useTable(tourProjectGroupPurchaseApplyOptions);
const {
  dataList: tourProjectGroupPurchaseApplyWidgetDataList,
  currentPage: tourProjectGroupPurchaseApplyWidgetCurrentPage,
  pageSize: tourProjectGroupPurchaseApplyWidgetPageSize,
  totalCount: tourProjectGroupPurchaseApplyWidgetTotalCount,
} = tourProjectGroupPurchaseApplyWidget;
/**
 * 是否处理下拉数据获取函数
 */
const loadIsHandleFilterDropdownList = (): Promise<ListData<DictData>> => {
  return new Promise((resolve, reject) => {
    DictionaryController.dictGlobalDict({ dictCode: 'IsHandle', itemIdType: 'Integer' }).then(res => {
      resolve({
        dataList: res.getList(),
      });
    }).catch(e => {
      reject(e);
    });
  });
};
// 是否处理配置参数
const isHandleFilterOptions: DropdownOptions<DictData> = {
  loadData: loadIsHandleFilterDropdownList,
  isTree: false,
};
// 是否处理下拉组件
const isHandleFilterWidget = useDropdown(isHandleFilterOptions);
const { dropdownList: isHandleFilterWidgetDropdownList } = isHandleFilterWidget
const refreshFormTourProjectGroupPurchaseApply = () => {
  // 刷新段落
  tourProjectGroupPurchaseApplyWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormTourProjectGroupPurchaseApply = () => {
  formFilter.projectIdFilter = undefined;
  formFilterCopy.projectIdFilter = undefined;
  formFilter.isHandleFilter = undefined;
  formFilterCopy.isHandleFilter = undefined;
  refreshFormTourProjectGroupPurchaseApply();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormTourProjectGroupPurchaseApply();
};
const formInit = () => {
  isHandleFilterWidget.onVisibleChange(true).catch(e => {console.error(e)});
  refreshFormTourProjectGroupPurchaseApply();
};

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

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