apiready = function() {
    var ctrl = {
        alarmId: null,
        websocket: null,
        alarm: null,

        init: {},
        bind: {},
        getAlarm: {},
        renderAlarm: {},
        initWebsocket: {},

        renderFilter: {} //渲染报警列表前的拦截器，用于处理体验账号模式下的数据
    }

    //初始化
    ctrl.init = function() {
        // 适配安卓状态栏
        CommonModel.fitInStatusBar();
        this.alarmId = api.pageParam.id;
        ctrl.employeeId = $api.getStorage('employeeId');
        this.bind();
        this.getAlarm();
    }

    //事件绑定
    ctrl.bind = function() {

        //点击返回按钮
        $api.addEvt($api.dom("#back"), 'touchend', function() {

            api.closeWin();

            // if(ctrl.websocket){
            //     ctrl.websocket.close();
            // }

        }, false);

        //退出app 关闭socket
        api.addEventListener({
            name: 'pause'
        }, function(ret, err) {
            //  ctrl.websocket.close();
        });

        //应用恢复 打开websocket
        api.addEventListener({
            name: 'resume'
        }, function(ret, err) {
            //  ctrl.initWebsocket(ctrl.alarm.serverIp, ctrl.alarm.varType, ctrl.alarm.varIndex);
        });

        // “生成工单” 按钮 点击
        $api.addEvt($api.dom("#create-order-btn"), 'touchend', function() {
            api.confirm({
                title: '',
                msg: '确定要生成工单吗',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                var index = ret.buttonIndex;
                if (index == 1) { // 确定
                    ctrl.createOrder();
                }
            });
        })
    }

    ctrl.getAlarm = function() {
        var data = {
            alarmId: this.alarmId
        }

        var url = "/ems/rest/alarm";

        $api.get(url, data, function(ret, err) {

            if (!err && ret && ret.code === 200) {
                $api.css($api.dom("#content"), 'display:block');
                $api.css($api.dom("#empty-tips"), 'display:none');
                ctrl.renderAlarm(ret.body);
                if (ret.body.handleStatus == 0) {
                    $api.css($api.dom("#create-order-btn"), 'display:block');
                }
                ctrl.alarm = ret.body;
            } else {
                $api.css($api.dom("#content"), 'display:none');
                $api.css($api.dom("#empty-tips"), 'display:block');
            }

        });
    }

    //渲染报警列表前的拦截器，用于处理体验账号模式下的数据
    ctrl.renderFilter = function(alarm) {
        if ($api.getStorage('cus') && alarm.prName) {
            alarm.prName = $api.guestPrName;
        }
    }

    ctrl.renderAlarm = function(alarm) {
        //渲染报警列表前的拦截器，用于处理体验账号模式下的数据
        ctrl.renderFilter(alarm);

        $api.text($api.dom("#pr-name"), alarm.prName);

        var unit = alarm.unit ? alarm.unit : '';

        var content = '<div>报警描述：' + alarm.alarmDesc + '</div>';

        if (alarm.ddNum) {
            content += ("<div>调度号：" + alarm.ddNum + "</div>");
        }

        if ($api.getStorage('cus')) {
            content = content.replace($api.guestKeysPattern, $api.guestName);
        }

        if (alarm.varType == "1") { // AI
            var alarmValue = ((alarm.alarmValue || alarm.alarmValue == 0) ? alarm.alarmValue.toFixed(2) : '');
            content += '<div>报警值：' + alarmValue + unit + '</div>';
            content += '<div>报警设定值：' +
                ((alarm.lowLimit || alarm.lowLimit == 0) ? "下限&nbsp;" + alarm.lowLimit + unit : '') +
                ((alarm.lowLimit || alarm.lowLimit == 0) && (alarm.topLimit || alarm.topLimit == 0) ? "，" : '') +
                ((alarm.topLimit || alarm.topLimit == 0) ? "上限&nbsp;" + alarm.topLimit + unit : '') +
                '</div>';
        } else {
            if (alarm.showValue) {
                content += ('<div>报警值：' + alarm.valueDefine + '</div>')
            }
        }

        // 处理状态
        content += '<div>处理状态：' + ['未处理', '处理中', '已处理', '已确认'][alarm.handleStatus] + '</div>'

        // 如果已确认 显示确认的原因
        if (alarm.handleStatus == 3) {
            content += '<div>确认原因：' + alarm.reason + '</div>';
        }

        // content += '<div>历史报警：' + alarm.historyAlarmCount + '次</div>';
        // content += '<div>历史抢修：' + alarm.historyQxCount + '次</div>';
        var alarmTime = (alarm.alarmTime ? moment(alarm.alarmTime).format("YYYY-MM-DD HH:mm:ss") : "")
        if (alarm.lastAlarmTime) {
            var lastAlarmTime = moment(alarm.lastAlarmTime).format("YYYY-MM-DD HH:mm:ss");
            content += '<div>本次报警时间：' + alarmTime + '</div>';
            content += '<div>上次报警时间：' + lastAlarmTime + '</div>';
        } else {
            content += ('<div>本次报警时间：' + alarmTime + '</div>');
        }

        $api.html($api.dom("#content-body"), content);


    }

    /**
     * 弹出提示框
     */
     ctrl.toast = function (msg) {
        api.toast({
            msg: msg,
            duration: 3000,
            locaiton: 'top'
        });
    };

    // 生成工单
    ctrl.createOrder = function () {
        var url = '/test/gong-dan/alarmTransferToWorkOrder/'
            + ctrl.employeeId + '/'
            + ctrl.alarmId;

        $api.post(url, function(ret, err) {
            if (!err && ret && ret.code == "200") {
                ctrl.toast('生成成功')

                api.closeWin();
            } else {
                ctrl.toast('生成失败,' + ret.msg)
            }
        })
    };

    ctrl.init();
}
