<!-- 总经理审批 退料审批弹框 -->
<template>
  <modal v-model="modal" width="500px">
    <!--  表单主要内容  -->
    <div class="title" slot="header">退货处理</div>
    <div class="footer" slot="footer">
      <Button class="btn" type="primary" size="small" @click="cfm(0)">提交</Button>
    </div>
    <div v-if="data.stockBackWay === 0">
      <div class="content">
        <div class="sub-title border">
          <span>物料信息</span>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">物料名称：</div>
            <div class="value">{{ data.materialName }}</div>
          </div>
          <div class="col">
            <div class="label">物料编码：</div>
            <div class="value">{{ data.materialCode }}</div>
          </div>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">供应商：</div>
            <div class="value">{{ data.supplierName }}</div>
          </div>
        </div>
        <div class="sub-title border">
          <span>退料信息</span>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">退料时间：</div>
            <div class="value">{{ data.backTime }}</div>
          </div>
          <div class="col">
            <div class="label">退料人员：</div>
            <div class="value">{{ data.backUserName }}</div>
          </div>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">退料数量：</div>
            <div class="value">{{ data.backQty }}</div>
          </div>
          <div class="col">
            <div class="label">退料金额：</div>
            <div class="value">{{ data.backAmount }}</div>
          </div>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">退料件数：</div>
            <div class="value">{{ data.backItems.length }}</div>
          </div>
        </div>
        <div class="row">
          <Button style="width: 100%" @click="backDetailShow = true">查看退料详情</Button>
        </div>
        <div class="sub-title border">
          <span>退料类型</span>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">退料类型：</div>
            <div class="value">{{ data.stockOutType }}</div>
          </div>
          <div class="col">
            <div class="label">判定人员：</div>
            <div class="value">{{ data.checkUserName }}</div>
          </div>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">判定时间：</div>
            <div class="value">{{ data.checkTime }}</div>
          </div>
        </div>
        <div class="sub-title border">
          <span>退款信息</span>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">退款方式：</div>
            <div class="value">{{ data.stockBackWay === 0? '合同扣款' : '供应商退款' }}</div>
          </div>
        </div>
        <div class="row">
          <div class="col" style="align-items: center">
            <div class="label">扣款合同：</div>
            <div class="value">
              <Select v-model="balancePurBillNo" size="small">
                <Option v-for="item in data.balancePurItems" :value="item" :key="item">{{ item }}</Option>
              </Select>
<!--              <span style="padding: 0 10px;color: blue;cursor: pointer;text-decoration: underline" @click="contractVisible = true">+选择合同</span>-->
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-else>
      <div class="content">
        <div class="sub-title border">
          <span>退款信息</span>
        </div>
        <div class="row">
          <div class="col">
            <div class="value">*该供应商暂无未付款合同</div>
          </div>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">退款方式：</div>
            <div class="value">{{ data.stockBackWay === 0? '合同扣款' : '供应商退款' }}</div>
          </div>
        </div>
        <div class="row">
          <div class="col" style="align-items: center">
            <div class="label">收款账户：</div>
            <div class="value">
              <Select clearable @on-select="select" size="small">
                <Option v-for="item in bankMesList" :value="item.id" :key="item.bankAccout">{{ item.bankAccoutName || '未知' }}</Option>
              </Select>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">开户行：</div>
            <div class="value">{{ bankData.openBank }}</div>
          </div>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">户名：</div>
            <div class="value">{{ bankData.accoutName }}</div>
          </div>
        </div>
        <div class="row">
          <div class="col">
            <div class="label">账号：</div>
            <div class="value">{{ bankData.bankAccout }}</div>
          </div>
        </div>
      </div>
    </div>
    <!-- 合同列表 -->
    <contract-list-form v-model="contractVisible" @on-select="contractSelect" :keyStr="data.supplierName"></contract-list-form>
    <modal v-model="backDetailShow" width="700px">
      <div class="title" slot="header">退料详情</div>
      <div class="footer" slot="footer">
        <Button class="btn" type="primary" size="small" @click="backDetailShow = false">确定</Button>
      </div>
      <div class="content" style="padding: 20px">
        <my-table :data="data.backItems" :columns="columns" :height="300" :is-show-filter="false" :is-show-page="false"></my-table>
      </div>
    </modal>
  </modal>
