const app = getApp()
const util = require("../../../utils/util");
const api = require('../../../api/api.js');
const user = require('../../../utils/user.js');
const map = require('../../../utils/map');
import moment from 'moment'



Page({
	data: {
		detailId: '',
		qrCodeUrl: '',
		status: '',
		latlng: '',
		finishImg: '',
		channelId: ''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		if (options.serviceId) {
			this.setData({
				serviceId: options.serviceId,
				detailServantId: options.detailServantId,
				channelId: options.channelId
			});
			// this.getQrCode()
		}

		let that = this;
		wx.getLocation({
			type: 'wgs84',
			success(res) {
				console.log(res);
				that.setData({
					latlng: res.longitude + ',' + res.latitude
				})
				// const latitude = res.latitude
				// const longitude = res.longitude

			},
			fail(res) {
				wx.showToast({
					title: "请点击右上角打开位置授权",
					icon: 'none'
				})

			}
		})
	},

	chooseImg(e) {
		var that = this;
		wx.chooseMedia({
			count: 1,
			mediaType: ['image'],
			sourceType: ['camera'],
			camera: 'back',
			sizeType: ['original', 'compressed'],
			success: function (res) {
				let file = res.tempFiles[0].tempFilePath
				// that.upload(file);
				wx.compressImage({
					src: file, // 图片路径
					quality: 75, // 压缩质量
					success(res) {
						console.log(res); 
						that.upload(res.tempFilePath);
					},
					fail: (err) => {
						console.error(err);
						wx.showToast({
							title: '微信压缩图片失败：' + err,
							icon: 'none'
						})
					}
				})

			}
		})
	},
	upload: function (path) {
		var that = this;
		const uploadTask = wx.uploadFile({
			url: api.StorageUpload(),
			filePath: path, //res.tempFilePaths[0],
			name: 'file',
			success: function (res) {
				var _res = JSON.parse(res.data);
				if (_res.errno === 0) {
					var url = _res.data.fileUrl
					that.setData({
						finishImg: url,
					});
				}

			}
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		this.getQrCode()
	},
	goBack() {
		wx.navigateBack();
	},
	getQrCode() {
		wx.showLoading({
			title: '加载中...',
		});
		util.request(api.serviceOrderAcceptanceCheck, {
			serviceId: this.data.serviceId
		}, 'GET').then(res => {
			wx.hideLoading();
			if (res.errno === 0) {
				this.setData({
					qrCodeUrl: res.data.qrCodeUrl,
					status: res.data.status
				});

			}
		}).catch(err => {
			wx.hideLoading();
		});
	},
	sendFinish() {
		this.finishOrder();
		// this.sendSms();
	},
	checkFinish() {
		this.finishOrder(true);
	},
	finishOrder(flag) {
		console.log(flag);
		let that = this;


		if (this.data.channelId == 112) { //花王渠道
			if (this.data.finishImg == '') {
				wx.showToast({
					title: '花王工单，需上传工具包整理照片',
					icon: 'none'
				});
				return;
			}
		}

		util.request(api.finishServiceOrder, {
			serviceId: that.data.serviceId,
			detailServantId: that.data.detailServantId,
			latlng: that.data.latlng,
			address: '',
			finishImg: that.data.finishImg
		}, 'GET').then(res2 => {
			console.log(res2);
			wx.hideLoading();
			if (res2.errno == 0) {
				wx.showToast({
					title: '操作成功',
					icon: 'none',
				});
				if (flag) {
					wx.navigateTo({
						url: '/pages/problem/list/list?bookTradeNo=' + that.data.serviceId,
					})
				} else {
					this.sendSms();
				}
			} else {
				wx.showToast({
					title: res2.errmsg + ',' + res2.data,
					icon: 'none',
				});
			}
		}).catch(err => {
			wx.hideLoading();
		});


		// map.getLocation().then((res)=>{
		// 	 console.log(res);
		// }).catch((res)=>{
		// 	console.log('fail' + JSON.stringify(res))
		// });

	},
	sendSms() {
		// wx.showLoading({
		// 	title: '加载中...',
		// });
		util.request(api.smsEvaluate, {
			serviceId: this.data.serviceId
		}, 'GET').then(res => {
			// wx.hideLoading();
			if (res.errno === 0) {

			}
		}).catch(err => {
			wx.hideLoading();
		});
	},
	onHide: function () {

	},

	onUnload: function () {

	},


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

	},

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

	},

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

	},

})