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;
        this.prName = api.pageParam.prname || ''; // 此配电室名是从“巡检安排”“已完成”查看详情跳转时传递的参数
        
        ctrl.bind();
        ctrl.getInspectLogDetail();
        var pr = $api.getStorage('powerRoom');
        $("#pr-name").text(this.prName || pr.prName);
        $("#power-name").text(this.prName || pr.prName);

        ctrl.initGuideTab();
    }
    ctrl.bind = function() {
        //点击返回按钮
        $api.addEvt($api.dom("#back"), "touchend", function() {
            api.closeWin();
        })

        // 跳转到工单详情
        $("#problem-list").on("touchend", ".detail-btn", function () {
            var id = $(this).data('id');

            api.openWin({
                'name': 'systemOrderDetail',
                'url': 'widget://html/feature/logQuery/inspectBug.html',
                'pageParam': {
                    id: id
                }
            })
        })

        // 点击设备分类，自动折叠当前分类的巡检项
        $(".container").on("touchend", ".category-name", function () {

            // 隐藏 当前分类的巡检项
            $api.toggleCls($(this).next()[0], 'hide');
            // 分类名前的箭头旋转90度
            $api.toggleCls($api.dom(this, 'img'), 'transform');

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

    // 获取日志详情
    ctrl.getInspectLogDetail = function() {
        var url = '/test/xun-jian-pr/queryById/'
            + this.logId;

        $api.get(url, function(res, err) {
            if (err) {
                ctrl.toast("网络请求失败");
            } else {
                if (res.code == 200 && res.data) {
                    ctrl.renderDetail(res.data);
                } else {
                        ctrl.toast("服务器响应错误" + (res.code ? ("，错误码：" + res.code) : ""));
                }
            }
        });

    }

    // 渲染日志详情
    ctrl.renderDetail = function (data) {
        $('#complete-time').text(data.wanChengShiJian);
        $('#weather').text(data.tianQi);
        $('#person').text(data.xunJianRen);
        $('#remark').text(data.beiZhu);

        // 渲染设备缺陷列表
        ctrl.renderProblemList(data.xjProblems);
    }

    // 渲染问题列表
    ctrl.renderProblemList = function (list) {
        var length = list.length;
        var listContenDom = $('#problem-list');
        listContenDom.empty();
        
        if (length) {
            var dom = ctrl.creatProblemListDom(list);

            listContenDom.html(dom);
        } else {
            listContenDom.html('<li class="problem-item">无</li>')
        }

    }

    // 创建设备缺陷列表 DOM
    ctrl.creatProblemListDom = function (listData) {
        var length = listData.length;
        var listDomStr = '';
        for (var i = 0; i < length; i++) {
            var item = listData[i];
            var temp = '';
            temp += '<li class="problem-item">';
            temp +=     '<div>{{index}}</div>';
            temp +=     '<div class="problem-desc">{{desc}}</div>';
            temp +=     '<div class="detail-btn" data-id="{{id}}">详情</div>';
            temp += '</li>';

            temp = temp.replace('{{index}}', i + 1)
                    .replace('{{desc}}', item.problemDesc)
                    .replace('{{id}}', item.problemId);

            listDomStr += temp;
        }

        return listDomStr;
    }

    // 初始化 “巡检指导书” tab
    ctrl.initGuideTab = function () {
        ctrl.getInspectItem();
    };

    // 获取 “巡检指导书” 内容
    ctrl.getInspectItem = function() {
        api.showProgress();

        var url = '/test/xun-jian-ri-zhi/queryXunJianXiang/111';
        
        $api.get(url, function(res, err) {
            if (err) {
                ctrl.toast("网络请求失败");
                api.hideProgress();
            } else {
                if (!res.code || res.code != "200") {
                    ctrl.toast("服务器响应错误");
                    api.hideProgress();
                } else {
                    ctrl.renderInspectItem(res.data);
                }
            }
        });
    };

    // 渲染巡检项
    ctrl.renderInspectItem = function (data) {
        $('#guide-panel-ul>ul').empty();

        var tpl = '';
        var len = data.length;

        for (var i = 0; i < len; i++) {
            var categoryItem = data[i];
            var itemList = categoryItem.xjTplContents;
            var categoryDom = '';

            categoryDom += '<li class="category-item">'
            categoryDom +=      '<div class="category-name">'
            categoryDom +=           '<img class="transform" src="../../../image/arrow-bottom.png" />'
            categoryDom +=                 '<span>{{categoryName}}</span>'
            categoryDom +=       '</div>'
            categoryDom +=       '<ul class="hide">'

            categoryDom += getItemDom(itemList)

            categoryDom +=        '</ul>'
            categoryDom += '</li>'

            var dom = categoryDom.replace('{{categoryName}}', categoryItem.tplName);

            tpl += dom;
        }

        $api.html($api.dom('#guide-panel-ul .inspect-item-box'), tpl);

        api.hideProgress();

        // 获取 巡检项 dom
        function getItemDom(itemList) {
            var tpl = ''
            var len = itemList.length

            for (var i = 0; i < len; i++) {
                var item = itemList[i]

                var itemDom = ''
                itemDom +=   '<li class="task-item">'
                itemDom +=        '<div class="task-item-desc">'
                itemDom +=            '{{taskItemDesc}}'
                itemDom +=        '</div>'
                itemDom +=    '</li>'

                var dom = itemDom.replace('{{taskItemDesc}}', item.checkItem);

                tpl += dom

            }

            return tpl;
        }
    };
    
    ctrl.init();
}
