<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <el-select v-model="supplierValue" placeholder="选择供应商" filterable style="width: 220px;margin-left: 5px" size="small" clearable>
          <el-option
            v-for="item in supplierValues"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span>申请时间:</span>
        <el-date-picker v-model="date1" size="small" type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择开始日期"/>&nbsp;~
        <el-date-picker v-model="date2" size="small" type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择结束日期"/>
        <el-button type="success" size="small" icon="el-icon-search" style="margin-left:10px" @click="total1=0;pageCurrent1=1;pageSize1=100000;searchTable1()">查询</el-button>
      </div>
      <!--      <h4 style="margin: 0">发票结算申请单</h4>-->
      <el-table id="expandTable" :data="tableData1" border highlight-current-row height="250px" style="width: 100%;height: 330px;overflow:auto" size="mini" @row-click="searchTable2">
        <el-table-column label="供应商" align="center" width="180" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <el-table-column label="到票号" align="center" min-width="90">
          <template slot-scope="scope">
            {{ scope.row.发票号 }}
          </template>
        </el-table-column>
        <el-table-column label="到票金额(元)" align="center" width="110">
          <template slot-scope="scope">
            {{ scope.row.发票金额 }}
          </template>
        </el-table-column>
        <el-table-column label="申请人" align="center" min-width="90">
          <template slot-scope="scope">
            {{ scope.row.申请人姓名 ? scope.row.申请人姓名 : '未提交' }}
          </template>
        </el-table-column>
        <el-table-column label="申请时间" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.申请时间 ? scope.row.申请时间.split(' ')[0] : '—' }}
          </template>
        </el-table-column>
        <el-table-column label="部长确认" align="center" min-width="80">
          <template slot-scope="scope">
            {{ scope.row.审批1结果 }}
          </template>
        </el-table-column>
        <!--        <el-table-column label="审批时间" align="center" width="100">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.审批1时间 ? scope.row.审批1时间.split(' ')[0] : '—' }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="未过原因" align="center" width="100">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.审批1未过原因 }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="总经理确认" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.审批4结果 }}
          </template>
        </el-table-column>
        <!--        <el-table-column label="审批时间" align="center" width="100">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.审批4时间 ? scope.row.审批4时间.split(' ')[0] : '—' }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="未过原因" align="center" width="100">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.审批4未过原因 }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="财务审批" align="center" min-width="80">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.审批人3姓名 }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="审批时间" align="center" width="100">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.审批3时间 ? scope.row.审批3时间.split(' ')[0] : '—' }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="未过原因" align="center" width="100">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.审批2未过原因 }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="操作" align="center" width="130">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="cancel(scope.row)">撤回</el-button>
            <!--            <el-button-->
            <!--              size="mini"-->
            <!--              type="text"-->
            <!--              plain-->
            <!--              icon="el-icon-search"-->
            <!--              @click="viewPicture(scope.row)">查看发票</el-button>-->
            <el-button
              :disabled="scope.row.文件名称 === '' || scope.row.文件名称 === null"
              size="mini"
              type="text"
              icon="el-icon-search"
              @click="viewPicture(scope.row)">查看发票</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-row>
      <el-col style="width: 46%">
        <el-card>
          <el-table :data="tableData2" :summary-method="getSummaries" border show-summary highlight-current-row style="width: 100%;max-height: 400px;" height="300px" size="mini" @row-click="searchTable3">
            <el-table-column label="合同编号" align="center" min-width="120">
              <template slot-scope="scope">
                {{ scope.row.采购合同编号 || scope.row.离线合同编号 }}
              </template>
            </el-table-column>
            <el-table-column label="合同金额" align="center" min-width="80" prop="合同总额">
              <template slot-scope="scope">
                {{ Number(scope.row.合同总额).toFixed(2) }}
              </template>
            </el-table-column>
            <el-table-column label="到货金额" align="center" min-width="80" prop="到货总额">
              <template slot-scope="scope">
                {{ Number(scope.row.到货总额).toFixed(2) }}
              </template>
            </el-table-column>
            <el-table-column label="已付金额" align="center" min-width="80" prop="已付金额">
              <template slot-scope="scope">
                {{ Number(scope.row.已付金额).toFixed(2) }}
              </template>
            </el-table-column>
            <el-table-column label="到票金额" align="center" min-width="80" prop="到票总额">
              <template slot-scope="scope">
                {{ Number(scope.row.到票总额).toFixed(2) }}
              </template>
            </el-table-column>
            <el-table-column label="尚欠金额" align="center" min-width="80" prop="尚欠金额">
              <template slot-scope="scope">
                {{ Number(scope.row.尚欠金额).toFixed(2) }}
              </template>
            </el-table-column>
            <el-table-column label="欠票金额" align="center" min-width="80" prop="欠票总额">
              <template slot-scope="scope">
                {{ Number(scope.row.欠票总额).toFixed(2) }}
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col style="width: 54%">
        <el-card>
          <el-table :data="tableData3" :summary-method="getSummaries1" border show-summary highlight-current-row style="width: 100%;max-height: 400px;" height="300px" size="mini">
            <el-table-column label="名称" align="center" min-width="100" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.采购物料名称 || scope.row.离线物料名称 || scope.row.采购零件名称 }}
              </template>
            </el-table-column>
            <el-table-column label="图号或型号" align="center" min-width="120" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.采购物料型号 || scope.row.离线物料型号 || scope.row.采购零件图号 }}
              </template>
            </el-table-column>
            <el-table-column label="采购数" align="center" width="70" prop="采购数量">
              <template slot-scope="scope">
                {{ scope.row.采购数量 }}
              </template>
            </el-table-column>
            <el-table-column label="到货数" align="center" width="70" prop="到货数量">
              <template slot-scope="scope">
                {{ scope.row.到货数量 }}
              </template>
            </el-table-column>
            <el-table-column label="到票数" align="center" width="70" prop="到票数量">
              <template slot-scope="scope">
                {{ scope.row.到票数量 }}
              </template>
            </el-table-column>
            <el-table-column label="单价" align="center" width="60">
              <template slot-scope="scope">
                {{ scope.row.单价.toFixed(2) }}
              </template>
            </el-table-column>
            <el-table-column label="到票金额" align="center" width="80" prop="到票金额">
              <template slot-scope="scope">
                {{ scope.row.到票金额.toFixed(2) }}
              </template>
            </el-table-column>
            <el-table-column label="到货金额" align="center" width="80" prop="到货金额">
              <template slot-scope="scope">
                {{ scope.row.到货金额.toFixed(2) }}
              </template>
            </el-table-column>
            <el-table-column label="规格" align="center" min-width="120" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.采购物料规格 || scope.row.离线物料规格 || scope.row.采购零件规格 }}
              </template>
            </el-table-column>
            <el-table-column label="机床图号" align="center" min-width="80" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.机床图号 }}
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
    <!--    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible4" title="图片" center style="" width="800px">-->
    <!--      <viewer>-->
    <!--        <img v-for="url in urls" :key="url" :src="url" lazy style="width: 700px;height: 700px">-->
    <!--      </viewer>-->
    <!--    </el-dialog>-->
    <!--    <el-card>-->
    <!--      <el-table :data="tableData2" border highlight-current-row style="width: 100%;max-height: 300px;" height="300px" size="mini">-->
    <!--        <el-table-column label="名称" align="center" min-width="100">-->
    <!--          <template slot-scope="scope">-->
    <!--            {{ scope.row.采购物料名称 || scope.row.离线物料名称 || scope.row.采购零件名称 }}-->
    <!--          </template>-->
    <!--        </el-table-column>-->
    <!--        <el-table-column label="图号或型号" align="center" min-width="100">-->
    <!--          <template slot-scope="scope">-->
    <!--            {{ scope.row.采购物料型号 || scope.row.离线物料型号 || scope.row.采购零件图号 }}-->
    <!--          </template>-->
    <!--        </el-table-column>-->
    <!--        <el-table-column label="规格" align="center" min-width="150">-->
    <!--          <template slot-scope="scope">-->
    <!--            {{ scope.row.采购物料规格 || scope.row.离线物料规格 || scope.row.采购零件规格 }}-->
    <!--          </template>-->
    <!--        </el-table-column>-->
    <!--        <el-table-column label="机床图号" align="center" min-width="100">-->
    <!--          <template slot-scope="scope">-->
    <!--            {{ scope.row.机床图号 }}-->
    <!--          </template>-->
    <!--        </el-table-column>-->
    <!--        &lt;!&ndash;<el-table-column label="供应商" align="center" min-width="150">&ndash;&gt;-->
    <!--        &lt;!&ndash;<template slot-scope="scope">&ndash;&gt;-->
    <!--        &lt;!&ndash;{{ scope.row.供应商名称 }}&ndash;&gt;-->
    <!--        &lt;!&ndash;</template>&ndash;&gt;-->
    <!--        &lt;!&ndash;</el-table-column>&ndash;&gt;-->
    <!--        <el-table-column label="合同编号" align="center" min-width="100">-->
    <!--          <template slot-scope="scope">-->
    <!--            {{ scope.row.采购合同编号 || scope.row.离线合同编号 }}-->
    <!--          </template>-->
    <!--        </el-table-column>-->
    <!--        &lt;!&ndash;<el-table-column label="合同名称" align="center" min-width="100">&ndash;&gt;-->
    <!--        &lt;!&ndash;<template slot-scope="scope">&ndash;&gt;-->
    <!--        &lt;!&ndash;{{ scope.row.采购合同名称 || scope.row.离线合同名称 }}&ndash;&gt;-->
    <!--        &lt;!&ndash;</template>&ndash;&gt;-->
    <!--        &lt;!&ndash;</el-table-column>&ndash;&gt;-->
    <!--        <el-table-column label="采购数量" align="center" width="100">-->
    <!--          <template slot-scope="scope">-->
    <!--            {{ scope.row.采购数量 }}-->
    <!--          </template>-->
    <!--        </el-table-column>-->
    <!--        <el-table-column label="到票数量" align="center" width="100">-->
    <!--          <template slot-scope="scope">-->
    <!--            {{ scope.row.到票数量 }}-->
    <!--          </template>-->
    <!--        </el-table-column>-->
    <!--        <el-table-column label="到票金额" align="center" width="100">-->
    <!--          <template slot-scope="scope">-->
    <!--            {{ Number(scope.row.金额).toFixed(2) }}-->
    <!--          </template>-->
    <!--        </el-table-column>-->
    <!--      </el-table>-->
    <!--      <span style="margin-left: 1000px">到票总额：{{ totalMoney.toFixed(2) }}</span>-->
    <!--    </el-card>-->
  </div>
