<template>
  <div class="app-container">
    <el-card>
      <span>合同号:</span>
      <el-select v-model="contractNumValue" placeholder="合同号" size="small" filterable clearable>
        <el-option
          v-for="item in contractNum"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span>供应商:</span>
      <el-input
        v-model="supplierName"
        placeholder="供应商"
        size="small"
        clearable
        style="width:180px"/>
      <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 :data="tableData1" size="mini" border style="width: 100%" highlight-current-row @row-click="searchContract">
        <el-table-column label="采购合同编号" min-width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.采购合同编号 }}
          </template>
        </el-table-column>
        <el-table-column label="采购合同名称" min-width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.采购合同名称 }}
          </template>
        </el-table-column>
        <el-table-column label="供应商" min-width="150" align="center">
          <template slot-scope="scope">
            {{ scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <el-table-column label="合同总额" width="70" align="center">
          <template slot-scope="scope">
            {{ scope.row.合同总额 }}
          </template>
        </el-table-column>
        <el-table-column label="本次请款金额" min-width="100" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.本次应请款" size="mini" style="width:70px"/>
          </template>
        </el-table-column>
        <el-table-column label="请款" min-width="80" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon=""
              @click="requestFund(scope.row)">请款</el-button>
          </template>
        </el-table-column>
        <el-table-column label="请款付款情况" width="710" align="center">
          <template slot-scope="scope">
            <el-table :id="scope.$index" :data="tableData11[scope.$index]" size="mini" border style="width: 100%;">
              <el-table-column label="取消请款" width="100" align="center">
                <template slot-scope="scope">
                  <el-button
                    :disabled="scope.row.审批情况流水号!==3"
                    type="danger"
                    size="mini"
                    @click="cancelRequestFund(scope.row)">取消请款</el-button>
                </template>
              </el-table-column>
              <el-table-column label="次数" min-width="60" align="center" type="index"/>
              <el-table-column label="请款金额" width="70" align="center">
                <template slot-scope="scope">
                  {{ scope.row.请款金额 }}
                </template>
              </el-table-column>
              <el-table-column label="请款时间" width="90" align="center">
                <template slot-scope="scope">
                  {{ scope.row.请款时间 ? scope.row.请款时间.split(' ')[0] : '' }}
                </template>
              </el-table-column>
              <el-table-column label="审批情况" width="80" align="center">
                <template slot-scope="scope">
                  {{ scope.row.审批情况内容 }}
                </template>
              </el-table-column>
              <el-table-column label="未通过审批原因" width="110" align="center">
                <template slot-scope="scope">
                  {{ scope.row.未通过审批原因 }}
                </template>
              </el-table-column>
              <el-table-column label="付款情况" width="80" align="center">
                <template slot-scope="scope">
                  {{ scope.row.付款情况内容 }}
                </template>
              </el-table-column>
              <el-table-column label="未付款原因" width="110" align="center">
                <template slot-scope="scope">
                  {{ scope.row.未付款原因 }}
                </template>
              </el-table-column>
            </el-table>
          </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>
    <div style="min-height:500px;margin: 0px auto;width:800px;border:1px solid black" v-html="returns"/>
  </div>
</template>

<script>
import { initContract, searchTable1, searchCondition, requestFund, cancelRequestFund, searchContract } from '@/api/PurchasingCenter/PurchaseContractRequestFund'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      tableData11: [],
      total1: 0,
      pageCurrent1: 1,
      pageSize1: 10,
      contractNum: [],
      contractNumValue: '',
      supplierName: '',
      tableData1: [],
      check1: 0,
      check2: 0,
      returns: ''
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id'
    ])
  },
  created() {
    this.fetchData()
    this.searchTable1()
  },
  methods: {
    fetchData() {
      initContract().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.contractNum.push({
            label: response.data[i].采购合同编号,
            value: response.data[i].采购合同流水号
          })
        }
      })
    },
    searchTable1() { // 查询
      this.tableData1 = []
      this.returns = ''
      this.contractNumValue ? this.check1 = 1 : this.check1 = 0
      this.supplierName ? this.check2 = 1 : this.check2 = 0
      searchTable1(this.pageCurrent1, this.pageSize1, this.check1, this.contractNumValue, this.check2, this.supplierName).then(response => {
        for (let j = 0; j < response.data.rows.length; j++) {
          this.tableData11[j] = []
          searchCondition(response.data.rows[j].采购合同流水号).then(response0 => {
            if (response0.data.length === 0) {
              this.tableData11[j].push(response0.data)
              document.getElementById(j).setAttribute('style', 'display:none')
            } else {
              for (let i = 0; i < response0.data.length; i++) {
                this.tableData11[j].push(response0.data[i])
              }
            }
          })
        }
        this.tableData1 = response.data.rows
        this.tableData1.map(v => {
          this.$set(v, '本次应请款', 0)
          return v
        })
        this.total1 = response.data.total
      })
    },
    handleSizeChange1(val) {
      this.pageCurrent1 = 1
      this.pageSize1 = val
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    },
    requestFund(row) { // 请款
      if (row.本次应请款 <= 0) {
        this.$message.warning('请输入正确金额')
      } else {
        const day1 = new Date()
        const day2 = day1.getFullYear() + '-' + (day1.getMonth() + 1) + '-' + day1.getDate()
        requestFund(row.采购合同流水号, row.本次应请款, day2, this.id).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('请款成功')
            this.searchTable1()
          } else { this.$message.error('请款失败') }
        })
      }
    },
    cancelRequestFund(row) { // 取消请款
      this.$confirm('确认取消请款吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cancelRequestFund(row.采购请款流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('操作成功')
            this.searchTable1()
          } else { this.$message.error('操作失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    searchContract(row) {
      searchContract(row.采购合同流水号).then(response => {
        this.returns = response.data[0].合同内容
      })
    }
  }
}
</script>

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