<template>
  <div class="app-container">
    <el-card>
      <span>采购合同号:</span>
      <el-select v-model="contractNum" placeholder="合同号" size="small" filterable clearable>
        <el-option
          v-for="item in contractNums"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          <div style="float: left">{{ item.label }}</div>
          <div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>
        </el-option>
      </el-select>
      <span>供应商:</span>
      <el-input v-model="supplierName" placeholder="供应商" size="small" clearable/>
      <el-button
        type="success"
        size="small"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="total1=0;pageCurrent1=1;pageSize1=10;searchTable1()">查询</el-button>
    </el-card>

    <el-card>
      <el-table v-loading="" :data="tableData1" size="mini" style="max-height: 300px" height="300px" stripe border @row-click="searchTable2">
        <el-table-column align="center" label="采购合同编号" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.采购合同编号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="采购合同名称" min-width="150px">
          <template slot-scope="scope">
            {{ scope.row.采购合同名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="供应商" min-width="200px">
          <template slot-scope="scope">
            {{ scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="合同总金额" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.合同总金额 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="到票总金额" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.到票总金额 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="扣款总金额" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.扣款总金额 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="到票情况" min-width="200px">
          <template slot-scope="scope">
            <el-progress :text-inside="true" :stroke-width="18" :percentage="(parseInt(10000*(scope.row.到票总金额)/scope.row.剩余总金额)) / 100" status="success"/>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent1"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize1"
          :total="total1"
          style="display:inline-block;width:50%"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"/>
      </div>
      <el-table v-loading="" :data="tableData2" size="mini" style="max-height: 200px;margin-top: 10px" height="200px" stripe border>
        <el-table-column align="center" label="发票号" min-width="120px">
          <template slot-scope="scope">
            {{ scope.row.发票号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="开票时间" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.开票时间 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="开票金额" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.开票金额 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="扣除金额" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.扣除金额 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="传送人" min-width="80px">
          <template slot-scope="scope">
            {{ scope.row.传送人姓名 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="接收人" min-width="80px">
          <template slot-scope="scope">
            {{ scope.row.接收人姓名 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="采购合同号" min-width="120px">
          <template slot-scope="scope">
            {{ scope.row.采购合同编号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注" min-width="200px">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { searchContractInvoice, searchInvoice, initPerson, initContract } from '@/api/WorkshopProcurement/InvoiceStateSearch'
import { mapGetters } from 'vuex'

export default {
  name: '', // 到票情况查询
  data() {
    return {
      contractNumValue: '',
      contractNums: [],
      supplierName: '',
      invoiceNum: '',
      invoiceTime: '',
      transmitValue: '',
      selectPerson: [], // 传送人接收人
      recipientValue: '',
      contractNum: '',
      pageCurrent1: 1,
      pageSize1: 10,
      total1: 0,
      tableData1: [],
      check1: 0,
      check2: 0,
      check3: 0,
      check4: 0,
      check5: 0,
      tableData2: []
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      initContract().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.contractNums.push({
            label: response.data[i].采购合同编号,
            name: response.data[i].采购合同名称,
            value: response.data[i].采购合同流水号
          })
        }
      })
      initPerson().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.selectPerson.push({
            label: response.data[i].姓名,
            value: response.data[i].人员编号
          })
        }
      })
    },
    searchTable1() {
      this.tableData2 = []
      this.contractNum ? this.check1 = 1 : this.check1 = 0
      this.supplierName ? this.check2 = 1 : this.check2 = 0
      searchContractInvoice(this.pageCurrent1, this.pageSize1, this.check1, this.contractNum, this.check2, this.supplierName).then(response => {
        for (let j = 0; j < response.data.rows.length; j++) {
          response.data.rows[j].合同总金额 = parseInt(response.data.rows[j].合同总金额 * 100) / 100
        }
        this.tableData1 = response.data.rows
        this.total1 = response.data.total
      })
    },
    handleSizeChange1(val) {
      this.pageCurrent1 = 1
      this.pageSize1 = val
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    },
    searchTable2(row) {
      searchInvoice(row.采购合同流水号).then(response => {
        for (let j = 0; j < response.data.length; j++) {
          if (response.data[j].开票时间 !== null) {
            response.data[j].开票时间 = response.data[j].开票时间.substring(0, response.data[j].开票时间.indexOf(' '))
          }
          if (response.data[j].开票时间 === '1900/1/1') {
            response.data[j].开票时间 = ''
          }
        }
        this.tableData2 = response.data
      })
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  .el-date-editor{
    width:150px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .searchForm .el-form-item{
    margin-top: 1px;
    margin-bottom: 1px;
  }
</style>
