const req = require("./request.js");

let tmplIds = [];
/**
 * 根据状态添加订阅id
 */

const getsendTemplateId = arr => {
	// console.log("dddaaaa", arr); // if(arr=='') return false

	return new Promise((res, rej) => {

		req.getRequest('/api/wxmes/querylist', {}, data => {
			req.setStorage('tmplIds', data);
		});
		if (req.getStorage('tmplIds')) {
			gettmplIds(req.getStorage('tmplIds'), arr, success => {
				// console.log("debug5", success);
				res(success);
			});
		} else {
			req.getRequest('/api/wxmes/querylist', {}, data => {
				req.setStorage('tmplIds', data);
				gettmplIds(data, arr, success => {
					// console.log("debug5", success);
					res(success);
				});
			});
		}

	});
};

const gettmplIds = async (data, arr, success) => {
	// debugger
	tmplIds = [];
	var item
	if (data && data.length > 0) {
		for (item in arr) {
			data.forEach(res => {
				if (res.bsusinessType == arr[item]) {
					tmplIds.push(res.sendTemplateId);
				}
			});
		}
		await uni.requestSubscribeMessage({
			tmplIds: tmplIds,

			success(res) {
				// console.log(res);
			},

			complete(res) {
				// console.log("deee", 1111);
				success.call(this, 2);
			},

			fail(res) {
				// console.log(res);
			}

		});
	} else {
		success.call(this, 1);
	}
}; //传入特定场景（bsusinessType）的参数来获取对应模板id

/**
 * num=[]
 * @param {*} num 
 * 1 门店新订单
 * 2 门店日报表
 * 3 付款成功
 * 4 发货通知
 * 5 待签收提醒
 * 6 签收成功提醒
 * 7 确认收货提醒
 * 8 已预约活动开始提醒
 * 9 处方审核通知
 * 99 送药到家进度提醒
 * 11 "自提提醒"
 * 13 "预约提醒"
 * 12 "直播开播通知"
 * 15 退款成功提醒
 * 16 开团拼团审核订阅消息
 * 17 团长申请结果通知
 * 100 提现申请通知
 * 18 活动报名成功
 * 19 挂号成功
 * 20 预约到期
 */
// 活动预约提醒


const remind = () => {
	return new Promise(aaa => {
		getsendTemplateId([8, 13]).then(res => {
			aaa(res);
		});
	}); // getsendTemplateId([4, 6])
	// console.log(tmplIds)
	// console.log(tmplIds)
}; //自提订阅


const selfRaising = () => {
	return new Promise(aaa => {
		try {
			getsendTemplateId([3,6,11]).then(res => {
				aaa(res);
				// console.log("debug1", res);
			}, rej => {
				// console.log("debug2", rej);
			});
		} catch (error) {
			// console.log("debug3", error);
		}
	});
};
//配送订阅
const distribution = () => {
	return new Promise(aaa => {
		var arr = [3, 4, 6, 5, 7];

		if (req.getStorage("tmplIds")) {
			let res = req.getStorage("tmplIds");
			let i = [];
			var item
			for (item in arr) {
				res.forEach(res => {
					if (res.bsusinessType == arr[item] && res.sendTemplateId != '') {
						i.push(res.bsusinessType);
					}
				});
			}

			if (i.length >= 3) {
				i.splice(3);
			}
			try {
				getsendTemplateId(i).then(res => {
					// console.log("debug2", res);
					aaa(res);
				}, rej => {
					// console.log("debug3", rej);
				});
			} catch (error) {
				// console.log("debug1", error);
			}
		} else {
			// console.log("==========================456");
			req.getRequest('/api/wxmes/querylist', {}, res => {
				req.setStorage('tmplIds', res);
				let i = [];

				for (item in arr) {
					res.forEach(res => {
						if (res.bsusinessType == arr[item] && res.sendTemplateId != '') {
							i.push(res.bsusinessType);
						}
					});
				}

				if (i.length >= 3) {
					i.splice(3);
				}

				try {
					getsendTemplateId(i).then(res => {
						aaa(res);
					}, rej => { // console.log("debug3", rej)
					});
				} catch (error) { // console.log("debug1", error)
				}
			});
		}
	}); // console.log("rrr",rrr)
	// return rrr
}; //及时达订阅


const jisda = () => {
	return new Promise(aaa => {
		getsendTemplateId([3,6,7]).then(res => {
			aaa(res);
		});
	});
};
// 处方订阅
const recipel = (mode) => {
	return new Promise(aaa => {
		let arr = [9]
		// console.log(mode)
		if (mode == '1') {
			arr = arr.concat([11])
			try {
				getsendTemplateId(arr).then(res => {
					aaa(res);
				}, rej => { // console.log("debug3", rej)
				});
			} catch (error) { // console.log("debug1", error)
			}
		} else {
			var a = [3, 4, 6, 5, 7];
			if (req.getStorage("tmplIds")) {
				let res = req.getStorage("tmplIds");
				let i = [];
				var item
				for (item in a) {
					res.forEach(res => {
						if (res.bsusinessType == a[item] && res.sendTemplateId != '') {
							i.push(res.bsusinessType);
						}
					});
				}

				if (i.length >= 2) {
					i.splice(2);
				}
				arr = arr.concat(i)
				// console.log(arr, "11111111", i)
				try {
					getsendTemplateId(arr).then(res => {
						aaa(res);
					}, rej => { // console.log("debug3", rej)
					});
				} catch (error) { // console.log("debug1", error)
				}
			} else {
				// console.log("=========================789=");
				req.getRequest('/api/wxmes/querylist', {}, res => {
					req.setStorage('tmplIds', res);
					let i = [];
					for (item in a) {
						res.forEach(res => {
							if (res.bsusinessType == arr[item] && res.sendTemplateId !=
								'') {
								i.push(res.bsusinessType);
							}
						});
					}

					if (i.length >= 2) {
						i.splice(2);
					}
					arr = arr.concat(i)
					try {
						getsendTemplateId(arr).then(res => {
							aaa(res);
						}, rej => { // console.log("debug3", rej)
						});
					} catch (error) { // console.log("debug1", error)
					}
				});
			}
		}
		// getsendTemplateId(arr).then(res => {
		//   aaa(res);
		// });
	});
};
const refund = () => {
	return new Promise(aaa => {
		try {
			getsendTemplateId([15]).then(res => {
				aaa(res);
				// console.log("debug1", res);
			}, rej => {
				// console.log("debug2", rej);
			});
		} catch (error) {
			// console.log("debug3", error);
		}
	});
};

//活动订阅
const remindActivity = () => {
	return new Promise(aaa => {
		try {
			getsendTemplateId([18]).then(res => {
				aaa(res);
				// console.log("debug1", res);
			}, rej => {
				// console.log("debug2", rej);
			});
		} catch (error) {
			// console.log("debug3", error);
		}
	});
};

module.exports = {
	remind: remind,
	selfRaising: selfRaising,
	getsendTemplateId: getsendTemplateId,
	distribution: distribution,
	jisda: jisda,
	recipel: recipel,
	refund: refund,
	remindActivity: remindActivity,
	// templateId,
	// requsetmessage,
	tmplIds
};
