<!-- 付款申请单 -->
<template>
  <modal v-model="modal" :footer-hide="true" width="700px">
    <!--  表单主要内容  -->
    <div class="content">
      <div class="title">
        付款申请
      </div>
      <div class="sub-title border">
        <span>基本信息</span>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">合同编号：</div>
          <div class="value">{{ infoData.purchaseBillNo }}</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>
        <contract-modal v-model="contractShow" :data="infoData"></contract-modal>
      </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="row" v-if="infoData.managerApproved">
        <div class="col">
          <div class="label">总经理签字：</div>
          <div class="value">
            <img style="width: 100px" src="https://fzjx.oss-cn-hangzhou.aliyuncs.com/order/20211111/211111053557079580.png" alt="">
          </div>
      </div>
    </div>
      <div class="sub-title border">
        <span>付款信息</span>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">户名：</div>
          <div class="value">{{ infoData.bankAccoutName }}</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">开户银行：</div>
          <div class="value">{{ infoData.openBank }}</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">收款账号：</div>
          <div class="value">{{ infoData.bankAccout }}</div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="label">申请支付：</div>
          <div class="value">{{ infoData.payAmount }}</div>
        </div>
        <div class="col">
          <div class="label">历史已付：</div>
          <div class="value">{{ infoData.hisPayAmount }}</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: 100px" disabled/> 元
          </div>
        </div>
        <div class="col">
          <div class="label">运费冲抵：</div>
          <div class="value">
            <Input v-model="infoData.expressAmount" size="small" type="number" style="width: 100px" disabled/> 元
          </div>
        </div>
        <div class="col">
          <div class="label">合计冲抵：</div>
          <div class="value">
            <Input v-model="infoData.totalBalance" size="small" type="number" style="width: 100px" 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: 100px" disabled/> 元
          </div>
        </div>
        <div class="col"></div>
        <div class="col"></div>
      </div>
      <div class="row">
        <div class="col" style="align-items: flex-start">
          <div class="label">付款说明：</div>
          <div class="value">{{ infoData.payAccount }}</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">
        <div class="col">
          <div class="label">附件：</div>
          <div class="value" style="display:flex;flex-direction:column;color: blue">
            <div
              v-for="(item, index) in infoData.fileItems"
              :key="index"
              @click="openPic(item.filePath)"
              style="cursor: pointer;text-decoration: underline">
              {{ item.fileName }}
            </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <Button type="primary" size="small" @click="print">打印</Button>
          <Button type="primary" size="small" @click="contractPrint" style="margin-left: 10px">打印合同</Button>
          <contrac-printt-modal v-model="contractPrintShow" :data="infoData"></contrac-printt-modal>
          <vue-easy-print ref="print">
            <template>
              <print-form :data="printData"></print-form>
            </template>
          </vue-easy-print>
        </div>
      </div>
    </div>
    <div class="content sub-content">
      <div class="info">
        <div class="title">审批流程</div>
        <div class="row">
          <div class="col">
            <Input v-model="remark" type="textarea" placeholder="请输入审批意见"/>
          </div>
        </div>
        <div class="row">
          <div class="col">
            <Button type="primary" :disabled="data.approveType !== 0" style="margin-right: 10px" @click="cfm(0)">同意</Button>
            <Button type="error" :disabled="data.approveType !== 0" style="margin-right: 10px" @click="cfm(1)">驳回</Button>
            <Button type="error" v-if="data.approveType === 1" @click="cfm(2)">撤回</Button>
          </div>
        </div>
        <div class="table">
          <table>
            <tr>
              <th>序号</th>
              <th>审批节点</th>
              <th>审批人</th>
              <th>审批意见</th>
              <th>审批结果</th>
              <th>审批日期</th>
            </tr>
            <tr v-for="(item, index) in flowsList" :key="index">
              <td>{{ item.rowNo }}</td>
              <td>{{ item.nodeName }}</td>
              <td>{{ item.userName }}</td>
              <td>{{ item.suggestions }}</td>
              <td>{{ item.checkResult }}</td>
              <td>{{ item.checkTime }}</td>
            </tr>
          </table>
        </div>
      </div>
    </div>
    <!--  决策辅助内容  -->
    <side-bar v-model="showSider">
      <div class="content" 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) >= 95 ? '#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" v-if="tabIndex === 1">-->
