// packageA/logistics/logistics.js
const Api = require('../../utils/api.js');
var Service = require('../../utils/service.js');
const util = require('../../utils/util.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		active: 0,
		arr: [],
		list: [],
		time: '',
		time2: '',
		time3: '',
		img: '',
		status: -1,//物流状态
		sforderid:'',//物流单号
		logistics: false,
		mobileTail: '',
		ordernumber: '' //订单单号
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let that = this
		that.setData({
			img: options.img,
			ordernumber: options.ordernumber,
		})
		this.getLogisticsInfo(options.ordernumber);
		// this.getLogisticsInfo("20210422589380")
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},
	/**
	 * 获取最新的订单收货地址
	 */
	getLogisticsInfo(ordernumber) {
		let that = this;
		Service.getOrdersAddress({
			ordernumber
		}).then(res => {
			console.log("res", res)
			if (res.code == 200) {
				console.log("res.data", res.data)
				that.setData({
					address: res.data,
					mobileTail: res.data.phone.substring(res.data.phone.length - 4),
				})
				this.getLogisticsNumber(ordernumber);
			} else {
				wx.showToast({
					title: res.msg,
					icon: 'success',
					mask: true
				})
			}
		}).catch(err => {
			wx.showToast({
				title: err,
				icon: 'success',
				mask: true
			})
		})
	},
	/**
	 * 通过订单单号获取物流单号
	 */
	getLogisticsNumber(ordernumber) {
		let that = this;
		Service.getLogisticsNumber({
			ordernumber
		}).then(res => {
			console.log("res.data", res)
			if (res.code == 200) {
				if (res.data) {
					that.setData({
						status:res.data.status,
						sforderid:res.data.sf_waybillno
					})
				} 
				that.getWl()
				// that.setData({
				// 	address: res.data,
				// 	mobileTail: res.data.phone.substring(res.data.phone.length - 4),
				// })
			} else {
				wx.showToast({
					title: res.msg,
					icon: 'success',
					mask: true
				})
			}
		}).catch(err => {
			console.err("getLogisticsNumber", err)
			wx.showToast({
				title: err,
				// icon: 'success',
				mask: true
			})
		})
	},
	/**
	 * 获取物流路由
	 */
	getWl: function () {
		let that = this
		let data = {}
		data.orderid = that.data.sforderid;
		data.mobileTail = that.data.mobileTail;
		data.xdorderid = that.data.ordernumber;
		data.phone = that.data.mobileTail;
		Service.logisticsInfo(data).then((res) => {
			if (res.data) {
				that.setData({
					arr: res.data.orderRoute.reverse(),
					time: res.data.create_time,
					time2: res.data.pcreate_time,
					time3: res.data.create_time_two,
					time4: res.data.drug_send_time,
					logistics: true
				})
			}
		})
	},
	/**
	 * 复制单号
	 * @param {*} e 
	 */
	getNum: function (e) {
		wx.setClipboardData({
			data: this.data.sforderid,
			success: function (res) {
				wx.showToast({
					title: '复制成功',
					icon: 'success',
					mask: true
				})
			}
		})
	},
	ddh: function () {
		wx.makePhoneCall({
			phoneNumber: '95338' //仅为示例，并非真实的电话号码
		})
	},
})