<template>
  <a-modal
    centered
    class="chooseProducts-modal"
    :footer="null"
    :maskClosable="false"
    title="选择产品"
    v-model="isShow"
    @cancel="isShow=false"
    width="70%">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="products-con">
        <div>
          <!-- 查询配件列表 -->
          <queryPart
            ref="partQuery"
            :showRemark="true"
            :showReceiveQty="true"
            :isShowPrice="isShowPrice"
            :returnReasonList="returnReasonList"
            :newLoading="isInster"
            @add="saveProduct"></queryPart>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import queryPart from '@/views/salesReturnManagement/salesReturn/queryPart.vue'
export default {
  name: 'ChooseProductsModal',
  components: { queryPart },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    chooseData: {
      type: Array,
      default: () => {
        return []
      }
    },
    buyerSn: {
      type: String,
      default: ''
    },
    warehouseSn: {
      type: String,
      default: ''
    },
    isShowPrice: { //  弹框显示状态
      type: Boolean,
      default: true
    },
    goodFlag: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      returnReasonList: [],
      isInster: false //  表格加载中
    }
  },
  methods: {
    // 添加或修改产品
    saveProduct (row) {
      // 防止多次添加产品
      if (this.isInster) {
        return
      }
      if (!row.receiveQty) {
        this.$message.info('请输入仓库实收数量')
        return
      }
      if (!row.returnReasonCode) {
        this.$message.info('请选择退货原因')
        return
      }
      const rows = this.returnReasonlist.find(item => item.code == row.returnReasonCode)
      const params = {
        'price': row.productPrice,
        'returnReasonCode': row.returnReasonCode,
        'returnReason': rows ? rows.dispName : '',
        'returnReasonRemarks': row.returnReasonRemarks,
        'productSn': row.productSn,
        'priceLevel': row.priceLevel,
        'initialQty': row.qty,
        'receiveQty': row.receiveQty
      }
      this.isInster = true
      this.$emit('addProduct', params)
    },
    resetLoading () {
      this.isInster = false
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        if (this.goodFlag == 'DEFECTIVE_PRODUCT_RETURN') {
          this.returnReasonlist = this.$store.state.app.defectiveReturnReason
        }
        // 良品
        if (this.goodFlag == 'GOOD_PRODUCT_RETURN') {
          this.returnReasonlist = this.$store.state.app.goodReturnReason
        }
        // 全部
        if (!this.goodFlag) {
          this.returnReasonlist = [...this.$store.state.app.goodReturnReason, ...this.$store.state.app.defectiveReturnReason]
        }
        this.$nextTick(() => {
          this.$refs.partQuery.pageInit(this.buyerSn, this.warehouseSn, this.goodFlag)
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .chooseProducts-modal{
    .products-con{
      .btn-con{
        text-align: center;
        margin: 30px 0 20px;
        .button-cancel{
          font-size: 12px;
        }
      }
    }
  }
</style>
