<template>
  <div class="app-container">
    <el-card>
      <el-col>
        <span>请款时间段:</span>
        <el-date-picker
          v-model="dateRange"
          :picker-options="pickerOptions"
          size="small"
          type="daterange"
          align="center"
          style="width: 300px;margin: 3px 0"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="～"
          start-placeholder="开始日期"
          end-placeholder="结束日期"/>
        <span style="margin-left: 10px">供应商:</span>
        <el-input v-model="supplierName0" placeholder="供应商" size="small" clearable/>
        <el-tooltip :open-delay="1000" effect="dark" content="查询外购备料清款信息" placement="top">
          <el-button
            type="success"
            size="small"
            icon="el-icon-search"
            style="margin-left: 10px"
            @click="pageCurrent1=1;pageSize1=10;total1=0;searchTable1()">查询</el-button>
        </el-tooltip>
        <el-tooltip :open-delay="1000" effect="dark" content="对选择的请款任务创建请款申请" placement="top">
          <el-button type="primary" size="small" icon="el-icon-goods" style="" @click="requestFund">请款</el-button>
        </el-tooltip>
      </el-col>
    </el-card>
    <el-card style="margin-left: 0px;width: 68%;float: left">
      <!--@expand-change="searchTable02"-->
      <!--<el-table-column type="expand" label="详情">-->
      <!--<template slot-scope="scope">-->
      <!--<el-table :data="gridData" class="subTableHeader" stripe size="mini" show-summary style="width: 40%">-->
      <!--<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="150" align="center" label="开票金额" prop="开票金额">-->
      <!--<template slot-scope="scope">-->
      <!--{{ scope.row.开票金额 }}-->
      <!--</template>-->
      <!--</el-table-column>-->
      <!--</el-table>-->
      <!--</template>-->
      <!--</el-table-column>-->
      <el-table
        v-loading=""
        :data="tableData1"
        :row-key="getRowKeys"
        :expand-row-keys="expands"
        border
        stripe
        style="margin-top: 3px;"
        height="600px"
        size="mini"
        show-summary
        highlight-current-row
        @row-click="searchTable02">
        <el-table-column label="供应商" prop="供应商名称" align="center" min-width="180" width="350" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <el-table-column label="请款时间" align="center" min-width="90" width="90" prop="date">
          <template slot-scope="scope">
            {{ scope.row.请款时间 | formatTime }}
          </template>
        </el-table-column>
        <el-table-column label="请款人" align="center" min-width="80" width="100">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column label="请款金额" align="center" min-width="90" width="90" prop="请款金额">
          <template slot-scope="scope">
            {{ parseFloat(scope.row.请款金额).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="审批情况内容" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.审批情况流水号)===3" type="primary" size="mini">未审批</el-tag>
            <el-tag v-if="Number(scope.row.审批情况流水号)===1" type="success" size="mini">已通过</el-tag>
            <el-tag v-if="Number(scope.row.审批情况流水号)===2" type="danger" size="mini">不通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="未通过审批原因" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.未通过审批原因 === '通过' ? '—' : scope.row.未通过审批原因 }}
          </template>
        </el-table-column>
        <el-table-column label="付款情况" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.付款情况流水号)===3" type="primary" size="mini">未操作</el-tag>
            <el-tag v-if="Number(scope.row.付款情况流水号)===1" type="success" size="mini">已同意</el-tag>
            <el-tag v-if="Number(scope.row.付款情况流水号)===2" type="danger" size="mini">不同意</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="未付款原因" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.未付款原因 === '通过' ? '—' : scope.row.未付款原因 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="80">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="取消请款" placement="top">
              <div style="display: inline-block">
                <el-button :disabled="Number(scope.row.审批情况流水号)!==3" type="text" size="mini" icon="el-icon-delete" style="margin-left: 10px" @click="cancelRequestFund(scope.row)"/>
              </div>
            </el-tooltip>
          </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 style="margin-left: 0px;width: 32%;float: left">
      <el-table-column type="expand" label="详情">
        <el-table :data="gridData" class="subTableHeader" stripe size="mini" show-summary style="width: 100%">
          <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="150" align="center" label="开票金额" prop="开票金额">
            <template slot-scope="scope">
              {{ scope.row.开票金额 }}
            </template>
          </el-table-column>
        </el-table>
      </el-table-column>
    </el-card>
    <el-dialog v-el-drag-dialog :visible.sync="dialogVisible2" title="外购备料请款" center style="min-height: 300px" width="800px">
      <span>供应商：</span>
      <el-select v-model="supplierValue" placeholder="供应商" filterable size="small" clearable @change="pageCurrent1=1;pageSize1=10;total1=0;searchTable01()">
        <el-option
          v-for="item in supplier"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-table v-loading="" :data="tableData01" border stripe highlight-current-row style="max-height: 300px;" height="300px" size="mini" @selection-change="handleSelectionChange">
        <el-table-column type="selection" align="center" width="50"/>
        <el-table-column label="发票号" align="center" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.发票号 }}
          </template>
        </el-table-column>
        <el-table-column label="供应商" align="center" min-width="250" show-overflow-tooltip>
          <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.开票时间 | formatTime }}
          </template>
        </el-table-column>
      </el-table>
      <span>合计：<el-input v-model="SUM" size="mini" style="width: 80px; margin: 10px 10px 0 10px"/>元</span>
      <el-button type="primary" size="small" style="float:right;margin: 10px 10px 0 10px" icon="el-icon-check" @click="submitRequestFund">提交请款审批</el-button>
    </el-dialog>
  </div>
