<!-- 特批付款申请 -->
<template>
  <modal v-model="modal" width="700px" class-name="vertical-center-modal">
    <!--  表单主要内容  -->
    <div class="title" slot="header">
      付款申请
    </div>
    <div class="footer" slot="footer">
      <Button @click="cfm" class="btn" type="primary">提交</Button>
      <Button @click="cancel" class="btn">取消</Button>
    </div>
    <div class="content">
      <div class="sub-title border">
        <span>基本信息</span>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">申请部门：</div>
          <div class="value">{{ infoData.deptName }}</div>
        </div>
        <div class="col">
          <div class="label">申请人：</div>
          <div class="value">{{ infoData.userName }}</div>
        </div>
        <div class="col">
          <div class="label">申请时间：</div>
          <div class="value">{{ infoData.applyTime }}</div>
        </div>
      </div>
      <div class="sub-title border">
        <span>合同信息</span>
        <a class="go" href="#" @click="contractShow = true">+ 选择合同</a>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">到货日期：</div>
          <div class="value">{{ infoData.deliverTime }}</div>
        </div>
        <div class="col">
          <div class="label">签约时间：</div>
          <div class="value">{{ infoData.signTime }}</div>
        </div>
        <div class="col"></div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">物料编码：</div>
          <div class="value">{{ infoData.materialCode }}</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">物料名称：</div>
          <div class="value">{{ infoData.materialName }}</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">物料规格：</div>
          <div class="value">{{ infoData.spec }}</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">供应商名称：</div>
          <div class="value">{{ infoData.supplierName }}</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">采购数量：</div>
          <div class="value">{{ infoData.purchaseQty }}</div>
        </div>
        <div class="col">
          <div class="label">采购单价：</div>
          <div class="value">{{ infoData.price }}</div>
        </div>
        <div class="col"></div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">合同金额：</div>
          <div class="value">{{ infoData.totalAmount }}</div>
        </div>
        <div class="col">
          <div class="label">合同账期：</div>
          <div class="value">{{ infoData.accountPeriod }}</div>
        </div>
        <div class="col"></div>
      </div>
      <div class="sub-title border">
        <span>付款信息</span>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">申请支付：</div>
          <div class="value">
            <Input v-model="infoData.payAmount" size="small" type="number" @on-change="autoPrice" style="width: 70px"/>
          </div>
        </div>
        <div class="col">
          <div class="label">历史已付：</div>
          <div class="value">
            <Input v-model="infoData.hisPayAmount" size="small" type="number" style="width: 70px" disabled/>
          </div>
        </div>
        <div class="col"></div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">退货冲抵：</div>
          <div class="value">
            <Input v-model="infoData.backAmount" size="small" type="number" style="width: 70px" disabled/> 元
          </div>
        </div>
        <div class="col">
          <div class="label">运费冲抵：</div>
          <div class="value">
            <Input v-model="infoData.expressAmount" size="small" type="number" style="width: 70px" disabled/> 元
          </div>
        </div>
        <div class="col">
          <div class="label">合计冲抵：</div>
          <div class="value">
            <Input v-model="infoData.totalBalance" size="small" type="number" style="width: 70px" disabled/> 元
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">实际应付：</div>
          <div class="value">
            <Input v-model="infoData.actAmount" size="small" type="number" style="width: 70px" disabled/> 元
          </div>
        </div>
        <div class="col"></div>
        <div class="col"></div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">户名：</div>
          <div class="value">
            <Select v-model="infoData.bankAccoutName" clearable @on-select ="countSelect" @on-clear="accoutClear">
              <Option v-for="item in infoData.bankMesList" :value="item.bankAccoutName" :key="item.bankAccout">{{ item.bankAccoutName || '未知' }}</Option>
            </Select>
          </div>
        </div>
        <div class="col"></div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">收款账号：</div>
          <div class="value">
            <Input v-model="infoData.bankAccout" size="small" disabled/>
          </div>
        </div>
        <div class="col">
          <div class="label">开户银行：</div>
          <div class="value">
            <Input v-model="infoData.openBank" size="small" disabled/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col" style="align-items: flex-start">
          <div class="label">付款说明：</div>
          <div class="value" style="flex: 1">
            <Input v-model="infoData.payAccount" type="textarea"/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col" style="align-items: center">
          <div class="label">上传附件：</div>
          <div class="value" style="flex: 1">
            <Upload action="" ref="uploadFileFlag" :before-upload="beforeFile">
              <Button icon="ios-cloud-upload-outline">
                上传文件
              </Button>
            </Upload>
          </div>
        </div>
      </div>
      <div class="row" v-for="(item, index) in fileList" :key="index">
        <div class="col" style="position:relative;">
          {{ item.fileName }}
          <Icon type="md-close" style="position:absolute;right: 0;" @click="removeFile(index)" />
        </div>
      </div>
    </div>
    <!--  决策辅助内容  -->
    <side-bar v-model="showSider">
      <div class="content sider" style="padding: 16px 10px;">
        <div class="title">决策辅助</div>
        <div class="card">
          <div class="sub-title border">合同执行情况</div>
          <div class="row">
            <div class="col">
              <div class="label">
                <Icon
                  :type="siderData.totalInStockQty >= (siderData.purQuantity * 0.95) ? 'ios-checkmark-circle-outline' : 'ios-close-circle-outline'"
                  :color="siderData.totalInStockQty >= (siderData.purQuantity * 0.95) ? '#16C400' : 'red'"
                  size="16"/>
                采购数量：
              </div>
              <div class="value">{{ siderData.purQuantity }}</div>
            </div>
            <div class="col">
              <div class="label">累计入库：</div>
              <div class="value">{{ siderData.totalInStockQty }}</div>
            </div>
          </div>
          <div class="row">
            <div class="col">
              <div class="label">
                <Icon
                  :type="Number.parseFloat(siderData.qtyCheckAcceptRate) >= 95 ? 'ios-checkmark-circle-outline' : 'ios-close-circle-outline'"
                  :color="Number.parseFloat(siderData.qtyCheckAcceptRate) ? '#16C400' : 'red'"
                  size="16"/>
                质检合格率：
              </div>
              <div class="value">{{ siderData.qtyCheckAcceptRate }}</div>
            </div>
          </div>
          <div class="row">
            <div class="col">
              <div class="label">
                <Icon
                  :type="(siderData.payDateLimit && (new Date().getTime()) >= (new Date(siderData.payDateLimit).getTime())) ? 'ios-checkmark-circle-outline' : 'ios-close-circle-outline'"
                  :color="(siderData.payDateLimit && (new Date().getTime()) >= (new Date(siderData.payDateLimit).getTime())) ? '#16C400' : 'red'"
                  size="16"/>
                付款期限：
              </div>
              <div class="value">{{ siderData.payDateLimit }}</div>
            </div>
          </div>
        </div>
        <div class="card">
          <div class="sub-title border">入库记录</div>
          <div v-for="(item, index) in siderData.inStockItems" :key="index">
            <div class="row">
              <div class="col">
                <div class="label">入库时间： </div>
                <div class="value">{{ item.inStockTime }}</div>
              </div>
            </div>
            <div class="row">
              <div class="col">
                <div class="label">入库数量： </div>
                <div class="value">{{ item.inStockQty }}</div>
              </div>
            </div>
            <div class="row">
              <div class="col">
                <div class="label">质检时间：</div>
                <div class="value">{{ item.qtyCheckTime }}</div>
              </div>
            </div>
            <div class="row">
              <div class="col">
                <div class="label">合格率： </div>
                <div class="value">{{ item.acceptRate }}</div>
              </div>
            </div>
            <div class="row" v-if="siderData.inStockItems.length !== index">
              <div class="line"></div>
            </div>
          </div>
        </div>
        <div class="card">
          <div class="sub-title border">
            <span>退货历史</span>
            <div class="tabs">
              <div
                :class="index === tabIndex ? 'active' : ''"
                class="tab"
                v-for="(item, index) in tabs" :key="index"
                @click="tabIndex = index"
              >
                {{ item }}
              </div>
            </div>
          </div>
          <div
              v-for="(item, index) in (tabIndex === 0 ? siderData.unSettleBackItems : tabIndex === 1 ? siderData.settleBackItems : siderData.allBackItems)"
               :key="index">
            <div class="row-wrap">
              <div class="pic">
                <img :src="overPic" alt="">
              </div>
              <div class="row">
                <div class="col">
                  <div class="label">退货日期： </div>
                  <div class="value">{{ item.backTime }}</div>
                </div>
              </div>
              <div class="row">
                <div class="col">
                  <div class="label">退货数量： </div>
                  <div class="value">{{ item.backQty }}</div>
                </div>
              </div>
              <div class="row">
                <div class="col">
                  <div class="label">退货金额： </div>
                  <div class="value">{{ item.backAmount }}</div>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="line"></div>
            </div>
          </div>
        </div>
        <div class="card">
          <div class="sub-title border">供应商记录</div>
          <div class="row">
            <div class="col">
              <div class="label">累计采购： </div>
              <div class="value">{{ siderData.totalPurQtyStr }}</div>
            </div>
          </div>
          <div class="row">
            <div class="col">
              <div class="label">最新单价： </div>
              <div class="value">{{ siderData.latestPrice }}</div>
            </div>
          </div>
          <div class="row">
            <div class="col">
              <div class="label">累计退货： </div>
              <div class="value">{{ siderData.totalBackQty }}</div>
            </div>
            <div class="col">
              <div class="label">退货率： </div>
              <div class="value">{{ siderData.backQtyRate }}</div>
            </div>
          </div>
          <div class="row">
            <div class="line"></div>
          </div>
          <div class="row">
            <div class="col">
              <div class="label">历史最低单价： </div>
              <div class="value">{{ siderData.lowestPrice }}</div>
            </div>
          </div>
          <div class="row">
            <div class="col">
              <div class="label">历史最高单价： </div>
              <div class="value">{{ siderData.highestPrice }}</div>
            </div>
          </div>
          <div class="row">
            <line-chart style="height: 200px;width: 226px" :data="data.purPriceItems"></line-chart>
          </div>
        </div>
      </div>
    </side-bar>
    <!-- 合同列表 -->
    <contract-list-form v-model="contractShow" @on-select="select"></contract-list-form>
  </modal>
