var util = require('../../../utils/util.js');
var api = require('../../../api/api.js');
var check = require('../../../utils/check.js');

var app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		clockInfo: {

		},
		clockImgFront: '',
		clockImgBack: ''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

		let dt = app.globalData.clockInfo;
		console.log(dt);
		this.setData({
			clockInfo: dt
		})
	},
	submitClock() {
		let req = this.data.clockInfo;

		if (req.channelId == 112) { //花王渠道
			if (this.data.clockImgFront == '') {
				wx.showToast({
					title: '花王工单，需上传工作半身照',
					icon: 'none'
				});
				return;
			}
			if (this.data.clockImgBack == '') {
				wx.showToast({
					title: '花王渠道，需上传工具摆放照',
					icon: 'none'
				});
				return;
			}
		}

		if (this.data.clockImgFront != '') {
			req.ondoorImg = this.data.clockImgFront;
		}
		if (this.data.clockImgBack != '') {
			if (req.ondoorImg.length > 0) {
				req.ondoorImg = req.ondoorImg + "," + this.data.clockImgBack;
			} else {
				req.ondoorImg = this.data.clockImgBack;
			}
		}

		console.log(req);
		util.request(api.arrivedByMerchantNew, req, 'GET').then(res => {
			if (res.errno === 0) {
				wx.showToast({
					title: '操作成功'
				});

				setTimeout(() => {
					wx.navigateBack({
						delta: 1
					});
				}, 2000);
			} else {
				wx.showToast({
					title: res.errmsg,
					icon: 'none'
				})

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

			}
		})
	},
	upload: function (path, type) {
		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
					if (type == 1) {
						that.setData({
							clockImgFront: url,

						});
					} else {
						that.setData({
							clockImgBack: url
						});
					}

				}

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

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

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

	},

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

	},

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

	},

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

	},

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

	}
})