<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" :disabled="disabledList.length>0" @change="onCheckAllChange">全选</a-checkbox>
        <span v-if="pageType==='1'">
          <a-checkbox :checked="checkOwn" :disabled="disabledList.length>0" @change="onCheckOwnChange">自主品牌</a-checkbox>
          <a-checkbox :checked="checkAgent" :disabled="disabledList.length>0" @change="onCheckAgentChange">代理品牌</a-checkbox>
        </span>
      </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" :disabled="item.isDisabled">
              {{ 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 []
      }
    },
    pageType: { //  弹框显示状态
      type: [String, Number],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      checkAll: false, //  全选
      checkOwn: false, // 自主品牌
      checkAgent: false, // 代理品牌
      checkedList: [], //  选中项
      checkedRowList: [], //  选中项
      brandList: [], //  品牌数据
      brandOwnList: [],
      brandAgentList: [],
      disabledList: []
    }
  },
  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)
        }
      })
      if (this.pageType == '1') {
        const newCheckList = []
        this.brandList.forEach(con => {
          if (checkedList.includes(con.brandSn)) {
            newCheckList.push(con)
          }
        })
        const obj = _this.handleArr(newCheckList)
        this.checkOwn = obj.ownList.length === this.brandOwnList.length
        this.checkAgent = obj.agentList.length === this.brandAgentList.length
      }
    },
    // 全选
    onCheckAllChange (e) {
      const _this = this
      this.checkAll = e.target.checked
      this.checkOwn = e.target.checked
      this.checkAgent = 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 = []
      }
    },
    onCheckOwnChange (e) {
      const _this = this
      this.checkOwn = e.target.checked
      if (e.target.checked) {
        this.checkedList = []
        this.checkedRowList = []
        this.brandList.map(item => {
          if (item.brandType == 1) {
            _this.checkedList.push(item.brandSn)
            _this.checkedRowList.push(item)
          }
        })
      } else {
        const noArr = this.getDifferData(this.checkedList, this.brandOwnList)
        _this.checkedList = noArr
      }
      _this.onChange(_this.checkedList)
    },
    getDifferData (list1, list2) {
      list2.forEach(item => {
        const pos = list1.indexOf(item.brandSn)
        if (pos != -1) {
          list1.splice(pos, 1)
        }
      })
      return list1
    },
    onCheckAgentChange (e) {
      const _this = this
      this.checkAgent = e.target.checked
      if (e.target.checked) {
        this.checkedList = []
        this.checkedRowList = []
        this.brandList.map(item => {
          if (item.brandType == 2) {
            _this.checkedList.push(item.brandSn)
            _this.checkedRowList.push(item)
          }
        })
      } else {
        const noArr = this.getDifferData(this.checkedList, this.brandAgentList)
        _this.checkedList = noArr
      }
      _this.onChange(_this.checkedList)
    },
    // 获取品牌数据
    getBrandList () {
      productBrandQuery({}).then(res => {
        if (res.status == 200) {
          const obj = this.handleArr(res.data)
          this.brandList = res.data
          this.brandOwnList = obj.ownList
          this.brandAgentList = obj.agentList
        } else {
          this.brandList = []
        }
      })
    },
    handleArr (list) {
      const ownList = []
      const agentList = []
      list.forEach(item => {
        if (item.brandType == 1) {
          ownList.push(item)
        } else {
          agentList.push(item)
        }
      })
      return { ownList, agentList }
    },
    // 禁用
    handleDisabled (list) {
      this.disabledList = list
      this.brandList.map(item => {
        if (list.includes(item.brandSn)) {
          item.isDisabled = true
        } else {
          item.isDisabled = false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.checkAll = false
        this.checkOwn = false
        this.checkAgent = 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>
