<template>
  <div class="FamilyList">
    <div class="title">户主列表<span style="font-size: 14px; color: #2979ff;" @click="changeType" v-if="edit">修改</span><span style="font-size: 14px; color: #2979ff;" @click="edit = true" v-else>取消</span></div>
    <div class="main" v-if="list.length">
      <ul v-for="(item,index) in list" :key="index">
        <li>
          <div class="user">
             <div v-if="!edit">
              <img src="./components/img/xz.png" alt="" class="checkbox" v-if="!item.checked" @click="userClick(index)">
              <img src="./components/img/xzh.png" alt="" class="checkbox" v-else @click="userClick(index)">
            </div>
            <div class="info">
              <div>
                <img :src="item.photo" class="userImg" v-if="item.photo">
                <img src="./components/img/user-img.png" alt="" class="userImg" v-else>
              </div>
              <div class="userInfo">
                <div class="name">
                  <span class="user-name">{{item.name}}</span>
                  <span class="user-tel">{{item.phone}}</span>
                </div>
                <div class="idCard">{{item.idNumber && item.idNumber.replace(/(.{6}).*(.{4})/,"$1********$2")}}</div>
                <div class="address">{{item.currentAreaName || ''}}{{ item.currentAddress || '' }}</div>
              </div>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <AiEmpty description="暂无数据" class="emptyWrap" v-if="!list.length"></AiEmpty>
    <div style="height:70px;"></div>
    <div class="footer" @click="toAddFamily()" v-if="edit">新增责任家庭</div>
    <div class="footer" @click="delFamily()" v-else>删除</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list:[],
      current:1,
      userId: '',
      edit: true,
      checked: true
    }
  },
  methods: {
    getList(){
       this.$http.post(`/app/appgirdmemberresident/listByGirdMember?current=${this.current}&girdMemberId=${this.userId}`).then((res) => {
        if (res.code == 0) {
         this.list = this.current > 1 ? [...this.list, ...res.data.records] : res.data.records
        }
      })
    },
    toAddFamily(){
      uni.navigateTo({url: `./AddFamily?id=${this.userId}`})
    },
    userClick(index) {
      this.list[index].checked = !this.list[index].checked
      this.$forceUpdate()
    },
    // 点击删除
    delFamily(){
      var ids = []
      this.list.map((item) => {
        if(item.checked) {
          ids.push(item.gmrId)
        }
      })
      if(!ids.length) {
        return this.$u.toast('请选中需要删除的家庭户主')
      }
      this.$confirm(`是否删除这些关联家庭信息？`).then(() => {
        this.$http.post(`/app/appgirdmemberresident/delete?ids=${ids.join(',')}`).then((res) => {
          if (res.code == 0) {
            this.$u.toast('删除成功')
            uni.$emit('updateList')
            this.$forceUpdate()
            this.edit = true
          }
        })
      })
    },
    changeType() {
      this.edit = false
      this.list.map((item) => {
        item.checked = false
      })
    }
  },
  onLoad(option) {
    this.userId = option.id
    console.log(this.userId)
  },
  onShow() {
    document.title = '责任家庭'
    this.getList()
    uni.$on('updateList', () => {
      this.current = 1
      this.getList()
    })
  },
  onReachBottom() {
    this.current = this.current + 1
    this.getList()
  },
}
</script>

<style lang="scss" scoped>
.FamilyList {
  .title {
    display: flex;
    justify-content: space-between;
    padding: 24px 32px;
    background-color: #F3F6F9;
    font-size: 34px;
    font-weight: 800;
  }
  .main {
    padding-bottom: 112px;
    ul {
      padding-left: 32px;
      background-color: #fff;
      li {
      list-style-type: none;
      .user {
        display: flex;
        justify-content: flex-start;
        padding-top: 24px;
          .checkbox{
          margin-top: 20px;
          margin-right: 32px;
          width: 48px;
          height: 48px;
        }
        .info {
          display: flex;
          justify-content: flex-start;
           width: 100%;
          .userImg {
          margin-right: 32px;
          width: 80px;
          height: 80px;
        }
          .userInfo {
            width: 100%;
            padding-right: 32px;
            border-bottom: 1px solid #E4E5E6;
            .name {
            display: flex;
            justify-content: space-between;
            font-size: 32px;
            margin-bottom: 8px;
            .user-name {
              font-weight: 800;
            }
            .user-tel {
              font-size: 26px;
              color: #999999 ;
            }
         }
         .idCard {
           margin-bottom: 16px;
           font-size: 26px;
           color: #999999;
         }
         .address {
           max-width: 100%;
           word-break:break-all;
           margin-bottom: 18px;
           font-size: 26px;
           color: #999999;
         }
          }
          
        }
      }
      }
    }
  }
  .footer{
    position: fixed;
    left: 0px;
    bottom: 0px;
    width: 100%;
    height: 112px;
    background-color: #1365DD;
    line-height: 112px;
    text-align: center;
    font-size: 36px;
    color: #FFFFFF;
  }
}
</style>
