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: {},
    testMobiles: ['132****8272', '173****9375', '182****9968', '130****7996', '131****2586', '138****9808', '151****2510', '178****5726'],
    isTest: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    try {
      var tab = wx.getStorageSync('tabQuick');
      if (tab) {
        this.setData({
          showType: tab == 3 ? 0 : tab
        });
      }
    } catch (e) {}

    let userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      var isTest = false;
      that.data.testMobiles.forEach(item => {
        if (item == userInfo.mobile)
          isTest = true;
      })
      that.setData({
        isTest: isTest
      });
    }

  },
  getOrderList() {
    let that = this;
    wx.showLoading();
    util.request(api.BookHmList, {
      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
        });
      }
    });
  },

  goArrived(e) { //我已上门
    let that = this;
    let bookTradeNo = e.currentTarget.dataset.id;
    wx.showModal({
      title: '我已上门',
      content: '确定我已到顾客服务地？',
      showCancel: true, //是否显示取消按钮 
      confirmText: "已上门", //默认是“确定”
      confirmColor: '#09afff', //确定文字的颜色 
      success: function (res) {
        wx.showLoading();
        util.request(api.HmApiArrived, {
          bookTradeNo: bookTradeNo
        }).then(res => {
          wx.hideLoading();
          if (res.errno == 0) {
            let selected = that.data.orderList;
            selected.forEach(item => {
              if (item.bookNo == bookTradeNo) {
                item.status = '待履约'
              }
            })
            that.setData({
              orderList: selected
            })
          } else
            util.showErrorToast(res.errmsg);
        }).catch(res => {
          wx.hideLoading();
          util.showErrorToast(res.errmsg);
        })
      }
    })
  },
  goConfirm(e) {
    let that = this;
    let bookTradeNo = e.currentTarget.dataset.id;
    wx.showModal({
      title: '服务已完成',
      content: '确定我已服务完成？',
      showCancel: true, //是否显示取消按钮 
      confirmText: "完成", //默认是“确定”
      confirmColor: '#09afff', //确定文字的颜色 
      success: function (res) {
        wx.showLoading();
        util.request(api.HmApiConfirm, {
          bookTradeNo: bookTradeNo
        }).then(res => {
          wx.hideLoading();
          if (res.errno == 0) {
            let selected = that.data.orderList;
            selected.forEach(item => {
              if (item.bookTradeNo == bookTradeNo) {
                item.status = '服务已完成'
                return;
              }
            })
            that.setData({
              orderList: selected
            })
          } else
            util.showErrorToast(res.errmsg);
        }).catch(res => {
          wx.hideLoading();
          util.showErrorToast(res.errmsg);
        })
      }
    })
  }, 
  switchTab: function (event) {
    let showType = event.currentTarget.dataset.index;
    wx.setStorageSync('tabQuick', showType);
    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) { 
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })
  },
  goProblemAdd(e) {
    let id = e.currentTarget.dataset.id;
    if (id) {
      wx.navigateTo({
        url: '/pages/problem/list/list?bookTradeNo=' + id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/problem/list/list',
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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 that = this;
    let bookTradeNo = e.currentTarget.dataset.id;
    let bookInfo = null;
    that.data.orderList.forEach(item => {
      if (item.bookTradeNo == bookTradeNo) {
        bookInfo = item; 
        return;
      }
    })  
    let address = bookInfo.address;
    let lng = bookInfo.position.split(',')[0];
    let lat =  bookInfo.position.split(',')[1]; 
    wx.openLocation({
      latitude:parseFloat(lat),
      longitude: parseFloat(lng),
      name: address,
      address: address,
      scale: 28
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },


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

  }

})