<template>
  <a-modal
    centered
    class="chooseBrand-modal"
    :footer="null"
    :maskClosable="false"
    title="选择产品品牌"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <div class="chooseBrand-con">
      <div>
        <a-checkbox :checked="checkAll" @change="onCheckAllChange">全选</a-checkbox>
      </div>
      <a-divider />
      <a-checkbox-group v-model="checkedList" @change="onChange" style="width: 100%;">
        <a-row style="height: 400px;overflow-y: scroll;">
          <a-col :span="6" v-for="item in brandList" :key="item.brandSn">
            <a-checkbox :value="item.brandSn">
              {{ item.brandName }}
            </a-checkbox>
          </a-col>
        </a-row>
      </a-checkbox-group>
      <!-- 按钮 -->
      <div class="btn-con">
        <a-button
          type="primary"
          id="chooseBrand-submit"
          size="large"
          class="button-primary"
          @click="handleSave"
          style="padding: 0 60px;">保存</a-button>
        <a-button
          id="chooseBrand-cancel"
          size="large"
          class="button-cancel"
          @click="isShow=false"
          style="padding: 0 60px;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, //  是否打开弹框
      checkAll: false, //  全选
      checkedList: [], //  选中项
      checkedRowList: [], //  选中项
      brandList: [] //  品牌数据
    }
  },
  methods: {
    // 保存
    handleSave () {
      const _this = this
      if (this.checkedList.length < 1) {
        this.$message.warning('请在列表勾选后再进行操作！')
        return
      }
      this.checkedRowList = []
      _this.brandList.map(item => {
        if (_this.checkedList.indexOf(item.brandSn) != -1) {
          _this.checkedRowList.push(item)
        }
      })
      this.$emit('ok', this.checkedRowList)
      this.isShow = false
    },
    // change
    onChange (checkedList) {
      const _this = this
      this.checkAll = checkedList.length === this.brandList.length
      this.brandList.map(item => {
        if (_this.checkedList.indexOf(item.brandSn) != -1) {
          _this.checkedRowList.push(item)
        }
      })
    },
    // 全选
    onCheckAllChange (e) {
      const _this = this
      this.checkAll = e.target.checked
      if (e.target.checked) {
        this.checkedList = []
        this.checkedRowList = []
        this.brandList.map(item => {
          _this.checkedList.push(item.brandSn)
          _this.checkedRowList.push(item)
        })
      } else {
        this.checkedList = []
        this.checkedRowList = []
      }
    },
    // 获取品牌数据
    getBrandList () {
      productBrandQuery({}).then(res => {
        if (res.status == 200) {
          this.brandList = res.data
        } else {
          this.brandList = []
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.checkAll = false
      } else {
        const _this = this
        this.checkedRowList = this.chooseData
        this.checkedList = []
        this.chooseData.map(item => {
          _this.checkedList.push(item.goodsSn)
        })
        if (this.brandList.length == 0) {
          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>
