const util = require("../../../utils/util");
const api = require('../../../api/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    showType: 0,
    page: 1,
    limit: 10,
    totalPages: 1,
    addressInfo:{},
    myqr_show:false,//我的分享二维码
    myqr_url:null,
    myqr_blur:0,//背景模糊值
    code:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    try {
      var tab = wx.getStorageSync('tab'); 

      this.setData({
        showType: tab
      });
    } catch (e) {
     
    }

  },
  getOrderList() {
    wx.showLoading({
      title: '查询中',
    }) 
    let that = this;
    util.request(api.BookDetailList, {
      showType: that.data.showType,
      page: that.data.page,
      limit: that.data.limit
    }).then(function(res) {
      wx.hideLoading();
      if (res.errno === 0) {
        console.log(res.data);
        that.setData({
          orderList: that.data.orderList.concat(res.data.list),
          totalPages: res.data.pages
        });
      }
    });
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getOrderList();
    } else {
      wx.showToast({
        title: '没有更多订单了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },
  switchTab: function(event) {
    let showType = event.currentTarget.dataset.index;
    this.setData({
      orderList: [],
      showType: showType,
      page: 1,
      limit: 10,
      totalPages: 1
    });
    this.getOrderList();
  },
  makeCall(e){
    let mobile = e.currentTarget.dataset.mobile;
    wx.makePhoneCall({
      phoneNumber: mobile,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      orderList: [],
      showType: this.data.showType,
      page: 1,
      limit: 10,
      totalPages: 1
    });
    this.getOrderList();
  },
  goProvideBook(e){//完成服务 
    let that=this;
    let id=e.currentTarget.dataset.id;
    wx.showModal({
      title: '完成服务',
      confirmColor: '#b4282d',
      content: '确定完成此服务？完成后，无法修改',
      success: function (res) {
        if (!res.confirm) {
          return;
        }
        util.request(api.BookDetailProvice,{id:id}).then(res=>{
          if(res.errno==0){
            wx.showToast({
              title: '服务完成',
              icon: 'success',
            });
            var list=that.data.orderList;
            list.forEach(item=>{
              item.details.forEach(dt=>{
                if(dt.id==id){
                  dt.status="服务已提供";
                  dt.statusId=3;
                }
              })
            }) 

            that.setData({
              orderList:list
            }) 

          }
        })
      }
    }) 
  }, 
  openMap(e){//打开地图
    let address=e.currentTarget.dataset.address;
    let lng=e.currentTarget.dataset.lng;
    let lat=e.currentTarget.dataset.lat;
    // console.info(lng)
    // console.info(lat)
    // console.info(address)
     wx.openLocation({       
          latitude: lat,  
          longitude: lng,
          name: address,
          address:address,
          scale: 28
        }) 
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },

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

  },

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

  },
 

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

  },
  
  shareGoods(e){ 
    let that=this;  
     wx.showLoading({title: '生成中',});  
    util.request(api.GoodsShare, {
      goodsId:e.currentTarget.dataset.id
    }).then(function(res) { 
      console.info(res)
      wx.hideLoading();
      if(res.errno==0){
      that.setData({
      myqr_url:res.data.imgUrl,
      myqr_show:true,
      myqr_blur:5
      });  
    } else if(res.errno==501){

      // wx.navigateTo({
      //   url: "/pages/index/index"
      // });
    } else {
     wx.showToast({
       title: res.errmsg, 
       duration:5000
     }) 
    } 
    });  
 
  }, 
  qrtouchstart:function(){
    this.setData({
      myqr_blur:0,
      myqr_show:false  
    });
  },
  saveShare: function() {
    let that = this;
    wx.showLoading({
      title: '图片保存中',
    });
    console.log(that.data);
    wx.downloadFile({
      url: that.data.myqr_url,
      success: function(res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function(res) {
            wx.hideLoading();
            wx.showModal({
              title: '存图成功',
              content: '图片成功保存到相册了，可以分享到朋友圈了',
              showCancel: false,
              confirmText: '好的',
              confirmColor: '#a78845',
              success: function(res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                  that.setData({ 
                    myqr_show:false,
                    myqr_blur:0
                    }); 
                }
              }
            })
          },
          fail: function(res) {
            wx.hideLoading();
           wx.showModal({
              title: '警告',
              content: '未授权无法保存',
              showCancel: false
          })
          that.setData({ 
            myqr_show:false,
            myqr_blur:0
            }); 
        }
        })
      },
      fail: function(res) {
        wx.hideLoading();
        console.log(res);
      }
    })
  },

})