<template>
    <view class="obtainInforBox column">
        <view class="obtainInforTitle">请选以下任意代表填写意见</view>
        <scroll-view :scroll-y="true" 
            :enable-back-to-top="true" style="height: 1px;flex: 1;overflow: hidden;"
            :enable-flex="true"> 
            <radio-group @change="radioChange">
				<label class="obtainInforLab row" v-for="(item, index) in guestInfor.guest" :key="index">
					<view>
                        <radio :value="item.Id" color="#0039B8"/>
					</view>
					<view class="obtainInforLabName">{{item.AllName}}</view>
				</label>
			</radio-group>
        </scroll-view>
        <view class="obtainInforGo"
        :class="[current==0||showLogin?'active':'']" @click="current==0||showLogin?'':setRegister()">
            确定
        </view>
    </view>
</template>
<script>
export default {
    data() {
        return {
            msg:{
                TCID:'',
                HPSLOpenId:'',
                Unoinid: '',
                Phone: '',
                Id: 0,
                OrderId: 0,
            },
            obj: null,
            list:[
                {
                    name:'我是业主',
                    value:1
                },
                {
                    name:'我是中介',
                    value:2  
                },
                {
                    name:'我是经纪人',
                    value:3
                },
            ],
            current:0,
            guestInfor: null,
            showLogin: false,
        }
    },
    onLoad(option) {
        this.msg = JSON.parse(decodeURIComponent(option.msg))
        this.obj = JSON.parse(decodeURIComponent(option.obj))
    },
    onShow(){
        this.QueryUsers()
    },
    methods: {
        // 更新手机号
        setPhone(){
            if(this.msg.TCID==''||this.msg.HPSLOpenId==''||
            this.msg.Unoinid==''||this.msg.Phone==''||this.msg.Id==''){
                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.showLogin) return;
                if(this.msg.Id==0||this.msg.OrderId==0){
                    uni.showToast({
                        title: '请选择任意人员',
                        icon: 'none'
                    })
                    return
                }
                let obj = this.obj
                var that = this;
				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) {
							uni.setStorageSync("mall_UserInfo", res.data);
							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.code==0&&
                this.guestInfor.IsEnd==0){
                    this.setPhone()
                }else{
                    this.showLogin = false;
                    uni.hideLoading();
                    this.goWebUrl()
                }
                
            },err=>{
                uni.hideLoading();
                uni.showToast({
                    title:err.message,
                    icon:'none'
                })
            })
        },
        radioChange(evt){
            this.current = evt.detail.value;
            this.msg.Id = evt.detail.value;
            for (let i = 0; i < this.guestInfor.guest.length; i++) {
                if(this.guestInfor.guest[i].Id==this.current){
                    this.msg.OrderId = this.guestInfor.guest[i].OrderId
                } 
            }
        },
        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) {
                uni.hideLoading();
                this.guestInfor = res.data; 
                if(res.data.guest&&res.data.guest.length>0){
                    if(res.data.guest.length==1){
                        this.current = res.data.guest[0].Id
                        this.msg.Id = res.data.guest[0].Id
                        this.msg.OrderId = res.data.guest[0].OrderId
                    }
                    this.list = res.data.guest
                }else{
                    this.GuestByPhone()
                }
                
            }}, (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(res.data.guest&&res.data.guest.length>0){
                        if(res.data.guest.length==1){
                            this.current = res.data.guest[0].Id
                            this.msg.Id = res.data.guest[0].Id
                            this.msg.OrderId = res.data.guest[0].OrderId
                        }
                        this.list = res.data.guest
                    }
                }
            }, (error) => {
                uni.hideLoading();
                uni.showToast({
                    title: error.message,
                    icon: "none",
                });
            })
        },
        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(process.env.NODE_ENV=="development") return console.log(url,"-----url")
            if (url && url.length > 0) {
                uni.navigateTo({
                    url: "/pages/webbox/webbox?u=" + encodeURIComponent(url),
                });
            }
        }
    }
}
</script>
<style lang="scss" scoped>
@import url("@/asset/css/flex.css");
.obtainInforBox{
    background: -webkit-gradient(linear, 0 0, 0 100%, from(#B2E7F7 ), to(#9BCFF3 ));
    padding: 50rpx 41rpx;
    height: 100vh;
}
.obtainInforTitle{
    font-family: 068-SSZhengYaTi;
    font-weight: 400;
    font-size: 43rpx;
    color: #080A0A;
    text-align: center;
    margin-bottom:50rpx;
 }
 .obtainInforGo{
    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: 50rpx;
 }
 .obtainInforGo.active{
    background: #eee;
    color: #000; 
 }
 .obtainInforLab{
    padding: 20rpx 0;
 }
 .obtainInforLabName{
    position: relative;
    top: 6rpx;
    left: 10rpx;
 }
</style>