<template>
    <view class="autLoginBox">
        <view class="autLoginTitle">
            <!-- 旅游行程意见反馈 -->
            <img style="width: 550rpx;height: 66rpx;" src="https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Upload/Goods/638784968570048640.png"/>
        </view>
        <view class="autLoginCenter">
            <view class="autLoginTidTime">
                <view>团期:{{ datas.StartDate?datas.StartDate:'' }}</view>
            </view>
            <view class="autLoginImg">
                <image mode="scaleToFill" :src="datas.GoodsPic" />
            </view>
            <view class="autLoginText">
                {{ datas.Title? datas.Title:'' }}
            </view>
            <view class="autLoginName row-sb-n">
                <view>领队：<text>{{ datas.LeaderName?datas.LeaderName:'' }}</text></view>
                <view>导游：<text>{{ datas.GuideName?datas.GuideName:'' }}</text></view>
            </view>
            <view class="autLoginTime">*截止日期：{{ datas.EndDate?datas.EndDate:'' }}</view>
        </view>
        <view class="autLoginGo"
        :class="[!showLogin?'':'active']"
        @click="guestInfor.code==0&&guestInfor.IsEnd==0?'':goWebUrl()">
            <template v-if="guestInfor.code==0&&guestInfor.IsEnd==0">
                去填写
            </template>
            <template v-if="(guestInfor.code==0||guestInfor.code==1)&&guestInfor.IsEnd==1">
                查看
            </template>
            <button v-if="guestInfor.code==0&&guestInfor.IsEnd==0"
            class="getPhone" 
            type="default" 
            open-type="getPhoneNumber" 
            @getphonenumber="getPhoneNumber">
            获取手机号
            </button>
        </view>
        
    </view>
