<template>
  <div id="selectContact">
    <div class="control">
      <div class="add_btn" @click="showAddChildPopupGroup = true">
        <van-icon name="add-o" />
        新增学生
      </div>
      <div class="add_btn" @click="addContact">
        <van-icon name="add-o" />
        新增家长
      </div>
    </div>
    <div class="box">
      <template v-for="(item, index) in contactList">
        <div class="contact_item" v-if="(!hideParent && item.userType != 1) || item.userType == 1" :key="index">
          <van-checkbox class="checkbox" v-model="item.checked" shape="square"></van-checkbox>
          <div class="info">
            <p class="name">{{ item.travelerName }}<span v-if="!item.travelerIdCard || !item.gender">需完善</span></p>
            <p class="des">{{ item.userType == 1 ? '学生' : '家长' }}·{{ showPhone(item.travelerMobile) }}</p>
          </div>
          <van-icon class="edit" name="edit" @click="editContact(index)" />
        </div>
      </template>
    </div>
    <van-button class="btn" type="primary" @click="selectContact">确定</van-button>
    <van-popup style="background: transparent;" get-container="body" v-model="showAddChildPopupGroup">
      <AddChildPopupGroup @complete="complete" step="1"></AddChildPopupGroup>
    </van-popup>
  </div>
</template>
<script>
import AddChildPopupGroup from '@/views/Home/component/AddChildPopupGroup'
import IdentityCodeValid from '@/common/validateIdent'
export default {
  data() {
    return {
      limit: '', //数量
      hideParent: false,
      contactList: [],
      showAddChildPopupGroup: false,
    }
  },
  mounted() {
    this.limit = this.$route.query.limit
    this.hideParent = this.$route.query.hideParent ? true : false
    this.getContactList()
  },
  methods: {
    // 获取联系人列表
    getContactList() {
      this.$toast.loading({
        message: '加载中...',
        duration: 0,
        forbidClick: true,
      })
      this.yxAxios.post(`${this.proxyUrl}/user/info/getPortalUserByNumV2?userNum=${localStorage.getItem('centerNo')}`).then((res) => {
        console.log('联系人列表：', res)
        this.$toast.clear()
        if (res.data.code == 200) {
          let userInfo = res.data.data.userInfo
          let contactList = userInfo.subUsers
          // 把家长添加到列表
          contactList.unshift({
            travelerMobile: userInfo?.phone,
            travelerNum: userInfo?.centerNo,
            travelerIdCard: userInfo?.idCard,
            userType: 2,
            travelerName: userInfo?.nickName,
            gender: userInfo?.gender,
          })
          this.contactList = contactList
        }
      })
    },
    // 修改联系人
    editContact(index) {
      let contactItem = this.contactList[index]
      let editContact = {
        travelerMobile: contactItem.travelerMobile,
        userType: contactItem.userType,
        travelerIdCard: contactItem.travelerIdCard,
        travelerNum: contactItem.travelerNum,
        travelerName: contactItem.travelerName,
        gender: contactItem.gender,
      }
      localStorage.setItem('editContact', JSON.stringify(editContact))
      this.$router.push({ name: 'EditContact' })
    },
    // 添加家长
    addContact() {
      this.$router.push({ name: 'EditContact', query: { add: 1 } })
    },
    complete() {
      this.showAddChildPopupGroup = false
      this.getContactList()
    },
    // 选择联系人完成
    selectContact() {
      let contactList = this.contactList
      let selectedArr = []
      for (let i in contactList) {
        if (contactList[i]?.checked) {
          if ((!contactList[i].travelerIdCard || !contactList[i].gender) && !this.hideParent) {
            this.$toast('请先完善您选择的出行人信息')
            return
          } else if (!IdentityCodeValid(contactList[i].travelerIdCard)) {
            this.$toast(`${contactList[i].travelerName}的身份证有误，请重新完善修改`)
            return
          } else {
            selectedArr.push({
              travelerName: contactList[i].travelerName,
              travelerMobile: contactList[i].travelerMobile,
              travelerNum: contactList[i].travelerNum,
              travelerIdCard: contactList[i].travelerIdCard,
              userType: contactList[i].userType,
            })
          }
        }
      }
      console.log(this.limit, selectedArr.length)
      // 判断是否选择学生
      let hasStudent = false
      for (let i in selectedArr) {
        if (selectedArr[i].userType == 1) {
          hasStudent = true
        }
      }
      if (!hasStudent) {
        this.$dialog.alert({
          title: '温馨提示',
          message: '活动参与主体为学生本人，请选择出行学生！未绑定学生，请新增出行学生进行添加。',
          confirmButtonColor: '#3385FF',
        })
        return
      }
      // console.log(selectedArr)
      localStorage.setItem('selectedContactArr', JSON.stringify(selectedArr))
      this.$router.back()
    },
    showPhone(phone) {
      return phone.replace(/^(\d{3})\d{4}(\d+)/, '$1****$2')
    },
  },
  components: {
    AddChildPopupGroup,
  },
}
</script>
<style lang="scss" scoped>
#selectContact {
  min-height: 100vh;
  box-sizing: border-box;
  padding: 16px 0;
  background: #f6f7fa;
  .control {
    width: 702px;
    margin: 0 auto;
    padding: 0 24px;
    box-sizing: border-box;
    background: #fff;
    border-radius: 16px;
    margin-bottom: 24px;
    .add_btn {
      display: inline-block;
      width: 50%;
      line-height: 104px;
      font-size: 28px;
      color: #4092ff;
      text-align: center;
    }
  }
  .box {
    width: 702px;
    margin: 0 auto;
    padding: 0 24px;
    padding-bottom: 200px;
    box-sizing: border-box;
    background: #fff;
    border-radius: 16px;

    .contact_item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .edit {
        padding: 20px 0px 20px 20px;
        font-size: 40px;
        color: #4092ff;
      }
      .info {
        width: 500px;
        padding: 28px 0;
        .name {
          display: flex;
          align-items: center;
          font-size: 30px;
          span {
            display: inline-block;
            padding: 2px 8px;
            margin-left: 16px;
            font-size: 20px;
            color: #fe3b3b;
            background: #ffeeee;
            border-radius: 4px;
          }
        }
        .des {
          font-size: 26px;
          color: #999;
        }
      }
      .checkbox {
        padding: 20px 20px 20px 0px;
      }
    }
  }
  .btn {
    position: fixed;
    bottom: 100px;
    left: 24px;
    width: 702px;
    height: 90px;
    background: #4092ff;
    border: 1px solid #4092ff;
    box-shadow: 0px 10px 40px 0px #a0c9ff;
    border-radius: 45px;
  }
}
</style>
<style lang="scss" scoped>
.elder {
  #selectContact {
    .contact_item .info {
      .name {
        font-size: 44px;
      }
      .des {
        font-size: 40px;
      }
    }
    .box {
      .add_btn {
        font-size: 40px;
      }
    }
    .btn {
      font-size: 40px;
    }
  }
}
</style>
