<template>
  <a-modal
    centered
    class="chooseBrand-modal"
    :footer="null"
    :maskClosable="false"
    title="选择产品品牌"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <div class="chooseBrand-con">
      <a-row style="height: 400px;overflow-y: scroll;">
        <a-col :span="6" v-for="(item,index) in brandList" :key="item.brandSn">
          <a-checkbox :checked="item.checked" :disabled="item.isDisabled" @change="e=>onChange(e,index)">
            {{ item.brandName }}
          </a-checkbox>
        </a-col>
      </a-row>
      <!-- 按钮 -->
      <div class="btn-con">
        <a-button
          id="chooseBrand-cancel"
          class="button-cancel"
          @click="isShow=false"
          style="padding: 0 30px;">取消</a-button>
        <a-button
          type="primary"
          id="chooseBrand-submit"
          @click="handleSave"
          style="padding: 0 30px;margin-left: 15px;">保存</a-button>
      </div>
    </div>
  </a-modal>
</template>

<script>
import { productBrandQuery } from '@/api/productBrand'
export default {
  name: 'ChooseBrandModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    chooseData: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      brandList: [] //  品牌数据
    }
  },
  methods: {
    // 保存
    handleSave () {
      const _this = this
      const checkedRowList = _this.brandList.filter(item => item.checked)
      if (checkedRowList.length < 1) {
        _this.$message.warning('请在列表勾选后再进行操作！')
        return
      }
      _this.$emit('ok', checkedRowList)
      _this.isShow = false
    },
    // change
    onChange (e, pos) {
      this.brandList.map(item => {
        item.checked = false
        return item
      })
      this.brandList[pos].checked = e.target.checked
      this.$forceUpdate()
    },
    // 获取品牌数据
    getBrandList () {
      const _this = this
      productBrandQuery({}).then(res => {
        if (res.status == 200) {
          if (_this.chooseData && _this.chooseData.length > 0) {
            const checkedList = _this.chooseData.map(item => item.dataSn)
            res.data.map(item => {
              item.checked = checkedList.includes(item.brandSn)
            })
          }
          _this.brandList = res.data
        } else {
          _this.brandList = []
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        const _this = this
        _this.getBrandList()
      }
    }
  }
}
</script>

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