const url = require("url");
const system = require("../../base/system");
const fs = require('fs');
const marked = require("marked");

module.exports = function (app) {

	app.get('/doc', function (req, res) {
		// if (!req.query.key) {
		// 	res.send("文件不存在！！！");
		// 	return;
		// }
		// if (req.query.key != "doc12345789") {
		// 	res.send("文件不存在！！！！！！");
		// 	return;
		// }
		var path = process.cwd() + "/app/front/entry/public/apidoc/README.md";
		fs.readFile(path, function (err, data) {
			if (err) {
				console.log(err);
				res.send("文件不存在！");
			} else {
				str = marked(data.toString());
				res.render('apidoc', { str });
			}
		});
	});

	app.get('/doc/:forder', function (req, res) {
		var path = process.cwd() + "/app/front/entry/public/apidoc/README.md";

		fs.readFile(path, function (err, data) {
			if (err) {
				console.log(err);
				res.send("文件不存在！");
			} else {
				str = marked(data.toString());
				res.render('apidoc', { str });
			}
		});
	});

	app.get('/doc/api/:forder/:fileName', function (req, res) {
		// if (req.url != "/doc/api/platform/fgbusinesschance.md") {
		// 	if (!req.query.key) {
		// 		res.send("文件不存在！！！");
		// 		return;
		// 	}
		// 	if (req.query.key != "doc12345789") {
		// 		res.send("文件不存在！！！！！！");
		// 		return;
		// 	}
		// }
		var forder = req.params["forder"];
		var fileName = req.params["fileName"] || "README.md";
		var path = process.cwd() + "/app/front/entry/public/apidoc";
		if (forder) {
			path = path + "/" + forder + "/" + fileName;
		} else {
			path = path + "/" + fileName;
		}
		fs.readFile(path, function (err, data) {
			if (err) {
				console.log(err);
				res.send("文件不存在！");
			} else {
				str = marked(data.toString());
				res.render('apidoc', { str });
			}
		});
	});
};
