<!-- 遮罩层 -->
<template name="mask">
  <view class="warp-mask">
    <view class="mask">
      <view class="maskbgc"></view>
      <view class="content">
        <view class="header">
          <view class="title">菜单权限</view>
          <image
            src="../../static/images/closeicon.png"
            class="iconclose"
            @tap="closeMask"
          />
        </view>
        <view class="content-warp">
          <view class="message">
            <view class="radiolist">
              <view class="checked-box-warp">
                <view class="father-warp">
                  <view class="father-item" v-for="fatheritem in menuList" :key='fatheritem.id'>
                    <view class="fatherbar" @tap.stop='chechedFather(fatheritem.id, fatheritem.children)'>

                      <view class="icon active" v-if="hasMenuList.includes(fatheritem.id)"></view>
                      <view class="icon" v-else></view>

                      <view class="title">{{ fatheritem.name }}</view>
                    </view>
                    <view class="son-warp">
                      <view class="son-item" v-for="sonitem in fatheritem.children" :key="sonitem.id">
                        <view class="sonerbar" @tap.stop='chechedSon(sonitem.id,fatheritem)'>

                          <view class="icon active" v-if="hasMenuList.includes(sonitem.id)"></view>
                          <view class="icon" v-else></view>

                          <view class="title">{{ sonitem.name }}</view>
                        </view>
                      </view>
                    </view>
                  </view>
                </view>
              </view>
             
            </view>
          </view>
          <view class="sure">
            <view class="btn" @tap="sureconver">确认</view>
          </view>
        </view>
      </view>
    </view>

		<rf-loading v-if="!menuList"></rf-loading>

  </view>
</template>

