<template>
  <div class="page-box" style="position: relative;">
    <el-form
      ref="formTourCountryCode"
      :model="formData"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshFormTourCountryCode()" @reset="resetFormTourCountryCode">
        <el-form-item label="英文名称">
          <el-input
            class="filter-item"
            v-model="formFilter.englishNameFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
        <el-form-item label="国家/地区">
          <el-input
            class="filter-item"
            v-model="formFilter.countryFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
        <el-form-item label="国家代码">
          <el-input
            class="filter-item"
            v-model="formFilter.countryCodeFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      ref="formTourCountryCodeTable"
      class="page-table"
      :data="formTourCountryCodeTableWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((formTourCountryCodeTableWidgetCurrentPage - 1) * formTourCountryCodeTableWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="true"
      @sort-change="formTourCountryCodeTableWidget.onSortChange"
      @refresh="formTourCountryCodeTableWidget.refreshTable()"
    >
      <template #operator>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          :disabled="!checkPermCodeExist('formTourCountryCode:formTourCountryCode:addTourCountryCode')"
          @click="onAddTourCountryCodeClick()"
          >
          新建
        </el-button>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          :disabled="!checkPermCodeExist('formTourCountryCode:formTourCountryCode:exportTourCountryCode')"
          @click="onExportTourCountryCodeClick()"
          >
          导出
        </el-button>
        <el-upload
          class="btn-import"
          :auto-upload="false"
          action=""
          :show-file-list="false"
          accept=".xls,.xlsx"
          style="display: inline-block;"
          :disabled="!checkPermCodeExist('formTourCountryCode:formTourCountryCode:importTourCountryCode')"
          :on-change="onImportTourCountryCodeClick"
        >
          <template #trigger>
            <el-button
              type="primary"
              :size="layoutStore.defaultFormItemSize"
              :disabled="!checkPermCodeExist('formTourCountryCode:formTourCountryCode:importTourCountryCode')"
            >
              导入
            </el-button>
          </template>
        </el-upload>
      </template>
      <vxe-column title="序号" type="seq" :index="formTourCountryCodeTableWidget.getTableIndex" :width="80" />
      <vxe-column title="英文名称" field="englishName" />
      <vxe-column title="国家/地区" field="country" />
      <vxe-column title="国家代码" field="countryCode" />
      <vxe-column title="区号" field="areaCode" />
      <vxe-column title="操作" fixed="right">
        <template v-slot="scope">
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onEditTourCountryCodeClick(scope.row)"
            :disabled="!checkPermCodeExist('formTourCountryCode:formTourCountryCode:editTourCountryCode')"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onDeleteTourCountryCodeClick(scope.row)"
            :disabled="!checkPermCodeExist('formTourCountryCode:formTourCountryCode:deleteTourCountryCode')"
          >
            删除
          </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="formTourCountryCodeTableWidgetTotalCount"
            :current-page="formTourCountryCodeTableWidgetCurrentPage"
            :page-size="formTourCountryCodeTableWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="formTourCountryCodeTableWidget.onCurrentPageChange"
            @size-change="formTourCountryCodeTableWidget.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: 'formTourCountryCode',
};
</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 { TourCountryCodeData } from '@/api/generated/tourCountryCodeController';
import { TourCountryCodeController } from '@/api/generated';
import FormEditTourCountryCode from '@/pages/generated/formEditTourCountryCode.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({
  // 英文名称
  englishNameFilter: undefined,
  // 国家/地区
  countryFilter: undefined,
  // 国家代码
  countryCodeFilter: undefined,
});
const formFilterCopy = reactive({
  // 英文名称
  englishNameFilter: undefined,
  // 国家/地区
  countryFilter: undefined,
  // 国家代码
  countryCodeFilter: undefined,
});

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

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

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadFormTourCountryCodeTableWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    tourCountryCodeDtoFilter: {
      englishName: formFilter.englishNameFilter,
      country: formFilter.countryFilter,
      countryCode: formFilter.countryCodeFilter,
    }
  };
  return new Promise((resolve, reject) => {
    TourCountryCodeController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadFormTourCountryCodeTableVerify = () => {
  formFilterCopy.englishNameFilter = formFilter.englishNameFilter;
  formFilterCopy.countryFilter = formFilter.countryFilter;
  formFilterCopy.countryCodeFilter = formFilter.countryCodeFilter;
  return true;
};
/**
 * 新建
 */
const onAddTourCountryCodeClick = (row?: TourCountryCodeData) => {
  let params: ANY_OBJECT = {
  };

  Dialog
    .show('新建', FormEditTourCountryCode, { area: ['900px', '1300px'] }, { ...params, subPage: true })
    .then(res => {
      formTourCountryCodeTableWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
/**
 * 编辑
 */
const onEditTourCountryCodeClick = (row?: TourCountryCodeData) => {
  let params: ANY_OBJECT = {
    id: row?.id,
  };

  Dialog
    .show('编辑', FormEditTourCountryCode, { area: ['900px', '1300px'] }, { ...params, subPage: true })
    .then(res => {
      formTourCountryCodeTableWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
/**
 * 导出
 */
const onExportTourCountryCodeClick = (row?: TourCountryCodeData) => {
  let params: ANY_OBJECT = {
  };

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

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

  ElMessageBox.confirm('是否删除此记录？').then(res => {
    TourCountryCodeController.delete(params).then(res => {
      ElMessage.success('删除成功');
      formTourCountryCodeTableWidget.refreshTable(false, 1);
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
// 表格组件表格组件参数
const formTourCountryCodeTableOptions: TableOptions<TourCountryCodeData> = {
  loadTableData: loadFormTourCountryCodeTableWidgetData,
  verifyTableParameter: loadFormTourCountryCodeTableVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const formTourCountryCodeTable = ref();
const formTourCountryCodeTableWidget = useTable(formTourCountryCodeTableOptions);
const {
  dataList: formTourCountryCodeTableWidgetDataList,
  currentPage: formTourCountryCodeTableWidgetCurrentPage,
  pageSize: formTourCountryCodeTableWidgetPageSize,
  totalCount: formTourCountryCodeTableWidgetTotalCount,
} = formTourCountryCodeTableWidget;
const refreshFormTourCountryCode = () => {
  // 刷新段落
  formTourCountryCodeTableWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormTourCountryCode = () => {
  formFilter.englishNameFilter = undefined;
  formFilterCopy.englishNameFilter = undefined;
  formFilter.countryFilter = undefined;
  formFilterCopy.countryFilter = undefined;
  formFilter.countryCodeFilter = undefined;
  formFilterCopy.countryCodeFilter = undefined;
  refreshFormTourCountryCode();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormTourCountryCode();
};
const formInit = () => {
  refreshFormTourCountryCode();
};

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