<template>
  <div class="AddFamily">
    <div class="search">
      <div class="left">
        <AiAreaPicker v-model="areaId" :areaId="user.areaId" @select="areaSelect" :name.sync="areaName" style="color: #666">
          <u-icon name="map-fill" color="#3192F4" size="20px" style="vertical-align: text-bottom"></u-icon>
          <span style="margin-left: 4px" v-if="areaName">{{ areaName }}</span>
          <span v-else>请选择</span>
          <u-icon name="arrow-down" color="#666" size="28" style="margin-left: 4px" />
        </AiAreaPicker>
      </div>
      <u-search v-model="value" placeholder:clearabled="true" placeholder="请输入姓名/身份证/手机号" :show-action="false" bg-color="#F5F5F5" search-icon-color="#999" color="#999" height="58" @search="getListInit" @clear="value='',getListInit"></u-search>
    </div>
    <div class="userList" v-if="list.length">
      <ul v-for="(item,index) in list" :key="index">
        <li class="main">
          <div class="user">
            <div>
              <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="userInfo">
              <div>
               <img src="./components/img/user-img.png" alt="" class="userImg" >
              </div>
              <div class="info">
                 <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(/^(\d{6})\d{8}(.{4}$)/g, `$1${Array(9).join('*')}$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="btn">
      <div class="handleCheck" @click="confirm">确定选择</div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      list: [],
      current: 1,
      value: '',
      checked: true,
      userList: {},
      userId: '',
      areaId: '',
      areaName:''
    }
  },
  computed: { ...mapState(['user']) },
  methods: {
    getList(){
      this.$http.post(`/app/appresident/list?areaId=${this.areaId}&householdName=1&current=${this.current}&con=${this.value}`).then((res) => {
        if (res.code == 0) {
          res.data.records.map((item) => {
            item.checked = false
            item.girdMemberId = this.userId
            item.residentId = item.id
          })
          this.list = this.current > 1 ? [...this.list, ...res.data.records] : res.data.records
        }
      })
    },
    getListInit(){
      this.list = []
      this.current = 1
      this.getList()
    },
    userClick(index) {
      this.list[index].checked = !this.list[index].checked
    },
    confirm() {
      var checkUserList = []
      this.list.map((item) => {
        if(item.checked) {
          var info = {
            girdMemberId: this.userId,
            residentId: item.id,
            name: item.name
          }
          checkUserList.push(info)
        }
      })
      if(!checkUserList.length) {
        return this.$u.toast('请选择户主')
      }
      this.$http.post(`/app/appgirdmemberresident/add`, {residentList:checkUserList}).then((res) => {
        if (res.code == 0) {
          this.$u.toast('提交成功')
          uni.$emit('updateList')
          setTimeout(() => {
            uni.navigateBack()
          }, 600)
        }
      }).catch((err) => {
         this.$u.toast(err)
      })
    },
    areaSelect(e) {
      this.areaId =e
      this.getListInit()
    },
  },
  onLoad(option) {
    this.userId = option.id
    this.areaId = this.user.areaId
    this.areaName = this.user.areaName
    console.log(this.userId)
  },
  onShow() {
    document.title = '新增责任家庭'
    this.getList()
  },
  onReachBottom() {
    this.current = this.current + 1
    this.getList()
  },
}
</script>

<style lang="scss" scoped>
.AddFamily {
    .search{
    width: 100%;
    height: 112px;
    padding: 32px;
    box-sizing: border-box;
    background: #FFF;
    display: flex;
    margin-bottom: 8px;
    .left{
      width: calc(100% - 450px);
    }
  }
  .userList {
    width: 100%;
    background-color: #FFFFFF ;
     ul {
       padding-left: 0px;
      li {
        list-style-type: none;
        
      .user {
        display: flex;
        justify-content: flex-start;
        padding-top: 24px;
        padding-left: 32px;
        height: 100%;
        .checkbox{
          margin-top: 20px;
          margin-right: 32px;
          width: 48px;
          height: 48px;
        }
        .userInfo {
          display: flex;
          justify-content: flex-start;
          border-bottom: 1px solid #E4E5E6;
          padding-right: 48px;
          width: calc(100% - 100px);
          .userImg {
          padding-right: 32px;
          width: 80px;
          height: 80px;
          }
          .info {
          width: calc(100% - 120px);
          padding-bottom: 32px;
          .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;
           font-size: 26px;
           color: #999999;
         }
        }
        }
        
      }
      }
    }
  }
  .btn {
    position: fixed;
    bottom: 0px;
    right: 0px;
    width: 100%;
    height: 120px;
    background-color: #F3F6F9;
    .handleCheck {
      position: fixed;
      bottom: 18px;
      right: 34px;
      width: 192px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      background-color: #1365DD;
      color: #FFFFFF; 
    }
  }
}
</style>




