// index.js
let myPro = require("../../utils/wxRequest.js");
let util = require("../../utils/util.js");
Page({
    data: {
		imgUrl: getApp().globalData.imgUrl,
		fare_type: 1, // 0外卖 1自提   默认自提
		transprotFare: 0, // 配送费
		userInfo: null,
		forbidden: null, // 1不允许下单
		store: null,
		is_member: 0 // 是否为会员
    },
    onLoad: function (options) {
        // Do some initialize when page load.
		let that = this;
		that.setData({
			userInfo: getApp().globalData.userInfo
		});
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		that.setData({
			is_member: getApp().globalData.is_member
		});
		// 清掉备注
		wx.removeStorageSync('user_mark');
		// 店铺
		if(wx.getStorageSync('store')){
			that.setData({
				store: JSON.parse(wx.getStorageSync('store'))
			});
		};
		that.getCreatelist();
    },
    onReady: function () {
        // Do something when page ready.
    },
    onHide: function () {
        // Do something when page hide.
    },
    onUnload: function () {
        // Do something when page close.
    },
    onPullDownRefresh: function () {
        // Do something when pull down.
		
    },
    onReachBottom: function () {
        // Do something when page reach bottom.
    },
    onPageScroll: function () {
        // Do something when page scroll
		
    },
    onResize: function () {
        // Do something when page resize
    },
	// 获取购物车商品及配送费
	getCreatelist(){
		let that = this;
		let params = {};
		// 获取配送方式
		if(wx.getStorageSync('fare_type')){
			let fare_type = wx.getStorageSync('fare_type');
			params.fare_type = fare_type;
			that.setData({
				fare_type: fare_type
			});
		};
		// 获取收货地址
		if(that.data.fare_type != 1){
			if(wx.getStorageSync('checkedAddress')){
				let checkedAddress =  JSON.parse(wx.getStorageSync('checkedAddress'));
				that.setData({
					checkedAddress: checkedAddress
				});
				params.address_id = checkedAddress.id;
			};
		};
		// console.log('params',params);
		myPro.wxRequest("user/v3/order/createlist","GET",params).then(res=>{
			that.setData({
				cartGoods: res.result.cartsList,
				transprotFare: res.result.orderInfo.delivery_fee,
				totalPrice: res.result.orderInfo.pay_price,
			});
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		})
	},
	// 获取购物车商品
	getCartsList(){
		let that = this;
		let params = {};
		myPro.wxRequest("user/v3/carts/list","GET",params).then(res=>{
			// 已加购商品
			let list = res.result.list;
			that.setData({
				cartGoods: list,
				totalPrice: res.result.money,
				totalNums: res.result.total_num
			});
			// 计算价格和数量
			that.computedGoods();
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			});
		})
	},
	// 计算运费(店铺与收获地址的距离)
	getTransportFare(){
		let that = this;
		let params = {
			lat: that.data.checkedAddress.lat,
			lng:  that.data.checkedAddress.lnt, // lnt没写错
			store_id: that.data.store.id
		};
		wx.request({
			method: "GET",
			url: getApp().globalData.api + "api/common/store/distance",
			data: params,
			success: function(res){
				let data = res.data;
				if(data.code == 200){
					that.setData({
						forbidden: null,
						transprotFare: data.result.delivery_fee
					});
					// 计算总费用
					that.computedGoods();
				}else if(data.code == 400){
					// 约定400 为超出配送范围
					wx.showToast({
						title: data.msg,
						icon: "none"
					});
					that.setData({
						transprotFare: data.result.delivery_fee
					});
					// 计算总费用 但不允许下单
					that.computedGoods();
					that.setData({
						forbidden: 1
					});
				}else{
					wx.showToast({
						title: data.msg,
						icon: "none"
					});
				}
			},
			fail: function(res){
				console.log("报错",res)
			}
		})
	},
	// 计算费用
	computedGoods(){
		let that = this;
		// 商品的总计数量和总价后台直接返回,但配送费得算上
		let totalPrice = that.data.totalPrice;
		// 加上配送费
		if(that.data.transprotFare){
			// console.log('配送费',that.data.transprotFare)
			totalPrice += parseFloat(that.data.transprotFare); // 返回的可能是字符串
		};
		that.setData({
			totalPrice: totalPrice
		});
	},
	// 输入的备注信息
	getUserMark(event){
		// console.log(event.detail);
		let that = this;
		wx.setStorage({
			key: 'user_mark',
			data: event.detail
		});
	},
	// 进创建订单页
	goCreateOrder(event){
		let that = this;
		if(that.data.forbidden){
			wx.showToast({
				title: "超出配送范围,请更换地址",
				icon: "none"
			})
		}else{
			let url = "";
			if(that.data.fare_type == 0){
				url = '/pages/pay-order/index?fare_type='+that.data.fare_type+'&address_id='+that.data.checkedAddress.id
			}else{
				url = '/pages/pay-order/index?fare_type='+that.data.fare_type
			}
			wx.redirectTo({
				url: url
			});
		}
	},
	// 更改配送地址
	goSelectAddress(){
		let that = this;
		wx.navigateTo({
			url: "/pages/user/address/list/index?is_back=1"
		});
	}
});
