// pages/shopdetail/shopdetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    indicatorDots: false,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    currentSwiper: 0,
    hidewrapno: false,
    hidewraptrue: false,
    shopid: '',
    shopdetail: '',
    images:[],
    pic:[],
    shoplist:[],
    posterurl:'',
    postershow:false,
    remarklen:''
  },

  swiperChange: function(e) {
    this.setData({
      currentSwiper: e.detail.current
    })
    wx.previewImage({
      current: that.data.explain_pic[index],
      urls: that.data.explain_pic,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
      })
    // wx.previewImage({
    //   urls: [],
    // })
  },
  // 获取推荐店铺
  getrecomondshop(){
    let that = this;
    var url = 'shop/infoShopRecommend';
    var params = {
     id: that.data.shopid,
      limit:6,

    }
    app.post(url, params).then((res) => {
      console.log(res);
    that.setData({
      shoplist:res
    })
    }).catch((err) => {

    })
  },
  goodtail(e) {
    let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/shopdetail/shopdetail?id=' + id,
      })

  },

share(){
  this.setData({
    postershow:true
  })
 
  this.getcode()
},
  // 获取海报
  getcode(){
    let that = this;
    var url = 'common/getCode';
    var params = {
      shop_id: that.data.shopid,
      url:'pages/shopdetail/shopdetail',

    }
    app.post(url, params).then((res) => {
      console.log(res);
      that.setData({
        posterurl:res
      })

      that.savefilecode()
     

   
    }).catch((err) => {

    })
  },
  // 评论图片预览
  previewimgk(e){
    console.log(e)
    let pindex=e.currentTarget.dataset.pindex;
    let index=e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.shopdetail.remarks[pindex].images[index],
      urls:this.data.shopdetail.remarks[pindex].images,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
      })

  },
  morecomment() {
    wx.navigateTo({
      url: '/pages/morecomment/morecomment?shopid='+this.data.shopid,
    })
  },
  xiangce() {
    console.log(33443)
    wx.navigateTo({
      url: '/pages/morepic/morepic',
    })
    // if(this.data.pic.length!=0){
    //   wx.navigateTo({
    //     url: '/pages/morepic/morepic',
    //   })
    // }else{
    //     wx.showToast({
    //       title: '没有更多图片了~',
    //       icon:'none'
    //     })
    // }
   
  },
  // 隐藏遮罩层
  sure() {
    this.setData({
      hidewraptrue: false
    })
  },
  // 点评详情
  comentdetail(e) {
    let commentid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/commentdetail/commentdetail?commentid=' + commentid + '&shopid=' + this.data.shopid,
    })
  },

  // 推荐
  recomond() {
    let that = this;
    var url = 'shop/shopRecommend';
    var params = {
      shop_id: that.data.shopid,

    }
    app.post(url, params).then((res) => {
      console.log(res);
      if (that.data.shopdetail.is_recommend == 0) {
        wx.showToast({
          title: '推荐成功',
          icon: 'none'
        })
        that.data.shopdetail.is_recommend = 1;
        that.setData({
          shopdetail: that.data.shopdetail
        })
      } else {
        wx.showToast({
          title: '取消推荐成功',
          icon: 'none'
        })
        that.data.shopdetail.is_recommend = 0;
        that.setData({
          shopdetail: that.data.shopdetail
        })
      }

    }).catch((err) => {

    })
  },
  // 预览图片
  previewimg(e){
    let that=this;
    let index=e.currentTarget.dataset.index
    wx.previewImage({
      current: that.data.images[index],
      urls: that.data.images,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    
  },

  // 收藏
  collect() {
    let that = this;
    var url = 'shop/shopCollect';
    var params = {
      shop_id: that.data.shopid,

    }
    app.post(url, params).then((res) => {
      console.log(res);
      if (that.data.shopdetail.is_collect == 0) {
        wx.showToast({
          title: '收藏成功',
          icon: 'none'
        })
        that.data.shopdetail.is_collect = 1;
        that.setData({
          shopdetail: that.data.shopdetail
        })
      } else {
        wx.showToast({
          title: '取消收藏成功',
          icon: 'none'
        })
        that.data.shopdetail.is_collect = 0;
        that.setData({
          shopdetail: that.data.shopdetail
        })
      }

    }).catch((err) => {

    })
  },

  // 打卡
  daka() {
    let that = this;
    var url = 'shop/addSign';
    var params = {
      shop_id: that.data.shopid

    }
    app.post(url, params).then((res) => {
      console.log(res);
      that.setData({
        hidewraptrue: true
      })


    }).catch((err) => {
      console.log(err)
      wx.showToast({
        title: err.data.msg,
        icon:'none'
      })

    })
  },
  write(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id)
    if (id == 1) {
      console.log(38843)
      this.daka()
    } else if (id == 2) {
      console.log(334)
      wx.navigateTo({
        url: '/pages/passPhotos/passPhotos?type=' + 1 + '&shopid=' + this.data.shopid
      })

    } else if (id == 3) {
      wx.navigateTo({
        url: '/pages/passPhotos/passPhotos?type=' + 2,
      })

    } else if (id == 4) {
      wx.navigateTo({
        url: '/pages/comment/comment?name='+this.data.shopdetail.name+'&shopid='+this.data.shopid,
      })
    }
  },

  // 进入个人主页
  personpage(e){
    console.log(e)
    let userid=e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '/pages/personpage/personpage?userid='+userid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      shopid: options.id
    })
    if (app.globalData.shopid != undefined) {
      this.setData({
        shopid: app.globalData.shopid
      })
    }
