<template>
  <a-modal
    centered
    class="chooseType-modal"
    :footer="null"
    :maskClosable="false"
    title="选择产品分类"
    v-model="isShow"
    @cancel="isShow=false"
    width="50%">
    <div class="chooseType-con">
      <div>
        <a-checkbox :checked="checkAll" :disabled="!linkageStatus&&chooseList.length>0 " @change="onCheckAllChange">全选</a-checkbox>
      </div>
      <a-divider style="margin:10px 0;" />
      <a-tree
        style="height: 400px;overflow-y: scroll;"
        checkable
        @check="onCheck"
        :checkStrictly="linkageStatus"
        v-model="checkedKeys"
        :treeData="productTypeList"
        :replaceFields="replaceFields"
      />
      <!-- 按钮 -->
      <div class="btn-con">
        <a-button
          id="chooseType-cancel"
          class="button-cancel"
          @click="isShow=false"
          style="padding: 0 30px;">取消</a-button>
        <a-button
          type="primary"
          id="chooseType-submit"
          @click="handleSave"
          style="padding: 0 30px;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: ''
    },
    linkageStatus: { // 父子是否联动
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      checkedKeys: [], //  选中项
      checkedRows: [], //  选中项  整条数据
      checkAll: false, //  全选
      indeterminate: true,
      replaceFields: {
        children: 'children',
        title: 'productTypeName',
        key: 'productTypeSn'
      },
      productTypeList: [],
      chooseList: []
    }
  },
  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) {
            if (!this.linkageStatus && this.chooseData.length > 0) {
              this.handleDisabled(res.data, this.chooseData)
            }
            if (!this.linkageStatus && this.chooseList.length > 0) {
              this.handleDisabled(res.data, this.chooseList)
            }
            this.productTypeList = res.data
          } else {
            this.productTypeList = []
          }
        })
      }
    },
    // 保存
    handleSave () {
      if (this.checkedKeys.checked && this.checkedKeys.checked.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)
          }
        })
      }
    },
    handleAllDisabled (list) {
      this.chooseList = list
    },
    // 禁用递归
    handleDisabled (data, selectData) {
      if (data) {
        data.map(item => {
          const posNum = selectData.findIndex(con => con.goodsSn == item.productTypeSn)
          if (posNum != -1) {
            item = Object.assign(item, selectData[posNum])
          }
          if (item.children && item.children.length > 0) {
            this.handleDisabled(item.children, selectData)
          }
        })
      }
    },
    onCheck (checkedKeys, info) {
      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 {
        this.getProductType()
        const _this = this
        this.checkedRows = this.chooseData
        this.checkedKeys = []
        this.chooseData.forEach(item => {
          _this.checkedKeys.push(item.goodsSn)
        })
      }
    }

  }
}
</script>

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