<template>
  <!-- 关联经销商 -->
  <a-drawer
    title="选择经销商"
    placement="right"
    closable
    :visible="isShow"
    class="lookUpCustomers-modal"
    @close="isShow=false"
    width="72%">
    <div class="dealerModalCon">
      <chooseDealer ref="dealerChoose" pageType="tireStorage" @plAdd="handleAddDealer"></chooseDealer>
    </div>
  </a-drawer>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import chooseDealer from '@/views/dealerManagement/provincialWarehouseManagement/chooseDealer.vue'
import { saveBatchDealerList } from '@/api/provinceServiceCenter'

export default {
  name: 'AddDealerModal',
  mixins: [commonMixin],
  components: { chooseDealer },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isShow: this.openModal //  是否打开弹框
    }
  },
  methods: {
    handleAddDealer (list) {
      const newList = list.map(item => {
        return {
          dealerSn: item.dealerSn,
          dealerName: item.dealerName,
          dealerLevel: item.dealerLevel
        }
      })
      this.spinning = true
      saveBatchDealerList(newList).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
        }
        this.spinning = false
        this.$emit('ok')
        this.isShow = false
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$refs.dealerChoose.resetSearchForm()
        this.$refs.dealerChoose.clearSelect()
        this.$emit('close')
      } else {
        this.$nextTick(() => {
          this.$refs.dealerChoose.pageInit()
        })
      }
    }
  }
}
</script>
