import { netSearchForm } from '@/api/index.js'
// 防止处理多次点击
function noMultipleClicks(methods, info) {
	// methods是点击后需要执行的函数， info是函数需要传的参数
	let that = this;
	if (that.noClick) {
		// 第一次点击
		that.noClick = false;
		if ((info && info !== '') || info == 0) {
			// info是执行函数需要传的参数
			methods(info);
		} else {
			methods();
		}
		setTimeout(() => {
			that.noClick = true;
		}, 5000)
	} else {
		//  这里是重复点击的判断

	}
}

//去重
function deWeight(arr) {
	for (var i = 0; i < arr.length - 1; i++) {
		for (var j = i + 1; j < arr.length; j++) {
			if (arr[i].id == arr[j].id) {

				arr.splice(j, 1);
				j--;
			}
		}
	}
	return arr;
}

function openCommonImg(data,index) {
	let arr = []
	data.forEach(ele=>{
		arr.push(ele.file_path)
	})
	uni.previewImage({
		urls:arr,
		current:index
	})
}
// 打开文档
function openCommonFile(data) {
	let arr = ['png','jpg','img','image','gif']
	let name = data.name
	let suffix = name.substring(name.indexOf('.')+1)
	if(!arr.includes(suffix)){
		// #ifdef MP-WEIXIN
		uni.downloadFile({
			url: data.file_path,
			success: (downres) => {
				uni.getSystemInfo({
					success:(res)=>{
						if(res.platform == 'windows' || res.platform == 'mac'){
							//pc端
							wx.saveFileToDisk({
								filePath:downres.tempFilePath,
								success:(saveSuc)=>{
									console.log(saveSuc)
								},
								fail:(saveFail)=>{
									console.log(saveFail)
								}
							})
						}else{
							downOrOpen(downres,data.name,data.file_path,suffix)
						}
					}
				})
			},
			fail: (errmsg) => {
				uni.hideLoading()
				console.log(errmsg,'===')
			}
		})
		// #endif
	}else{
		uni.previewImage({
			urls: [data.file_path],
			complete:(res)=>{
				console.log(res)
			}
		})
	}
}

function downOrOpen(downres,name,file,type) {
	uni.showLoading({title:'正在加载'})
	let manage = uni.getFileSystemManager()
	let urls = wx.env.USER_DATA_PATH + '/' + name
	manage.saveFile({		
		tempFilePath: downres.tempFilePath,
		filePath: urls,
		fileType: type,
		success: (saveData) => {
			uni.openDocument({
				filePath: saveData.savedFilePath,
				showMenu: true,
				fileType:type,
				success: (res) => {
					console.log(res,'成功打开')
					uni.hideLoading()
				},
				fail: (er) => {
					console.log(er,'打开失败')
					uni.hideLoading()
				}
			})
		}
	})
}

//提交的数据 进行校验必填 非必填
function checkCommonForm(data){
	let arr = []
	data.forEach(ele=>{
		arr.push({
			name:ele.id,
			required: ele.config.required,
			msg: ele.config.placeholder,
			value: ele.value,
			component: ele.component
		})
	})
	let obj = {}
	for (let i = 0; i < arr.length; i++) {
		if (arr[i].required && !arr[i].value) {
			uni.showToast({
				title: arr[i].msg,
				icon: 'none'
			})
			return
		}
		if(arr[i].component == 'uploadImage' || arr[i].component == 'uploadFile'){
			let a = arr[i].value?arr[i].value:[]
			let ids = []
			a.forEach(ele=>{
				ids.push(ele.id)
			})
			obj[arr[i].name] = ids.join(',')
		}else{
			obj[arr[i].name] = arr[i].value
		}
	}
	return obj
}

//根据年月2022-07  获取这个月份的开始日期  结束日期
function checkStartAndEnd(date) {
	let year = date.substring(0,4)
	let month = date.substring(5,7)
	console.log(year,Number(month),'分割')
	let arr = []
	if(year%4==0&&year%100!=0||year%400==0){
		arr=[31,29,31,30,31,30,31,31,30,31,30,31]
	}else{
		arr=[31,28,31,30,31,30,31,31,30,31,30,31]
	}
	let m = Number(month)
	let start = '01'
	let end = arr[m-1]
	return [year+'-'+month+'-'+start,year+'-'+month+'-'+end]
}

// 处理时间
function handleTime(num) {
	if(num >= 10){
		return num
	}else{
		return '0'+num
	}
}

//搜索表单渲染时 的  数据处理
function handleSearchForm(type,callback) {
	netSearchForm({type}).then(res=>{
		let data = res.data
		let newData = []
		data.forEach(ele=>{
			let obj = {
				form_type:ele.form_type,
				name:ele.name,
				field:ele.field,
				input_tips:ele.input_tips,
			}
			if(ele.form_type == 'TimePicker' || ele.form_type == 'DatePicker'){
				obj.start_time = ''
				obj.end_time = ''
			}else{
				obj.value = ''
			}
			if(ele.form_type == 'radio' || ele.form_type == 'checkbox'){
				obj.content = ele.setting.split('|')
			}
			if(ele.form_type == 'select'){
				obj.content = []
				let arr = ele.setting.split('|')
				arr.forEach((item,index)=>{
					obj.content.push({
						label:item,
						nodeKey:index
					})
				})
			}
			newData.push(obj)
		})
		callback(newData)
	})
}

//搜索表单 确定时的  数据处理
function handleSureSearch(data){
	let obj = {}
	data.forEach(ele=>{
		if(ele.form_type == "DatePicker" || ele.form_type == "TimePicker"){
			obj[ele.field] = (ele.start_time && ele.end_time) ? ele.start_time+','+ele.end_time : ''
		}else{
			obj[ele.field] = ele.value
		}
	})
	return obj
}

//重置搜索 表单
function handleResetSearch(data) {
	data.forEach(ele=>{
		if(ele.form_type == "DatePicker" || ele.form_type == "TimePicker"){
			ele.start_time = ''
			ele.end_time = ''
		}else{
			ele.value = ''
		}
	})
	return data
}


//导出
export default {
	noMultipleClicks, // 禁止多次点击
	deWeight,
	openCommonFile,
	openCommonImg,
	checkCommonForm,
	checkStartAndEnd,
	handleTime,
	handleSearchForm,
	handleSureSearch,
	handleResetSearch
}
