<template>
  <a-modal
    centered
    class="chooseType-modal"
    :footer="null"
    :maskClosable="false"
    title="选择产品分类"
    v-model="isShow"
    @cancle="isShow=false"
    width="50%">
    <div class="chooseType-con">
      <div>
        <a-checkbox :checked="checkAll" @change="onCheckAllChange">全选</a-checkbox>
      </div>
      <a-divider />
      <a-tree
        style="height: 400px;overflow-y: scroll;"
        checkable
        @check="onCheck"
        :checkStrictly="true"
        v-model="checkedKeys"
        :treeData="productTypeList"
        :replaceFields="replaceFields"
      />
      <!-- 按钮 -->
      <div class="btn-con">
        <a-button
          type="primary"
          id="chooseType-submit"
          size="large"
          class="button-primary"
          @click="handleSave"
          style="padding: 0 60px;">保存</a-button>
        <a-button
          id="chooseType-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 { productTypeQuery, productTypeQueryAll } from '@/api/productType'
export default {
  name: 'ChooseTypeModal',
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    chooseData: {
      type: Array,
      default: () => {
        return []
      }
    },
    type: { //  类型，促销规则promo
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      checkedKeys: [], //  选中项
      checkedRows: [], //  选中项  整条数据
      checkAll: false, //  全选
      indeterminate: true,
      replaceFields: {
        children: 'children',
        title: 'productTypeName',
        key: 'productTypeSn'
      },
      productTypeList: []
    }
  },
  methods: {
    //  产品分类  列表
    getProductType () {
      if (this.type == 'promo') {
        productTypeQueryAll({}).then(res => {
          if (res.status == 200) {
            this.productTypeList = res.data
          } else {
            this.productTypeList = []
          }
        })
      } else {
        productTypeQuery({}).then(res => {
          if (res.status == 200) {
            this.productTypeList = res.data
          } else {
            this.productTypeList = []
          }
        })
      }
    },
    // 保存
    handleSave () {
      if (this.checkedKeys.length < 1) {
        this.$message.warning('请在列表勾选后再进行操作！')
        return
      }
      this.$emit('ok', this.checkedRows)
      this.isShow = false
    },
    // 全选
    onCheckAllChange (e) {
      this.checkAll = e.target.checked
      if (e.target.checked) {
        this.checkedKeys = []
        this.checkedRows = []
        this.recursionFun(this.productTypeList)
      } else {
        this.checkedKeys = []
        this.checkedRows = []
      }
    },
    //  递归函数
    recursionFun (data) {
      if (data) {
        data.map((item, index) => {
          this.checkedKeys.push(item.productTypeSn)
          this.checkedRows.push(item)
          if (item.children && item.children.length > 0) {
            this.recursionFun(item.children)
          }
        })
      }
    },
    onCheck (checkedKeys, info) {
      // console.log('onCheck', checkedKeys, info)
      // this.checkedData = [...checkedKeys, ...info.halfCheckedKeys]
      this.checkedKeys = checkedKeys
      this.checkedRows = []
      info.checkedNodes.map((item, index) => {
        this.checkedRows.push(item.data.props)
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.checkAll = false
      } else {
        const _this = this
        this.checkedRows = this.chooseData
        this.checkedKeys = []
        this.chooseData.map(item => {
          _this.checkedKeys.push(item.goodsSn)
        })
        if (this.productTypeList.length == 0) {
          this.getProductType()
        }
      }
    }
  }
}
</script>

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