<template>
  <div class="page-box" style="position: relative;">
    <table-box
      ref="tourTravelNotesProjectRelation"
      class="page-table"
      :data="tourTravelNotesProjectRelationWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: true, isHover: true}"
      :seq-config="{startIndex: ((tourTravelNotesProjectRelationWidgetCurrentPage - 1) * tourTravelNotesProjectRelationWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="true"
      @sort-change="tourTravelNotesProjectRelationWidget.onSortChange"
      @refresh="tourTravelNotesProjectRelationWidget.refreshTable()"
      @radio-select-change="onTourTravelNotesProjectRelationRadioSelectChange"
    >
      <template #operator>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          @click="onFormSelectProjectClick()"
          >
          关联旅游项目
        </el-button>
      </template>
      <vxe-column title="" type="radio" align="center" :width="80" />
      <vxe-column title="项目标题" field="tourismProject.projectTitle" />
      <vxe-column title="项目标签" field="tourismProject.projectLabel" />
      <vxe-column title="出发地" field="tourismProject.startPlace" />
      <vxe-column title="目的地" field="tourismProject.endPlace" />
      <!-- <vxe-column title="项目展示图片" min-width="100px">
        <template v-slot="scope">
          <el-image v-for="item in parseUploadData((scope.row.tourismProject || {}).tourismUrl, { id: scope.row.TourismProject.id, fieldName: 'tourismUrl', asImage: true })"
            :preview-src-list="getPictureList((scope.row.tourismProject || {}).tourismUrl, { id: scope.row.TourismProject.id, fieldName: 'tourismUrl', asImage: true })"
            class="table-cell-image" :key="item.url" :src="item.url" fit="fill">
            <template #error>
              <div class="table-cell-image">
                <i class="el-icon-picture-outline"></i>
              </div>
            </template>
          </el-image>
        </template>
      </vxe-column> -->
      <vxe-column title="项目展示图片" min-width="100px">
        <template v-slot="scope">
          <el-image v-for="item in parseUploadData((scope.row.tourismProject || {}).tourismUrl, { id: scope.row.tourismProject.id, fieldName: 'tourismUrl', asImage: true })"
            :preview-src-list="getPictureList((scope.row.tourismProject || {}).tourismUrl, { id: scope.row.tourismProject.id, fieldName: 'tourismUrl', asImage: true })"
            class="table-cell-image" :key="item.url" :src="item.url" fit="fill">
            <template #error>
              <div class="table-cell-image">
                <i class="el-icon-picture-outline"></i>
              </div>
            </template>
          </el-image>
        </template>
      </vxe-column>
      <vxe-column title="操作" fixed="right">
        <template v-slot="scope">
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onDeleteClick(scope.row)"
          >
            移除
          </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="tourTravelNotesProjectRelationWidgetTotalCount"
            :current-page="tourTravelNotesProjectRelationWidgetCurrentPage"
            :page-size="tourTravelNotesProjectRelationWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="tourTravelNotesProjectRelationWidget.onCurrentPageChange"
            @size-change="tourTravelNotesProjectRelationWidget.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: 'formTravelNotesProjectList',
};
</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 { TourismProjectData } from '@/api/generated/tourismProjectController';
import { TourTourismProjectTravelNotesData } from '@/api/generated/tourTourismProjectTravelNotesController';
import { TourTravelNotesProjectRelationData } from '@/api/generated/tourTravelNotesProjectRelationController';
import { TourismProjectController, TourTourismProjectTravelNotesController, TourTravelNotesProjectRelationController } from '@/api/generated';
import FormSelectProject from '@/pages/tourTourismTravelNotes/formSelectProject.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;
    travelNotesId?: ANY_OBJECT;
  }>(),
  {
    subPage: 0,
    travelNotesId: undefined,
  },
);


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

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

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadTourTravelNotesProjectRelationWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    tourTravelNotesProjectRelationDtoFilter: {
      travelNotesId: props.travelNotesId
    }
  };
  return new Promise((resolve, reject) => {
    TourTravelNotesProjectRelationController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
      // 恢复当选择行
      if (tourTravelNotesProjectRelationSelectRow.value != null) {
        nextTick(() => {
          let currentRow = findItemFromList(tourTravelNotesProjectRelationWidgetDataList.value, tourTravelNotesProjectRelationSelectRow.value.id, 'id');
          tourTravelNotesProjectRelation.value.getTableImpl().setRadioRow(currentRow);
        });
      }
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadTourTravelNotesProjectRelationVerify = () => {
  return true;
};
/**
 * 表格组件表格选中行改变
 */
const onTourTravelNotesProjectRelationRadioSelectChange = (row?: TourTravelNotesProjectRelationData) => {
  tourTravelNotesProjectRelationSelectRow.value = row;
};
/**
 * 移除
 */
const onDeleteClick = (row?: TourTravelNotesProjectRelationData) => {
  let params: ANY_OBJECT = {
    id: row?.id,
  };

  ElMessageBox.confirm('是否删除此记录？').then(res => {
    TourTravelNotesProjectRelationController.delete(params).then(res => {
      ElMessage.success('移除成功');
      tourTravelNotesProjectRelationWidget.refreshTable(false, 1);
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
/**
 * 关联旅游项目
 */
const onFormSelectProjectClick = (row?: TourTravelNotesProjectRelationData) => {
  let params: ANY_OBJECT = {
    travelNotesId: props.travelNotesId,
  };

  Dialog
    .show('关联旅游项目', FormSelectProject, { area: '900px' }, { ...params, subPage: true })
    .then(res => {
      tourTravelNotesProjectRelationWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
// 表格组件表格组件参数
const tourTravelNotesProjectRelationOptions: TableOptions<TourTravelNotesProjectRelationData> = {
  loadTableData: loadTourTravelNotesProjectRelationWidgetData,
  verifyTableParameter: loadTourTravelNotesProjectRelationVerify,
  paged: true,
  rowSelection: true,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件选中数据
const tourTravelNotesProjectRelationSelectRow = ref<TourTravelNotesProjectRelationData>();
// 表格组件表格组件
const tourTravelNotesProjectRelation = ref();
const tourTravelNotesProjectRelationWidget = useTable(tourTravelNotesProjectRelationOptions);
const {
  dataList: tourTravelNotesProjectRelationWidgetDataList,
  currentPage: tourTravelNotesProjectRelationWidgetCurrentPage,
  pageSize: tourTravelNotesProjectRelationWidgetPageSize,
  totalCount: tourTravelNotesProjectRelationWidgetTotalCount,
} = tourTravelNotesProjectRelationWidget;
const refreshFormTravelNotesProjectList = () => {
  // 刷新段落
  tourTravelNotesProjectRelationWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormTravelNotesProjectList = () => {
  refreshFormTravelNotesProjectList();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormTravelNotesProjectList();
};
const formInit = () => {
  refreshFormTravelNotesProjectList();
};

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