</template>

<script>
import { cancel, searchTable1, searchSupplier, searchTable2, searchTable3 } from '@/api/PurchasingCenter/InvoiceSettlementApproveSearch'
import { mapGetters } from 'vuex'
// import { readFile } from '@/utils/request'
// import request from '@/utils/request'
export default {
  name: '', // 发票结算审批
  data() {
    return {
      date1: '',
      date2: '',
      tableData1: [],
      total1: 0,
      pageCurrent1: 1,
      pageSize1: 10,
      invoiceNumber: '',
      tableData2: [],
      tableData3: [],
      supplierValues: [],
      supplierValue: '',
      totalMoney: 0
      // dialogFormVisible4: false,
      // urls: []
    }
  },
  computed: {
    ...mapGetters([
      'token',
      'sidebar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
    this.searchTable1()
    this.searchSupplier()
  },
  methods: {
    // viewPicture(row) {
    //   this.dialogFormVisible4 = true
    //   this.urls = []
    //   searchPic(row.发票结算申请单流水号).then(response => {
    //     for (let i = 0; i < response.data.length; i++) {
    //       if (response.data[i].文件内容 !== null) {
    //         this.urls.push('data:image/png;base64,' + response.data[i].文件内容)
    //       }
    //     }
    //   })
    // },
    viewPicture(row) {
      var param = []
      param[0] = ['发票结算申请单流水号', row.发票结算申请单流水号]
      var Data = this.CreateData('2004', '采购管理_发票附件_导出', param)
      this.exportExcel_NPOI(Data)
    },
    searchTable3(row) {
      this.tableData3 = []
      searchTable3(row.发票结算申请单流水号, row.采购合同流水号, row.离线合同流水号).then(response => {
        this.tableData3 = response.data
      })
    },
    searchTable2(row) {
      this.tableData3 = []
      this.monetaryTotal = 0
      this.applyOrderNum = row.发票结算申请单流水号
      this.invoiceNumber = row.发票号
      searchTable2(row.发票结算申请单流水号).then(response => {
        this.tableData2 = response.data
        if (response.data.length !== 0) {
          for (let i = 0; i < response.data.length; i++) {
            this.monetaryTotal += response.data[i].金额
          }
        }
      })
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总价'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = sums[index].toFixed(2) + ' 元'
        } else {
          sums[index] = 'N/A'
        }
      })
      return sums
    },
    getSummaries1(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        } else if (index === 6 || index === 7) {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index].toFixed(2) + '元'
          } else {
            sums[index] = 'N/A'
          }
        } else if (index === 2 || index === 3 || index === 4) {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = sums[index]
          } else {
            sums[index] = 'N/A'
          }
        }
      })
      return sums
    },
    // searchTable2(row) {
    //   this.invoiceNumber = row.发票号
    //   searchTable2(row.发票结算申请单流水号).then(response => {
    //     this.tableData2 = response.data
    //     this.totalMoney = 0
    //     for (let i = 0; i < response.data.length; i++) {
    //       this.totalMoney += parseFloat(response.data[i].金额)
    //     }
    //   })
    // },
    cancel(row) {
      console.log(row, 'row')
      if (Number(this.token) === 18) {
        cancel(row.发票结算申请单流水号, this.token).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('撤回成功')
            this.searchTable1()
          } else {
            this.$message.error('撤回失败')
          }
        })
      } else if (Number(this.token) === 4) {
        if (row.审批3流水号 === '3') {
          cancel(row.发票结算申请单流水号, this.token).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('撤回成功')
              this.searchTable1()
            } else {
              this.$message.error('撤回失败')
            }
          })
        } else {
          this.$message.error('已进行财务审批，无法撤回！')
        }
      } else if (Number(this.token) === 2) {
        if (row.审批4流水号 === '3') {
          cancel(row.发票结算申请单流水号, this.token).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('撤回成功')
              this.searchTable1()
            } else {
              this.$message.error('撤回失败')
            }
          })
        } else {
          this.$message.error('总经理已审批，无法撤回！')
        }
      }
    },
    fetchData() {
      console.log(this.token, 'token')
    },
    searchSupplier() { // 查询创立日期和注册资本大于符合条件的数据
      this.supplierName ? this.supplierCheck1 = 1 : (this.supplierCheck1 = 0, this.supplierName = '')
      this.enterpriseNature ? this.supplierCheck2 = 1 : (this.supplierCheck2 = 0, this.enterpriseNature = '')
      this.createDate ? this.supplierCheck3 = 1 : (this.supplierCheck3 = 0, this.createDate = '')
      this.registeredCapital ? this.supplierCheck4 = 1 : (this.supplierCheck4 = 0, this.registeredCapital = '')
      this.taxNum ? this.supplierCheck5 = 1 : (this.supplierCheck5 = 0, this.taxNum = '')
      this.EMail ? this.supplierCheck6 = 1 : (this.supplierCheck6 = 0, this.EMail = '')
      this.address ? this.supplierCheck7 = 1 : (this.supplierCheck7 = 0, this.address = '')
      this.account ? this.supplierCheck8 = 1 : (this.supplierCheck8 = 0, this.account = '')
      this.openingBank ? this.supplierCheck9 = 1 : (this.supplierCheck9 = 0, this.openingBank = '')
      this.phone ? this.supplierCheck10 = 1 : (this.supplierCheck10 = 0, this.phone = '')
      this.fax ? this.supplierCheck11 = 1 : (this.supplierCheck11 = 0, this.fax = '')
      this.goodTime ? this.supplierCheck12 = 1 : (this.supplierCheck12 = 0, this.goodTime = '')
      searchSupplier(this.supplierCheck1, this.supplierName, this.supplierCheck2, this.enterpriseNature, this.supplierCheck3, this.createDate, this.supplierCheck4, this.registeredCapital, this.supplierCheck5, this.taxNum, this.supplierCheck6, this.EMail, this.supplierCheck7, this.address, this.supplierCheck8, this.account, this.supplierCheck9, this.openingBank, this.supplierCheck10, this.phone, this.supplierCheck11, this.fax, this.supplierCheck12, this.goodTime).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          if (response.data[i].创立日期 !== '' && response.data[i].创立日期 !== null) {
            response.data[i].创立日期 = response.data[i].创立日期.split(' ')[0]
          }
        }
        for (let i = 0; i < response.data.length; i++) {
          this.supplierValues.push({
            label: response.data[i].供应商名称,
            value: response.data[i].供应商流水号
          })
        }
      })
    },
    searchTable1() {
      let check
      this.date1 && this.date2 ? this.check1 = 1 : (this.check1 = 0, this.date1 = '', this.date2 = '')
      this.supplierValue ? check = 1 : check = 0
      searchTable1(this.check1, this.date1, this.date2, this.pageCurrent1, this.pageSize1, this.token, check, this.supplierValue).then(response => {
        this.tableData1 = response.data.rows
        // this.tableData1.map(v => {
        //   this.$set(v, 'scanSrc', '')
        //   return v
        // })
        // for (let i = 0; i < response.data.rows.length; i++) {
        //   this.tableData1[i].scanSrc = this.searchPic(response.data.rows[i], i)
        // }
        this.total1 = response.data.total
      })
    },
    // async searchPic(row, i) {
    //   const { data } = await request({
    //     url: '',
    //     method: 'post',
    //     data: {
    //       type: '3',
    //       name: `select * from 采购管理_采购发票_附件 where 是否启用 = 1 and 发票交接流水号 = ${row.发票结算申请单流水号}`
    //     }
    //   })
    //   // this.tableData1[i].scanSrc = readFile + data[0].文件标识 + '.' + data[0].文件后缀
    // },
    handleSizeChange1(val) {
      this.pageCurrent1 = 1
      this.pageSize1 = val
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    }
  }
}
</script>

<style scoped>
  .el-card{
    margin-bottom: 15px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    margin: 10px 0 10px 10px;
  }
  #expandTable .el-form-item{
    margin: 0;
  }
  .el-card{
    margin: 0px;
  }
</style>