</template>

<script>
import ContractListForm from './contract-list-form'
import { GetContractPayPolicy, AddApplyPurchasePay, SavePayContractBill } from '@/api/applyPurchase'
import SideBar from '_c/my-modal-pay/my-modal-sidebar'
import LineChart from '@/view/index/schedule/line-chart'
import overPic from '@/assets/images/over.png'
import { UploadBase64 } from '@/api/upload'
export default {
  name: 'contract-form',
  data () {
    return {
      fileList: [],
      overPic,
      tabIndex: 0,
      tabs: ['未结算', '已结算', '全部'],
      formData: {},
      remark: '',
      modal: false,
      showSider: true,
      contractShow: false,
      infoData: {},
      siderData: {}
    }
  },
  components: {
    SideBar,
    LineChart,
    ContractListForm
  },
  props: {
    data: {
      type: Object,
      default () {
        return {}
      }
    },
    value: {
      type: Boolean,
      default: false,
      require: true
    }
  },
  watch: {
    value: {
      handler (n) {
        this.modal = n
      },
      immediate: true
    },
    modal (n) {
      this.$emit('input', n)
    }
  },
  methods: {
    accoutClear () {
      this.$set(this.infoData, 'bankAccout', '')
      this.$set(this.infoData, 'openBank', '')
    },
    countSelect (data) {
      console.log(data)
      let obj = this.infoData.bankMesList.find(item => item.bankAccoutName === data)
      obj && this.$set(this.infoData, 'bankAccout', obj.bankAccout || '')
      obj && this.$set(this.infoData, 'openBank', obj.openBank || '')
    },
    removeFile (index) {
      this.fileList.splice(index, 1)
    },
    beforeFile (file) {
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = (event) => {
        UploadBase64({
          fileBase64: event.srcElement.result,
          fileName: file.name,
          floder: 'order'
        }).then(res => {
          if (res.code === 0) {
            let fileObj = {
              fileSize: file.size,
              fileName: file.name,
              filePath: res.result
            }
            this.fileList.push(fileObj)
            console.log('????????????form info-----------')
            console.log(this.fileList)
          }
        })
      }
      return false
    },
    // 自动计算实际应付
    autoPrice () {
      if (this.infoData.purchaseBillNo) {
        this.infoData.actAmount = this.infoData.payAmount - this.infoData.totalBalance
      }
    },
    // 选择合同后获取数据 回填数据
    select (row) {
      this.infoData = row
      AddApplyPurchasePay({
        purchaseBillNo: row.purchaseBillNo
      }).then(res => {
        if (res.code === 0) {
          this.infoData = res.result
          this.infoData.bankMesList = row.bankMesList.filter(data => data.bankAccoutName).map(item => {
            return {
              bankAccout: item.bankAccout || '',
              bankAccoutName: item.bankAccoutName || '',
              openBank: item.openBank || ''
            }
          })
        }
      })
      GetContractPayPolicy({
        purchaseBillNo: row.purchaseBillNo
      }).then(res => {
        if (res.code === 0) {
          console.log(res)
          this.siderData = res.result
        }
      })
    },
    cfm () {
      if (!this.infoData.purchaseBillNo) return this.$Message.error('请选择合同！')
      if (!this.infoData.bankAccoutName) return this.$Message.error('请选择收款账号！')
      SavePayContractBill({
        ...this.infoData,
        fileItems: this.fileList,
        applyPayType: 1
      }).then(res => {
        if (res.code === 0) {
          this.$Message.info(res.msg || '提交成功！')
          this.cancel()
        }
      })
    },
    cancel () {
      this.modal = false
      this.siderData = {}
      this.infoData = {}
      this.fileList = []
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  padding: 10px 0;
  border-radius: 5px;
  background-color: #FFFFFF;
  &.sub-content {
    background-color: #f2f2f2;
    margin-top: 3px;
    .title {
      padding-top: 0;
    }
  }
  .title {
    text-align: center;
    font-size: 20px;
    font-weight: bold;
  }
  .sub-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px;
    font-size: 14px;
    font-weight: bold;
    &.border {
      border-bottom: 1px solid #000000;
    }
    .go {
      font-size: 10px;
      font-weight: normal;
      color: #0000ff;
    }
    .tabs {
      margin-bottom: 1px;
      display: flex;
      justify-content: space-between;
      border: 1px solid #5d6c7b;
      border-radius: 2px;
      font-size: 12px;
      font-weight: normal;
      .tab {
        width: 50px;
        text-align: center;
        border-right: 1px solid #5d6c7b;
        cursor: pointer;
        &:last-child {
          border-right: none;
        }
        &.active {
          background-color: rgba(22, 155, 213, 1);
          color: #FFFFFF;
        }
      }
    }
  }
  .row-wrap {
    position: relative;
    .pic {
      position: absolute;
      right: 20px;
      top: 50%;
      transform: translateY(-50%);
      width: 50px;
      img {
        width: 100%;
      }
    }
  }
  .row {
    padding: 0 20px;
    margin-bottom: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .col {
      flex: 1;
      display: flex;
      align-items: center;
      .label {
        width: 80px;
        text-align: right;
      }
      .value {
        flex: 1;
      }
    }
  }
  .card {
    margin-bottom: 5px;
    background-color: #FFFFFF;
    border: 1px solid rgba(215, 215, 215, 1);
    border-radius: 5px;
    .border {
      border-bottom: 1px solid #5d6c7b;
    }
    .line {
      width: 100%;
      border-bottom: 1px solid #5d6c7b;
    }
  }
  .table {
    padding: 0 20px;
    padding-bottom: 20px;
    table {
      border-collapse:collapse;
      border:none;
      width: 100%;
      tr {
        td,th {
          padding: 5px;
          text-align: center;
          border:solid #dcdee2 1px;
          position: relative;
          font-size: 12px;
        }
        th {
          background-color: #eeeeee;
        }
      }
    }
  }
}
.sider {
  .label {
    width: auto !important;
  }
}
/deep/ .ivu-modal-body {
  padding: 0;
  max-height: 80vh;
  overflow: auto;
}
/deep/ .ivu-modal-content {
  background-color: #FFFFFF;
}
/deep/.ivu-upload-list {
  display: none;
}
</style>