<script>
import editclassifyVue from '../goods/editclassify.vue';
export default {
  name: 'mask',
  data () {
    //这里存放数据
    return {
      menuList: uni.getStorageSync('menuList') ,
      hasMenuList: uni.getStorageSync('hasMenuList') || []
    }
  },
  //方法集合
  methods: {
    // 确认
    sureconver () {
      this.$emit('isSureConver', true)
    },
    // 关闭
    closeMask () {
      this.$emit('isSureConver', false)
    },
    // 父级权限
    chechedFather (num,children) {
      let fatherArr = [num]
      let childrenArr = []
      children.forEach(element => {
        if (element.id) {
          childrenArr.push(element.id)
        }
      });
      if (this.hasMenuList.includes(num)) {
        let index = this.hasMenuList.length
        let sonindex = childrenArr.length
        while (index --) {
          // 得到下标后删除
          if (this.hasMenuList[index] === num) {
            this.hasMenuList.splice(index,1)
          }
          for (let i = 0; i < childrenArr.length; i++) {
            if (this.hasMenuList[index] === childrenArr[i]) {
             this.hasMenuList.splice(index,1)
            }
          }
        }
      } else {
        this.hasMenuList = this.hasMenuList.concat(fatherArr,childrenArr)
      }
      console.log(this.hasMenuList);
      this.$mStore.commit('setHasRoleList',this.hasMenuList)
    },
    // 子权限
    chechedSon (num,father) {

      let temp = []
      let childrenArr = []
      father.children.forEach(element => {
        if (element.id) {
          childrenArr.push(element.id)
        }
      });
      // 有权限，点击删除权限
      if (this.hasMenuList.includes(num)) {
        let index = this.hasMenuList.length
        while (index--) {
          if (this.hasMenuList[index] === num) {
            this.hasMenuList.splice(index,1)
          }
        }
        for (const item of childrenArr) {
          this.hasMenuList.includes(item) ? temp.push(item) : ''
        }
        // if (temp.length === 0) {
        //   for (let i = 0; i < this.hasMenuList.length; i++) {
        //     if (this.hasMenuList[i] === father.id) {
        //       this.hasMenuList.splice(i,1)
        //     }
        //   }
        // }
      } else {
        this.hasMenuList.push(num)
        for (const item of childrenArr) {
          this.hasMenuList.includes(item) ? temp.push(item) : ''
        }
        
        if ( !this.hasMenuList.includes(father.id)) {
          this.hasMenuList.push(father.id)
        }
       
      }
      console.log(this.hasMenuList);
      this.$mStore.commit('setHasRoleList',this.hasMenuList)
    }


  }
};
</script>
<style lang='scss' scoped>
.warp-mask {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}
.mask {
  position: relative;
  width: 100vw;
  height: 100vh;

  .maskbgc {
    position: absolute;
    bottom: 0;
    top: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.4);
  }
  .content {
    position: absolute;
    top: 20%;
    left: 50%;
    margin-left: -320upx;
    width: 640upx;
    z-index: 101;
    box-sizing: border-box;
    overflow: hidden;
    .header {
      position: relative;
      width: 100%;
      height: 102upx;
      border-top-left-radius: 20upx;
      border-top-right-radius: 20upx;
      background-color: #ffcd00;
      .title {
        text-align: center;
        font-weight: 600;
        line-height: 102upx;
        color: #0f1e1a;
        font-size: 36upx;
        letter-spacing: 2upx;
      }
      .iconclose {
        position: absolute;
        right: 24upx;
        top: 50%;
        margin-top: -15upx;
        height: 31upx;
        width: 31upx;
      }
    }
    .content-warp {
      background-color: #fff;
      border-bottom-left-radius: 20upx;
      border-bottom-right-radius: 20upx;
      .message {
        padding: 40upx 60upx 15upx;
        overflow-y: scroll;
        height: 670upx;
        .radiolist {
          .checked-box-warp {
            .father-warp {
              .father-item{
                display: flex;
                flex-direction: column;
                margin-bottom: 40upx;
                .fatherbar{
                  display: flex;
                  align-items: center;
                  .icon{
                    width: 38upx;
                    height: 38upx;
                    border-radius: 50%;
                    background: url('../../static/images/checked-box.png') no-repeat center;
                    background-size: 100%;
                  }
                  .active {
                    background: url('../../static/images/checked-active.png') no-repeat center;
                    background-size: 100%;
                  }
                  .title{
                    margin-left: 30upx;
                    color: #444444;
                    font-size: 30upx;
                  }
                }
                .son-warp {
                  display: flex;
                  flex-flow: wrap;
                  margin-left: 45upx;
                  .son-item {
                    margin-top: 34upx;
                    width: 45%;
                    &:nth-child(odd){
                      margin-right: 35upx;
                    }
                    .sonerbar{
                      display: flex;
                      align-items: center;
                      .icon{
                        width: 38upx;
                        height: 38upx;
                        border-radius: 50%;
                        background: url('../../static/images/checked-box.png') no-repeat center;
                        background-size: 100%;
                      }
                      .active {
                        background: url('../../static/images/checked-active.png') no-repeat center;
                        background-size: 100%;
                      }
                      .title{
                        margin-left: 30upx;
                        font-size: 28upx;
                        color: #666666;
                      }
                    }
                  }
                }
              }
            }
          }
          /*.father-list {
            .checked-bar {
              display: flex;
              flex-direction: column;
              justify-content: flex-end;
              margin-top: 45upx;
              .main-checked-list{
                display: flex;
                width: 100%;
                // flex: 1;
                .title {
                  margin-left: 30upx;
                  color: #666666;
                  font-size: 32upx;
                }
              }
              .moreicon{
                width: 19upx;
                height: 27upx;
              }
              .downicon {
                width: 27upx;
                height: 19upx;
              }
              .childemenu {
                width: 100%;
                .son-checked-warp {
                  display: flex;
                  flex-wrap: wrap;
                  padding-left: 40upx;
                  box-sizing: border-box;
                  .son-bar {
                    width: 45%;
                    display: flex;
                    align-items: center;
                    box-sizing: border-box;
                    &:nth-child(odd){
                      margin-right: 35upx;
                    }
                    .son-checked-list {
                      display: flex;
                    }
                  }
                }
                .son-title {
                  margin-left: 15upx;
                  color: #666666;
                }
              }
            }
          }
          */
        }
      }
      .sure {
        padding-bottom: 40upx;
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;
        .btn {
          width: 280upx;
          height: 80upx;
          background-color: #a95cff;
          color: #fff;
          font-size: 32upx;
          letter-spacing: 2upx;
          text-align: center;
          line-height: 80upx;
          border-radius: 10upx;
        }
      }
    }
  }
}
</style>