</template>

<script>
import {
  GetSupplierBank
} from '@/api/baseData'
import { openPic } from '@/libs/util'
import { GetApplySceneBackData, PurchaserDealWithStockBack } from '@/api/stock'
import ContractListForm from './contract-list-form'
import MyTable from '_c/my-table/my-table'

export default {
  name: 'contract-form',
  data () {
    return {
      backDetailShow: false,
      balancePurBillNo: '',
      data: {},
      modal: false,
      contractVisible: false,
      returnTypeList: [],
      openPic,
      bankMesList: [],
      bankData: {},
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '退料数量',
          key: 'backQty',
          minWidth: 150
        },
        {
          title: '退料金额',
          key: 'backAmount',
          minWidth: 150
        },
        {
          title: '关联合同',
          key: 'purchaseBillNo',
          minWidth: 150
        },
        {
          title: '入库批号',
          key: 'batchNo',
          minWidth: 150
        },
        {
          title: '采购单价',
          key: 'price',
          minWidth: 150
        },
        {
          title: '采购数量',
          key: 'purchaseQty',
          minWidth: 150
        },
        {
          title: '退料原因',
          key: 'backReason',
          minWidth: 150
        },
        {
          title: '照片',
          key: 'imageUrls',
          minWidth: 200,
          render: (h, params) => {
            if (params.row.imageUrls) {
              let arr = params.row.imageUrls.split(',')
              let els = []
              arr.forEach(item => {
                els.push(
                  h('p', {
                    style: {
                      cursor: 'pointer',
                      color: 'blue',
                      padding: '3px 0',
                      textDecoration: 'underline'
                    },
                    on: {
                      click () {
                        openPic(item)
                      }
                    }
                  }, item.split('/')[item.split('/').length - 1])
                )
              })
              return h('div', els)
            } else {
              return h('span')
            }
          }
        }
      ]
    }
  },
  components: {
    ContractListForm,
    MyTable
  },
  props: {
    backMaterialProgress: {},
    id: {},
    value: {
      type: Boolean,
      default: false,
      require: true
    }
  },
  watch: {
    value: {
      handler (n) {
        this.modal = n
      },
      immediate: true
    },
    modal (n) {
      this.$emit('input', n)
      if (n) {
        this.bankData = {}
        this.balancePurBillNo = ''
        this.getlist()
      }
    }
  },
  methods: {
    contractSelect (row) {
      console.log('select')
      console.log(row)
      this.balancePurBillNo = row.purchaseBillNo
    },
    /* 选择供应商 */
    select (key) {
      let data = this.bankMesList.find(item => (item.id === key))
      this.bankData = data
    },
    getPurContractPageList () {
      GetSupplierBank().then(res => {
        if (res.code === 0) {
          this.bankMesList = res.result
        }
      })
    },
    cfm () {
      if (this.data.stockBackWay === 0) {
        if (!this.balancePurBillNo) {
          return this.$Message.error('请选择扣款合同')
        }
        PurchaserDealWithStockBack({
          id: this.id,
          stockBackWay: 0,
          balancePurBillNo: this.balancePurBillNo
        }).then(res => {
          if (res.code === 0) {
            this.cancel()
            this.$Message.info(res.msg || '操作成功！')
            this.$emit('getlist')
          }
        })
      } else {
        if (!this.bankData.bankAccoutName) {
          return this.$Message.error('请选择收款账户')
        }
        PurchaserDealWithStockBack({
          id: this.id,
          stockBackWay: 1,
          bankData: this.bankData
        }).then(res => {
          if (res.code === 0) {
            this.cancel()
            this.$Message.info(res.msg || '操作成功！')
            this.$emit('getlist')
          }
        })
      }
    },
    cancel () {
      this.balancePurBillNo = ''
      this.modal = false
    },
    getlist () {
      GetApplySceneBackData({
        id: this.id,
        backMaterialProgress: this.backMaterialProgress
      }).then(async res => {
        if (res.code === 0) {
          this.data = res.result
          if (this.data.stockBackWay === 1) {
            this.getPurContractPageList()
          }
        }
      })
    }
  }
}
</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: flex-start;
    .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;
    max-height: 200px;
    overflow: auto;
    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;
}
</style>
