import {
    VueLikePage
} from '../../utils/page'
import {
    isPhoneX
} from './../../utils/tools'
import Router from '../../utils/routes'
import Api from '../../apis/index'
import ImApi from '../../apis/im'

const app = getApp();
VueLikePage([], {
    data: {
        images: [],
        detail: {},
        categoryList: [],
        companyList: [],
        currentIndex: 0,
        tabList: [{
                label: '科技珠海',
                route: 'exhibitionDetail',
                icon: '/image/4Dage/index/img_tags_situation.svg'
            },
            {
                label: '企业名录',
                route: 'exhibitionEnterprise',
                icon: '/image/4Dage/index/img_tags_directories.svg'
            },
            {
                label: '联系我们',
                route: 'exhibitionRegister',
                icon: '/image/4Dage/index/img_tags_register.svg'
            },
        ]
    },
    methods: {
        onLoad: function (options) {
            this.notAdd = true
            this.getActivityDetail()
            this.getCategoryList()
            this.getCompanyList()
            isPhoneX().then(res => {
                this.setData({
                    isIphoneX: res
                })
            })
        },
        onShow () {
            if (this.notAdd && getApp().globalData.token && this.data.companyList.length > 0) {
                this.notAdd = false
                this.data.companyList.forEach(item => {
                    if (item.guideId) {
                        ImApi.addFriend(item.guideId)
                    }
                })
            }   
        },
        onShareAppMessage: function (res) {
            var path = '/pages/index/index'
            return {
                title: '三维数字化技术助力内循环',
                path: path,
                imageUrl: 'https://houseoss.4dkankan.com/domain/exhibition/demo/hwzff.jpg', // 分享的封面图
            }
        },
        toGoodsTab(e) {
            const { index } = e.currentTarget.dataset
            Router.push({
                url: 'goodsTabs',
                query: {
                    active: index
                }
            })
        },
        toExhibitionList () {
            Router.push('exhibitionEnterprise')
        },
        toTabDetail (e) {
            const { route } = e.currentTarget.dataset
            Router.push(route)
        },
        toSearch () {
            Router.push('search')
        },
        getActivityDetail () {
            Api.getActivityDetail().then(res => {
                let detail = res.data
                detail.firstPageContent = JSON.parse(res.data.firstPageContent)
                this.setData({
                    detail
                })
            })
        },
        getCategoryList () {
            Api.getCategoryList().then(res => {
                this.setData({
                    categoryList: res.data.list
                })
            })
        },
        getCompanyList () {
            Api.getCompanyList({ pageSize: 12 }).then(res => {
                this.setData({
                    companyList: res.data.list
                })
                if (this.notAdd && getApp().globalData.token) {
                    this.notAdd = false
                    res.data.list.forEach(item => {
                        if (item.guideId) {
                            ImApi.addFriend(item.guideId)
                        }
                    })
                }   
            })
        },
        toScene (e) {
            const { vr_link, companyid } = e.currentTarget.dataset
            Api.postApiData({ companyId: companyid }).then((res) => {
                console.log(res)
            })
            Router.push({
                url: 'scene',
                query: {
                    vr_link
                }
            })
            
        },
        imageLoad: function(e) {
            var $width=e.detail.width, //获取图片真实宽度
            $height=e.detail.height,
            ratio=$width/$height; //图片的真实宽高比例
            var viewWidth=88 * ratio, //设置图片显示宽度，左右留有16rpx边距
            viewHeight = 88; //计算的高度值
            var image=this.data.images;
            //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
            image[e.target.dataset.index]={
                width:viewWidth,
                height:viewHeight
            }
            this.setData({
                images:image
            })
        },
        changeCurrentIndex (e) {
            this.setData({
                currentIndex: e.detail.current
            })
        }
    }
})