// 获取推荐店铺
  this.getrecomondshop();
  this.getcode()

  },

  // 获取店铺详情
  getshopdetail() {
    let that = this;
    var url = 'shop/getShopInfo';
    var params = {
      id: that.data.shopid

    }
    app.post(url, params).then((res) => {
      console.log(res);
    
      that.setData({
        remarklen:res.remarks.length
      })
      let image=[]


      console.log(res.images.length)
      // res.synthetical_score.tofixed("1");
      var pic=[]
      for(var j=0;j<res.images.length;j++){
        pic.push(res.images[j])
      }
     
      that.setData({
        pic:pic
      })
      app.globalData.images=pic;
      console.log( app.globalData.images)
      if (res.images.length>6){
      
        for(var i=0;i<=6;i++){
          image.push(res.images[i])
        }
        that.setData({
          images: image
        }) 
       
      }else{
       
        that.setData({
          images: res.images
        }) 
      }
      // 只保留两条评论
      let newremarks=[];
      console.log('784889439',res.remarks)
      for(var i=0;i<res.remarks.length;i++){
        if(i<2){
          newremarks.push(res.remarks[i])
        }
      }
      console.log(newremarks)
      res.remarks=newremarks;

      that.setData({
        shopdetail: res
      })

      // 评论里的图片保留9张
      that.data.shopdetail.remarks.forEach(function(value,index,array){
        console.log('484389843',value)

        var imagesk=[];
        if(value.images.length>9){
          for(var i=0;i<value.images.length;i++){
            if(i<=9){
              imagesk.push(value.images[i])
            }
          }
          value.images=imagesk
        }
       
      })

      that.setData({
        shopdetail:that.data.shopdetail
      })
      let token=wx.getStorageSync("token");
    if(token==''){
      wx.showToast({
        title: '请先登录',
        icon:"none"
      })
      setTimeout(function(){
        wx.redirectTo({
          url: '/pages/index/index?id='+that.data.shopid+'&type='+1,
        })
      },1500)
    }
      console.log(that.data.shopdetail)
    }).catch((err) => {

    })

  },

    //点击保存到相册
    baocun: function () {
      console.log(999)
      var that = this
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success() { //这里是用户同意授权后的回调
  
  
                wx.saveImageToPhotosAlbum({
                  filePath: that.data.posterurl,
                  success(res) {
                    wx.showModal({
                      content: '图片已保存到相册',
                      showCancel: false,
                      confirmText: '好的',
                      confirmColor: '#333',
                      success: function (res) {
                        if (res.confirm) {
                          console.log('用户点击确定');
                          /* 该隐藏的隐藏 */
                          that.setData({
                            maskHidden: false
                          })
                        }
                      },
                      fail: function (res) {
  
                      }
                    })
                  }
                })
  
  
  
              },
              fail() { //这里是用户拒绝授权后的回调
  
  
                // wx.openSetting({
                //   success: function (data) {
                //     console.log(data)
                //     if (data.authSetting["scope.writePhotosAlbum"] === true) {
                //      console.log("是否授权成功")
                //     } else {
                //       applyApi.toast("授权失败");
                //     }
                //   }
                // })
                wx.showModal({
                  title: '提示',
                  content: '您取消授权,无法保存图片,点击确定打开权限',
                  success(res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.openSetting({
                        success(res) {
                          console.log(res.authSetting)
                          // res.authSetting = {
                          //   "scope.userInfo": true,
                          //   "scope.userLocation": true
                          // }
                        }
                      })
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
  
              }
            })
          } else { //用户已经授权过了
  
            console.log(that.data.codeurl)
            wx.saveImageToPhotosAlbum({
              filePath: that.data.posterurl,
              success(res) {
                wx.showModal({
                  content: '图片已保存到相册',
                  showCancel: false,
                  confirmText: '好的',
                  confirmColor: '#333',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定');
                      /* 该隐藏的隐藏 */
                      that.setData({
                        maskHidden: false
                      })
                    }
                  },
                  fail: function (res) {
  
                  }
                })
              }
            })
          }
        }
      })
  
    },

    // 分享到朋友圈
    shareimg(){
      var that = this
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success() { //这里是用户同意授权后的回调
  
  
                wx.saveImageToPhotosAlbum({
                  filePath: that.data.posterurl,
                  success(res) {
                    wx.showModal({
                      content: '图片已保存到相册,快去朋友圈分享吧~',
                      showCancel: false,
                      confirmText: '好的',
                      confirmColor: '#333',
                      success: function (res) {
                        if (res.confirm) {
                          console.log('用户点击确定');
                          /* 该隐藏的隐藏 */
                          that.setData({
                            maskHidden: false
                          })
                        }
                      },
                      fail: function (res) {
  
                      }
                    })
                  }
                })
  
  
  
              },
              fail() { //这里是用户拒绝授权后的回调
  
  
                // wx.openSetting({
                //   success: function (data) {
                //     console.log(data)
                //     if (data.authSetting["scope.writePhotosAlbum"] === true) {
                //      console.log("是否授权成功")
                //     } else {
                //       applyApi.toast("授权失败");
                //     }
                //   }
                // })
                wx.showModal({
                  title: '提示',
                  content: '您取消授权,无法保存图片,点击确定打开权限',
                  success(res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.openSetting({
                        success(res) {
                          console.log(res.authSetting)
                          // res.authSetting = {
                          //   "scope.userInfo": true,
                          //   "scope.userLocation": true
                          // }
                        }
                      })
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
  
              }
            })
          } else { //用户已经授权过了
  
            console.log(that.data.codeurl)
            wx.saveImageToPhotosAlbum({
              filePath: that.data.posterurl,
              success(res) {
                wx.showModal({
                  content: '图片已保存到相册,快去朋友圈分享吧~',
                  showCancel: false,
                  confirmText: '好的',
                  confirmColor: '#333',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定');
                      /* 该隐藏的隐藏 */
                      that.setData({
                        maskHidden: false
                      })
                    }
                  },
                  fail: function (res) {
  
                  }
                })
              }
            })
          }
        }
      })
    },
    hideposter(){
      this.setData({
        postershow:false
      })
    },
  
    savefilecode() {
      let that = this;
      console.log(that.data.codeurl)
      wx.downloadFile({
        url: that.data.posterurl,
        header: {},
        success: function (res) {
          console.log('下载图片下载图片下载图片', res)
          var tempFilePath = res.tempFilePath
          //console.log('临时文件地址是:' + tempFilePath)
          wx.saveFile({
            tempFilePath: tempFilePath,
            success: function (res) {
              console.log(res)
              var saveFilePath = res.savedFilePath;
              that.setData({
                posterurl: res.savedFilePath
              })
  
              console.log('123456855555555', that.data.qrcode)
  
            }, //可以将saveFilePath写入到页面数据中
            fail: function (res) { },
            complete: function (res) {
              console.log('complete后的res数据:')
            },
          }) //,
        },
        // fail: function (res) {
        //   wx.showModal({
        //     title: '下载失败',
        //     content: '请联系管理员',
        //   })
        // },
        complete: function (res) { },
      })
    },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getshopdetail()
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: this.data.shopdetail.name,
      path: '/pages/shopdetail/shopdetail?id=' + this.data.shopid,
      success: function (res) { }
    }

  }
})