// pages/foodDetail/foodDetail.js

import request from '../../utils/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopTitle: '', //店铺名
        foodList:[],    //美食照片路径
        shopDescribe: '', //商铺简介
        perCost:'', //人均消费
        businessHours:'',   //营业时间
        heat: '', //商铺热度
        dish:'',    //招牌菜
        shopPhone: '', //商铺电话
        shopAddress: '', //商铺地址
        shopLatitude: 0, //商铺纬度
        shopLongtitude: 0 //商铺经度
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        // 进入页面时,提示“数据加载中”
        wx.showLoading({
            title: '数据加载中',
            mask:true
        })

        // 获取上一个页面传过来的美食店铺id
        // console.log(options.id);
        let id = options.id
        //更新浏览量
        await request('/front/merchant/' + id).catch(err=>{
            wx.hideLoading()
            wx.showModal({
                title: '数据加载失败',
                content: '当前网络不佳,请稍后再试',
                complete: (res) => {
                  if (res.cancel) {
                      wx.navigateBack()                      
                  }
              
                  if (res.confirm) {
                      wx.navigateBack()                        
                  }
                }
              })
        }) 

        //获取店铺照片
        let imagePath = await request('/front/findAllMerchantFilesPath/' + id) 
        // console.log(imagePath);

        //获取店铺列表
        let result = await request('/front/merchantAndCallout') 
        // console.log(result);
        for (let i = 0; i < result.length; i++) {
            if (result[i].id == id) {
                this.setData({
                    shopTitle: result[i].title,
                    shopDescribe: result[i].describes,
                    heat: result[i].viewCount,
                    dish:result[i].dish,
                    shopPhone: result[i].phone,
                    shopAddress: result[i].location,
                    shopLatitude: result[i].latitude,
                    shopLongtitude: result[i].longitude,
                    perCost:result[i].perCost,
                    businessHours:result[i].businessHours
                })
            }
        }
        let foodList = this.data.foodList
        for(let j = 0;j < imagePath.length;j++){
            foodList[j] = imagePath[j].iconPath
        }
        this.setData({
            foodList
        })
        // console.log(this.data.shopAddress);

        // 关闭“数据加载中”的提示
        if(this.data.shopTitle != ''){
            wx.hideLoading()
        }else{
            setTimeout(function () {
                wx.hideLoading()
            }, 2000)
        }
    },

    //点击预览图片
    toPreviewImage() {
        let foodList = this.data.foodList
        wx.previewImage({
            urls: foodList // 需要预览的图片 http 链接列表
        })
    },

    // “去这里”按钮功能
    toFoodAddress() {
        let latitude = parseFloat(this.data.shopLatitude)
        let longtitude = parseFloat(this.data.shopLongtitude)
        // console.log(latitude); 
        // console.log(longtitude);
        wx.openLocation({
            latitude: latitude,
            longitude: longtitude,
            name: this.data.shopTitle
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})