const util = require("../../../utils/util");
const api = require('../../../api/api.js'); 
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    today: '',
    orderList: [],
    showType: 0,
    page: 1,
    limit: 10,
    totalPages: 1,
    addressInfo: {},
    myqr_show: false, //我的分享二维码
    myqr_url: null,
    myqr_blur: 0, //背景模糊值
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    let that = this
    try {
      var tab = wx.getStorageSync('tab');  
       
     if(tab){ 
      this.setData({
        showType: tab==3?0:tab
      });
     }
    } catch (e) {}

  },
  getOrderList() {
    let that = this;
    wx.showLoading();
    util.request(api.PreList, {
      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
        });
      }
    });
  },
  goPre(e) { //去预检
    let id = e.currentTarget.dataset.id;
    let bookNo = e.currentTarget.dataset.bookno;
    let phone = e.currentTarget.dataset.userphone;
    wx.navigateTo({
      url: '/pages/pre/previewing/index?id=' + id+'&bookNo='+bookNo+'&phone='+phone,
    })
  },
  editPre(e) {
    let bookNo = e.currentTarget.dataset.bookno;
    wx.navigateTo({
      url: '/pages/pre/previewing/index?bookNo=' + bookNo,
    });
  },
  switchTab: function (event) {
    let showType = event.currentTarget.dataset.index;
    console.log(showType);
    wx.setStorageSync('tab',showType); 
    if(showType=='3'){
      wx.navigateTo({
        url: '/pages/pre/diyPreviewing/diypreviewing',
      });
    }else{
      this.setData({
        orderList: [],
        showType: showType,
        page: 1,
        limit: 10,
        totalPages: 1
      });
      this.getOrderList();
    }
    
  },
  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;
    }
  },

  makeCall(e) {
    let mobile = e.currentTarget.dataset.mobile;
    wx.makePhoneCall({
      phoneNumber: mobile,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(app.globalData.hasLogin==false){
      wx.navigateTo({
        url: '/pages/index/index',
      })
      return;
    }

    this.setData({
      orderList: [],
      showType: this.data.showType,
      page: 1,
      limit: 10,
      totalPages: 1,
      today:util.formatDate(new Date()),
    });  
    this.getOrderList();
  },
  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 () {

  }

})