<template>
  <div class="app-container">
    <el-card>
      <span>时间段:</span>
      <el-date-picker
        v-model="dateRange"
        :picker-options="pickerOptions"
        size="small"
        type="daterange"
        align="center"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        range-separator="～"
        style="width:300px"
        start-placeholder="开始日期"
        end-placeholder="结束日期"/>
      <el-button
        type="success"
        size="small"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="searchTable0()">查询</el-button>
    </el-card>

    <el-card>
      <el-table :data="tableData0" size="mini" border style="max-height: 250px;" height="250px" highlight-current-row @row-click="searchContract">
        <el-table-column label="计划状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="timeToStamp(scope.row.计划付款日期) <= getDate" type="danger" size="small">已拖期</el-tag>
            <el-tag v-if="timeToStamp(scope.row.计划付款日期) > getDate" type="success" size="small">未拖期</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="计划付款日期" min-width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.计划付款日期.split(' ')[0] }}
          </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="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="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>
    </el-card>

    <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">
                  <el-tag v-if="Number(scope.row.审批情况流水号)===3" type="primary" size="small">未审批</el-tag>
                  <el-tag v-if="Number(scope.row.审批情况流水号)===1" type="success" size="small">已通过</el-tag>
                  <el-tag v-if="Number(scope.row.审批情况流水号)===2" type="danger" size="small">不通过</el-tag>
                </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">
                  <el-tag v-if="Number(scope.row.付款情况流水号)===3" type="primary" size="small">未操作</el-tag>
                  <el-tag v-if="Number(scope.row.付款情况流水号)===1" type="success" size="small">已同意</el-tag>
                  <el-tag v-if="Number(scope.row.付款情况流水号)===2" type="danger" size="small">不同意</el-tag>
                </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 { searchTable0, initContract, searchTable1, searchCondition, requestFund, cancelRequestFund, searchContract } from '@/api/PurchasingCenter/PurchaseContractRequestFund'
import { mapGetters } from 'vuex'
import { getNowTime, timeToStamp } from '@/utils/tool'

export default {
  data() {
    return {
      getDate: null,
      tableData0: [],
      check: 0,
      pickerOptions: {
        shortcuts: [{
          text: '未来一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '未来一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '未来两个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 30 * 2)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '未来三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 30 * 3)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '未来半年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 30 * 6)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      dateRange: '',
      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()
    this.searchTable0()
  },
  methods: {
    timeToStamp(param) { // 转时间戳
      return timeToStamp(param)
    },
    searchTable0() { // 查询付款计划
      this.dateRange ? this.check = 1 : (this.check = 0, this.dateRange = '')
      searchTable0(this.check, this.dateRange[0], this.dateRange[1]).then(response => {
        this.tableData0 = response.data
      })
    },
    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.getDate = timeToStamp(getNowTime()) // 获取当前时间
      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-tag{
    margin: 0
  }
  .el-card{
    margin-bottom: 15px;
  }
  .el-date-editor{
    width:150px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    margin: 10px 0 10px 10px;
  }
  .searchForm .el-form-item{
    margin-bottom: 5px;
  }
</style>
