<template>
  <div id="invoice_split" v-loading="load" element-loading-text="开具中，请稍等">
    <div class="invoice_operation">
      <div style="flex: 1"></div>
      <div>
        <el-button class="invoice_button" type="primary" v-on:click="submitInvoice">
          提交开票
        </el-button>
      </div>
    </div>
    <div>
      <el-table ref="multipleTable" :data="invoiceList" style="width: 100%" border
                :height="350" v-on:selection-change="selectData">
        <el-table-column type="selection" width="55" :selectable="selectStatus"></el-table-column>
        <el-table-column prop="productInfo.invoiceCode" label="发票代码" width="120"></el-table-column>
        <el-table-column prop="productInfo.invoiceNumber" label="发票号码" width="100"></el-table-column>
        <el-table-column prop="productInfo.invoiceStatus" label="发票状态" width="120" :formatter="fmtInvoiceStatus"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.SPF_MC" label="购方企业名称" width="180"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.SPF_NSRSBH" label="购方企业税号" width="160"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.SPF_DZ" label="购方地址、电话" width="220"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.SPF_KHHMC" label="购方开户行及帐号" width="220"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.HJJE" label="合计金额" width="120"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.HJSE" label="合计税额" width="120"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.JSHJ" label="价税合计" width="120"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.KPR" label="开票人" width="100"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.SKR" label="收款人" width="100"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.FHR" label="复核人" width="100"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.KPF_MC" label="销方企业名称" width="180"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.KPF_NSRSBH" label="销方企业税号" width="160"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.KPF_DZ" label="销方地址、电话" width="220"></el-table-column>
        <el-table-column prop="REQUEST_COMMON_FPKJ.KPF_KHHMC" label="销方开户行及帐号" width="220"></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="120">
          <template slot-scope="scope">
            <el-button @click="refreshInvoiceInfo(scope.row)" v-if="scope.row.productInfo.invoiceStatus === 2" type="primary" size="small" style="margin: 5px">刷新</el-button>
            <el-button @click="printInvoice(scope.row)" v-if="scope.row.productInfo.invoiceStatus === 3" type="primary" size="small" style="margin: 5px">打印</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {AxiosRequestData} from '@/data/axios_request'
import {ProductsListChange} from '@/data/invoice_calculation'
import cache from '@/utils/cache'

