// packageG/supplierDist/supplierDriver/supplierDriver.js
const app = getApp();
import util from "../../../utils/util";
var location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
     * 页面的初始数据
     */
  data: {
    isCode: true,
    delivery_order_id: '',
    confirm_credentials: [],
    point: {
      lng: "",
      lat: ""
    },
  },

  /**
     * 生命周期函数--监听页面加载
     */

  onLoad: function (options) {
    let code = false;
    if (options.code) {
      code = true;
    } else {
      code = false;
    }
    this.setData({
      isCode: code,
      order_id: options.id
    });
    wx.setNavigationBarTitle({
      title: '配送入口',
    });
    this._getLocation((res) => {
      let point = app.BMapTransqqMap(parseFloat(res.location.lng), parseFloat(res.location.lat));
      this.setData({
        address: res.address,
        'point.lat': point.lat,
        'point.lng': point.lng,
      });
    });
  },
  // 弹窗
  showPop() {
    this.setData({
      show: !this.data.show
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  updateImages(e) {
    this.setData({
      confirm_credentials: this.data.confirm_credentials.concat(e.detail)
    });
  },
  confirm() {
    let urlStr = app.getNetAddresss('plugin.supplier-driver-distribution.frontend.delivery-order.confirmReceive');
    app._postNetWork({
      url: urlStr,
      data: {
        delivery_order_id: this.data.order_id,
        confirm_credentials: JSON.stringify(this.data.confirm_credentials),
        lng: this.data.point.lng,
        lat: this.data.point.lat
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            show: false
          });
          this.getDetail();
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
          if (res.data.next_delivery_address.length == 0) {
            // console.log(res.data.next_delivery_address) 没传经纬度后端返回的是空数组
            return;
          } else {
            let point = app.BMapTransqqMap(parseFloat(res.data.next_delivery_address.longitude), parseFloat(res.data.next_delivery_address.latitude));
            wx.openLocation({
              latitude: point.lat,
              longitude: point.lng,
              scale: 18,
              name: res.data.next_delivery_address.address
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  // 弹窗end
  goToAdress(e) {
    console.log(e);
    let info = e.currentTarget.dataset.item;
    let latitude = info.latitude;
    let longitude = info.longitude;
    let name = info.address;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      scale: 18,
      name
    });
  },
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.info.consigneeInfo.mobile,
    });
  },
  /**
     * 生命周期函数--监听页面初次渲染完成
     */
  onReady: function () {

  },

  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    if (this.data.isCode) {
      this.codeGetData();
    } else {
      this.getDetail();
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },
  codeGetData() {
    let urlStr = app.getNetAddresss('plugin.supplier-driver-distribution.frontend.delivery-order.driverScanCodePage');
    app._postNetWork({
      url: urlStr,
      data: {
        delivery_order_id: this.data.order_id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            info: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getDetail() {
    let urlStr = app.getNetAddresss('plugin.supplier-driver-distribution.frontend.delivery-order.deliveryOrderDetail');
    app._postNetWork({
      url: urlStr,
      data: {
        delivery_order_id: this.data.order_id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          res.data.deliveryInfo.scan_time = util.formatTimeTwo(res.data.deliveryInfo.scan_time, 'Y.M.D h-s');
          this.setData({
            info: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  sureTap_iscode(e) {
    console.log(e);
    this.setData({
      show: true
    });
  },
  sureTap() {
    let urlStr = app.getNetAddresss('plugin.supplier-driver-distribution.frontend.delivery-order.confirmScanAction');
    app._postNetWork({
      url: urlStr,
      data: {
        delivery_order_id: this.data.order_id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000,
            success: (resInfo) => {
              // 微信扫一扫
              wx.scanCode({
                onlyFromCamera: true,
                success(res) {
                  console.log(res);
                }
              });
            }
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  }
});