<template>
  <div class="SelectUser">
    <div class="header-middle">
      <div class="hint">
        <u-search v-model="name" placeholder="请输入姓名" :show-action="false" bg-color="#F5F5F5" search-icon-color="#999" color="#999" height="58" @search="getListInit" :clearabled="false"></u-search>
      </div>

      <div class="showUsers">
        <div v-if="userList.length > 0">
          <div class="cards" v-for="(e, index) in userList" :key="index">
            <div class="imges">
              <img src="./components/img/xzh.png" alt="" class="imgselect" v-if="e.isChecked" @click="userClick(index)" />
              <img src="./components/img/xz.png" alt="" class="imgselect" v-else @click="userClick(index)" />

              <img src="./components/img/tx@2x.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 style="pad-b120"></div>
    <div class="subBtn" @click="submit">
      <div>确定选择</div>
    </div>

  </div>
</template>

<script>
export default {
  name: 'SelectUser',
  data() {
    return {
      selectUserList: [],
      userList: [],
      name: '',
      current: 1
    }
  },
  onLoad() {
    this.selectUserList = uni.getStorageSync('selectUserList')
    this.selectUserList.map((item) => {
      item.id =item.wxUserId
      item.mobile = item.phone
      item.avatar = item.photo
    })
    this.getList()
  },
  methods: {
    getListInit() {
      this.userList = []
      this.current = 1
      this.getList()
    },
    getList() {
      this.$http.post(`/app/wxcp/wxuser/list?name=${this.name}&current=${this.current}&size=10000`).then((res) => {
        if (res?.data) {
          res.data.records.map((item) => {
            item.isChecked = false
          })

          this.userList = this.current > 1 ? [...this.userList, ...res.data.records] : res.data.records
          
          this.userList.map((item) => {
            this.selectUserList.map((e) => {
              if(e.id == item.id) {
                item.isChecked = true
              }
            })
          })
        }
      })
    },

    userClick(index) {
      this.userList[index].isChecked = !this.userList[index].isChecked
      this.$forceUpdate()
    },

    submit() {
      var selectUserList = []
      this.userList.map((item) => {
        if(item.isChecked) {
          selectUserList.push(item)
        }
      })
      uni.$emit('selectUser', selectUserList)
      uni.navigateBack()
      // if (!selectUserList.length) {
      //   return this.$u.toast('请选择人员')
      // } else {
       
      // }
    },
  },
  onReachBottom() {
    this.current ++
    this.getList()
  }
}
</script>

<style scoped lang="scss">
.SelectUser {
  height: 100%;
  padding-bottom: 140px;
  background: #fff;
  .pad-b120{
    padding-bottom: 120px;
  }
  .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-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>
