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

module.exports = function (app) {

	app.get('/doc', function (req, res) {
		var path = process.cwd() + "/app/front/entry/public/apidoc/README.md";
		// var path = process.cwd() + "/xgg-saas-platform/app/front/entry/public/apidoc/README.md";
		fs.readFile(path, function(err, data){
			if(err){
				console.log(err);
				res.send("文件不存在！");
			} else{
				console.log(data);
				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";
		// var path = process.cwd() + "/xgg-saas-platform/app/front/entry/public/apidoc/README.md";

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

	app.get('/doc/:forder/:fileName', function (req, res) {
		var forder = req.params["forder"];
		var fileName = req.params["fileName"] || "README.md";
		var path = process.cwd() + "/app/front/entry/public/apidoc";
		// var path = process.cwd() + "/xgg-saas-platform/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{
				console.log(data);
				str = marked(data.toString());
				console.log(str);
				res.render('apidoc',{str});
			} 
		});
	});
};
