<template>
  <div class="page-box" style="position: relative;">
    <el-form
      ref="formTourOrderPassenger"
      :model="formData"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshFormTourOrderPassenger()" @reset="resetFormTourOrderPassenger">
        <el-form-item label="中文姓名">
          <el-input
            class="filter-item"
            v-model="formFilter.chineseNameFilter"
            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.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.nationalityFilter"
            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.mobileFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      ref="tourOrderPassenage"
      class="page-table"
      :data="tourOrderPassenageWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((tourOrderPassenageWidgetCurrentPage - 1) * tourOrderPassenageWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="true"
      @sort-change="tourOrderPassenageWidget.onSortChange"
      @refresh="tourOrderPassenageWidget.refreshTable()"
    >
      <template #operator>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          @click="onAddClick()"
          >
          新增
        </el-button>
      </template>
      <vxe-column title="序号" type="seq" :index="tourOrderPassenageWidget.getTableIndex" :width="80" />
      <vxe-column title="中文姓名" field="chineseName" />
      <vxe-column title="英文姓名" field="englishName" />
      <vxe-column title="国籍" field="nationality" />
      <vxe-column title="生日" field="birthday">
        <template v-slot="scope">
          <span>{{formatDateByStatsType(scope.row.birthday, 'day')}}</span>
        </template>
      </vxe-column>
      <vxe-column title="手机号码" field="mobile" />
      <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="tourOrderPassenageWidgetTotalCount"
            :current-page="tourOrderPassenageWidgetCurrentPage"
            :page-size="tourOrderPassenageWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="tourOrderPassenageWidget.onCurrentPageChange"
            @size-change="tourOrderPassenageWidget.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: 'formTourOrderPassenger',
};
</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 { TourOrderPassenageData } from '@/api/generated/tourOrderPassenageController';
import { TourOrderPassenageController } from '@/api/generated';
import FormEditOrderPassenger from '@/pages/tourOrder/formEditOrderPassenger.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;
    orderId?: ANY_OBJECT;
  }>(),
  {
    subPage: 0,
    orderId: undefined,
  },
);

const formFilter = reactive({
  // 中文姓名
  chineseNameFilter: undefined,
  // 英文姓名
  englishNameFilter: undefined,
  // 国籍
  nationalityFilter: undefined,
  // 手机号码
  mobileFilter: undefined,
});
const formFilterCopy = reactive({
  // 中文姓名
  chineseNameFilter: undefined,
  // 英文姓名
  englishNameFilter: undefined,
  // 国籍
  nationalityFilter: undefined,
  // 手机号码
  mobileFilter: undefined,
});

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

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

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadTourOrderPassenageWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    tourOrderPassenageDtoFilter: {
      chineseName: formFilter.chineseNameFilter,
      englishName: formFilter.englishNameFilter,
      nationality: formFilter.nationalityFilter,
      mobile: formFilter.mobileFilter,
      orderId: props.orderId,
    }
  };
  return new Promise((resolve, reject) => {
    TourOrderPassenageController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadTourOrderPassenageVerify = () => {
  formFilterCopy.chineseNameFilter = formFilter.chineseNameFilter;
  formFilterCopy.englishNameFilter = formFilter.englishNameFilter;
  formFilterCopy.nationalityFilter = formFilter.nationalityFilter;
  formFilterCopy.mobileFilter = formFilter.mobileFilter;
  return true;
};
/**
 * 新增
 */
const onAddClick = (row?: TourOrderPassenageData) => {
  let params: ANY_OBJECT = {
    orderId :props.orderId
  };

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

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

  TourOrderPassenageController.delete(params).then(res => {
    ElMessage.success('删除成功');
    tourOrderPassenageWidget.refreshTable(false, 1);
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
// 表格组件表格组件参数
const tourOrderPassenageOptions: TableOptions<TourOrderPassenageData> = {
  loadTableData: loadTourOrderPassenageWidgetData,
  verifyTableParameter: loadTourOrderPassenageVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const tourOrderPassenage = ref();
const tourOrderPassenageWidget = useTable(tourOrderPassenageOptions);
const {
  dataList: tourOrderPassenageWidgetDataList,
  currentPage: tourOrderPassenageWidgetCurrentPage,
  pageSize: tourOrderPassenageWidgetPageSize,
  totalCount: tourOrderPassenageWidgetTotalCount,
} = tourOrderPassenageWidget;
const refreshFormTourOrderPassenger = () => {
  // 刷新段落
  tourOrderPassenageWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormTourOrderPassenger = () => {
  formFilter.chineseNameFilter = undefined;
  formFilterCopy.chineseNameFilter = undefined;
  formFilter.englishNameFilter = undefined;
  formFilterCopy.englishNameFilter = undefined;
  formFilter.nationalityFilter = undefined;
  formFilterCopy.nationalityFilter = undefined;
  formFilter.mobileFilter = undefined;
  formFilterCopy.mobileFilter = undefined;
  refreshFormTourOrderPassenger();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormTourOrderPassenger();
};
const formInit = () => {
  refreshFormTourOrderPassenger();
};

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