<template>
  <div class="app-container">
    <el-card>
      <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=10;searchTable1()">查询</el-button>
    </el-card>

    <el-card>
      <h4 style="margin-top: 5px">发票结算申请单</h4>
      <el-table id="expandTable" :data="tableData1" border highlight-current-row style="width: 100%;height: 330px;overflow:auto" size="mini" @row-click="searchTable2">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-row>
                <el-col :span="6">
                  <viewer>
                    <img :src="scope.row.scanSrc" alt="发票扫描件" height="120px">
                  </viewer>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="已付款项">
                    <span>{{ scope.row.已付款项 }}</span>
                  </el-form-item><br>
                  <el-form-item label="尚欠金额">
                    <span>{{ scope.row.尚欠金额 }}</span>
                  </el-form-item><br>
                  <el-form-item label="备注">
                    <span>{{ scope.row.备注 }}</span>
                  </el-form-item><br>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="是否费用类发票">
                    <span>{{ scope.row.是否费用类 }}</span>
                  </el-form-item><br>
                  <el-form-item label="税额">
                    <span>{{ scope.row.税额 }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="供应商" align="center" min-width="200">
          <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" min-width="70">
          <template slot-scope="scope">
            {{ scope.row.发票金额 }}
          </template>
        </el-table-column>
        <el-table-column label="申请人" align="center" min-width="60">
          <template slot-scope="scope">
            {{ scope.row.申请人姓名 ? scope.row.申请人姓名 : '未提交' }}
          </template>
        </el-table-column>
        <el-table-column label="申请时间" align="center" min-width="110">
          <template slot-scope="scope">
            {{ scope.row.申请时间 ? scope.row.申请时间 : '—' }}
          </template>
        </el-table-column>
        <el-table-column label="采购审批" align="center" min-width="60">
          <template slot-scope="scope">
            {{ scope.row.审批人1姓名 ? scope.row.审批人1姓名 : '未审批' }}
          </template>
        </el-table-column>
        <el-table-column label="审批时间" align="center" min-width="110">
          <template slot-scope="scope">
            {{ scope.row.审批1时间 ? scope.row.审批1时间 : '—' }}
          </template>
        </el-table-column>
        <el-table-column label="财务审批" align="center" min-width="60">
          <template slot-scope="scope">
            {{ scope.row.审批人2姓名 ? scope.row.审批人2姓名 : '未审批' }}
          </template>
        </el-table-column>
        <el-table-column label="审批时间" align="center" min-width="110">
          <template slot-scope="scope">
            {{ scope.row.审批2时间 ? scope.row.审批2时间 : '—' }}
          </template>
        </el-table-column>
        <el-table-column label="总经理审批" align="center" min-width="70">
          <template slot-scope="scope">
            {{ scope.row.审批人3姓名 ? scope.row.审批人3姓名 : '未审批' }}
          </template>
        </el-table-column>
        <el-table-column label="审批时间" align="center" min-width="110">
          <template slot-scope="scope">
            {{ scope.row.审批3时间 ? scope.row.审批3时间 : '—' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button :disabled="false" type="primary" size="mini" @click="excuteApprove(scope.row)">审批</el-button>
          </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-card>

    <el-card>
      <h4 style="margin-top: 5px;display: inline-block;width: 150px;">发票结算申请单明细</h4>
      <span>发票号：</span>
      <el-input v-model="invoiceNumber" size="small" placeholder="请选择发票结算申请单" readonly/>
      <el-table :data="tableData2" border highlight-current-row style="width: 100%;max-height: 250px;" height="250px" 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="150">
          <template slot-scope="scope">
            {{ scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <el-table-column label="合同编号" align="center" min-width="100">
          <template slot-scope="scope">
            {{ 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.离线合同名称 }}
          </template>
        </el-table-column>
        <el-table-column label="到票数量" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.到票数量 }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { searchTable1, searchTable2, excuteApprove } from '@/api/WorkshopProcurement/InvoiceSettlementApprove1'
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: []
    }
  },
  computed: {
    ...mapGetters([
      'token',
      'sidebar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
    this.searchTable1()
  },
  methods: {
    fetchData() {
      console.log(this.token)
    },
    searchTable1() {
      this.date1 && this.date2 ? this.check1 = 1 : (this.check1 = 0, this.date1 = '', this.date2 = '')
      searchTable1(this.check1, this.date1, this.date2, this.pageCurrent1, this.pageSize1).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()
    },
    excuteApprove(row) {
      this.$confirm('确认审批签字?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        excuteApprove(row.发票结算申请单流水号, this.id, this.token).then(response => {
          if (response.data[0].result === '审批成功') {
            this.$message.success('审批成功')
            this.searchTable1()
          } else if (response.data[0].result === '已完成审批') {
            this.$message.warning('已完成审批，不可重复审批')
          } else { this.$message.error('审批失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消审批'
        })
      })
    },
    searchTable2(row) {
      this.invoiceNumber = row.发票号
      searchTable2(row.发票结算申请单流水号).then(response => {
        this.tableData2 = response.data
      })
    }
  }
}
</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;
  }
</style>
