
import { Notification } from 'element-ui';
import request from '@/utils/request';
import { Tools } from './common';
import { Highchart } from '@/assets/js/chartTemplates.js';
import { initData, download } from '@/api/data';

//var baseAPI = process.env.VUE_APP_LOCAL_API + '/';
var baseAPI = process.env.NODE_ENV
 === 'development' ? process.env.VUE_APP_LOCAL_API + '/' : (VUE_APP_API.ServiceURL || process.env.VUE_APP_LOCAL_API) + '/';
var DownloadUrl = process.env.VUE_APP_Download + '/';

var reqPublic = function(url, type, param, opts){
	type = type.toLowerCase();
	var paramKey = type === 'get' ? 'params' : 'data', opts = opts || {};
	var reqConfig = {
		url: url,
		method: type,
	};
	reqConfig[paramKey] = param;
	opts.responseType && (reqConfig['responseType'] = opts.responseType);
	return request(reqConfig)
};

var reqApis = function(){
	return {
		//donwloadURl:DownloadUrl,
		getWeather: function(param) {
			return request({
				url: process.env.VUE_APP_BASE_API + '/weather',
				method: 'post',
				params:param
			})
		},
		getRules:function(baseInfo){
			var rules = {};
			for(var key in baseInfo){
				var item = baseInfo[key];
				if(item.must){
					var typeStr = item.type ? '请选择' : '请输入';
					var rule = {required: true, message:typeStr+item.name, trigger:'blur'};
					item.ruleType && (rule.type = item.ruleType)
					rules[key] = [rule];
				};
			};return Object.keys(rules)[0] ? rules : void 0;
		},
		// 根据设备code获取设备字典列表
		sensorListByCode: function(param){
			return reqPublic('/tab/drybeachequipinfor/sensorList', 'get', param)
		},
		// 设备在线状态字典
		getDictDeviceStatus: function(){
			return reqPublic('/dic/alarm/status', 'get', {})
		},
		getBaseParams: function(searchItem){
			var searchItem = searchItem || reqApi.common.getSearchParam(this.form) || {};
			var param = searchItem || {};
			param.page = this.table.page - 1;
			param.size = this.table.size;
			param.sort = 'date,desc';
			return param;
		},
		common:{
			requst:function(type, url, param){
				var param = param || {};
				return reqPublic(url, type, param).then((res)=>{
					return res
				}).catch(function(error) {
					console.log('requst catch ________________ ', error);
				});
			},
			requstEdge:function(type, url, param){
				var param = param || {};
				return new Promise((resolve, reject) => {
					reqPublic(url, type, param).then((res)=>{
						if(res && res.code === 200){
							return resolve(res)
						}else{
							Notification({
								title: res.status + ': ' + (res.msg || res.message),
								type: 'error',
								duration: 2500
							});
							return reject(res);
						};
					}).catch(function(error) {
						console.log('catch ________________ ', error);
						return reject(error);
					});
				});
			},
			getRequst: function(searchItem){
				var param = reqApi.getBaseParams.call(this);
				this.table.loading = true;
				this.pageApi.request('get', param).then((res) => {
					this.table.loading = false;
					if(res && res.code){
						var body = res.body || {};
						var list = body.list || [];
						this.table.dataList = list;
						this.table.total = body.total * 1;
					}else{
						this.$notify({
							title: res.msg || res.message,
							type: 'error',
							duration: 2500
						})
					};
				}).catch(function(error) {
					this.table.loading = false;
				})
			},
			removeRequst: function(item){
				return this.pageApi.request('delete', item).then((res) => {
					if(res.code === 200){
						this.$notify({
							title: '删除成功！',
							type: 'success',
							duration: 2500
						});
						this.loadData()
					}else{
						this.$notify({
							title: res.msg || res.message,
							type: 'error',
							duration: 2500
						})
					};
				});
			},
			addRequst: function(item){
				return this.pageApi.request('post', item).then((res) => {
					this.form.visible = false;
					if(res.code === 200){
						this.$notify({
							title: '添加成功！',
							type: 'success',
							duration: 2500
						});
						this.loadData()
					}else{
						this.$notify({
							title: res.msg || res.message,
							type: 'error',
							duration: 2500
						})
					};
				}).catch(function(error) {
					this.form.status.cu = 0
				});
			},
			putRequst: function(item){
				return this.pageApi.request('put', item).then((res) => {
					this.form.visible = false;
					if(res.code === 200){
						this.$notify({
							title: '修改成功！',
							type: 'success',
							duration: 2500
						});
						this.loadData()
					}else{
						this.$notify({
							title: res.msg || res.message,
							type: 'error',
							duration: 2500
						})
					};
				}).catch(function(error) {
					this.form.status.cu = 0
				});
			},
			upload:function(e, key){
				var tag = e.target || e.srcElement;
				var file = tag.files[0];
				if(file){
					this.fullLoading = this.$loading({
						lock: true,
						text: 'Loading',
						spinner: 'el-icon-loading',
						background: 'rgba(0, 0, 0, 0.7)'
					});

					var data = new FormData();
					data.append('file', file);
					return this.pageApi.upload(data).then((res) => {
						this.fullLoading.close();
						if(res.code === 200){
							tag.value = '';
							this.form.item[key] = res.body;
							this.$notify({
								title: '提示',
								message: '文件上传成功',
								type: 'success',
								duration: 5000
							})
						};
					}).catch(function(error) {
						this.form.status.cu = 0
					});
				};
			},
			getSearchParam:function(form){
				var search = form.config.search, query = form.query, searchItem = {};
				if(Object.prototype.toString.call(search) === '[object Array]' && Object.keys(query || [])[0]){
					for(var item of search){
						var queryVal = query[item.word];
						if(queryVal && item.type !== "button"){
							var key = item.mode === 0 ? 'vague' : 'exact';
							if(item.type === "daterange"){
								searchItem[item.type] = queryVal.join(',');
							}else{
								var groub = searchItem[key] || (searchItem[key] = {});
								groub[item.word] = queryVal;
							};
						};
					};
				};return searchItem
			},
			getPermission: function(item){
				var list = item.permission || [];
				var username = this.$store.state.user.user.username;
				return list.includes(username);
			},
		},
		
	};
};

// 配制信息
var Config = function(){
	// 获取单元信息
	var getModuleInfo = function(that){
		var path = that.$route.path || '';
		var parts = path.replace('/', '').split('/');
		var i = 0;
		// 递归遍历
		return (function loop(json){
			var key = parts[i]; i++;
			return json[key] ? loop(json[key]) : json;
		})(reqApi);
	};
	return {getModuleInfo:getModuleInfo}
};


//export var tailingPond = tailingPond();
export var reqApi = reqApis();
export var Config = Config();

// WEBPACK FOOTER //
// ./src/common/js/common.js