<template>
  <div class="SelectUser">
    <u-navbar back-icon-color="#000" title="选择人员" title-color="#000" title-width="300" title-size="32" 
      :title-bold="true" :background="backgroundNavbar" :is-fixed="true" height="44"></u-navbar>
    <div class="header-content-bg">
      <img src="https://cdn.sinoecare.com/i/2024/07/12/6690a1303d423.png" alt="">
    </div>
    <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" @click="itemClick(item, index)">
            <div class="imges">
              <!-- <span>
                <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> -->
              <img src="https://cdn.sinoecare.com/i/2024/07/18/66987f1968182.png" alt="" class="avatras" />
            </div>
            <div class="rightes">
              <div class="applicationNames">{{ item.girdName }}</div>
              <img src="https://cdn.sinoecare.com/i/2024/07/18/66987f19bf532.png" alt="" class="imgs" />
            </div>
          </div>
        </div>
        <AiEmpty description="暂无数据" class="emptyWrap" v-else></AiEmpty>
      </div>

      <div class="showUsers" v-else>
        <div v-if="userList.length > 0">
          <div class="cards" v-for="(e, index) in userList" :key="index">
            <div class="imges">
              <img src="https://cdn.sinoecare.com/i/2024/07/16/6695ddf91113c.png" alt="" class="imgselect" v-if="e.isChecked" @click="userClick(e, index)" />
              <img src="https://cdn.sinoecare.com/i/2024/07/16/6695ddf890b1e.png" alt="" class="imgselect" v-else @click="userClick(e, index)" />

              <img src="https://cdn.sinoecare.com/i/2024/07/16/6695df9713aee.png" alt="" class="avatras" />
            </div>

            <div class="rights">
              <div class="applicationNames">{{ e.name }}</div>
              <div class="idNumbers">{{ e.phone }}</div>
            </div>
          </div>
        </div>

        <AiEmpty description="暂无数据" class="emptyWrap" v-else></AiEmpty>
      </div>
    </div>

    <div class="subBtn" @click="submit">
      <div>确定选择</div>
    </div>

  </div>
</template>

<script>
export default {
  name: 'SelectUser',
  customNavigation: true,
  data() {
    return {
      selectUser: {},
      allData: null,
      treeList: [],
      slectList: [],
      userList: [],
      detailId: '',
      backgroundNavbar: {
        background: 'url(https://cdn.sinoecare.com/i/2024/07/12/6690a1309c7d3.png) no-repeat',
        backgroundSize: 'cover',
      },
    }
  },
  onLoad(option) {
    this.detailId = option.detailId
    this.getTree()
  },
  onShow() {
    // document.title = '选择人员'
  },
  methods: {
    getTree() {
      this.slectList = []
      this.$instance.post(`/app/apppatrolreportinfov2/listGirdInfoByTransfer?id=${this.detailId}`).then((res) => {
        if (res?.data) {
          this.allData = res.data
          this.treeInit()
        }
      })
    },

    treeInit() {
      // if(this.allData[0].girdLevel == 2) {
      //   if(this.allData[0].girdMemberList && this.allData[0].girdMemberList.length) {
      //       this.userList = this.allData[0].girdMemberList
      //       this.userList.map((item) => {
      //       item.isChecked = false
      //     })
      //   }
      // }else {
      //   this.treeList = this.allData[0].girdList
      // }
      var obj = {
        girdName: '可选范围',
        id: '',
      }
      this.slectList.push(obj)
      this.treeList = this.allData
    },

    itemClick(row, index) {
      if(!this.treeList[index].girdMemberList.length) {
        return this.$u.toast('该网格下暂无网格员')
      }
      var obj = {
        girdName: row.girdName,
        id: row.id,
      }
      this.slectList.push(obj)
      this.searckGird(index)
    },

    searckGird(index) {
      this.userList = this.treeList[index].girdMemberList
    },

    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.searckGird(row)
      }
    },
    

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

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

    submit() {
      if (this.selectUser.id != null) {
        uni.$emit('goback', this.selectUser)
        uni.navigateBack()
      } else {
        return this.$u.toast('请选择网格员')
      }
    },
  }
}
</script>

<style scoped lang="scss">
.SelectUser {
  height: 100%;
  .header-content-bg {
    width: 100%;
    position: fixed;
    z-index: -1;
    img {
      width: 100%;
      height: 592px;
    }
  }
  .header-top {
    background: #fff;
    padding: 20px 32px;
  }

  .header-middle {
    padding-bottom: 140px;
    width: calc(100% - 64px);
    margin: 16px 0 0 32px;
    background-color: #fff;
    border-radius: 16px;
    .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-left: 36px;
          }
        }
        img {
          width: 74px;
          height: 74px;
          border-radius: 8px;
        }
        .rightes {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-left: 32px;
          border-bottom: 1px solid #e4e5e6;
          .applicationNames {
            font-size: 36px;
            font-weight: 500;
            color: #333333;
          }
          .imgs {
            width: 40px;
            height: 40px;
            margin-right: 20px;
          }
        }
      }
    }

    .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;
    }
  }
}
</style>
