import Taro, { Component } from '@tarojs/taro';
import './index.scss'
import Around from './Around/index'
import { savePoint } from '@services/common'
import 'dayjs/locale/zh-cn' // 按需加载
import ready from '@utils/ready'
import { connect } from '@tarojs/redux'
import { dispatchProjectDetail } from '@actions/project'


@connect(
    ({ user, project }) => ({ ...user, ...project }),
    { dispatchProjectDetail }
)
export default class Index extends Component {
    config = {
        navigationBarTitleText: '更多周边设施',
    }

    state = {
        buildingId: null,
    }

    componentWillMount() {
        ready.queue(() => {
            const router = Taro.getStorageSync('router')
            const id = this.$router.params.id || router.query.id
            this.initPageData(id)

        })
    }
    initPageData(id) {

        this.setState({
            buildingId: id
        }, () => {
            this.loadDetail()
        })
    }



    loadDetail() {
        const { buildingId } = this.state
        const { dispatchProjectDetail } = this.props
        this.setState({
            loaded: false,
        })
        Taro.showToast({
            title: '正在加载数据',
            icon: 'loading'
        })

        dispatchProjectDetail(buildingId).then(res => {
            this.setState({
                loaded: true,
                // circumOpts: newCircumOpts,
                isSaved: res.isSave
            }, () => {
                if (res.posters && res.posters.length) {
                    this.setState({
                        posterShow: 'flex'
                    })
                }
            })
            Taro.hideToast()
        })
    }



    openMap() {
        const {
            buildingName,
            address,
            coordinate = ''
        } = this.props.projectDetail

        const coord = coordinate.split(',')
        const latitude = parseFloat(coord[0])
        const longitude = parseFloat(coord[1])
        Taro.openLocation({
            latitude,
            longitude,
            name: buildingName,
            address,
            scale: 18
        })
        savePoint({
            event: 'navigation',
            eventType: 'building',
            propertyName: '查看项目地图',
            data: '{}'
        }).then(res => {
            console.log('查看项目地图')
        })
    }

    toViewAlbum(index) {
        const { buildingId } = this.state
        console.log('1111111', index)
        this.navigateTo(`/pages/project/album/index?id=${buildingId}&current=${index}`)
    }

    HandleCircumTabClick(id) {
        this.setState({
            curTab: id
        })
    }


    render() {
        const building = {
            ...this.props.projectDetail
        }
        return (
            <Block>

                <Around building={building} onAction={this.openMap.bind(this)}></Around>

            </Block>
        );
    }
}
