<template>
  <div class="page-box" style="position: relative;">
    <el-form
      ref="formTourTravelNotesTopicRef"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshFormTourTravelNotesTopic()" @reset="resetFormTourTravelNotesTopic">
        <el-form-item label="话题名称">
          <el-input
            class="filter-item"
            v-model="formFilter.nameFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select
            class="filter-item"
            v-model="formFilter.enableFilter"
            placeholder=""
            :clearable="true"
            :filterable="true"
            @visible-change="enableFilterWidget.onVisibleChange"
          >
            <el-option
              v-for="item in enableFilterWidgetDropdownList"
              :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.hotValueFilter"
            startPlaceholder=""
            endPlaceholder=""
            :step="1"
          />
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      ref="tourTravelNotesTopic"
      class="page-table"
      :data="tourTravelNotesTopicWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((tourTravelNotesTopicWidgetCurrentPage - 1) * tourTravelNotesTopicWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="true"
      @sort-change="tourTravelNotesTopicWidget.onSortChange"
      @refresh="tourTravelNotesTopicWidget.refreshTable()"
    >
      <template #operator>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          :disabled="!checkPermCodeExist('formTourTravelNotesTopic:formTourTravelNotesTopic:add')"
          @click="onAddClick()"
          >
          新增
        </el-button>
      </template>
      <vxe-column title="序号" type="seq" :index="tourTravelNotesTopicWidget.getTableIndex" :width="80" />
      <vxe-column title="话题名称" field="name" />
      <vxe-column title="是否启用" field="enableDictMap.name" />
      <vxe-column title="热度值" field="hotValue" />
      <vxe-column title="操作" fixed="right">
        <template v-slot="scope">
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onEditClick(scope.row)"
            :disabled="!checkPermCodeExist('formTourTravelNotesTopic:formTourTravelNotesTopic:edit')"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onDeleteClick(scope.row)"
            :disabled="!checkPermCodeExist('formTourTravelNotesTopic:formTourTravelNotesTopic: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="tourTravelNotesTopicWidgetTotalCount"
            :current-page="tourTravelNotesTopicWidgetCurrentPage"
            :page-size="tourTravelNotesTopicWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="tourTravelNotesTopicWidget.onCurrentPageChange"
            @size-change="tourTravelNotesTopicWidget.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: 'formTourTravelNotesTopic',
};
</script>

<script setup lang="ts">
import * as validateRules from '@/common/utils/validate';
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 { TourTravelNotesTopicData } from '@/api/generated/tourTravelNotesTopicController';
import { TourTravelNotesTopicController } from '@/api/generated';
import EditTourTravelNotesTopic from '@/pages/tourTravelNotesTopic/editTourTravelNotesTopic.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({
  // 话题名称
  nameFilter: undefined,
  // 是否启用
  enableFilter: undefined,
  // 热度值
  hotValueFilter: [],
});
const formFilterCopy = reactive({
  // 话题名称
  nameFilter: undefined,
  // 是否启用
  enableFilter: undefined,
  // 热度值
  hotValueFilter: [],
});

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

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

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadTourTravelNotesTopicWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    tourTravelNotesTopicDtoFilter: {
      name: formFilter.nameFilter,
      enable: formFilter.enableFilter,
      hotValueStart: Array.isArray(formFilter.hotValueFilter) ? formFilter.hotValueFilter[0] : formFilter.hotValueFilter,
      hotValueEnd: Array.isArray(formFilter.hotValueFilter) ? formFilter.hotValueFilter[1] : formFilter.hotValueFilter,
    }
  };
  return new Promise((resolve, reject) => {
    TourTravelNotesTopicController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadTourTravelNotesTopicVerify = () => {
  formFilterCopy.nameFilter = formFilter.nameFilter;
  formFilterCopy.enableFilter = formFilter.enableFilter;
  formFilterCopy.hotValueFilter = formFilter.hotValueFilter;
  return true;
};
/**
 * 新增
 */
const onAddClick = (row?: TourTravelNotesTopicData) => {
  let params: ANY_OBJECT = {
  };

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

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

  ElMessageBox.confirm('是否删除该条数据?').then(res => {
    TourTravelNotesTopicController.delete(params).then(res => {
      ElMessage.success('删除成功');
      tourTravelNotesTopicWidget.refreshTable(false, 1);
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
// 表格组件表格组件参数
const tourTravelNotesTopicOptions: TableOptions<TourTravelNotesTopicData> = {
  loadTableData: loadTourTravelNotesTopicWidgetData,
  verifyTableParameter: loadTourTravelNotesTopicVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const tourTravelNotesTopic = ref();
const tourTravelNotesTopicWidget = useTable(tourTravelNotesTopicOptions);
const {
  dataList: tourTravelNotesTopicWidgetDataList,
  currentPage: tourTravelNotesTopicWidgetCurrentPage,
  pageSize: tourTravelNotesTopicWidgetPageSize,
  totalCount: tourTravelNotesTopicWidgetTotalCount,
} = tourTravelNotesTopicWidget;
/**
 * 是否启用下拉数据获取函数
 */
const loadEnableFilterDropdownList = (): Promise<ListData<DictData>> => {
  return Promise.resolve({
    dataList: StaticDict.Enable.getList(),
  });
};
// 是否启用配置参数
const enableFilterOptions: DropdownOptions<DictData> = {
  loadData: loadEnableFilterDropdownList,
  isTree: false,
};
// 是否启用下拉组件
const enableFilterWidget = useDropdown(enableFilterOptions);
const { dropdownList: enableFilterWidgetDropdownList } = enableFilterWidget
const refreshFormTourTravelNotesTopic = () => {
  // 刷新段落
  tourTravelNotesTopicWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormTourTravelNotesTopic = () => {
  formFilter.nameFilter = undefined;
  formFilterCopy.nameFilter = undefined;
  formFilter.enableFilter = undefined;
  formFilterCopy.enableFilter = undefined;
  formFilter.hotValueFilter = undefined;
  formFilterCopy.hotValueFilter = undefined;
  refreshFormTourTravelNotesTopic();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormTourTravelNotesTopic();
};
const formInit = () => {
  enableFilterWidget.onVisibleChange(true).catch(e => {console.error(e)});
  refreshFormTourTravelNotesTopic();
};

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

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