apiready = function() {
    var ctrl = {
        logId: null,
        init: {}, //初始化
        bind: {}, //事件绑定
        toast: {}, //弹出提示框
        getInspectLogDetail: {}, //获取巡检日志详情
        renderInspectLogDetail: {}, //渲染巡检日志详情
        type: {
            common: 1, // 一般
            recheck: 3, // 复检
            burst: 2 // 突发
        }
    }
    ctrl.init = function() {
        // 适配安卓状态栏
        CommonModel.fitInStatusBar();
        this.logId = api.pageParam.id;
        var prName = api.pageParam.name;

        ctrl.bind();
        ctrl.getInspectLogDetail();

        $("#pr-name").text(prName);
        $("#power-name").text(prName);
    }
    ctrl.bind = function() {
            //点击返回按钮
            $api.addEvt($api.dom("#back"), "touchend", function() {
                api.closeWin();
            })

            $("#inspect-con").on("touchend", ".more", function() {
                    $(this).parent().parent().parent().parent().find("tbody").toggle();
                    $(this).toggleClass("close")
                })
                //点击有问题的巡检项
            $("#inspect-con").on("touchend", "tr", function(e) {
                e.stopPropagation();
                var status = $(this).data("status");
                if (status != 2) {
                    return false;
                }

                var logId = ctrl.logId;
                var type = $(this).data("type");
                var itemId = $(this).data("id");
                api.openWin({
                    name: 'inspectBug',
                    url: 'widget://html/feature/logQuery/inspectBug.html',
                    pageParam: {
                        logId: logId,
                        type: type,
                        itemId: itemId,
                    }
                });

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

    ctrl.getInspectLogDetail = function() {
            var url = '/ems/rest/common/xj/log/detail';
            var data = {
                "taskId": this.logId
            };
            $api.get(url, data, function(res, err) {
                if (err) {
                    ctrl.toast("网络请求失败");
                } else {
                    if (res.code === 200) {
                        ctrl.renderInspectLogDetail(res);
                    } else if (res.code == 404) {
                        var messageId = api.pageParam.messageId;
                        var msg = "";
                        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.handleData = function() {
        ctrl.dailyLogs = [];
        ctrl.weekLogs = [];
        ctrl.monthLogs = [];
        ctrl.ycLogs = [];
        if (!ctrl.log.routineLogs) {
            return;
        }
        var logs = ctrl.log.routineLogs.concat(ctrl.log.tfLogs, ctrl.log.fjLogs);
        for (var i = 0; i < logs.length; i++) {
            var item = logs[i];
            if (item.checkType === 1) {
                ctrl.dailyLogs.push(item);
            }
            if (item.checkType === 2) {
                ctrl.weekLogs.push(item);
            }
            if (item.checkType === 3) {
                ctrl.monthLogs.push(item);
            }
            if (item.status === 2) {
                ctrl.ycLogs.push(item);
            }
        }
        ctrl.log.dailyCount = ctrl.dailyLogs.length;
        ctrl.log.weekCount = ctrl.weekLogs.length;
        ctrl.log.monthCount = ctrl.monthLogs.length;
        ctrl.log.reviewCount = ctrl.log.fjLogs.length;
        ctrl.log.burstCount = ctrl.log.tfLogs.length;
        ctrl.log.bugCount = ctrl.ycLogs.length;

    }
    ctrl.getItem = function(title, logs, type) {
        //巡检详情
        var statusContent = ['<div class="detail-status color-green no-wrap">正常</div>',
            '<div class="detail-status color-yellow no-wrap">异常已解决</div>',
            '<div class="detail-status color-red no-wrap">异常已汇报</div>'
        ];
        var length = logs.length;
        var str = '';
        if (length > 0) {
            str += '<table>';
            str += '<thead><tr><td colspan="2" class="title"><span>' + title + '</span></td><td><img src="../../image/more.png" class="more"/></td></tr></thead>';
            str += "<tbody>";
            for (var i = 0; i < length; i++) {
                str += '<tr data-id="' + logs[i].itemId + '" data-type="' + type + '"  data-status="' + logs[i].status + '"><td >' + (i + 1) + '</td><td>' + logs[i].checkItem + '</td><td>' + statusContent[logs[i].status] + '</td></tr>';
            }
            str += "</tbody>";
            str += "</table>";
        }
        return str;
    }
    ctrl.renderInspectLogDetail = function(res) {
        var dateUtil = new DateUtils();
        ctrl.log = res.body;
        ctrl.handleData();
        var log = ctrl.log;
        $api.text($api.dom('#xjDate'), log.xjDate);

        dateUtil.setDate(log.startTime);
        $api.text($api.dom('#start-time'), dateUtil.getFormattedDate('yyyy-MM-dd hh:mm'));

        dateUtil.setDate(log.endTime);
        $api.text($api.dom('#complete-time'), dateUtil.getFormattedDate('yyyy-MM-dd hh:mm'));

        $api.text($api.dom('#inspector'), log.employeeName);
        $api.text($api.dom('#dailyCount'), log.dailyCount);
        $api.text($api.dom('#weekCount'), log.weekCount);
        $api.text($api.dom('#monthCount'), log.monthCount);
        $api.text($api.dom('#ycCount'), log.bugCount);

        $api.text($api.dom('#reviewCount'), log.reviewCount);
        $api.text($api.dom('#burstCount'), log.burstCount);

        var dailyLogs = ctrl.dailyLogs; //日常巡检
        var weekLogs = ctrl.weekLogs; //周检
        var monthLogs = ctrl.monthLogs; //月检


        var reviewLogs = log.fjLogs; //复检
        var burstLogs = log.tfLogs; //突发
        var con = "";
        con += ctrl.getItem('日常巡检', dailyLogs, ctrl.type.common);
        con += ctrl.getItem('周检', weekLogs, ctrl.type.common);
        con += ctrl.getItem('月检', monthLogs, ctrl.type.common);
        con += ctrl.getItem('复检项目', reviewLogs, ctrl.type.recheck);
        con += ctrl.getItem('突发项目', burstLogs, ctrl.type.burst);
        $api.html($api.dom('#inspect-con'), con);


    }

    ctrl.init();
}
