<template src="./webgl_rxdz_customize.html">
</template>

<script>
const util = require('@/utils/util.js');
const config = require('@/services/urlConfig.js');
// const app = getApp(); //获取应用实例
// import requestConfig from '@/static/lib/requestConfig.js';
// import commonPageMethod from '@/common/commonPageMethod.js';
// import commonMethod from '@/common/commonMethod.js';
export default {
    // mixins: [commonPageMethod,commonMethod],
    /**
     * 页面的初始数据
     */
    data() {
        return {
            pvId: 'p_2cmina_23080403',
            locusBehaviorName: "提交页面",
            locusValue: "",
            locusName: "",
            pvCurPageName: "submit_ret",
            pvCurPageParams: null,
            navbar: {
                showCapsule: 1,
                title: '我的定制',
                titleColor: '#fff',
                navPadding: 0,
                navPaddingBg:'transparent',
                navBarColor: 'transparent',
                navBackColor: 'transparent',
                haveCallback: true, // 如果是 true 会接手 navbarBackClk
                fromShare: false,
                fromProject: 0,
                shareToken: "",
                pageName: this.pvCurPageName,
            },
            houseId: config.houseIdHs,

            shareUserId: null, // 分享者ID
            id: '64def992f65c6237103869a5',//方案库id

            currImgIdx:0,
            tabIndex: 0,
            userId: '',
            // featureImgList: [],
            hardboundEffect: [],//当前swiper数据列表对象
            form: {},
			tabData:[],
            number: '',
			plotHeadDeafultImg: 'https://dm.static.elab-plus.com/mini-program/plotHeadDeafultImg.png', // 地块人物默认头像
			showPhoneModel:false,
			gsImage:'',
			layoutStruct:null,
		}
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        await this.loadPromise;//等待解析分享者参数结束
        console.log("***onLoad-webgl_rxdz_customize***", options)
        const that = this;
        if (options.scene) { //二维码进来
            const data = JSON.parse(app.globalData.exchangedFromChannel);
            if (data.houseId) {
                that.houseId = data.houseId || this.houseId;
            }
            if (data.attrs.id) {
                that.id = data.attrs.id;
            }
			if(data.attrs.shareUserId){
			    this.shareUserId = data.attrs.shareUserId;
			}
        }else{
            if(options.shareUserId){
                this.shareUserId = options.shareUserId;
            }
            if(options.id){
                this.id = options.id
            }
            if(options.houseId){
                this.houseId = options.houseId
            }
        }
		if(!this.id){
			uni.showToast({
				icon: "none",
				title: "没有数据！",
			});
			return false;
		}else{
			this.getIdData();
		}
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
		// #ifdef MP-WEIXIN
		wx.showShareMenu({
		    menus: ['shareAppMessage', 'shareTimeline']
		});
		// #endif
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        app.login(async ()=>{
            let currPage = getCurrentPages()[getCurrentPages().length - 1] ? getCurrentPages()[getCurrentPages().length - 1].$vm : null;
            if (this.houseId || app.globalData.houseId || (currPage ? currPage.houseId : '')) {
                this.houseId = this.houseId || app.globalData.houseId || (currPage ? currPage.houseId : '');
            }
            if (this.userId || app.globalData.single && app.globalData.single.id) {
                this.userId = this.userId || app.globalData.single && app.globalData.single.id
            }
        });
    },
    methods: {
		//返回处理-不同的小程序这里很可能不一样，需要注意
		navbarBackClk() {
            //上一个页面
			let lastPage = getCurrentPages()[getCurrentPages().length-2] ? getCurrentPages()[getCurrentPages().length-2].$vm : null;
			let _pvLastPagePath = lastPage ? (lastPage.route || lastPage.__route__) : '';//上一页面路径
            //存在上一个页面，则返回上一页
            if(_pvLastPagePath){
				uni.navigateBack()
            } else {
				if(this.houseId){
					//返回首页
					uni.reLaunch({
						url: '/pages/index/index',
						success: function() {},
						fail: function(res) {
							console.log(res)
						},
					})
				}else{
					uni.reLaunch({
						url: '/pages/index/index',
						success: function() {},
						fail: function(res) {
							console.log(res)
						},
					})
				}
				let param = {
					type: 'CLK', //埋点类型
					clkId: 'clk_2cmina_23080424', //点击ID
					clkName: 'homepage_clk', //点击前往的页面名称
					clkParams: {
						locusName: "返回项目首页",
					}
				};
				util.trackRequest(param);
            }
		},
        swiperChangeImg(e){
            console.log("***swiperChangeImg***",e);
			this.currImgIdx = e.detail.current;
			this.gsImage =  this.hardboundEffect[this.currImgIdx];//获取高斯模糊的图片
        },
        
        async getIdData(){
			let userId = (app.globalData.single && app.globalData.single.id) ? app.globalData.single.id : '';
            let params = {
                id: this.id,
                brandId: config.brandId,
				houseId:this.houseId,
				userId,
            };
			if(this.shareUserId){//分享时 不带用户id
				delete params.userId
			}
            // params.userId = this.userId;
            const res = await requestConfig('getCustomizedRecord', params);
            if (res.success ) {
                let single = res.list[0];
				this.layoutStruct = JSON.parse(JSON.stringify(single));
                // this.number = single.landCode
                this.form = single.layoutStruct.find(it=>it.floor==single.curFloor);

                this.hardboundEffect = this.form.layoutImgCustomized? [this.form.layoutImgCustomized] : [];//赋值轮播图
				this.tabIndex = 0;//表明是户型首图
				this.tabData = [];
				let data = {
					name : this.form.layoutName,
					imgUrl : this.form.layoutImgCustomized,
					area:'',
				}
                this.tabData.push(data);
				this.form.houseJson = this.form.houseJson.filter((item)=>{
					return item.spaceName && item.spaceName.length>0 && item.spaceType!=11 && item.hardboundEffect && item.hardboundEffect[0]
				})
				this.form.houseJson.sort(function(a,b){return parseFloat((b.spaceWidth * b.spaceHeight) / 10000) -parseFloat((a.spaceWidth * a.spaceHeight) / 10000)});
				this.form.houseJson.forEach((item,index)=>{
					//获取面积
					let curSpaceArea = parseFloat((item.spaceWidth * item.spaceHeight) / 10000).toFixed(1);
					let data = {
						name : item.spaceName,
						imgUrl : item.hardboundEffect[0],
						area:curSpaceArea,
					}
					this.tabData.push(data);
				})
				this.gsImage = this.form.layoutImgCustomized;//获取高斯模糊的图片
            }
        },
        changeTab(id){
            if(id == this.tabIndex){
                return
            }
            this.tabIndex = id;
            this.currImgIdx = 0;
			if(id>0){//提取空间轮播图
				this.hardboundEffect = this.form.houseJson[id - 1].hardboundEffect;//赋值轮播图
				this.gsImage = this.hardboundEffect[0];//获取高斯模糊的图片
			}else{
				this.hardboundEffect = this.form.layoutImgCustomized? [this.form.layoutImgCustomized] : [];//赋值轮播图
				this.gsImage = this.form.layoutImgCustomized;//获取高斯模糊的图片
			}
            let param = {
                type: 'CLK', //埋点类型
                clkId: 'clk_2cmina_23080421', //点击ID
                clkName: 'roomretpic_clk', //点击前往的页面名称
                clkParams: {
                    type: this.tabData[this.tabIndex].name,
                    locusName: "房间名称",
                }
            };
            util.trackRequest(param);
        },
        
        catchTouchMove: function() {
            return false;
        },
        
        toMode(){
            uni.navigateTo({
                url: '/webgl/pages/webgl_rxdz_look/webgl_rxdz_look?houseId='+this.houseId,
				success: (res)=> {
					// 通过eventChannel向被打开页面传送数据
					res.eventChannel.emit('acceptDataFromOpenerPage', {
						layoutStruct:this.layoutStruct,
					})
				},
				fail: function(res) {
				    console.log(res)
				},
            })
			let param = {
				type: 'CLK', //埋点类型
				clkId: 'clk_2cmina_23080420', //点击ID
				clkName: 'checkroom_clk', //点击前往的页面名称
				clkParams: {
					locusName: "查看户型",
				}
			};
			util.trackRequest(param);
		},
        async afterShareOptionHandle(e) {
            let sign = await app.setShareToken({
                subtype: 'project',
                houseId: this.houseId,
                attrs: {
                    shareType: 'qrcode',
                    fromProject: app.globalData.projectShare,
                    id: this.id,
					shareUserId:this.shareUserId,
                }
            });
			let shareImg = this.gsImage;
            let obj = {
                type: 8,
                path: '/webgl/pages/webgl_rxdz_customize/webgl_rxdz_customize',
                shareToken: sign || app.globalData.shareToken,
                houseId: (this.houseId || ''),
                logo: app.globalData.houseProject.logo || "",
                gpName: app.globalData.houseProject.projectName || '',
				mountHuangXcx:true,
            };
            if(shareImg){
                obj.defaultImage = shareImg
            }
            app.globalData.shareCardData = JSON.stringify(obj);
            uni.navigateTo({
                url: '/pages/shareCard/shareCard?houseId=' + (this.houseId || ''),
            })
        },
        /**
         * 用户点击右上角分享
         */
        onShareAppMessage(options) {
            let param = {
                type: 'CLK', //埋点类型
                clkId: 'clk_2cmina_18', //点击ID
                clkName: 'system-share', //点击前往的页面名称
                clkParams: {
                    from: options.from
                }
            };
            util.trackRequest(param);
            let sharePath = this.shareInitPath();
            let shareUserParam = "";
            shareUserParam = ('&shareUserId='+((app.globalData.single && app.globalData.single.id) ? app.globalData.single.id : ''));
            if(app.globalData.executeToHelpShare){//帮好友转发分享
                shareUserParam = ('&shareUserId='+this.shareUserId);
            }
            app.globalData.executeToHelpShare = false;
            let path = '/webgl/pages/webgl_rxdz_customize/webgl_rxdz_customize?' + sharePath + shareUserParam + '&id=' + this.id;
            console.log(path)
            let shareImg = this.gsImage;
            return {
                imageUrl: shareImg || app.globalData.shareImage || '',
                title: app.globalData.projectName || '',
                path: path
            }
        },
    }
    
}
</script>
<style lang="scss" scoped>
	@import "./webgl_rxdz_customize.scss";
</style>
<style lang="css" scoped>
	/* @import "@/common/css/common.css"; */
</style>