<template>
  <div class="page-box" style="position: relative;">
    <el-form
      ref="formTourismProjectDatePriceRef"
      :model="formData"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshFormTourismProjectDatePrice()" @reset="resetFormTourismProjectDatePrice">
        <el-form-item label="旅游项目信息">
          <SelectTourismProject
            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-date-picker
            class="filter-item"
            v-model="formFilter.departureDateFilter"
            type="daterange"
            start-placeholder=""
            end-placeholder=""
            :clearable="true"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD HH:mm:ss"
            :default-time="[new Date().setHours(0, 0, 0, 0), new Date().setHours(23, 59, 59, 999)]"
          />
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      ref="tourismDatePrice"
      class="page-table"
      :data="tourismDatePriceWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((tourismDatePriceWidgetCurrentPage - 1) * tourismDatePriceWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="true"
      @sort-change="tourismDatePriceWidget.onSortChange"
      @refresh="tourismDatePriceWidget.refreshTable()"
    >
      <template #operator>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          :disabled="!checkPermCodeExist('formTourismProjectDatePrice:formTourismProjectDatePrice:add')"
          @click="onAddClick()"
          >
          新增
        </el-button>
      </template>
      <vxe-column title="序号" type="seq" :index="tourismDatePriceWidget.getTableIndex" :width="80" />
      <vxe-column title="旅游项目信息" field="projectIdDictMap.name" />
      <vxe-column title="出发日期" field="departureDate">
        <template v-slot="scope">
          <span>{{formatDateByStatsType(scope.row.departureDate, 'day')}}</span>
        </template>
      </vxe-column>
      <vxe-column title="成人价格" field="adultPrice" />
      <vxe-column title="儿童价格" field="childrenPrice" />
      <vxe-column title="操作" fixed="right">
        <template v-slot="scope">
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onEditClick(scope.row)"
            :disabled="!checkPermCodeExist('formTourismProjectDatePrice:formTourismProjectDatePrice:edit')"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onDeleteClick(scope.row)"
            :disabled="!checkPermCodeExist('formTourismProjectDatePrice:formTourismProjectDatePrice: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="tourismDatePriceWidgetTotalCount"
            :current-page="tourismDatePriceWidgetCurrentPage"
            :page-size="tourismDatePriceWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="tourismDatePriceWidget.onCurrentPageChange"
            @size-change="tourismDatePriceWidget.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: 'formTourismProjectDatePrice',
};
</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 } from '@/common/utils';
import { TourismDatePriceData } from '@/api/generated/tourismDatePriceController';
import { TourismProjectData } from '@/api/generated/tourismProjectController';
import { TourismDatePriceController, TourismProjectController } from '@/api/generated';
import FormEditTourismDatePrice from '@/pages/tourismProjectDatePrice/formEditTourismDatePrice.vue';
import SelectTourismProject from '@/pages/tourismProjectDatePrice/selectTourismProject/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,
  // 出发日期
  departureDateFilter: [],
});
const formFilterCopy = reactive({
  // 旅游项目信息
  projectIdFilter: undefined,
  // 出发日期
  departureDateFilter: [],
});

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

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

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadTourismDatePriceWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    tourismDatePriceDtoFilter: {
      projectId: formFilter.projectIdFilter,
      departureDateStart: Array.isArray(formFilter.departureDateFilter) ? formFilter.departureDateFilter[0] : formFilter.departureDateFilter,
      departureDateEnd: Array.isArray(formFilter.departureDateFilter) ? formFilter.departureDateFilter[1] : formFilter.departureDateFilter,
    }
  };
  return new Promise((resolve, reject) => {
    TourismDatePriceController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadTourismDatePriceVerify = () => {
  formFilterCopy.projectIdFilter = formFilter.projectIdFilter;
  formFilterCopy.departureDateFilter = formFilter.departureDateFilter;
  return true;
};
/**
 * 新增
 */
const onAddClick = (row?: TourismDatePriceData) => {
  let params: ANY_OBJECT = {
  };

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

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

  TourismDatePriceController.delete(params).then(res => {
    ElMessage.success('删除成功');
    tourismDatePriceWidget.refreshTable(false, 1);
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
// 表格组件表格组件参数
const tourismDatePriceOptions: TableOptions<TourismDatePriceData> = {
  loadTableData: loadTourismDatePriceWidgetData,
  verifyTableParameter: loadTourismDatePriceVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: 'projectId',
  ascending: false,
};
// 表格组件表格组件
const tourismDatePrice = ref();
const tourismDatePriceWidget = useTable(tourismDatePriceOptions);
const {
  dataList: tourismDatePriceWidgetDataList,
  currentPage: tourismDatePriceWidgetCurrentPage,
  pageSize: tourismDatePriceWidgetPageSize,
  totalCount: tourismDatePriceWidgetTotalCount,
} = tourismDatePriceWidget;
const refreshFormTourismProjectDatePrice = () => {
  // 刷新段落
  tourismDatePriceWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormTourismProjectDatePrice = () => {
  formFilter.projectIdFilter = undefined;
  formFilterCopy.projectIdFilter = undefined;
  formFilter.departureDateFilter = undefined;
  formFilterCopy.departureDateFilter = undefined;
  refreshFormTourismProjectDatePrice();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormTourismProjectDatePrice();
};
const formInit = () => {
  refreshFormTourismProjectDatePrice();
};

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