<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <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">
            <div style="float: left">{{ item.label }}</div>
            <div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>
          </el-option>
        </el-select>
        <span>供应商:</span>
        <el-input v-model="supplierName" placeholder="供应商" size="small" clearable/>
        <el-button
          type="success"
          size="small"
          icon="el-icon-search"
          style="margin-left:10px"
          @click="total1=0;pageCurrent1=1;pageSize1=10;searchTable1()">查询</el-button>
      </div>
      <el-table
        v-loading=""
        id="expandTable"
        :data="tableData1"
        :row-key="getRowKeys"
        :expand-row-keys="expands"
        border
        style="width: 100%;"
        height="500px"
        size="mini"
        @row-click="searchContract"
        @expand-change="searchPlan">
        <el-table-column label="详情" type="expand">
          <template slot-scope="props">
            <el-table v-show="tableData01.length!==0" :data="tableData01" class="subTableHeader" border size="mini" style="width: 450px">
              <el-table-column label="序号" align="center" type="index"/>
              <el-table-column label="计划付款日期" align="center">
                <template slot-scope="scope">
                  {{ scope.row.计划付款日期.split(' ')[0] }}
                </template>
              </el-table-column>
              <el-table-column label="计划付款金额" align="center">
                <template slot-scope="scope">
                  {{ scope.row.计划付款金额 }}
                </template>
              </el-table-column>
              <el-table-column label="备注" align="center">
                <template slot-scope="scope">
                  {{ scope.row.计划付款备注 }}
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                  <el-tooltip effect="dark" content="删除" placement="top">
                    <div style="display: inline-block">
                      <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click.stop="handleDelete(scope.row)"/>
                    </div>
                  </el-tooltip>
                  <!--<el-button type="danger" size="mini" plain icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>-->
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="采购合同编号" prop="采购合同编号" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.采购合同编号 }}
          </template>
        </el-table-column>
        <el-table-column label="采购合同名称" prop="采购合同名称" align="center" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.采购合同名称 }}
          </template>
        </el-table-column>
        <el-table-column label="供应商" prop="供应商名称" align="center" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.供应商名称 }}
          </template>
        </el-table-column>
        <el-table-column label="设置计划" align="center" min-width="100" fixed="right">
          <template slot-scope="scope">
            <el-button type="warning" size="mini" plain icon="el-icon-circle-plus-outline" @click="handleSetPayPlan(scope.row)">新增</el-button>
          </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"
          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"/>

    <el-dialog v-el-drag-dialog :visible.sync="dialogVisible1" title="增加付款计划" center style="min-height: 300px" width="400px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="left" label-width="110px">
        <el-form-item label="计划付款日期" prop="计划付款日期">
          <el-date-picker v-model="form1.计划付款日期" value-format="yyyy-MM-dd" format="yyyy-MM-dd" size="small" type="date" placeholder="选择日期"/>
        </el-form-item>
        <el-form-item label="计划付款金额" prop="计划付款金额">
          <el-input v-model="form1.计划付款金额" placeholder="填写金额" size="small" clearable/>
        </el-form-item>
        <el-form-item label="计划付款备注">
          <el-input v-model="form1.计划付款备注" placeholder="填写备注" size="small" clearable/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible1=false">取消</el-button>
        <el-button type="primary" size="small" @click="submitSetPayPlan">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { initContract, searchTable1, searchTable01, addTable01, deleteTable01, searchTable2 } from '@/api/PurchasingCenter/SetPayPlan'
import { mapGetters } from 'vuex'
export default {
  name: '', // 采购预算查询
  data() {
    return {
      expands: [],
      contractNumValue: '',
      contractNum: [],
      supplierName: '',
      tableData1: [],
      pageCurrent1: 1,
      pageSize1: 10,
      total1: 0,
      returns: '',
      tableData01: [],
      dialogVisible1: false,
      form1: {
        采购合同流水号: '',
        计划付款日期: '',
        计划付款金额: '',
        计划付款备注: ''
      },
      rules1: {
        计划付款日期: [{ required: true, message: '请选择计划付款日期' }],
        计划付款金额: [{ required: true, message: '请输入计划付款金额' }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'name',
      'id' // 人员编号
    ])
  },
  created() {
    this.fetchData()
    this.searchTable1()
  },
  methods: {
    getRowKeys(row) {
      return row.采购合同流水号
    },
    fetchData() {
      initContract().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.contractNum.push({
            label: response.data[i].采购合同编号,
            name: 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 => {
        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()
    },
    searchPlan(row, expandedRows) {
      this.form1.采购合同流水号 = row.采购合同流水号
      searchTable01(row.采购合同流水号).then(response => {
        if (expandedRows.length > 1) {
          this.expands = []
          expandedRows.shift()
        }
        this.tableData01 = response.data
        this.expands.indexOf(row.采购合同流水号) === -1 ? this.expands.push(row.采购合同流水号) : this.expands = []
      })
    },
    handleSetPayPlan(row) { // 设置付款计划
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      searchTable01(row.采购合同流水号).then(response => {
        this.tableData01 = response.data
      }).then(() => {
        this.expands.indexOf(row.采购合同流水号) === -1 ? this.expands.push(row.采购合同流水号) : true
      })
      this.form1.采购合同流水号 = row.采购合同流水号
      this.form1.计划付款日期 = ''
      this.form1.计划付款金额 = ''
      this.form1.计划付款备注 = ''
      this.dialogVisible1 = true
    },
    submitSetPayPlan() { // 提交设置
      this.$refs['form1'].validate((valid) => {
        if (valid) {
          addTable01(this.form1.采购合同流水号, this.form1.计划付款日期, this.form1.计划付款金额, this.form1.计划付款备注).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('设置成功')
              searchTable01(this.form1.采购合同流水号).then(response => {
                this.tableData01 = response.data
                this.expands.indexOf(this.form1.采购合同流水号) === -1 ? this.expands.push(this.form1.采购合同流水号) : true
              })
              this.dialogVisible1 = false
            } else {
              this.$message.error('设置失败')
            }
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTable01(row.付款计划流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            searchTable01(this.form1.采购合同流水号).then(response => {
              this.tableData01 = response.data
              this.expands.indexOf(this.form1.采购合同流水号) === -1 ? this.expands.push(this.form1.采购合同流水号) : true
            })
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    searchContract(row) {
      searchTable2(row.采购合同流水号).then(response => {
        this.returns = response.data[0].合同内容
      })
    }
  }
}
</script>
<style>
  #expandTable .el-table__expanded-cell{
    padding: 5px 50px;
  }
</style>
<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
  }
  .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>
