<template>
    <div class="page-box" v-loading="Loading.appMainLoading">
      <tablePagination
        ref="pageTable"
        :currenButtonData="currenButtonData"
        :tableData="tableData"
        :tableLoading="Loading.tableLoading"
        :tableColumns="tableColumns"
        @rowDrop="rowDrop"
        :totalCount="totalCount"
        :multipleSelection="multipleSelection"
        :MaxResultCount="PageListParams.MaxResultCount"
        @topbutton="topbuttonHandle"
        @inlineDialog="inlineDialog"
        @sortChange="sortChange"
        @alertoldSkipCount="alertoldSkipCount"
        @alterResultCount="alterResultCount"
        @handleSelectionChange="handleSelectionChange"
        :currentPageProps="oldSkipCount"
        :quicklySearchOption="quicklySearchOption"
        @quicklySearchClick="quicklySearchClick"
        @quicklySearchClear="quicklySearchClear"
        :primarySearchOption="primarySearchOption"
        @overallSearchFormClick="overallSearchFormClick"
        :httpOverallSearchData="httpOverallSearchData"
        :tableSelection="true"
      >
      </tablePagination>
    </div>
  </template>
  <script>
  import { tableMixins } from "@/mixins/TableMixins";
  import { LoadingMixins } from "@/mixins/LoadingMixins";
  import { TableHeaderMixins } from "@/mixins/TableHeaderMixins";
  import { mixins } from "@/mixins/mixins";
  import { exchangeDataResendById } from "@/api/wms-api"
  
  export default {
    name: "exchangeDataMaintenancePage",
    mixins: [
      tableMixins,
      LoadingMixins,
      TableHeaderMixins,
      mixins,
    ],
    data() {
      return {
        URL: 'wms/store/exchange-data',
        //常用按钮数据
        currenButtonData: [
            this.defaultFieldSettingBtn(),//字段设置
            this.defaultFreshBtn(),//刷新
            this.defaultFilterBtn(),//筛选
            {
                type: "primary",
                icon: "el-icon-refresh",
                label: "批量重发",
                name: "resend",
                size: "mini",
            }
        ],
        selectData:null,//已选数据
      };
    },
    mounted() {
      this.paging();
    },
    methods: {
        handleSelectionChange(val){
            this.selectData = val
        },
        topbuttonHandle(val,item){
            if(val == 'resend'){
              if(this.selectData && this.selectData.length > 0){
                let _numbers = ''
                this.selectData.forEach(item=>{
                  _numbers += item.id + ',<br/>'
                })
                this.$confirm(`已选${this.selectData.length}条数据，是否确定重发? 重发数据id为:<br/>${_numbers}`, '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  distinguishCancelAndClose: true,
                  closeOnClickModal: false,
                  type: 'warning',
                  dangerouslyUseHTMLString: true
                }).then(() => {
                  this.Loading.appMainLoading = true
                  this.resendHandle(0)
                })
              }else{
                this.$warningMsg('请选择数据')
              }
            }else{
                this.topbutton(val,item)
            }
        },
        // 递归处理
        async resendHandle(index){
          if(index >= this.selectData.length){
            this.Loading.appMainLoading = false
            this.$successMsg("全部数据（共"+ this.selectData.length +"条）重发成功！")
            this.$refs.pageTable.clearSelection();
            return
          }
          let _resultBack = await this.resendApiHandle(this.selectData[index].id)
          //如果成功了，继续发送
          if(_resultBack == 1){
            let _index = Number(index) + 1
            this.resendHandle(_index)
          }
          //失败了提示不进行任何处理
          else{
            this.Loading.appMainLoading = false
            this.$errorMsg(`${this.selectData[index].id} 重发失败`)
          }
        },
        // api返回值转换（1成功 0失败）
        async resendApiHandle(id){
            let resultBack = null
            await exchangeDataResendById(id)
                .then(res=>{ resultBack = 1 })
                .catch(err=>{ resultBack = 0 })
            return resultBack
        },
    }
  };
  </script>
<style lang="scss" scoped>
@import "@/styles/basicData.scss";
</style>