</template>
<script>import { searchRequestFund, searchOfflineContract, searchOfflineContract1, submitRequestFund, searchRequsetFundDetail, cancelRequestFund } from '@/api/WorkshopProcurement/RequestOutsourcingMaterials'
import { mapGetters } from 'vuex'

export default {
  name: '', // 离线合同
  data() {
    return {
      check1: 0,
      check2: 0,
      dateRange: '',
      expands: [],
      pickerOptions: {
        shortcuts: [{
          text: '上季度',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 30 * 3)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '过去半年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 366 / 2)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '过去一年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 365)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      offlineContract: '',
      supplierName0: '',
      supplierValue: '',
      supplier: [],
      pageCurrent1: 1,
      pageSize1: 10,
      total1: 0,
      tableData1: [],
      pageCurrent2: 1,
      pageSize2: 10,
      total2: 0,
      tableData01: [],
      dialogVisible2: false,
      SUM: 0,
      contractGroup: [],
      sumGroup: [],
      gridData: []
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
    this.searchTable1()
  },
  methods: {
    // dateFormat(row, column) {
    //   var date = row[column.property]
    //   console.log(date)
    //   if (date === undefined) {
    //     return ''
    //   }
    //   return util.formatDate.format(new Date(date), 'yyyy-MM-dd')
    // },
    fetchData() {
      this.supplier = []
      searchOfflineContract().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.supplier.push({
            label: response.data[i].供应商名称,
            value: response.data[i].供应商流水号
          })
        }
      })
      console.log(this.supplier)
    },
    searchTable1() { // 查询请款表
      this.dateRange ? this.check1 = 1 : (this.check1 = 0, this.dateRange = '')
      this.supplierName0 ? this.check2 = 1 : (this.check2 = 0, this.supplierName0 = '')
      searchRequestFund(this.pageCurrent1, this.pageSize1, this.check1, this.dateRange[0], this.dateRange[1], this.check2, this.supplierName0).then(response => {
        this.tableData1 = response.data.rows
        this.total1 = response.data.total
      })
    },
    handleSizeChange1(val) {
      this.pageSize1 = val
      this.pageCurrent1 = 1
      this.searchTable1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchTable1()
    },
    requestFund() { // 请款
      this.searchTable01()
      this.dialogVisible2 = true
      this.supplierValue = ''
      this.fetchData() // 重新获取供应商
      this.tableData01 = []
    },
    searchTable01() { // 查询待请款
      this.contractGroup = []
      this.supplierValue ? this.check = 1 : this.check = 0
      searchOfflineContract1(this.check, this.supplierValue).then(response => {
        this.tableData01 = response.data
      })
    },
    handleSelectionChange(val) {
      this.SUM = 0
      this.contractGroup = []
      this.sumGroup = []
      for (let i = 0; i < val.length; i++) {
        this.SUM += parseFloat(val[i].开票金额)
        this.contractGroup.push(val[i].发票流水号)
        this.sumGroup.push(val[i].开票金额)
      }
    },
    submitRequestFund() { // 确认请款
      if (this.supplierValue && this.contractGroup.length !== 0) {
        submitRequestFund(this.contractGroup, this.sumGroup, this.supplierValue, this.SUM, this.id).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('请款成功')
            this.searchTable1()
            this.dialogVisible2 = false
          } else { this.$message.error('请款失败') }
        })
      } else {
        this.$message.error('请选择需要请款的合同')
      }
    },
    searchTable02(row) { // 查看详情（请款信息包含的合同）
      console.log(row, 1111)
      // if (this.a === 1) {
      //   this.expands.push(row.采购计划请款流水号)
      //   this.b = row.采购计划请款流水号
      //   this.a = 0
      // } else {
      //   if (this.b === row.采购计划请款流水号) {
      //     this.expands = []
      //     this.a = 1
      //   } else {
      //     this.expands = []
      //     this.expands.push(row.采购计划请款流水号)
      //     this.a = 0
      //     this.b = row.采购计划请款流水号
      //   }
      // }
      searchRequsetFundDetail(row.采购计划请款流水号).then(response => {
        this.gridData = response.data
        console.log(response.data, 2222)
      })
    },
    getRowKeys(row) {
      return row.采购计划请款流水号
    },
    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: '已取消操作'
        })
      })
    }
  }
}
</script>
<style scoped>
  .el-card{
    /*margin-bottom: 15px;*/
  }
  .el-date-editor{
    width:150px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  span{
    margin: 10px 0;
  }
  .el-tag{
    margin: 0
  }
  .searchForm .el-form-item{
    margin-bottom: 5px;
  }
</style>

<style lang="scss">
  .subTableHeader {
    th {
      background: #8ff4ea !important;
      color: black;
    }

    tr.el-table__row td {
      background-color: #d8e5f6 !important;
    }

    tr.el-table__row.el-table__row--striped td {
      background: #e9f0f9 !important;
    }
  }
</style>