<!--                <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" :x-data="siderData.purPriceItemsX" :y-data="siderData.purPriceItemsY"></line-chart>
          </div>
        </div>
      </div>
    </side-bar>
  </modal>
</template>

<script>
import VueEasyPrint from 'vue-easy-print'
import PrintForm from './print-form'
import ContractModal from './contract-modal'
import ContracPrinttModal from './contract-print-form'
import {
  GetContractPayPolicy,
  GetApplyPurchasePay,
  ApprovalBillFlow,
  UploadContractBills,
  GetContractPrintData
} from '@/api/applyPurchase'
import { GetApprovalFlows } from '@/api/stock'
import SideBar from '_c/my-modal-pay/my-modal-sidebar'
import LineChart from './line-chart'
import overPic from '@/assets/images/over.png'
import { UploadBase64 } from '@/api/upload'
import { filePreview } from '@/libs/util'
export default {
  name: 'contract-form',
  data () {
    return {
      contractShow: false,
      contractPrintShow: false,
      overPic,
      tabIndex: 0,
      tabs: ['未结算', '已结算', '全部'],
      formData: {},
      remark: '',
      modal: false,
      showSider: true,
      flowsList: [],
      infoData: {},
      siderData: {},
      printData: {}
    }
  },
  components: {
    SideBar,
    LineChart,
    ContractModal,
    PrintForm,
    VueEasyPrint,
    ContracPrinttModal
  },
  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)
      if (n) {
        GetApplyPurchasePay({
          id: this.data.linkId
        }).then(res => {
          if (res.code === 0) {
            console.log(res)
            this.infoData = res.result
          }
        })
        GetContractPayPolicy({
          id: this.data.linkId
        }).then(res => {
          if (res.code === 0) {
            console.log(res)
            this.siderData = res.result
          }
        })
        GetApprovalFlows({
          linkId: this.data.linkId
        }).then(res => {
          if (res.code === 0) {
            this.flowsList = res.result
          }
        })
      } else {
        this.infoData = {}
        this.siderData = {}
        this.flowsList = {}
      }
    }
  },
  methods: {
    contractPrint () {
      this.contractPrintShow = true
    },
    print () {
      GetContractPrintData({
        contractPayId: this.data.linkId
      }).then(res => {
        if (res.code === 0) {
          console.log(res)
          this.printData = {
            applyPurchase: res.result.applyPurchase,
            purInStock: res.result.purInStock,
            purchaseReimburse: res.result.purchaseReimburse,
            applyPayBill: res.result.applyPayBill,
            applyPayType: res.result.applyPayType,
            handlerName: res.result.handlerName,
            financeName: res.result.financeName,
            factoryManager: res.result.factoryManager,
            payAccount: this.infoData.payAccount,
            totalAmount: this.infoData.totalAmount,
            payAmount: this.infoData.payAmount,
          }
          this.$nextTick(() => {
            this.$refs.print.print()
          })
        }
      })
    },
    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 = {
              size: file.size,
              fileName: file.name,
              filePath: res.result,
              fileType: 2
            }
            UploadContractBills({
              linkPayId: this.data.linkId,
              purchaseBillNo: this.infoData.purchaseBillNo,
              contractsList: [ fileObj ]
            }).then(res => {
              if (res.code === 0) {
                this.$Message.info('上传成功！')
                this.infoData.fileItems.push(fileObj)
              }
            })
          }
        })
      }
      return false
    },
    openPic (url) {
      filePreview(url)
    },
    cfm (type) {
      ApprovalBillFlow({
        linkCheckId: [this.data.flowChekId],
        applyPayAmount: this.infoData.payAmount,
        balanceAmount: this.infoData.totalBalance,
        updateAction: type,
        suggestions: this.remark
      }).then(res => {
        if (res.code === 0) {
          this.cancel()
          this.$Message.info(res.msg || '操作成功！')
          this.$emit('getlist')
        }
      })
    },
    cancel () {
      this.modal = false
    }
  }
}
</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: flex-start;
      .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;
        }
      }
    }
  }
}
/deep/ .ivu-modal-body {
  padding: 0;
  max-height: 80vh;
  overflow: auto;
}
/deep/ .ivu-modal-content {
  background-color: transparent;
}
/deep/.ivu-upload-list {
  display: none;
}
</style>
