<template>
  <a-modal
    centered
    class="chooseClassify-modal"
    :footer="null"
    :maskClosable="false"
    title="选择产品分类"
    v-model="isShow"
    @cancel="isShow=false"
    :width="680">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="checkboxAllBtn">
        <a-checkbox :indeterminate="indeterminate" :checked="checkAll" @change="onCheckAllChange">
          全选
        </a-checkbox>
      </div>
      <a-checkbox-group v-model="checkedList" :options="plainOptions" @change="onChange" />
      <div class="btn-cont">
        <a-button type="primary" id="product-category-edit-modal-save" @click="handleSave">保存</a-button>
        <a-button id="product-category-edit-modal-back" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { bindRelation } from '@/api/dealerRelation'
export default {
  name: 'ChooseClassifyModal',
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      checkedList: [],
      plainOptions: ['欧司朗', '优志旺', '阪东BANDO', '东洋', '盖达', '欧皮特(Optibelt)'],
      checkAll: false,
      indeterminate: false
    }
  },
  methods: {
    getData () {

    },
    onChange () {
      this.indeterminate = !!this.checkedList.length && this.checkedList.length < this.plainOptions.length
      this.checkAll = this.checkedList.length === this.plainOptions.length
    },
    onCheckAllChange (e) {
      Object.assign(this, {
        checkedList: e.target.checked ? this.plainOptions : [],
        indeterminate: false,
        checkAll: e.target.checked
      })
    },
    //  保存
    handleSave () {
      const _this = this
      _this.$emit('ok', _this.checkedList)
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.indeterminate = false
        this.checkAll = false
        this.checkedList = []
      } else {
        this.getData()
      }
    }
  }
}
</script>

<style lang="less">
  .chooseClassify-modal{
    .ant-modal-body {
      padding: 20px 30px;
      .checkboxAllBtn{
        border-bottom:1px solid #E9E9E9;
        padding-bottom:20px;
        margin-bottom:20px;
      }
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
