<template>
  <div class="page-box" style="position: relative;">
    <table-box
      ref="tourHouseFiles"
      class="page-table"
      :data="tourHouseFilesWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((tourHouseFilesWidgetCurrentPage - 1) * tourHouseFilesWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="true"
      @sort-change="tourHouseFilesWidget.onSortChange"
      @refresh="tourHouseFilesWidget.refreshTable()"
    >
      <template #operator>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          @click="onAddClick()"
          >
          新建
        </el-button>
      </template>
      <vxe-column title="序号" type="seq" :index="tourHouseFilesWidget.getTableIndex" :width="80" />
      <!-- <vxe-column title="文件名称" field="fileName" />
      <vxe-column title="文件大小" field="fileSize" /> -->
      <vxe-column title="价格" field="price" />
      <vxe-column title="货币" field="currency" />
      <vxe-column title="面积" field="area" />
      <vxe-column title="文件存储地址" min-width="100px">
        <template v-slot="scope">
          <el-image v-for="item in parseUploadData(scope.row.fileUrl, { id: scope.row.id, fieldName: 'fileUrl', asImage: true })"
            :preview-src-list="getPictureList(scope.row.fileUrl, { id: scope.row.id, fieldName: 'fileUrl', 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="onEditClick(scope.row)"
          >
            编辑
          </el-button>
          <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="tourHouseFilesWidgetTotalCount"
            :current-page="tourHouseFilesWidgetCurrentPage"
            :page-size="tourHouseFilesWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="tourHouseFilesWidget.onCurrentPageChange"
            @size-change="tourHouseFilesWidget.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: 'formTourHouseTypeInfo',
};
</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 { TourHouseFilesData } from '@/api/generated/tourHouseFilesController';
import { TourHouseFilesController } from '@/api/generated';
import EditFormTourHouseType from '@/pages/TourHouseSaleInfo/editFormTourHouseType.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;
    houseId?: ANY_OBJECT;
  }>(),
  {
    subPage: 0,
    houseId: undefined,
  },
);

const tourHouseFilesDtoFilter = reactive({
  // 房屋id
  tourHouseId: route.query.houseId,
});

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

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

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadTourHouseFilesWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    tourHouseFilesDtoFilter
  };
  return new Promise((resolve, reject) => {
    TourHouseFilesController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadTourHouseFilesVerify = () => {
  return true;
};
/**
 * 新建
 */
const onAddClick = (row?: TourHouseFilesData) => {
  let params: ANY_OBJECT = {
    tourHouseId: route.query.houseId,
  };

  Dialog
    .show('新建', EditFormTourHouseType, { area: ['900px', '700px'] }, { ...params, subPage: true })
    .then(res => {
      tourHouseFilesWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
/**
 * 编辑
 */
const onEditClick = (row?: TourHouseFilesData) => {
  let params: ANY_OBJECT = {
    id: row?.id,
    tourHouseId: route.query.houseId,
  };

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

  TourHouseFilesController.delete(params).then(res => {
    ElMessage.success('删除成功');
    tourHouseFilesWidget.refreshTable(false, 1);
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
// 表格组件表格组件参数
const tourHouseFilesOptions: TableOptions<TourHouseFilesData> = {
  loadTableData: loadTourHouseFilesWidgetData,
  verifyTableParameter: loadTourHouseFilesVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const tourHouseFiles = ref();
const tourHouseFilesWidget = useTable(tourHouseFilesOptions);
const {
  dataList: tourHouseFilesWidgetDataList,
  currentPage: tourHouseFilesWidgetCurrentPage,
  pageSize: tourHouseFilesWidgetPageSize,
  totalCount: tourHouseFilesWidgetTotalCount,
} = tourHouseFilesWidget;
const refreshFormTourHouseTypeInfo = () => {
  // 刷新段落
  tourHouseFilesWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormTourHouseTypeInfo = () => {
  refreshFormTourHouseTypeInfo();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormTourHouseTypeInfo();
};
const formInit = () => {
  refreshFormTourHouseTypeInfo();
};

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