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

    ctrl.bind = function () {
        //为确定和取消绑定事件
        $("#btn-cancel").on("touchend", ctrl.btCancel_clicked);
        $("#btn-ok").on("touchend", ctrl.btOk_clicked);
    }

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

        ctrl.bind();

        ctrl.getPrList();
    }

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

    // 点击 确认
    ctrl.btOk_clicked = function () {
        ctrl.sendResult();
        api.closeDrawerPane();
    }

    // 点击 取消
    ctrl.btCancel_clicked = function () {
        api.closeDrawerPane();
    }

    // 获取配电室列表
    ctrl.getPrList = function () {
        var prList = $api.getStorage('prList');
        ctrl.renderPrList(prList);
    };

    // 渲染配电室列表
    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);

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

    // 配电室 绑定事件
    ctrl.bindSwitchButtonEvent = function (buttons) {
        for (var i = 0; i < buttons.length; i++) {
            var btn = $(buttons[i]);
            btn.on('touchend', ctrl.prButton_clicked);
        }
    };

    // 选择配电室
    ctrl.prButton_clicked = function () {
        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 pridvalue = sender.attr("pridvalue");
    
        if (pridvalue < 0) {
            ctrl.prId = "";
            ctrl.prName = "全部配电室";
        } else {
            ctrl.prId = pridvalue;
            ctrl.prName = sender.attr("value");
        }
    };

    ctrl.init();
}
