<template>
  <div id="invoice_copy">
    <div class="search">
      <el-input
          v-model="searchData.customerName"
          clearable
          prefix-icon="el-icon-search"
          placeholder="请输入客户名称"
          class="search_input">
      </el-input>
      <el-input
          v-model="searchData.customerEntTaxId"
          clearable
          prefix-icon="el-icon-search"
          placeholder="请输入企业税号"
          class="search_input">
      </el-input>
      <el-input
          v-model="searchData.invoiceCode"
          clearable
          prefix-icon="el-icon-search"
          placeholder="请输入发票代码"
          class="search_input">
      </el-input>
      <el-input
          v-model="searchData.invoiceNumber"
          clearable
          prefix-icon="el-icon-search"
          placeholder="请输入发票号码"
          class="search_input">
      </el-input>
      <el-date-picker
          v-model="searchData.invoiceDate"
          type="datetimerange"
          align="right"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
      <div style="margin-left: 5px">
        <el-button @click="getInvoiceList" type="primary" icon="el-icon-search">搜索</el-button>
        <el-button class="invoice_button" type="primary" v-on:click="searchClean">重 置</el-button>
      </div>
    </div>
    <div>
      <el-container>
        <el-table :data="invoiceList" border style="width: 100%;flex: 1" height="350px" highlight-current-row
                  ref="table" @row-dblclick="selectInvoiceData" @row-click="getInvoiceData">
          <el-table-column prop="invoiceDate" label="开票日期" width="90"></el-table-column>
          <el-table-column prop="invoiceCode" label="发票代码" width="90"></el-table-column>
          <el-table-column prop="invoiceNumber" label="发票号码" width="90"></el-table-column>
          <el-table-column prop="customerName" label="客户名称" width="160"></el-table-column>
          <el-table-column prop="customerEntTaxId" label="客户企业税号" width="160"></el-table-column>
          <el-table-column prop="invoicePerson" label="开票人" width="100"></el-table-column>
          <el-table-column prop="totalAmount" label="合计金额" width="90"></el-table-column>
          <el-table-column prop="totalTaxes" label="合计税额" width="90"></el-table-column>
          <el-table-column prop="totalAmountTaxes" label="价税合计" width="90"></el-table-column>
          <el-table-column prop="belongEntName" label="开发票的企业名称" width="160"></el-table-column>
          <el-table-column prop="belongEntTaxId" label="开发票的企业税号" width="160"></el-table-column>
        </el-table>
      </el-container>
    </div>
    <div>
      <el-row type="flex" :gutter="20">
        <el-col :span="12"></el-col>
        <el-col :span="12">
          <div style="text-align:right">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNum"
                :page-sizes="pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next"
                :total="totalNumber"
            >
            </el-pagination>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>

</template>

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

export default {
  name: 'invoice_copy',
  data() {
    return {
      token: '',
      account: '',
      pageNum: 1,
      pageSize: 10,
      totalNumber: 0,
      totalPage: 0,
      pageSizes: [10, 30, 50, 100],
      invoiceList: [],
      searchData: {
        customerName: '',
        customerEntTaxId: '',
        invoiceCode: '',
        invoiceNumber: '',
        invoiceDate: ''
      },
      belongEntTaxId: '',
      invoiceType: '',
      orderId: '',
      searchInvoiceType: '',
      entTaxId: ''
    }
  },
  created() {
    this.setInvoiceCopyInfo()
  },
  methods: {
    // 变更每页显示的数据条数
    handleSizeChange(val) {
      this.pageSize = val
      this.getInvoiceList()
    },
    // 翻页
    handleCurrentChange(val) {
      this.pageNum = val
      this.getInvoiceList()
    },
    // 获取开票列表信息
    async getInvoiceList() {
      let invoiceBeginDate = ''
      let invoiceEndDate = ''
      if (this.searchData.invoiceDate.length > 0) {
        invoiceBeginDate = this.searchData.invoiceDate[0]
        invoiceEndDate = this.searchData.invoiceDate[1]
      }
      if (this.account && this.token) {
        const url = '/sys/invoice/findPage'
        let response = await AxiosRequestData(url, this.token, {
          status: 3,
          belongEntTaxId: this.entTaxId,
          invoiceCode: this.searchData.invoiceCode,
          invoiceNumber: this.searchData.invoiceNumber,
          invoiceDateBegin: invoiceBeginDate,
          invoiceDateEnd: invoiceEndDate,
          customerName: this.searchData.customerName,
          customerEntTaxId: this.searchData.customerEntTaxId,
          invoiceCategory: this.searchInvoiceType,
          mobile: this.account,
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          reqChannel: 4
        }, false, 2)
        if (response) {
          if (response.code === 0) {
            this.totalNumber = response.data.total
            this.totalPage = response.data.pages
            this.invoiceList = response.data.records
          }
        }
      }
    },
    // 清空查询条件
    searchClean() {
      this.searchData.customerName = ''
      this.searchData.customerEntTaxId = ''
      this.searchData.invoiceCode = ''
      this.searchData.invoiceNumber = ''
      this.searchData.invoiceDate = ''
      this.getInvoiceList()
    },
    // 双击选择发票信息
    selectInvoiceData(row, event, column) {
      this.getInvoiceInfo(row.orderId, row.invoiceType, row.belongEntTaxId)
    },
    // 单击选择发票信息
    getInvoiceData(row, event, column) {
      this.orderId = row.orderId
      this.belongEntTaxId = row.belongEntTaxId
      this.invoiceType = row.invoiceType
    },
    // 单选确认发票信息
    confirmInvoiceInfo() {
      this.getInvoiceInfo(this.orderId, this.invoiceType, this.belongEntTaxId)
    },
    // 获取发票信息
    async getInvoiceInfo(orderId, invoiceType, belongEntTaxId) {
      if (this.token) {
        const url = '/sys/invoiceExt/getResultData'
        let response = await AxiosRequestData(
            url,
            this.token,
            {
              entTaxId: belongEntTaxId,
              orderId: orderId,
              invoiceType: invoiceType
            },
            false,
            2
        )
        if (response) {
          if (response.code === 0 && response.data.invoiceStatus === true) {
            await this.$attrs.invoiceSpecial.getInvoice(response.data.invoiceData)
          }
        }
      }
    },
    // 重置信息
    resetInfo() {
      this.pageNum = 1
      this.pageSize = 10
      this.pageSizes = [10, 30, 50, 100]
      this.invoiceReqFlowNo = ''
      this.invoiceType = ''
    },
    // 设置开票信息
    setInvoiceCopyInfo() {
      this.searchInvoiceType = this.$attrs.invoiceSpecial.searchInvoiceType
      let user = JSON.parse(cache.getSession('user'))
      let company = JSON.parse(cache.getSession('company'))
      this.entTaxId = company.entTaxId
      this.account = user.account
      this.token = cache.getToken()
      this.getInvoiceList()
    }
  }
}
</script>

<style scoped>
.search {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 5px;
}

.search_input {
  width: 250px;
  margin: 5px;
}
</style>
