import config from './config'
export const baseUrl = config.API
import store from '@/store'

const getHeaders = () => {
	let Authorization = getAuthorization()
	let platform = getPlatform()
	let Locale = getLocale()
	let header = {
		'platform': platform,
		"Authorization": Authorization,
		"locale": Locale,
		'Content-Type': 'application/json;charset=utf-8',
		"Accept": "application/json",
	}
	return header
}

const getPdfHeader = () => {
	let Authorization = getAuthorization()
	let platform = getPlatform()
	let Locale = getLocale()
	let header = {
		'platform': platform,
		"Authorization": Authorization,
		"locale": Locale,
		"Accept": "application/octet-stream",
		'Content-Type': 'application/octet-stream',
	}
	return header
}

const getImgHeader = () => {
	let Authorization = getAuthorization()
	let platform = getPlatform()
	let Locale = getLocale()
	let header = {
		'platform': platform,
		"Authorization": Authorization,
		"locale": Locale,
		"Accept": "application/json",
	}
	return header
}

function getPlatform() {
	const platform = uni.getSystemInfoSync().platform;
	if (['android', 'ios'].indexOf(platform) > -1) return platform
	return 'h5'
}

function getLocale() {
	let Locale = uni.getStorageSync('locale');
	return Locale ? Locale : 'zh'
}

function getAuthorization() {
	let Authorization = uni.getStorageSync('Authorization');
	return Authorization ? 'Bearer ' + Authorization : null
}

function callback(resolve, reject, response) {
	let data = response[1].data
	if (data.code) {
		if (data.code == 0) {
			uni.showModal({
				title: data.msg,
				showCancel: false,
			})
		}
		if (data.code == 401) {
			store.commit('logout')
			uni.reLaunch({
				url: '/pages/login/login'
			})
		}
		resolve(data)
	} else {
		resolve(data)
	}
}

const get = (url, param) => {
	let header = getHeaders()
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'GET',
			url: baseUrl + url,
			data: param,
			header: header
		}).then(response => {
			callback(resolve, reject, response)
		}).catch(
			(response) => {
				reject(response)
			}
		)
	})
}

const post = (url, param) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'POST',
			url: baseUrl + url,
			data: param,
			header: getHeaders()
		}).then(response => {
			callback(resolve, reject, response)
		}).catch(
			(response) => {
				reject(response)
			}
		)
	})
}

const put = (url, param) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'PUT',
			url: baseUrl + url,
			data: param,
			header: getHeaders()
		}).then(response => {
			callback(resolve, reject, response)
		}).catch(
			(response) => {
				reject(response)
			}
		)
	})
}

const deleted = (url, param) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'DELETE',
			url: baseUrl + url,
			data: param,
			header: getHeaders()
		}).then(response => {
			callback(resolve, reject, response)
		}).catch(
			(response) => {
				reject(response)
			}
		)
	})
}

const goPage = (url, type = 1) => {
	if(type == 1){
		uni.navigateTo({
			url: url,
		})
	} else {
		uni.switchTab({
			url: url,
		})
	}
}

const backGo = () => {
	var pageArr = getCurrentPages()
	if(pageArr.length==1){
		uni.navigateTo({
			url:'../index/index'
		})
	}
	uni.navigateBack()
}

const getLocalTime = (nS) => {
	  if(!nS){
		  return ''
	  }
	  const date = new Date(nS);
	  let year = date.getFullYear();
	  let month = date.getMonth() + 1;
	  let day = date.getDate();
	  let hour = date.getHours()
	  let min = date.getMinutes()
	  let sec = date.getSeconds()
	  hour = hour > 9 ? hour : '0' + hour;
	  month = month > 9 ? month : '0' + month;
	  day = day > 9 ? day : '0' + day;
	  min = min > 9 ? min : '0' + min;
	  sec = sec > 9 ? sec : '0' + sec;

	  return `${year}-${month}-${day} ${hour}:${min}:${sec}`;
}
const getDate = (nS) => {
	  const date = new Date(nS);
	  let year = date.getFullYear();
	  let month = date.getMonth() + 1;
	  let day = date.getDate();
	  month = month > 9 ? month : '0' + month;

	  return `${year}-${month}-${day}`;
}
const checkFormData = (params, rules) => {
	for(let i in params){
		if(params[i] == '' || params[i] == 0){
			if(rules[i]){
				uni.showToast({
					title: rules[i],
					icon: 'none',
				})
				return false
			}

		}
	}
	return true
}
const checkAddIcon = (data)=>{
	if(!data){
		return data
	}
	if(JSON.stringify(data).indexOf('+')==-1){
		return '+'+data
	}
	return data
}

const getConfig = (dictType = '', pageNo = 1, pageSize = 20) => {
	let header = getHeaders()
	return new Promise((resolve, reject) => {
		uni.request({
			method: 'GET',
			url: baseUrl + '/app-api/system/dict-data/page',
			data: {
				pageNo: pageNo,
				pageSize: pageSize,
				status:0,
				dictType: dictType,
			},
			header: header
		}).then(response => {
			callback(resolve, reject, response)
		}).catch(
			(response) => {
				reject(response)
			}
		)
	})
}

export default {
	get: get,
	put: put,
	post: post,
	deleted: deleted,
	goPage: goPage,
	backGo: backGo,
	getLocale: getLocale,
	getLocalTime: getLocalTime,
	checkFormData: checkFormData,
	getConfig: getConfig,
	baseUrl: baseUrl,
	getHeaders: getHeaders,
	getImgHeader: getImgHeader,
	getPdfHeader:getPdfHeader,
	getDate:getDate,
	checkAddIcon:checkAddIcon
}
