apiready = function() {
    var ctrl = {
        init: {},
        bind: {},
    }

    ctrl.init = function() {
        CommonModel.fitInStatusBar();
        var searcherTop = $('#heightHeader').height();
        $('#searcher').css('top',searcherTop);

        ctrl.inspectionId = api.pageParam.id;

        ctrl.prList = $("#prList");
        ctrl.emptyTips = $("#empty-tips");
        ctrl.xjDateDom = $('#inspect-date');
        ctrl.prCountDom = $('#prCount');
        ctrl.person = $('#inspect-person');

        ctrl.bind();

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

        // 点查看日志 跳转到巡检日志
        $("#prList").on("touchend", ".log-detail", function () {
            var id = $(this).data('logid');
            var prName = $(this).data('prname');

            api.openWin({
                name: 'inspectLogDetailNew',
                url: 'widget://html/feature/logQuery/inspectLogDetailNew.html',
                pageParam: {
                    id: id,
                    prname: prName
                }
            });
        })
    };

    // 初始化巡检单详情
    ctrl.initInspectDetail = function() {
        ctrl.getInspectDetail();
    };

    /**
     * 获取巡检单详情
    */
    ctrl.getInspectDetail = function() {
        api.showProgress();

        var url = '/test/xun-jian-dan/queryById/' + ctrl.inspectionId;
        
        $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.renderInspectDetail(res.data);
                    // 获取当前巡检单对应的巡检配电室列表
                    ctrl.getInspectPrList();
                }
            }
        });
    }

    // 渲染详情
    ctrl.renderInspectDetail = function(data) {
        // 更新巡检日期
        ctrl.xjDateDom.text(data.xunJianShiJian);
        // 更新巡检人员
        ctrl.person.text(data.jieDanRen || '无');
    };

    /**
     * 获取巡检配电室列表
    */
     ctrl.getInspectPrList = function() {
        api.showProgress();

        var url = '/test/xun-jian-pr/queryByList/1/9999/' + ctrl.inspectionId;
        
        $api.get(url, function(res, err) {
            if (err) {
                ctrl.toast("网络请求失败");
                api.hideProgress();
            } else {
                if (!res.code || res.code != "200") {
                    ctrl.toast("服务器响应错误");
                    api.hideProgress();
                } else {
                    api.hideProgress();
                    ctrl.renderInspectPrList(res.data);
                }
            }
        });
    }

    // 渲染巡检配电室列表
    ctrl.renderInspectPrList = function(data) {
        // 判断配电室数量
        if (data.total <= 0) {
            ctrl.prCountDom.text('0')
            ctrl.emptyTips.show();

            return;
        } else {
            // 更新配电室数量
            ctrl.prCountDom.text(data.total);
            ctrl.emptyTips.hide();

            // 渲染配电室列表
            ctrl.clearPrList();
            var records = data.records;
            var len = data.records.length;
            for (var i = 0; i < len; i++) {
                var prItem = records[i];
                ctrl.createPrDom(prItem);
            }
        }

    };

    // 清空配电室列表
    ctrl.clearPrList = function() {
        $("#prList").empty();
    }

    // 创建一个配电室DOM
    ctrl.createPrDom = function(prItem) {
        var statusDesc = prItem.shiFouWanCheng?  '已完成' : '未完成';
        var colorClass = prItem.shiFouWanCheng?  'color-green' : 'color-red';
        var prName = prItem.prName;
        var logId = prItem.id;

        var type = '例行巡检';
        switch (prItem.xunJianLeiXing - 0) {
            case 1:
                type = '例行巡检';
                break;
            case 2:
                type = '特殊巡检';
                break;
            case 3:
                type = '会诊巡检';
                break;
            case 4:
                type = '熄灯巡检';
                break;
        };

        var template = ''
        
        template +='<div class="pr">'
        template +=    '<div class="left">'
        template +=        '<div class="icon-con"><img src="../../image/black-pr-icon.png"></div>'
        template +=        '<div class="content font1">'
        template +=          '<div class="pr-name-con"><span class="pr-name">{prName}</span>&nbsp;'
        template +=          '</div>'
        template +=          '<div> < <span class="inspect-type">{type}</span> > </div>'
        template +=        '</div>'
        template +=    '</div>'
        template +=    '<div class="right">'
        template +=        '<div>'
        template +=            '<span class="{colorClass}">{statusDesc}</span>'
        if (prItem.shiFouWanCheng) {
            template +=            '<div class="text-yellow log-detail" data-logid="{logid}" data-prname="{prName}">查看日志 </div>'
        }
        template +=        '</div>'
        template +=    '</div>'
        template +='</div>'

        template = template.replace("{prName}", prName)
                            .replace("{type}", type)
                            .replace("{colorClass}", colorClass)
                            .replace("{statusDesc}", statusDesc)
                            .replace("{logid}", logId)
                            .replace("{prName}", prName);
        
        ctrl.prList.append(template);
    };

    ctrl.init();
}
