<template>
  <div class="sendPoints" v-if="yhjCheck">
    <div @click="yhjCheck = false" class="testBackground"></div>
    <div class="test" v-if="yhjCheck" position="bottom" custom-style="height: 40%">
        <p class="couponTit">确认{{showAddr?'地址':''}}</p>
        <div v-if="showAddr">
          <div class="address_list-d flex" v-if="addressCur" @click="toChooseAddr()">
            <div class="top">
              <div class="name">{{addressCur.consignee}}</div>
              <div class="default flex" v-if="addressCur.isDefaultShipping == 1">默认</div>
            </div>
            <div class="mid">
              <div class="tel">
                 <span v-if="addressCur.mobilephone">{{addressCur.mobilephone}}</span>
                  <span v-else>{{addressCur.telephone}}</span>
              </div>
              <div
                class="address"
              >{{addressCur.provinceName}}{{addressCur.cityName}}{{addressCur.districtName}}{{addressCur.address}}</div>
              <i class="iconfont icon-tubiao_jiyao-xiangyou"></i>
            </div>
          </div>
          <div class="adr1" v-else @click="toAddAddr()">
            <img class="adr1_img" src="https://cdn.mayi888.com/_nuxt/client/img/e547fe3.png?mixid=" alt="">
            <p>请选择收货地址</p>
          </div>
        </div>
        <div class="check_box" :style="{height: newdata.length > 1 ? '200px' : '100%'}">
          <div class="gift_detail" v-for="(item,index) in newdata" :key="index">
            <van-card
              num="1"
              price="0.00"
              :title="item.grantName"
              :thumb="item.goodsSpecificationsUrl"
            />
            <div class="gift_list" v-for="(sitem,sindex) in item.giftGoodsRels" :key="sindex">
              <span>{{sitem.productName}} </span>
              <span>X{{Number(sitem.qty)}}</span>
            </div>
          </div>
        </div>
        <div class="sure_box">
          <div class="sure_item" @click="suregift()">确定</div>
        </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import addres from "@/api/userAddress";
export default {
  name: 'birthInfo',
  data() {
    return {
      newdata:[],
      yhjCheck:false,
      addressList:[],
      addressCur:null,
      checked:true,
      selectgift:[],
      showAddr:true,//是否展示选择地址
    };
  },
  props:{
    giftData: {
      type: Array,
      default() {
        return []
      }
    },
    maxReceiveGift:{
       type:String,
       default() {
        return ''
      }
    },
    title:{
       type:String,
       default() {
        return ''
      }
    }
  },
  watch:{
    yhjCheck(val){
      console.log('tztztz', val);
      this.getUserAddr();
      if(val) {
        if(this.newdata.length == 1) {
          this.$set(this.newdata[0],'ischange',true);
        }
      }
    },
    giftData() {
      this.newdata = this.giftData;
    }
  },
  async created(){
    this.newdata = this.giftData;
    this.getUserAddr();
  },
  methods: {
    getData(val){
      this.newdata = val;
    },
    getUserAddr(){
      //获取地址
      addres.getUserAddressList().then(res => {
        if (res.data.code == 200) {
          if (res.data.data.list.length > 0) {
            this.addressList = res.data.data.list;
            this.addressList.forEach(v => {
              if (v.isDefaultShipping == "1"||v) {
                return this.addressCur = v
              }
            });
            return this.addressCur = this.addressList[0]
          } else {
            this.addressCur = null
          }
        }
      });
    },
   // 去修改地址
    toChooseAddr(){
      wx.navigateTo({url: '/pages/index/main?from=home&backpath=%2Faddress%2FaddressList'});
      this.yhjCheck = false
    },
    // 确定
    suregift(){
      if(this.showAddr && !this.addressCur) {
        wx.showToast({
          title:'请选择收货地址!',
          icon: "error"
        })
        return false
      }
      this.yhjCheck = false;
      this.$emit('suregiftopencard',this.addressCur,this.showAddr,this.selectgift)
    },
    // 新增地址
    toAddAddr(){
      wx.navigateTo({url: '/pages/index/main?from=home&backpath=%2Faddress%2FaddressList'});
      this.yhjCheck = false
    },
  }
};
</script>

