var inspectList;

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

/**
 * 获取巡检安排，并显示在屏幕上
 * @param {*} callback
 */
function getInspectArrangement(callback) {
    var id = api.pageParam.id;
    var messageId = api.pageParam.messageId;
    var args = {
        msgId: messageId
    };
    var url = '/ems/rest/xj/task';
    $api.get(url, args, function(data, err) {
        if (err) {
            toast("网络请求失败");
        } else {
            if (data.code == 200) {
                if (data.body && data.body.length > 0) {

                    for (var i = 0; i < data.body.length; i++) {
                        var item = data.body[i];
                        var xjCount = item.xjCount?item.xjCount: 0;
                        var inspectItem = '';
                        inspectItem += '<div class="item">';
                        inspectItem += '<div class="nav">';
                        inspectItem += '<span class="pr-name">' + ($api.getStorage('cus') ? $api.guestPrName : item.prName) + '</span>';
                        inspectItem += '<span class="date-time">' + item.xjDate + '</span>';
                        inspectItem += '</div>';
                        inspectItem += '<div class="con">';
                        inspectItem += '<div class="con-item"><span>巡检次数：</span><span>' + xjCount+ '次</span></div>';
                        for (var j = 0; j < item.xjPeoples.length; j++) {
                            var person = item.xjPeoples[j];
                            inspectItem += '<div class="con-item"><span>' + (j + 1) + '.巡检组长：</span><span>' + person.xjPeople + '</span></div>';
                        }
                        inspectItem += '</div>';
                        inspectItem += '</div>';

                        inspectList.append(inspectItem);
                    }
                }
            } else if (data.code == 404) {
                var msg = "消息不存在";
                toast(msg);
                api.sendEvent({
                    name: 'removeMessage',
                    extra: {
                        messageId: messageId
                    }
                });

                window.setTimeout(function() {
                    api.closeWin();
                }, 2000);

            } else {
                toast("服务器响应错误" + (data.code ? ("，错误码：" + data.code) : ""));
            }
        }

        if (callback) {
            callback();
        }
    });
}

apiready = function() {
    // 适配安卓状态栏
    CommonModel.fitInStatusBar();
    inspectList = $("#inspectList");
    //用户点击了返回按钮
    $(".back").on("touchend", function() {
        api.closeWin();
    });

    getInspectArrangement();
}
