var url = require("url");
var System = require("../../base/system");
var metaCtl = System.getObject("web.common.metaCtl");
var tmqueryCtl = System.getObject("web.trademark.tmqueryCtl");
module.exports = function (app) {
	app.get("/jdtm/getUser", async function (req, res) {
		console.log(req.query,"/jdtm/getUser....................");
		var params = req.query;
		if (!params.state) {
			res.redirect("/#/jd/jdtrademark");
			return;
		}
		var userItemResult = await tmqueryCtl.getUserInfo(req.body, req.query, req);
		if (userItemResult.status != 0) {
			res.redirect("/#/jd/jdtrademark");
			return;
		}
		var skipUrl = "/#/jd/" + params.state + "?channelUserId=" + encodeURIComponent(userItemResult.data.account);
		res.redirect(skipUrl);
	});
	app.get("/jdtm/orderNotify", async function (req, res) {
		//TODO:处理新购
	});
	app.get("/", async function (req, res) {
		try {
			var appinfo = await metaCtl.getAppInfo(req);
			if (!appinfo) {
				res.render("index", { appinfo: null, app: null });
			} else {
				res.render("index", appinfo);
			}
		} catch (error) {
			console.log(error.stack, "get/admin，error.............");
		}
	});
	app.get("/admin", async function (req, res) {
		try {
			var appinfo = await metaCtl.getAppInfo(req);
			res.render("admin", appinfo);
		} catch (error) {
			console.log(error.stack, "get/admin，error.............");
		}

	});
	app.get('/jdtm/:gname/:qname/:method', function (req, res) {
		try {
			var classPath = req.params["qname"];
			var methodName = req.params["method"];
			var gname = req.params["gname"];
			classPath = gname + "." + classPath;
			var params = [];
			params.push(methodName);
			params.push(req.body);
			params.push(req.query);
			params.push(req);
			var p = null;
			var invokeObj = System.getObject("web." + classPath);
			if (invokeObj["doexec"]) {
				p = invokeObj["doexec"].apply(invokeObj, params);
			}
			p.then(r => {
				res.end(JSON.stringify(r));
			});
		} catch (error) {
			console.log(error.stack, "get/web/:gname/:qname/:method，error.............");
		}
	});	
	app.get('/web/:gname/:qname/:method', function (req, res) {
		try {
			var classPath = req.params["qname"];
			var methodName = req.params["method"];
			var gname = req.params["gname"];
			classPath = gname + "." + classPath;
			var params = [];
			params.push(methodName);
			params.push(req.body);
			params.push(req.query);
			params.push(req);
			var p = null;
			var invokeObj = System.getObject("web." + classPath);
			if (invokeObj["doexec"]) {
				p = invokeObj["doexec"].apply(invokeObj, params);
			}
			p.then(r => {
				res.end(JSON.stringify(r));
			});
		} catch (error) {
			console.log(error.stack, "get/web/:gname/:qname/:method，error.............");
		}
	});
	app.post('/web/:gname/:qname/:method', function (req, res) {
		try {
			console.log(req.params, "post req.params ..............");
			var classPath = req.params["qname"];
			var methodName = req.params["method"];
			var gname = req.params["gname"];
			var params = [];
			classPath = gname + "." + classPath;
			var tClientIp = System.get_client_ip(req);
			req.body.clientIp = tClientIp;
			req.body.agent = req.headers["user-agent"];
			req.body.classname = classPath;
			params.push(methodName);

			params.push(req.body);
			params.push(req.query);
			params.push(req);
			var p = null;
			var invokeObj = System.getObject("web." + classPath);
			if (invokeObj["doexec"]) {
				p = invokeObj["doexec"].apply(invokeObj, params);
			}
			p.then(r => {
				res.end(JSON.stringify(r));
			});
		} catch (error) {
			console.log(error.stack, "post/web/:gname/:qname/:method，error.............");
		}
	});
};
