<template>
  <a-drawer
    :zIndex="zIndex"
    :title="title"
    placement="right"
    :visible="isShow"
    width="80%"
    :get-container="false"
    :wrap-style="{ position: 'absolute' }"
    :headerStyle="{ padding: '10px' }"
    wrapClassName="chooseProducts-modal"
    @close="isShow=false"
  >
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="products-con">
        <div>
          <!-- 查询配件列表 -->
          <!-- 正常产品 -->
          <normalQueryPart v-if="cptype==0" ref="partQuery" :newLoading="isInster" @add="insterProduct"></normalQueryPart>
          <!-- 活动产品 -->
          <activeQueryPart v-if="cptype==1" ref="partQuery" :newLoading="isInster" @add="insterProduct"></activeQueryPart>
          <!-- 累计产品 -->
          <cumulativeQueryPart v-if="cptype==2" ref="partQuery" :newLoading="isInster" @add="insterProduct"></cumulativeQueryPart>
        </div>
      </div>
    </a-spin>
  </a-drawer>
</template>

<script>
import normalQueryPart from './normalQueryPart.vue'
import activeQueryPart from './activeQueryPart.vue'
import cumulativeQueryPart from './cumulativeQueryPart.vue'
export default {
  name: 'ChooseProductsModal',
  components: { normalQueryPart, activeQueryPart, cumulativeQueryPart },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    chooseData: { // 已选数据
      type: Array,
      default: () => {
        return []
      }
    },
    isInster: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    zIndex: { // 层级
      type: [String, Number],
      default: 1000
    }
  },
  data () {
    return {
      spinning: false,
      cptype: 3, // 组件类型
      isShow: this.openModal, //  是否打开弹框
      orderData: null, // 订单数据
      promoData: null, //  促销活动数据
      title: '选择产品',
      hasRefash: false // 是否刷新
    }
  },

  methods: {
    // 关闭弹框
    onClose () {
      this.$emit('close', this.hasRefash, this.cptype)
    },
    // 添加产品，
    // row 产品信息, promoProductClz 促销产品类型, 0 正常产品
    insterProduct (row, promoProductClz) {
      this.hasRefash = true // 添加时不刷新，关闭弹框再刷新
      this.spinning = true
      this.$emit('addProduct', row, this.promoData, promoProductClz, this.cptype)
    },
    // 初始化数据，
    // data 销售单详情数据， promo 促销活动规则数据， type 组件类型
    pageInit (data, promo, type) {
      this.cptype = type
      this.orderData = data
      this.promoData = promo
      this.title = type == 2 ? '累计产品' : '选择产品'
      this.$nextTick(() => {
        this.$refs.partQuery.pageInit(data, promo)
      })
    },
    clear () {
      if (this.cptype == 2) {
        this.$refs.partQuery.clear()
      }
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.onClose()
        this.cptype = 3
      } else {
        this.hasRefash = false
      }
    }
  }
}
</script>

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