export default {
  name: 'invoice_split',
  data() {
    return {
      multipleSelection: [],
      invoiceList: [],
      invoiceSelectList: [],
      token: '',
      account: '',
      entTaxId: '',
      timerRequestNum: 0,
      getDataNumber: 0,
      load: false,
      status: 0
    }
  },
  created() {
    this.invoiceList = this.$attrs.invoiceSplit.invoiceSplitList
    this.entTaxId = this.$attrs.invoiceSplit.billingTaxId
    this.token = cache.getToken()
    this.account = JSON.parse(cache.getSession('user')).account
  },
  methods: {
    // 获取选中数据
    selectData(selection) {
      this.invoiceSelectList = selection
    },
    // 获取开票数据
    getInvoiceSplitInfo(invoiceSplitList, billingTaxId) {
      console.log(invoiceSplitList)
      console.log(billingTaxId)
      this.invoiceList = invoiceSplitList
      this.entTaxId = billingTaxId
      this.token = cache.getToken()
      this.account = JSON.parse(cache.getSession('user')).account
    },
    // 批量提交开票信息
    async submitInvoice() {
      if (this.invoiceSelectList.length > 0) {
        this.status = 1
        this.load = true
        for (let i = 0; i < this.invoiceSelectList.length; i++) {
          let product = ProductsListChange(this.invoiceSelectList[i], 2)
          await this.invoiceIssuing(product)
        }
        this.load = false
        this.status = 0
      } else {
        this.$message({
          message: '请选择开票数据！',
          type: 'warning'
        })
      }
    },
    // 发票开具
    async invoiceIssuing(invoiceInfo) {
      const url = '/sys/blueInvoice/save'
      let response = await AxiosRequestData(
          url,
          this.token,
          {
            interactType: 1,
            interactTypeDetail: '增值税专用纸质发票',
            invoiceWay: 1,
            invoiceCategory: 1,
            cmdParam: {
              REQUEST_COMMON_FPKJ: invoiceInfo.REQUEST_COMMON_FPKJ
            },
            mobile: this.account,
            reqChannel: 4
          },
          false,
          1
      )
      if (response) {
        console.log(response)
        if (response.code === 0) {
          let row = invoiceInfo.productInfo.rowId
          this.getDataNumber = 0
          this.invoiceList[row].productInfo.invoiceReqFlowNo = response.data.invoiceReqFlowNo
          this.invoiceList[row].productInfo.orderId = response.data.orderId
          this.invoiceList[row].productInfo.invoiceType = response.data.invoiceType
          this.timerRequestNum = 0
          for (let i = 0; i < 3; i++) {
            if (this.getDataNumber === 0) {
              await this.getInvoiceInfo(invoiceInfo)
            } else {
              break
            }
          }
        }
      }
    },
    // 获取开票成功后发票的相关信息
    async getInvoiceInfo(invoiceInfo) {
      if (this.token) {
        let row = invoiceInfo.productInfo.rowId
        this.timerRequestNum += 1
        const url = '/sys/invoiceExt/getResultData'
        let response = await AxiosRequestData(
            url,
            this.token,
            {
              entTaxId: this.entTaxId,
              orderId: this.invoiceList[row].productInfo.orderId,
              invoiceType: this.invoiceList[row].productInfo.invoiceType
            },
            false,
            2
        )
        if (response) {
          if (response.code === 0 && response.data.invoiceStatus === true) {
            this.getDataNumber = 1
            this.invoiceList[row].productInfo.invoiceCode = response.data.invoiceData.invoiceCode
            this.invoiceList[row].productInfo.invoiceNumber = response.data.invoiceData.invoiceNo
            this.invoiceList[row].productInfo.invoiceStatus = response.data.invoiceStatus
          }
        }
      }
    },
    // 清空开票页面数据
    clean() {
      this.invoiceList = []
      this.invoiceSelectList = []
      this.status = 0
    },
    // 处理发票状态
    fmtInvoiceStatus(row, column, cellValue) {
      if (cellValue === 2) {
        return '开票中'
      } else if (cellValue === 3) {
        return '正数开票成功'
      } else if (cellValue === 4) {
        return '开票失败'
      }
      return cellValue
    },
    // 打印发票
    printInvoice(row) {
      if (row.REQUEST_COMMON_FPKJ.invoiceReqFlowNo) {
        window.location.href =
            'PrintAssist://{"Type":"PrintInvoice","reqFlowNo":"' +
            row.REQUEST_COMMON_FPKJ.invoiceReqFlowNo +
            '","InvoiceType":"0","Token":"' +
            this.token +
            '"}'
      }
    },
    // 刷新开票信息
    async refreshInvoiceInfo(row) {
      if (this.token) {
        const url = '/sys/invoiceExt/getResultData'
        let response = await AxiosRequestData(
            url,
            this.token,
            {
              entTaxId: this.entTaxId,
              orderId: row.productInfo.orderId,
              invoiceType: row.productInfo.invoiceType
            },
            false,
            2
        )
        if (response) {
          console.log(123)
          console.log(response)
          if (response.code === 0 && response.data.invoiceStatus === true) {
            let index = row.productInfo.rowId
            this.invoiceList[index].productInfo.invoiceCode = row.productInfo.invoiceCode
            this.invoiceList[index].productInfo.invoiceNumber = row.productInfo.invoiceNo
            this.invoiceList[index].productInfo.invoiceStatus = row.productInfo.invoiceStatus
          } else {
            this.$message({
              showClose: true,
              message: response.msg,
              type: 'error'
            })
          }
        }
      }
    },
    // 判断多选框是否可以选中
    selectStatus(row, index) {
      if (row.productInfo.invoiceStatus === 3) {
        return false
      } else {
        return true
      }
    }
  }
}
</script>

<style scoped>
.invoice_operation {
  display: flex;
  margin-bottom: 5px;
}
</style>
