<template>
  <div id="selectContact">
    <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-icon class="edit" name="edit" @click="editContact(index)" />
          <div class="info">
            <p class="name">{{item.travelerName}}<span v-if="!item.travelerIdCard||!item.gender">需完善</span></p>
            <p class="des">{{item.userType==1?'学生':'家长'}}·{{item.travelerMobile}}</p>
          </div>
          <van-checkbox v-if="item.userType==1" class="checkbox" v-model="item.checked" shape="square"></van-checkbox>
          <van-checkbox v-else class="checkbox" v-model="item.checked" @change="checkboxChange" shape="square"></van-checkbox>
        </div>
      </template>
      <div class="add_btn" @click="showAddChildPopupGroup=true">
        <van-icon name="add-o" />
        新增出行学生
      </div>
    </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"></AddChildPopupGroup>
    </van-popup>
  </div>
</template>
<script>
import AddChildPopupGroup from '@/views/Home/component/AddChildPopupGroup'
import { Dialog } from 'vant';
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;
    console.log(this.hideParent)
    this.getContactList()
  },
  methods: {
    // 获取联系人列表
    getContactList () {
      this.$toast.loading({
        message: '加载中...',
        duration: 0,
        forbidClick: true
      })
      this.mgop({
        api: 'mgop.sz.hswsy.getPortalUserByNum', // 必须
        host: 'https://mapi.zjzwfw.gov.cn/',
        dataType: 'JSON',
        type: 'GET',
        appKey: 'fuxgnukl+2001895516+edccpx', // 必须
        headers: {
          //   'isTestUrl': '1'
        },
        data: {
          "userNum": localStorage.getItem('centerNo')
        },
        onSuccess: 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
          }
        },
        onFail: err => {
          console.log('err', err)
        }
      });
    },
    // 修改联系人
    editContact (index) {
      let contactItem = this.contactList[index]
      let editContact = {
        travelerMobile: contactItem.travelerMobile,
        userType: contactItem.userType,
        travelerIdCard: contactItem.travelerIdCard,
        travelerNum: contactItem.travelerNum,
        travelerName: contactItem.travelerName
      }
      localStorage.setItem('editContact', JSON.stringify(editContact))
      this.$router.push({ name: 'EditContact' })
    },
    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 {
            selectedArr.push({
              travelerName: contactList[i].travelerName,
              travelerMobile: contactList[i].travelerMobile,
              travelerNum: contactList[i].travelerNum
            })
          }
        }
      }
      console.log(this.limit, selectedArr.length)
      if (this.limit && this.limit != selectedArr.length) {
        this.$toast(`请选择${this.limit}位出行人`)
        return;
      }
      // console.log(selectedArr)
      localStorage.setItem('selectedContactArr', JSON.stringify(selectedArr))
      this.$router.back()
    },
    checkboxChange (e) {
      if (e == true) {
        Dialog.alert({
          title: '温馨提示',
          message: '活动参与主体为学生本人，请选择出行学生！未绑定学生，请新增出行学生进行添加。',
          confirmButtonColor: '#3385FF'
        })
      }
    }
  },
  components: {
    AddChildPopupGroup
  }

}
</script>
<style lang="scss" scoped>
#selectContact {
  min-height: 100vh;
  box-sizing: border-box;
  padding: 16px 0;
  background: #f6f7fa;
  .box {
    width: 702px;
    margin: 0 auto;
    padding: 0 24px;
    padding-bottom: 200px;
    box-sizing: border-box;
    background: #fff;
    border-radius: 16px;
    .add_btn {
      line-height: 104px;
      font-size: 28px;
      color: #4092ff;
      text-align: center;
    }
    .contact_item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .edit {
        padding: 20px 20px 20px 0;
        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 0 20px 20px;
      }
    }
  }
  .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>