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

    <el-card>
      <el-table v-loading="" :data="tableData" border style="width: 100%;max-height: 500px;" height="300px" size="mini">
        <el-table-column label="供应商" align="center" min-width="170">
          <template slot-scope="scope">
            {{ 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.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="请款金额" align="center" min-width="100" prop="请款金额">
          <template slot-scope="scope">
            {{ scope.row.请款金额 }}
          </template>
        </el-table-column>
        <el-table-column label="审核" align="center" width="320">
          <template slot-scope="scope">
            <el-popover placement="right" width="400" trigger="click">
              <el-table :data="gridData2" stripe size="mini" show-summary>
                <el-table-column width="50" align="center" type="index" label="序号"/>
                <el-table-column min-width="150" align="center" label="采购合同编号">
                  <template slot-scope="scope">
                    {{ scope.row.采购合同编号 }}
                  </template>
                </el-table-column>
                <el-table-column min-width="100" align="center" label="合同总额" prop="请款金额">
                  <template slot-scope="scope">
                    {{ scope.row.请款金额 }}
                  </template>
                </el-table-column>
              </el-table>
              <el-button slot="reference" type="success" size="mini" icon="el-icon-search" style="margin-right: 10px" @click="searchTable01(scope.row)">查看详情</el-button>
            </el-popover>
            <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>
    <el-card>
      <el-table v-loading="" :data="tableData2" border style="width: 100%;max-height: 300px;" height="300px" size="mini">
        <el-table-column label="供应商" align="center" min-width="170">
          <template slot-scope="scope">
            {{ 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.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="请款金额" align="center" min-width="100" prop="请款金额">
          <template slot-scope="scope">
            {{ scope.row.请款金额 }}
          </template>
        </el-table-column>
        <el-table-column label="审核" align="center" width="320">
          <template slot-scope="scope">
            <el-popover placement="right" width="400" trigger="click">
              <el-table :data="gridData" stripe size="mini" show-summary>
                <el-table-column width="50" align="center" type="index" label="序号"/>
                <el-table-column min-width="150" align="center" label="离线合同编号">
                  <template slot-scope="scope">
                    {{ scope.row.离线合同编号 }}
                  </template>
                </el-table-column>
                <el-table-column min-width="100" align="center" label="合同总额" prop="请款金额">
                  <template slot-scope="scope">
                    {{ scope.row.请款金额 }}
                  </template>
                </el-table-column>
              </el-table>
              <el-button slot="reference" type="success" size="mini" icon="el-icon-search" style="margin-right: 10px" @click="searchTable02(scope.row)">查看详情</el-button>
            </el-popover>
            <el-button type="primary" size="mini" icon="el-icon-check" @click="yesApprovalOffline(scope.row)">通过</el-button>
            <el-button type="danger" size="mini" icon="el-icon-close" @click="noApprovalOffline(scope.row)">不通过</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent2"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize2"
          :total="total2"
          style="display:inline-block;width:50%"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange2"
          @current-change="handleCurrentChange2"/>
      </div>
    </el-card>
    <el-card>
      <el-table v-loading="" :data="tableData3" border style="width: 100%;max-height: 300px;" height="300px" size="mini">
        <el-table-column label="供应商" align="center" min-width="170">
          <template slot-scope="scope">
            {{ 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.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="请款金额" align="center" min-width="100" prop="请款金额">
          <template slot-scope="scope">
            {{ scope.row.请款金额 }}
          </template>
        </el-table-column>
        <el-table-column label="审核" align="center" width="320">
          <template slot-scope="scope">
            <el-popover placement="right" width="500" trigger="click">
              <el-table :data="gridData1" stripe size="mini" show-summary>
                <el-table-column width="50" align="center" type="index" label="序号"/>
                <el-table-column min-width="150" align="center" label="采购单号">
                  <template slot-scope="scope">
                    {{ scope.row.采购单号 }}
                  </template>
                </el-table-column>
                <el-table-column min-width="150" align="center" label="采购单名称">
                  <template slot-scope="scope">
                    {{ scope.row.采购单名称 }}
                  </template>
                </el-table-column>
                <el-table-column min-width="100" align="center" label="计划总额" prop="请款金额">
                  <template slot-scope="scope">
                    {{ scope.row.请款金额 }}
                  </template>
                </el-table-column>
              </el-table>
              <el-button slot="reference" type="success" size="mini" icon="el-icon-search" style="margin-right: 10px" @click="searchTable03(scope.row)">查看详情</el-button>
            </el-popover>
            <el-button type="primary" size="mini" icon="el-icon-check" @click="yesMaterials(scope.row)">通过</el-button>
            <el-button type="danger" size="mini" icon="el-icon-close" @click="noMaterials(scope.row)">不通过</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent3"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize3"
          :total="total3"
          style="display:inline-block;width:50%"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange3"
          @current-change="handleCurrentChange3"/>
      </div>
    </el-card>
  </div>
</template>

<script>
import { execApproval, initApproval1, initApprovalOffline, searchRequsetFundDetail, execApprovalOffline, searchRequsetFundDetail1, initApprovalMaterials, execMaterials, searchRequsetFundDetail3 } from '@/api/WorkshopProcurement/FundApproval'
import { mapGetters } from 'vuex'

export default {
  name: '', // 财务审核
  data() {
    return {
      tableData: [],
      pageSize: 10,
      pageCurrent: 1,
      total: 0,
      tableData2: [],
      pageSize2: 10,
      pageCurrent2: 1,
      total2: 0,
      tableData3: [],
      pageSize3: 10,
      pageCurrent3: 1,
      total3: 0,
      gridData: [],
      gridData1: [],
      gridData2: []
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.searchTable()
      this.searchTable2()
      this.searchTable3()
    },
    searchTable() {
      initApproval1(this.pageCurrent, this.pageSize, 1).then(response => {
        for (let j = 0; j < response.data.rows.length; j++) {
          response.data.rows[j].含税总金额 = parseInt(response.data.rows[j].含税总金额 * 100) / 100
          response.data.rows[j].欠款金额 = parseInt(response.data.rows[j].欠款金额 * 100) / 100
        }
        this.tableData = response.data.rows
        this.total = response.data.total
      })
    },
    searchTable01(row) { // 查看详情（请款信息包含的合同）
      searchRequsetFundDetail3(row.采购合同请款流水号).then(response => {
        this.gridData2 = response.data
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageCurrent = 1
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTable()
    },
    searchTable2() {
      initApprovalOffline(this.pageCurrent2, this.pageSize2, 1).then(response => {
        console.log(response.data.rows)
        this.tableData2 = response.data.rows
        this.total2 = response.data.total
      })
    },
    handleSizeChange2(val) {
      this.pageSize2 = val
      this.pageCurrent2 = 1
      this.searchTable2()
    },
    handleCurrentChange2(val) {
      this.pageCurrent2 = val
      this.searchTable2()
    },
    searchTable02(row) { // 查看详情（请款信息包含的合同）
      searchRequsetFundDetail(row.离线合同请款流水号).then(response => {
        this.gridData = response.data
      })
    },
    searchTable3() {
      initApprovalMaterials(this.pageCurrent3, this.pageSize3, 1).then(response => {
        console.log(response.data.rows)
        this.tableData3 = response.data.rows
        this.total3 = response.data.total
      })
    },
    handleSizeChange3(val) {
      this.pageSize3 = val
      this.pageCurrent3 = 1
      this.searchTable3()
    },
    handleCurrentChange3(val) {
      this.pageCurrent3 = val
      this.searchTable3()
    },
    searchTable03(row) { // 查看详情（请款信息包含的合同）
      searchRequsetFundDetail1(row.采购计划请款流水号).then(response => {
        this.gridData1 = response.data
      })
    },
    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: '取消操作'
        })
      })
    },
    yesApprovalOffline(row) { // 通过审核
      this.$confirm('确定审核通过?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        execApprovalOffline(row.离线合同请款流水号, 1, '通过', this.id, 1).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('执行成功')
            this.searchTable2()
          } else { this.$message.error('执行失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    noApprovalOffline(row) { // 不通过审核
      this.$prompt('请输入不通过原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        execApprovalOffline(row.离线合同请款流水号, 2, value, this.id, 1).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('执行成功')
            this.searchTable2()
          } else { this.$message.error('执行失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    yesMaterials(row) { // 通过审核
      this.$confirm('确定审核通过?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        execMaterials(row.采购计划请款流水号, 1, '通过', this.id, 1).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('执行成功')
            this.searchTable3()
          } else { this.$message.error('执行失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    noMaterials(row) { // 不通过审核
      this.$prompt('请输入不通过原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        execMaterials(row.采购计划请款流水号, 2, value, this.id, 1).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('执行成功')
            this.searchTable3()
          } 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>
