<template>
  <div class="setGird">
    <div class="header-middle">
      <div class="hint">
        <span v-for="(item, index) in slectList" :key="index"><span v-if="index" style="margin:0 4px;">/</span><span
            style="color:#3F8DF5" @click="girdNameClick(item, index)">{{ item.girdName }}</span></span>
      </div>

      <div class="showTypes" v-if="!userList.length">
        <div v-if="treeList.length > 0">
          <div class="cards" v-for="(item, index) in treeList" :key="index">
            <div class="imges">
              <span v-if="item.girdRight==1">
                <img src="./components/img/xzh.png" alt="" class="imgselect" v-if="item.isChecked"
                     @click.stop="girdClick(item, index)"/>
                <img src="./components/img/xz.png" alt="" class="imgselect" v-else
                     @click.stop="girdClick(item, index)"/>
              </span>
              <span v-else style="width:24px;"></span>
              <img src="./components/img/gird--select-icon.png" alt="" class="avatras"/>
            </div>
            <div class="rightes fill">
              <div class="applicationNames fill" @click="showGirdInfo(item)">{{ item.girdName }}</div>
              <u-icon v-if="item.hasChildren" @click="itemClick(item)" name="arrow-right" color="#ddd"/>
            </div>
          </div>
        </div>

        <AiEmpty description="暂无数据" class="emptyWrap" v-else/>
      </div>
    </div>
    <!-- <div style="padding-bottom: 70px;"></div> -->
    <div class="subBtn" @click="toAddGird">
      <div>添加网格</div>
    </div>
  </div>
</template>

`
<script>
export default {
  name: 'setGird',
  data() {
    return {
      setGird: {},
      allData: null,
      treeList: [],
      slectList: [],
      userList: [],
    }
  },
  onLoad() {
    this.isGirdUser()
    uni.$on('update', () => {
      this.getAllGrids()
    })
  },
  onShow() {
    document.title = '网格配置'
  },
  methods: {
    isGirdUser() {
      this.$http.post('/app/appgirdmemberinfo/checkLogOnUser').then((res) => {
        if (res.code == 0) {
          if (res.data.checkType) {
            this.userGird = res.data
            this.getAllGrids()
          } else {
            this.$u.toast('当前人员不是网格员或网格管理员')
          }
        }
      })
    },
    getAllGrids() {
      this.slectList = []
      let {girdMemberId} = this.userGird
      this.$http.post(`/app/appgirdinfo/queryAppGirdInfoByGirdLevel`, null, {
        params: {girdMemberId}
      }).then((res) => {
        if (res?.data) {
          let parents = res.data.map(e => e.parentGirdId)
          this.allData = res.data.map(e => ({...e, hasChildren: parents.includes(e.id)}))
          this.treeInit()
        }
      })
    },
    treeInit() {
      this.treeList = this.allData.filter(e => !e.parentGirdId)
      this.treeList.map((item) => {
        item.isChecked = false
      })
      let obj = {
        girdName: '可选范围',
        id: '',
      }
      this.slectList.push(obj)
    },

    itemClick(row) {
      let obj = {
        girdName: row.girdName,
        id: row.id,
      }

      this.slectList.push(obj)
      this.getGridsByGridMemberAndParent(row)
    },

    getGridsByGridMemberAndParent(row) {
      let {id: parentGirdId} = row
      this.setGird = {}
      this.treeList = this.allData.filter(e => e.parentGirdId == parentGirdId)
    },

    girdNameClick(row, index) {
      this.userList = []
      if (!index) { //第一级别
        this.slectList = []
        this.treeInit()
      } else {
        var list = []
        this.slectList.map((item, i) => {
          if (i <= index) {
            list.push(item)
          }
        })
        this.slectList = list
        this.getGridsByGridMemberAndParent(row)
      }
    },


    girdClick(row, index) {
      if (this.treeList[index].isChecked) {//取消
        this.treeList[index].isChecked = false
        this.setGird = {}
      } else {
        this.treeList.map((item) => {
          item.isChecked = false
        })
        this.treeList[index].isChecked = true
        this.setGird = row
      }
      this.$forceUpdate()
    },

    showGirdInfo(row) {
      uni.navigateTo({url: `./gridMembers?id=${row.id}`})
    },


    callPhone(phone) {
      uni.makePhoneCall({phoneNumber: phone})
    },

    toAddGird() {
      if (this.setGird.id != null) {
        uni.navigateTo({url: `./AddGird?id=${this.setGird.id}&fromType=add`})
      } else {
        return this.$u.toast('请选择网格')
      }
    }
  }
}
</script>

<style scoped lang="scss">
.setGird {
  height: 100%;
  background: #fff;
  padding-bottom: 140px;

  .header-top {
    background: #fff;
    padding: 20px 32px;
  }

  .header-middle {
    .hint {
      padding: 28px 20px 28px 32px;
      line-height: 56px;
      box-shadow: 0px 1px 0px 0px #e4e5e6;
      font-size: 30px;
      font-weight: 500;
      word-break: break-all;
    }

    .showTypes {
      .empty-div {
        height: 16px;
        background: #f5f5f5;
      }

      .cards {
        display: flex;
        align-items: center;
        height: 120px;
        line-height: 120px;
        // background: pink;
        padding: 0 0 0 32px;

        .imges {
          display: flex;
          align-items: center;
          // width: 200px;
          .imgselect {
            width: 48px;
            height: 48px;
            vertical-align: middle;
          }

          .avatras {
            width: 74px;
            height: 74px;
            border-radius: 8px;
            margin: 0 20px 0 36px;
          }
        }

        img {
          width: 74px;
          height: 74px;
          border-radius: 8px;
        }

        .rightes {
          display: flex;
          border-bottom: 1px solid #e4e5e6;
          padding-right: 16px;

          .applicationNames {
            display: inline-block;
            font-size: 36px;
            font-weight: 500;
            color: #333333;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            vertical-align: bottom;
          }

          .right-icon {
            display: inline-block;
            width: 100px;
            box-sizing: border-box;
            text-align: center;
          }

          .imgs {
            width: 40px;
            height: 40px;
            margin-right: 0;
            vertical-align: middle;
          }
        }
      }
    }

    .showUsers {
      .cards {
        display: flex;
        align-items: center;
        height: 120px;
        line-height: 120px;
        // background: pink;
        padding: 0 0 0 32px;

        .imges {
          display: flex;
          align-items: center;
          width: 200px;

          .imgselect {
            width: 48px;
            height: 48px;
          }

          .avatras {
            width: 74px;
            height: 74px;
            border-radius: 8px;
            margin-left: 36px;
          }
        }

        .rights {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-left: 32px;
          border-bottom: 1px solid #e4e5e6;
          padding-right: 40px;

          .applicationNames {
            font-size: 36px;
            font-weight: 500;
            color: #333333;
          }

          .idNumbers {
            color: #666;
          }
        }
      }
    }
  }

  .subBtn {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 118px;
    background: #f4f8fb;

    div {
      width: 192px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      background: #1365dd;
      border-radius: 4px;
      font-size: 32px;
      color: #fff;
      margin: 20px 34px 0 0;
      float: right;
    }
  }

  .popup {
    padding: 0 32px 16px;

    .bg {
      width: 64px;
      height: 10px;
      background: #CCC;
      border-radius: 6px;
      margin: 32px 0 32px 344px;
    }


  }
}
</style>
