<template>
  <div class="app-container">

    <el-card>
      <el-table v-loading="" :data="tableData" border style="width: 100%;max-height: 500px;" height="600px" size="mini">
        <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.采购合同名称 }}
          </template>
        </el-table-column>
        <el-table-column label="规定到货时间" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.规定到货时间.split(' ')[0] }}
          </template>
        </el-table-column>
        <el-table-column label="供应商" align="center" min-width="250">
          <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.预付款 }}
          </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="70">
          <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="70">
          <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="200">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-check" @click="yesApproval(scope.row)">通过</el-button>
            <el-button type="danger" size="mini" icon="el-icon-close" @click="noApproval(scope.row)">不通过</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          :total="total"
          style="display:inline-block;width:50%"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>

  </div>
</template>

<script>
import { execApproval, initApproval1 } from '@/api/WorkshopProcurement/FundApproval'
import { mapGetters } from 'vuex'

export default {
  name: '', // 财务审核
  data() {
    return {
      tableData: [],
      pageSize: 10,
      pageCurrent: 1,
      total: 0,
      gridData: []
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.searchTable()
    },
    searchTable() {
      initApproval1(this.pageCurrent, this.pageSize, 1).then(response => {
        this.tableData = response.data.rows
        this.total = response.data.total
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageCurrent = 1
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    },
    yesApproval(row) { // 通过审核
      this.$confirm('确定审核通过?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        execApproval(row.采购科请款流水号, 1, '通过', this.id, 1).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('执行成功')
            this.searchTable()
          } else { this.$message.error('执行失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    noApproval(row) { // 不通过审核
      this.$prompt('请输入不通过原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        execApproval(row.采购科请款流水号, 2, value, this.id, 1).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('执行成功')
            this.searchTable()
          } else { this.$message.error('执行失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    }
  }
}
</script>

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