filterResult = {
    catalogType: null,
    locName: "全部位置",
    alarmClassName: "全部时效",
    alarmClasses: null, // 时效
    afName: "全部状态",
    af: null,
    prId: ''
};

/**
 * 为按钮绑定点击事件
 * @param {zepto object} buttons
 */
function bindSwitchButtonEvent(buttons) {
    for (var i = 0; i < buttons.length; i++) {
        var btn = $(buttons[i]);
        btn.on('touchend', switchButtonClicked);
    }
}

/**
 * 单选按钮被点击
 */
function switchButtonClicked() {
    var sender = $(this);
    var brothers = sender.parent().children();
    for (var i = 0; i < brothers.length; i++) {
        var inputElement = $(brothers[i]);
        inputElement.removeClass("btn-highlight");
    }
    sender.addClass("btn-highlight");

    var groupName = sender.attr('group');
    switch (groupName) {
        case "loc":
            locationButton_clicked(sender);
            break;
        case "type":
            alarmTypeButton_clicked(sender);
            break;
        case "process":
            processButton_clicked(sender);
            break;
        case "pr":
            prButton_clicked(sender);
            break;
        default:
            break;
    }
}

// 配电室选择
function prButton_clicked(sender) {
    var pridvalue = sender.attr("pridvalue");
    if (pridvalue < 0) {
        filterResult.prId = "";
    } else {
        filterResult.prId = pridvalue;
    }
}

/**
 * 用户点击了位置按钮
 * @param {zepto object} sender
 */
function locationButton_clicked(sender) {
    var locvalue = sender.attr("locvalue");
    if (locvalue < 0) {
        filterResult.locName = "全部位置";
        filterResult.catalogType = null;
    } else {
        filterResult.locName = sender.attr("value");
        filterResult.catalogType = locvalue;
    }
}

/**
 * 用户点击了报警类型按钮
 * @param {zepto object} sender
 */
function alarmTypeButton_clicked(sender) {
    var tpvalue = sender.attr("tpvalue");
    if (tpvalue < 0) {
        filterResult.alarmClassName = "全部时效";
        filterResult.alarmClasses = null;
    } else {
        filterResult.alarmClassName = sender.attr("value");
        filterResult.alarmClasses = tpvalue;
    }
}

/**
 * 用户点击了处理状态按钮
 * @param {zepto object} sender
 */
function processButton_clicked(sender) {
    var psvalue = sender.attr("psvalue");
    if (psvalue < 0) {
        filterResult.afName = "全部状态";
        filterResult.af = null;
    } else {
        filterResult.afName = sender.attr("value");
        filterResult.af = psvalue;
    }
}

/**
 * 用户点击了确定按钮
 */
function btOk_clicked() {
    sendResult();
    api.closeDrawerPane();
}

/**
 * 用户点击了取消按钮
 */
function btCancel_clicked() {
    api.closeDrawerPane();
}

/**
 * 向主窗体发送筛选条件
 */
function sendResult() {
    api.sendEvent({
        name: 'filterSelectionChanged',
        extra: filterResult
    });
}

apiready = function () {
    // 解决iphonex底部标签栏被虚拟home键遮挡
    $api.fixTabBar($api.byId('footer'));
    var ctrl = {
        init: {},
        bind: {}
    }

    ctrl.bind = function () {
        //为位置和类型绑定事件
        bindSwitchButtonEvent($("#location-group").children());
        bindSwitchButtonEvent($("#type-group").children());
        bindSwitchButtonEvent($("#process-group").children());
        //为确定和取消绑定事件
        $("#btn-cancel").on("touchend", btCancel_clicked);
        $("#btn-ok").on("touchend", btOk_clicked);
    }

    ctrl.init = function () {
        ctrl.employeeId = $api.getStorage('employeeId');
        ctrl.roleId = $api.getStorage('roleId');
        ctrl.prListDom = $('#pr-group');

        if (!$api.getStorage('isEmployee')) {
            $('.pr').hide();
        };

        ctrl.bind();

        ctrl.getPrList();
    }

    // 获取配电室列表
    ctrl.getPrList = function() {
        
        var url = '/ems/rest/power/room/list';
        var data = {
            employeeId: ctrl.employeeId,
            roleId: ctrl.roleId
        }
        
        api.showProgress({
            title: '载入中...',
            text: '请稍后',
            modal: false
        });
        $api.get(url, data, function(res, err) {
            api.hideProgress();
            if (err) {
                ctrl.toast("网络请求失败");
            } else {
                if (!res.code || res.code != "200" || !res.body) {
                    ctrl.toast("服务器响应错误" + (res.code ? ("，错误码：" + res.code) : ""));
                } else {
                    ctrl.renderPrList(res.body);
                }
            }
        });
    };

    ctrl.renderPrList = function (prListData) {
        var len = prListData.length;
        var temp = '<input class="btn btn-highlight" type="button" value="全部" group="pr" pridvalue="-1" />';

        ctrl.prListDom.empty();

        for (var i = 0; i < len; i++) {
            var prItem = prListData[i];
            var domStr = '';
            
            domStr += '<input class="btn" type="button" value="{prName}" group="pr" pridvalue="{prId}" />';

            domStr = domStr.replace("{prId}", prItem.prId)
                        .replace("{prName}", prItem.prName);

            temp += domStr;
        }

        ctrl.prListDom.append(temp);

        bindSwitchButtonEvent($("#pr-group").children());
    };

    ctrl.init();
}
