<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" @change="onCheckOwnChange">自主品牌</a-checkbox>
          <a-checkbox :checked="checkAgent" @change="onCheckAgentChange">代理品牌</a-checkbox>
        </span>
      </div>
      <a-divider />
      <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
          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
      this.checkedRowList = []
      _this.brandList.map(item => {
        if (item.checked) {
          _this.checkedRowList.push(item)
        }
      })
      if (this.checkedRowList.length < 1) {
        this.$message.warning('请在列表勾选后再进行操作！')
        return
      }
      this.$emit('ok', this.checkedRowList)
      this.isShow = false
    },
    // change
    onChange (e, pos) {
      this.brandList[pos].checked = e.target.checked
      // 全选
      const isCheckAllFlag = this.brandList.every(item => item.checked)
      if (isCheckAllFlag) {
        this.checkAll = true
      } else {
        this.checkAll = false
      }
      // 自主
      const isCheckOwnFlag = this.brandList.every(item => {
        if (item.brandType == 1) {
          return item.checked
        }
      })
      if (isCheckOwnFlag) {
        this.checkOwn = true
      } else {
        this.checkOwn = false
      }
      // 代理
      const isCheckAgentFlag = this.brandList.every(item => {
        if (item.brandType == 2) {
          return item.checked
        }
      })
      if (isCheckAgentFlag) {
        this.checkAgent = true
      } else {
        this.checkAgent = false
      }
      this.$forceUpdate()
    },
    // 全选
    onCheckAllChange (e) {
      const _this = this
      this.checkAll = e.target.checked
      this.checkOwn = false
      this.checkAgent = false
      if (e.target.checked) {
        this.brandList.forEach(item => {
          item.checked = true
        })
      } else {
        this.brandList.map(con => {
          con.checked = false
        })
      }
    },
    onCheckOwnChange (e) {
      const _this = this
      this.checkAll = false
      this.checkOwn = e.target.checked
      this.checkAgent = false
      const disArr = _this.chooseData.map(item => item.goodsSn)
      if (e.target.checked) {
        this.brandList.forEach(item => {
          item.checked = false
          if (item.brandType == 1) {
            item.checked = true
          }
          if (this.disabledList && this.disabledList.length > 0) {
            if (this.disabledList.includes(item.brandSn)) {
              item.checked = false
            }
          }
          if (this.chooseData && this.chooseData.length > 0) {
            if (disArr.includes(item.brandSn)) {
              item.checked = true
            }
          }
        })
      } else {
        this.brandList.forEach(con => {
          if (con.brandType == 1) {
            con.checked = false
          }
          if (this.disabledList && this.disabledList.length > 0) {
            if (this.disabledList.includes(con.brandSn)) {
              con.checked = false
            }
          }
          if (this.chooseData && this.chooseData.length > 0) {
            if (disArr.includes(con.brandSn)) {
              con.checked = true
            }
          }
        })
      }
    },
    onCheckAgentChange (e) {
      const _this = this
      this.checkAll = false
      this.checkOwn = false
      this.checkAgent = e.target.checked
      const disArr = _this.chooseData.map(item => item.goodsSn)
      if (e.target.checked) {
        this.brandList.forEach(item => {
          item.checked = false
          if (item.brandType == 2) {
            item.checked = true
          }
          if (this.disabledList && this.disabledList.length > 0) {
            if (this.disabledList.includes(item.brandSn)) {
              item.checked = false
            }
          }
          if (this.chooseData && this.chooseData.length > 0) {
            if (disArr.includes(item.brandSn)) {
              item.checked = true
            }
          }
        })
      } else {
        this.brandList.forEach(con => {
          if (con.brandType == 2) {
            con.checked = false
          }
          if (this.disabledList && this.disabledList.length > 0) {
            if (this.disabledList.includes(con.brandSn)) {
              con.checked = false
            }
          }
          if (this.chooseData && this.chooseData.length > 0) {
            if (disArr.includes(con.brandSn)) {
              con.checked = true
            }
          }
        })
      }
    },
    // 获取品牌数据
    getBrandList () {
      const _this = this
      productBrandQuery({}).then(res => {
        if (res.status == 200) {
          _this.brandList = res.data
          if (_this.chooseData && _this.chooseData.length > 0) {
            const checkedList = []
            _this.chooseData.map(item => {
              checkedList.push(item.goodsSn)
            })
            this.brandList.map(item => {
              if (checkedList.includes(item.brandSn)) {
                item.checked = true
              } else {
                item.checked = false
              }
            })
          }
        } else {
          this.brandList = []
        }
      })
    },
    // 禁用
    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
        this.brandList.map(item => {
          item.checked = false
        })
      } 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>
