import _ from 'lodash'
import * as util from '@/util/util'
import * as api from '@/service/api'
import store from '@/store/index.js'

const checkError = (code, errorMsg, isNoError, errorCode,data,checkPaying) => {
	
	let isError = false
	let str = errorMsg || ''
	switch (code) {
		case 200:
			isError = false
			break
		case 201:
			isError = false
			break
		case 500:
			isError = false
			str = '服务器错误'
			break
		case 401:
			isError = false
			str = '登录信息过期，请重新登录'
			break
			
		default:
			isError = true
			str = errorMsg
	}

	if(errorCode != 200){
		isError = true
	}
	if(errorCode == 401){
		isError = false
		str = '登录信息过期，请重新登录'
		uni.showModal({
			content: str,
			confirmColor: '#FF7400',
			showCancel: false,
			success: (res) => {
				util.removeStorageSync('userInfo')
				util.removeStorageSync('userToken')
				store.commit('setUserToken',null)
				store.commit('setIsLogin',false)
				uni.reLaunch({
					url:'/pages/login/login'
				})
			}
		})
	}
	
	if(checkPaying && (errorCode == 202 || errorCode == 203 || errorCode == 204) ){
		isError = false
		uni.showModal({
			title: '提示',
			content: data.message,
			cancelText: "不想要了", // 取消按钮的文字  
			confirmText: "去支付", // 确认按钮文字  
			showCancel: true, // 是否显示取消按钮，默认为 true
			confirmColor: '#f55850',
			cancelColor: '#39B54A',
			success: (res) => {
				if (res.confirm) {
					uni.$emit('nopayOrderConfirm',data)
				} else {
					uni.$emit('nopayOrderCancel',data)
				}
			}
		})
	}

	if (!isNoError && isError) {
		uni.showModal({
			content: str,
			confirmColor: '#FF7400',
			showCancel: false
		})
	}
	return isError ? {
		code: errorCode,
		errorMsg: str
	} : false
}

const setFixedHeader = (params) => {
	const userToken = util.getStorageSync('userToken')

	if (userToken) {
		params.data['token'] = `${userToken}`
	}

	myConsole(params.header)


	// params.header['content-type'] = 'application/json'
	return params
}

const requist = (data) => {
	return new Promise((resolve, reject) => {
		let isNoError = data.data && data.data.isNoError

		let params = {
			data: {
				...data.data
			},
			header: {}
		}
		params = setFixedHeader(params)
		delete params.data.isNoError
		if (!params.data.hideLoading) {
			store.commit('setShowLoading',true)
		}
		myConsole(`url:${data.url}`);
		myConsole(params);
		uni.request({
			...data,
			...params,
			success(res) {
				myConsole(`url:${data.url}`);
				myConsole(res);
				uni.stopPullDownRefresh()
				const {
					statusCode
				} = res
				const d = res.data
				const error_message = d.msg
				// d && ((d._error && d._error.message) || d.message || d)
				// const success =  d.code ==
				const errorCode = d.code

				const check = checkError(statusCode, error_message, isNoError, errorCode,res.data,params.data.checkPaying)
				if (check) {
					return reject(check)
				}
	
				return resolve(res.data)
			},
			fail(error) {
				myConsole(
					`error:
		  ${{
            ...data,
            ...params
          }}
          ,
          ${error},`
				)

				uni.showModal({
					content: '网络异常',
					confirmColor: '#FF7400',
					showCancel: false
				})

				uni.stopPullDownRefresh()
				return reject(error)
			},
			complete() {
				if (!params.data.hideLoading) {
					store.commit('setShowLoading',false)
				}
			}
		})
	})
}

export const apiGET = (url, data) => {
	return requist({
		method: 'GET',
		url,
		data
	})
}

export const apiPOST = (url, data) => {
	return requist({
		method: 'POST',
		url,
		data
	})
}

export const apiDELETE = (url, data) => {
	return requist({
		method: 'DELETE',
		url,
		data
	})
}

export const apiPUT = (url, data) => {
	return requist({
		method: 'PUT',
		url,
		data
	})
}


const myConsole = (obj) => {
	console.log(obj)

}
