apiready = function() {
    var ctrl = {
        init: {},
        toast: {},
        getContent: {}
    };
    ctrl.init = function() {
            ctrl.getContent();
        }
        /**
         * 弹出提示框
         */
    ctrl.toast = function(msg) {
        api.toast({
            msg: msg,
            duration: 3000,
            locaiton: 'top'
        });
    }
    ctrl.getContent = function() {
        var url = '/ems/rest/ticket/gzp';
        var id = api.pageParam.id;
        var data = {
            "gzpId": id
        };

        $api.get(url, data, function(res, err) {
            if (err) {
                ctrl.toast("网络请求失败");
            } else {
                if (res.code == 200) {
                    ctrl.render(res);
                } else if (res.code == 404) {
                    var msg = "";
                    var messageId = api.pageParam.messageId;
                    if (messageId) {
                        msg = "消息不存在";
                        ctrl.toast(msg);
                        api.sendEvent({
                            name: 'removeMessage',
                            extra: {
                                messageId: messageId
                            }
                        });
												window.setTimeout(function() {
														api.closeWin();
												}, 2000);
                    }else {
                    	ctrl.toast("服务器响应错误" + (res.code ? ("，错误码：" + res.code) : ""));
                    }
                } else {
									ctrl.toast("服务器响应错误" + (res.code ? ("，错误码：" + res.code) : ""));
                }

            }
        });
    }
		ctrl.render = function(res) {
			var ticket = res.body;
			var content = ticket.content.replace(/\\\"/g, "\"");

			if ($api.getStorage('cus')) {

					content = content.replace($api.guestKeysPattern, $api.guestName);

			}
			//如果有快照
			if (ticket.snapshot && !$api.getStorage('cus')) {

					var img = new Image();
					img.src = res.prefix + ticket.snapshot;
					img.onload = function() {
							document.getElementById("snapshot").appendChild(img);
							document.body.style.width = img.clientWidth + "px";
							initLayout();
					}

					img.onerror = function() {
							var contentDiv = document.getElementById("content");
							contentDiv.innerHTML = content;
							document.body.style.width = (contentDiv.clientWidth + 100) + "px"; //加边距
							initLayout();
					}

			} else {
					var contentDiv = document.getElementById("content");
					contentDiv.innerHTML = content;
					document.body.style.width = (contentDiv.clientWidth + 100) + "px"; //加边距
					initLayout();
			}

			function initLayout() {
					// 初始化页面宽度
					var container = document.getElementById('container');
					var viewPort = document.querySelector('meta[name="viewport"]');
					var containerWidth = container.clientWidth;
					var screenWidth = window.screen.width;

					var scale = screenWidth / containerWidth;

					if (api.systemType == 'android') {
							document.body.scrollLeft = containerWidth / 2 - (containerWidth * scale / 2);
							document.body.style.zoom = scale;
							viewPort.setAttribute("content", "maximum-scale=3.0,minimum-scale=1,user-scalable=yes,width=device-width")
					} else {
							viewPort.setAttribute("content", "maximum-scale=3.0,minimum-scale=" + scale + ",user-scalable=yes,width=device-width,initial-scale=" + scale)
					}
			}
		}
    ctrl.init();

}