<style lang="scss" scoped>
.sendPoints {
  position: fixed;
  bottom: 0;
  z-index: 99999;
  width: 100%;
  height: 100%;
  margin-top: 14px;
  .testBackground {
    height: 100%;
    width: 100%;
    background-color: rgba(0,0,0,0.4);
    opacity: .8;
    z-index: 1;
  }
  .test {
    position: fixed;
    bottom: 0;
    width: 100%;
    z-index: 2;
  }
  .tit {
    font-size: 16px;
    color: #333;
    align-items: center;
    font-weight: bold;
  }
  .gift_wrap{
      padding-top: 10px;
      .gift_item{
          display: flex;
          margin-bottom: 10px;
          .gift_index{
             background-size: cover;
             height: 61px;
             width: 43.5px;
             flex: none;
             margin-right: 10px;
             .num{
                 position: relative;
                right: -7px;
                top: -4px;
                font-size: 29px;
                font-weight: bold;
                color: rgba(255,255,255,1);
             }
          }
          .gift_product{
              overflow: hidden;
              .product_img{
                  display: flex;
                  overflow: auto;
                  padding-left: 10px;
                img{
                    width: 47px;
                    height: 48px;
                    margin-right: 15px;
                }
              }
              .product_text{
                  font-size: 12px;
                  font-family: Source Han Sans CN;
                  font-weight: 400;
                  color: rgba(46,46,46,1);
                  line-height: 18px;
                  margin-top: 5px;
              }
          }
      }
      .tips{
          color: #666666;
          font-weight:bold;
          padding-bottom: 10px;
      }
  }
  .couponTit{
      text-align: center;
      font-size: 16px;
      padding: 10px 0 0 0;
      border-bottom: 1px solid #e5e5e5;
      width: 100%;
      height: 100%;
      background-color: #fff;
      padding-bottom: 10px;
  }
       .adr1 {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        margin:0 auto;
        padding:10px 0;
        text-align:center;
        width: 100%;
        background-color: #fff;
        border-bottom: 1px solid #e5e5e5;
        .adr1_img{
          width:36px;
          height:33px;
          margin: 0 auto;
        }
        p{
          margin-top:10px;
          font-size:14px;
          color:#828282;
          text-align:center;
        }
      }
  .address_list-d {
        position: relative;
        width: 100%;
        height: 100px;
        background: rgba(255, 255, 255, 1);
        border-bottom: 1px solid #e5e5e5;
        .top{
          text-align: center;
              padding-left: 20px;
        .name {
          height: 15px;
          font-size: 15px;
          font-family: MicrosoftYaHei;
          font-weight: 400;
          color: rgba(39, 39, 39, 1);
          margin-top: 25px;
        }
        .default {
          width: 40px;
          border: 1px solid var(--main-color);
          border-radius: 3px;
          font-size: 12px;
          font-family: MicrosoftYaHei;
          font-weight: 400;
          color: var(--main-color);
          justify-content: center;
          align-items: center;
          margin-top: 10px;
          display: inline-block;
        }
        }
        .mid {
          width: 240px;
          // margin-top: 6px;
          margin-left: 20px;
          span{
            margin-top: 25px;
          }
          .tel {
            // width: 100px;
            height: 20px;
            font-size: 15px;
            font-family: MicrosoftYaHei;
            font-weight: 400;
            color: rgba(39, 39, 39, 1);
            margin-top: 25px;
          }
          .address {
            width: 240px;
            height: 37px;
            font-size: 13px;
            font-family: MicrosoftYaHei;
            font-weight: 400;
            color: rgba(39, 39, 39, 1);
            margin-top: 3px;
          }
          .iconfont {
            position: absolute;
            right: 15px;
            top: 34px;
            font-size: 18px;
            // margin-left: 13px;
          }
        }
        .top {
          width: 20%;
        }
      }
    .check_box{
     /* height: 200px; */
     overflow: auto;
      font-size:16px;
      .van-checkbox{
        padding-top: 10px;
        padding-left: 30px;
        margin-bottom: 5px;
      }
      .gift_detail{
         border-bottom: 1px solid #e5e5e5;
         padding-bottom: 10px;
         width: 100%;
         background-color: #fff;
         .gift_list{
           font-size: 12px;
           padding-left: 58px;
           color: #999;
           margin-bottom: 5px;
         }
      }
    }
    .sure_box{
        display: flex;
        justify-content: center;
        background-color: #fff;
        height: 100%;
        width: 100%;
        .sure_item{
            width: 80%;
            text-align: center;
            border-radius: 20px;
            height: 35px;
            line-height: 35px;
            margin-top: 10px;
            margin-bottom: 10px;
            font-size: 16px;
            background: #c6b088;
            color: #fff;
        }
    }
}
</style>