</template>
<script>
export default {
    data() {
			return {
                msg:{
                    TCID:'',
                    HPSLOpenId:'',
                    Unoinid: '',
                    Phone: '',
                    Id: 0,
                    OrderId: 0,
                },
                datas: null,
                guestInfor: null,
                obj: null,
                showLogin: false,
            }
    },
    onLoad(option) {
        // let scene = "tcid=23430"
        // console.log(encodeURIComponent(scene),'------')
        let t = decodeURIComponent(option.scene).split('&')
        t.forEach(x => {
            let item = x.split('=')
            if (item.length === 2) {
                if (item[0] === 'tcid') {
                    this.msg.TCID = item[1]
                }
            }
        })
        this.infor()
        this.getUserInfo()
    },
    methods: {
        infor(){
            uni.showLoading({
                title: "加载中",
                mask: true,	
            })
            this.apipost("post_GetPriceBaseInfo",{
                TCID: this.msg.TCID,
            }, (res) => {
            if (res.resultCode == 1) {
                this.datas = res.data;
                uni.hideLoading();
            }}, (error) => {
                uni.hideLoading();
                uni.showToast({
                    title: error.message,
                    icon: "none",
                });
            })
        },
        getUserInfo() {
            var that = this;
            wx.login({
                success: (res) => {
                    let obj = {
                        Source: 1,
                        OpenId: "",
                        Name: '微信小程序用户',
                        Photo: 'https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Test/Upload/Goods/1695262844000_280.png',
                        Gender: 0,
                        Moblie: "",
                        SuperiorId: 0,
                        code: res.code,
                    };
                    that.getCode(obj);
                },
                fail() {
                    that.getUserInfo()
                    uni.showToast({
                        title: '微信登录失败'
                    })
                }
            })
        },
        //调用获取code
        getCode(obj) {
            var that = this;
            that.request2({
                    url: "/api/mall/GetHPWeChatOpenId",
                    data: {
                        Code: obj.code,
                    },
                },
                (res) => {
                    if (res.resultCode == 1) {
                        this.obj = obj;
                        this.msg.HPSLOpenId = res.data.openId;
                        this.msg.Unoinid = res.data.unionid
                        this.obj.OpenId = res.data.openId;
                        this.obj.Unionid = res.data.unionid;
                        this.QueryUsers()
                    }
                }
            );
        },
        // openid获取旅客信息是否填写意见调查 
        QueryUsers(){
            uni.showLoading({
                title: "加载中",
                mask: true,
            })
            this.apipost("post_GetOrderGuest",{
                TCID: this.msg.TCID,
                HPSLOpenId: this.msg.HPSLOpenId,
                Unoinid: this.msg.Unoinid,
            }, (res) => {
            if (res.resultCode == 1) {
                this.guestInfor = res.data;
                if(this.guestInfor.guest&&
                this.guestInfor.guest.length>0&&
                this.guestInfor.guest.length==1){
                	this.msg.Id = this.guestInfor.guest[0].Id;	
                    this.msg.OrderId = this.guestInfor.guest[0].OrderId;	
                }
                uni.hideLoading();
            }}, (error) => {
                uni.hideLoading();
                uni.showToast({
                    title: error.message,
                    icon: "none",
                });
            })
        },
        // 手机号获取旅客信息
        GuestByPhone(){
            this.apipost("post_GetOrderGuestByPhone",{
                TCID: this.msg.TCID,
                Phone: this.msg.Phone,
            }, (res) => {
            if (res.resultCode == 1) {
                this.guestInfor = res.data;
                if(this.guestInfor.guest&&
                this.guestInfor.guest.length>0&&
                this.guestInfor.guest.length==1){
                	this.msg.Id = this.guestInfor.guest[0].Id;	
                    this.msg.OrderId = this.guestInfor.guest[0].OrderId;	
                }
                this.setRegister()
            }}, (error) => {
                uni.hideLoading();
                uni.showToast({
                    title: error.message,
                    icon: "none",
                });
            })
        },
        // 手机号授权
        async getPhoneNumber(e) {
            let that = this
            if(e.detail.errMsg&&e.detail.errMsg=="getPhoneNumber:ok"&&e.detail.code) {
                // 用户点击了允许授权
                uni.showLoading({
                    title: "加载中",
                    mask: true,
                })
                const { code } = e.detail;
                // 将 code 发送到服务器，服务器通过 code 获取用户手机号
                await that.apipost("post_GetUserPhoneNumber",{
                    Code: code,
                }, (res) => {
                if (res.resultCode == 1) {
                    that.msg.Phone = res.data.phoneNumber;
                    if(that.guestInfor.guest.length==0)that.GuestByPhone()
                    else that.setRegister()
                }}, (error) => {
                    uni.hideLoading();
                    uni.showToast({
                        title: error.message,
                        icon: "none",
                    });
                })
            } else {
                uni.hideLoading();
                uni.showToast({
                    title: '取消授权',
                    icon: 'none'	
                })
                // 用户点击了拒绝授权
                console.error('用户拒绝授权');
            }		
        },
        // 更新手机号
        setPhone(){
            if(this.msg.TCID==''||this.msg.HPSLOpenId==''||
            this.msg.Unoinid==''||this.msg.Phone==''||this.msg.Id==''){
                this.showLogin = false;
            	return uni.showToast({
                    title: '请先授权手机号',
                    icon: 'none'
                })
            }
            this.apipost("post_UpdateOrderGuest",this.msg,res => {
            if (res.resultCode == 1) {
                this.showLogin = false;
                uni.hideLoading();
                this.goWebUrl()
            }}, (error) => {
                uni.hideLoading();
                uni.showToast({
                    title: error.message,
                    icon: "none",
                });
            })
        },
        // 注册账号
        setRegister(){
                if(this.msg.Phone==''){
                    uni.showToast({
                        title: '请点击按钮授权手机号',
                        icon: 'none'
                    })
                    return
                }
                let obj = this.obj
                var that = this;
				that.showLogin = false;
				let pid = uni.getStorageSync("pid") ? uni.getStorageSync("pid").pid : 0;
				let Up = uni.getStorageSync("Up") ? uni.getStorageSync("Up").Up : 0;
				let SmallShopId = uni.getStorageSync("SmallShopId") ?
					uni.getStorageSync("SmallShopId").SmallShopId :
					0;
				let CounponPassword = uni.getStorageSync("CounponPassword") ?
					uni.getStorageSync("CounponPassword").CounponPassword :
					0;
				let KeyWord = uni.getStorageSync("KeyWord") ?
					uni.getStorageSync("KeyWord").KeyWord :
					"";
				let loginMsg = {
					Source: 1,
					OpenId: obj.OpenId,
					Name: obj.Name,
					Photo: obj.Photo,
					Gender: obj.Gender,
					Moblie: "",
					SuperiorId: pid,
					SmallShopId: SmallShopId,
					UserPageType: Up,
					CounponPassword: CounponPassword,
					KeyWord: KeyWord,
					Unoinid:obj.Unionid,
				}
				let url = "/api/AppletLogin/HPSLLogin"
				that.request2({
						url,
						data: loginMsg,
					},
					(res) => {
						that.showLogin = true; //防止多次点击登录
						if (res.resultCode == 1) {
							this.showDialog = false;
							uni.setStorageSync("mall_UserInfo", res.data);
							if (res.couponResultCode == 1) {
								if (uni.getStorageSync("ComeFrom")) {
									that.ComeFrom = uni.getStorageSync("ComeFrom").ComeFrom;
								}
								that.showCoupons = true;
								that.couponMessage = res.couponMessage;
							}
							uni.removeStorageSync("pid");
							uni.removeStorageSync("SmallShopId");
							uni.removeStorageSync("Up");
							uni.removeStorageSync("CounponPassword");
							uni.removeStorageSync("KeyWord");
							const {OpenId,Unoinid,Name} = res.data
							
							this.directUserLoginHandle(OpenId,Unoinid,Name)
						}
					},err=>{
                        uni.hideLoading();
						uni.showToast({
							title:err.message,
							icon:'none'
						})
					}
				);
        },
        directUserLoginHandle(OpenId,Unoinid,Name){
            let Pid = 0
            if(uni.getStorageSync('direct_user_id')){
                Pid = uni.getStorageSync('direct_user_id')
                if(isNaN(Pid)) Pid = 0
                uni.removeStorageSync('direct_user_id')
            }
            const parameters={
                OpenId,
                Unoinid,
                Pid,
                Moblie: this.msg.Phone,
            }
            this.apipost("HePingGuoLvLogin_post", parameters, r =>{
                if(this.guestInfor.guest&&
                this.guestInfor.guest.length>0&&
                this.guestInfor.guest.length==1&&
                this.guestInfor.code==0&&
                this.guestInfor.IsEnd==0){
                    this.setPhone()
                }else{
                    this.showLogin = false;
                    uni.hideLoading();
                    this.goUrl()
                }
                
            },err=>{
                uni.hideLoading();
                uni.showToast({
                    title:err.message,
                    icon:'none'
                })
            })
        },
        goUrl() {
            let msg = JSON.stringify(this.msg)
            uni.navigateTo({
                url: `/pages/customerAction/obtainInfor?msg=${encodeURIComponent(msg)}`,
            });
        },
        goWebUrl() {
            let url = `https://activity.oytour.com/html/ImpressionOpinionSurvey/opinionSurvey.html?TCID=${this.msg.TCID}&GuestId=${this.msg.Id}&OrderId=${this.msg.OrderId}&Phone=${this.msg.Phone}`
            if (url && url.length > 0) {
                uni.redirectTo({
                    url: "/pages/webbox/webbox?u=" + encodeURIComponent(url),
                });
            }
        }
    }
}
</script>
<style lang="scss" scoped>
@import url("@/asset/css/flex.css");
.autLoginBox{
    background: -webkit-gradient(linear, 0 0, 0 100%, from(#B2E7F7 ), to(#9BCFF3 ));
    padding: 50rpx 41rpx;
    min-height: 100vh;
}
.autLoginTitle{
    font-family: 068-SSZhengYaTi;
    font-weight: 400;
    font-size: 73rpx;
    color: #080A0A;
    text-align: center;
 }
 .autLoginCenter{
    background: #FFFFFF;
    border-radius: 18rpx;
    padding: 90rpx 43rpx 35rpx 43rpx;
    position: relative;
    margin-top: 80rpx;
 }
 .autLoginTidTime{
    position: absolute;
    left: 0;
    right: 0;
    top: -39rpx;
 }
 .autLoginTidTime view{
    width: 347rpx;
    height: 77rpx;
    line-height: 77rpx;
    background: #0039B8;
    border-radius: 14rpx;
    color: #fff;
    font-weight: bold;
    font-size: 32rpx;
    text-align: center;
    margin: auto;
 }
 .autLoginImg{
    width: 583rpx;
    height: 340rpx;
    background: #FFFFFF;
    border-radius: 12rpx;
    overflow: hidden;
 }
 .autLoginImg image{
    width: 100%;
    height: 100%;
 }
 .autLoginText{
    font-weight: bold;
    font-size: 32rpx;
    color: #080A0A;
    line-height: 48rpx;
    padding: 52rpx 0 60rpx;
 }
 .autLoginName{
    
 }
 .autLoginName view{
    width: 272rpx;
    height: 73rpx;
    line-height: 73rpx;
    background: #B9DBF7;
    border-radius: 14rpx;
    font-weight: bold;
    font-size: 32rpx;
    color: #080A0A;
    text-align: center;
 }
 .autLoginName text{
    // margin-left: 10rpx;
 }
 .autLoginTime{
    font-weight: bold;
    font-size: 28rpx;
    color: #0039B8;
    text-align: center;
    padding-top: 74rpx;
 }
 .autLoginGo{
    width: 100%;
    line-height: 100rpx;
    color: #fff;
    background: #0039B8;
    border-radius: 18rpx;
    text-align: center;
    font-weight: bold;
    font-size: 36rpx;
    margin-top: 85rpx;
    margin-bottom: 195rpx;
    position: relative;
 }
 .autLoginGo.active{
    background: #eee;
    color: black;
 }
 .getPhone{
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    opacity: 0;
 }
</style>