<template>
  <div class="page-box" style="position: relative;">
    <el-form
      ref="formVisaMetarialsListRef"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshFormVisaMetarialsList()" @reset="resetFormVisaMetarialsList">
        <el-form-item label="基础材料名字">
          <el-input
            class="filter-item"
            v-model="formFilter.titleFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      ref="formVisaMetarialsListTable"
      class="page-table"
      :data="formVisaMetarialsListTableWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((formVisaMetarialsListTableWidgetCurrentPage - 1) * formVisaMetarialsListTableWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="true"
      @sort-change="formVisaMetarialsListTableWidget.onSortChange"
      @refresh="formVisaMetarialsListTableWidget.refreshTable()"
    >
      <template #operator>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          @click="onAddVisaMetarialsListClick()"
          >
          新建
        </el-button>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          @click="onExportVisaMetarialsListClick()"
          >
          导出
        </el-button>
        <el-upload
          class="btn-import"
          :auto-upload="false"
          action=""
          :show-file-list="false"
          accept=".xls,.xlsx"
          style="display: inline-block;"
          :on-change="onImportVisaMetarialsListClick"
        >
          <template #trigger>
            <el-button
              type="primary"
              :size="layoutStore.defaultFormItemSize"
            >
              导入
            </el-button>
          </template>
        </el-upload>
      </template>
      <vxe-column title="序号" type="seq" :index="formVisaMetarialsListTableWidget.getTableIndex" :width="80" />
      <vxe-column title="基础材料名字" field="title" />
      <vxe-column title="基础材料介绍" field="content" />
      <vxe-column title="操作" fixed="right">
        <template v-slot="scope">
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onEditVisaMetarialsListClick(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onDeleteVisaMetarialsListClick(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="formVisaMetarialsListTableWidgetTotalCount"
            :current-page="formVisaMetarialsListTableWidgetCurrentPage"
            :page-size="formVisaMetarialsListTableWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="formVisaMetarialsListTableWidget.onCurrentPageChange"
            @size-change="formVisaMetarialsListTableWidget.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: 'formVisaMetarialsList',
};
</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 { VisaMetarialsListData } from '@/api/generated/visaMetarialsListController';
import { VisaMetarialsListController } from '@/api/generated';
import FormEditVisaMetarialsList from '@/pages/tourVisa/formEditVisaMetarialsList.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;
    visaId?: ANY_OBJECT;
  }>(),
  {
    subPage: 0,
    visaId: undefined,
  },
);

const formFilter = reactive({
  // 基础材料名字
  titleFilter: undefined,
});
const formFilterCopy = reactive({
  // 基础材料名字
  titleFilter: undefined,
});

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

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

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadFormVisaMetarialsListTableWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    visaMetarialsListDtoFilter: {
      visaId: props.visaId,
      title: formFilter.titleFilter,
    }
  };
  return new Promise((resolve, reject) => {
    VisaMetarialsListController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadFormVisaMetarialsListTableVerify = () => {
  formFilterCopy.titleFilter = formFilter.titleFilter;
  return true;
};
/**
 * 新建
 */
const onAddVisaMetarialsListClick = (row?: VisaMetarialsListData) => {
  let params: ANY_OBJECT = {
    visaId: props.visaId,
  };
  Dialog
    .show('新建', FormEditVisaMetarialsList, { area: '900px' }, { ...params, subPage: true })
    .then(res => {
      formVisaMetarialsListTableWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
/**
 * 编辑
 */
const onEditVisaMetarialsListClick = (row?: VisaMetarialsListData) => {
  let params: ANY_OBJECT = {
    id: row?.id,
    visaId: props.visaId,
  };

  Dialog
    .show('编辑', FormEditVisaMetarialsList, { area: '900px' }, { ...params, subPage: true })
    .then(res => {
      formVisaMetarialsListTableWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
/**
 * 导出
 */
const onExportVisaMetarialsListClick = (row?: VisaMetarialsListData) => {
  let params: ANY_OBJECT = {
    visaMetarialsListDtoFilter: {
      visaId: props.visaId,
    }
  };

  VisaMetarialsListController.export(params, '表格组件.xlsx').then(res => {
    ElMessage.success('导出成功');
  }).catch(e => {
    ElMessage.error(e.errorMessage);
  });
};
/**
 * 导入
 */
const onImportVisaMetarialsListClick = (file) => {
  let params: ANY_OBJECT = {
    importFile: file.raw,
    // 是否忽略表头
    skipHeader: false
  };

  VisaMetarialsListController.import(params).then(res => {
    ElMessage.success('导入成功');
    formVisaMetarialsListTableWidget.refreshTable();
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
/**
 * 删除
 */
const onDeleteVisaMetarialsListClick = (row?: VisaMetarialsListData) => {
  let params: ANY_OBJECT = {
    id: row?.id,
  };

  ElMessageBox.confirm('是否删除此记录？').then(res => {
    VisaMetarialsListController.delete(params).then(res => {
      ElMessage.success('删除成功');
      formVisaMetarialsListTableWidget.refreshTable(false, 1);
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
// 表格组件表格组件参数
const formVisaMetarialsListTableOptions: TableOptions<VisaMetarialsListData> = {
  loadTableData: loadFormVisaMetarialsListTableWidgetData,
  verifyTableParameter: loadFormVisaMetarialsListTableVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const formVisaMetarialsListTable = ref();
const formVisaMetarialsListTableWidget = useTable(formVisaMetarialsListTableOptions);
const {
  dataList: formVisaMetarialsListTableWidgetDataList,
  currentPage: formVisaMetarialsListTableWidgetCurrentPage,
  pageSize: formVisaMetarialsListTableWidgetPageSize,
  totalCount: formVisaMetarialsListTableWidgetTotalCount,
} = formVisaMetarialsListTableWidget;
const refreshFormVisaMetarialsList = () => {
  // 刷新段落
  formVisaMetarialsListTableWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormVisaMetarialsList = () => {
  formFilter.titleFilter = undefined;
  formFilterCopy.titleFilter = undefined;
  refreshFormVisaMetarialsList();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormVisaMetarialsList();
};
const formInit = () => {
  refreshFormVisaMetarialsList();
};

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