#!/bin/sh

. ./ajax_common

paui_vpath=""
query_string=""
page_args="?v=`date +%s`"

for obj in `echo "${QUERY_STRING}" | tr "&" " "`
do
	key=`echo ${obj} | cut -d"=" -f1`
	val=`echo ${obj} | cut -d"=" -f2`
	
	val="`${URLENCODE} -d ${val}`"
	val=`${FLOWEYE} iconv utf8togb2312 "${val}"`

	export CGI_${key}="${val}"

	query_string="${query_string}\"${key}\":\"${val}\","
done

if [ "${CGI_saaskey}" != "" ]; then
	paui_vpath="${CGI_saaspath}/VPATH_${CGI_saaskey}"
	page_args="${page_args}&saaskey=${CGI_saaskey}&saaspath=${CGI_saaspath}"
fi

pop_tilte=${CGI_pop_title}
page_path="/html/${CGI_pop_route}.html"
js_head="layui.use(function(){var \$ = layui.\$, util = layui.util,laytpl = layui.laytpl;"

if [ "${CGI_popup_from_ntm}" = "1" ]; then
	page_path="/html/App/ntm/${CGI_pop_route}.html"
	js_head="layui.use('ntmlog', function(){var \$ = layui.\$, util = layui.util,laytpl = layui.laytpl,ntmlog=layui.ntmlog;"
fi


printf "Content-type: text/html; charset=GB2312\r\n"
printf "Cache-Control: no-cache\r\n"
printf "\r\n"

awk '{gsub("\\t", "", $0); printf $0}' << EOF
<!DOCTYPE html>
<html>
<head>
	<meta charset="gb2312">
	<meta http-equiv="cache-control" content="no-cache" />
	<meta http-equiv="Pragma" content="no-cache" />
	<meta http-equiv="expires" content="0" />
	<title>${pop_tilte}</title>
	<link rel="icon" type="image/png" href="${paui_vpath}/favicon.ico">
	<link rel="stylesheet" href="${paui_vpath}/cgi-bin/common/page_loader${page_args}&file=laycss">
	<link rel="stylesheet" href="${paui_vpath}/cgi-bin/common/page_loader${page_args}&file=paicon">
	<link rel="stylesheet" href="${paui_vpath}/cgi-bin/common/page_loader${page_args}&file=pacss">
	<script src="${paui_vpath}/cgi-bin/common/page_loader${page_args}&file=layjs"></script>
	<script src="${paui_vpath}/html/assert/panabit.js${page_args}"></script>
	<script src="${paui_vpath}/html/assert/extent/sortable.js${page_args}"></script>
	<script src="${paui_vpath}/html/assert/extent/xm-select.js${page_args}"></script>
	<script src="${paui_vpath}/html/assert/extent/echarts.min.js${page_args}"></script>
	<script src="${paui_vpath}/img/highcharts.js${page_args}"></script>
	<script src="${paui_vpath}/img/global_timezone.js${page_args}"></script>
	<script src="${paui_vpath}/img/crypto.js${page_args}"></script>
</head>

<body>
	<div id="app-page-body" class="paui-bg-black" paui-app-event="btnevt_close_laytips">
		<div class="layui-anim layui-anim-rotate layui-anim-loop" style="margin-top: 100px; text-align: center; ">
			<i class="pa-icon pa-loading" style="font-size:36px; color:#666666"></i>
		</div>
	</div>
</body>

<script>
g_sys_env.ntm = "${NTM}";
g_sys_env.key = "`${FLOWEYE} key getcookie2`";
g_sys_env.paui_vpath = "${paui_vpath}";
paui_db = new Paui_db(g_sys_env.key);

if(g_sys_env.ntm == 1){
	layui.extend({
		ntmlog: '/html/App/ntm/assert/new_ntm'
	}).use('ntmlog');
}

${js_head}
	var event_center = {
		paui_btn_more: function(obj){
			\$(obj).find("dl").show();
			\$(obj).off('mouseleave');
			\$(obj).on('mouseleave', function(){
				\$(this).find("dl").hide();
			});
		},

		btnevt_close: function(obj){
			layer.closeAll('tips');
		}
	};

	var get_html = function(){
		\$.when(
			pa_get_html('/html/common/layout_tpl.html'),
			pa_get_html('${page_path}')
		).done(function(tpls, ctx){
			\$("#app-page-body")
			.html(laytpl(tpls[0]).render({}))
			.append(laytpl(ctx[0]).render({${query_string}}));
		});
	};

	layui_config();

	if(location.pathname.indexOf("VPATH") > 0){
		g_sys_env.saaskey = getQueryStr("saaskey");
		g_sys_env.saaspath = getQueryStr("saaspath");
		g_sys_env.paui_vpath = g_sys_env.saaspath + "/VPATH_" + g_sys_env.saaskey;
		g_sys_env.static_url_args = "?saaskey=" + g_sys_env.saaskey + "&saaspath=" + g_sys_env.saaspath;
	} else {
		g_sys_env.paui_vpath = "";
		g_sys_env.saaskey = "";
		g_sys_env.saaspath = "";
		g_sys_env.static_url_args = "";
	}

	\$.ajaxSetup({
		headers: {
			"Auth-Token": sessionStorage.getItem("token") || ''
		}
	});

	pa_ajax({
		url: '/cgi-bin/system/ajax_sys_info',
		data: {'action':'load_sys_release'},
		success: function(json){
			g_sys_env = $.extend({}, g_sys_env, json.data);
			get_html();
			paui_app_event_bind();
			paui_change_theme('init');
		}
	});

	\$(document).keydown(function(e){
		if(e.which === 27)
			window.close();
	});

});
</script>
</html>

EOF
