
apiready = function () {
    var ctrl = {
        init: {},
        bind: {},
        initPrPicker: {},
        prList: [], // 配电室列表
        employeeId: null, // 员工登陆时的员工id
    }

    ctrl.init = function () {
        // 判断是否是员工登陆
        var isEmployee = $api.getStorage("isEmployee");
        var isKHZBY = $api.getStorage("isKHZBY");
        ctrl.prList = $api.getStorage("prList");

        if (isEmployee) { // 员工登录（自己员工、客户员工）
            // 客户值班员，隐藏巡检安排、系统工单
            if (isKHZBY) {
                $('#inspection-arrange').hide();
                $('#work-order').hide();
            }

            ctrl.bind();
            ctrl.initPrPicker();

        } else { // 客户登陆
            // 隐藏巡检安排、系统工单
            $('#inspection-arrange').hide();
            $('#work-order').hide();

            // 控制菜单权限
            privilegeModule.getMenuList(function () {
                ctrl.initMenu();
            });
            ctrl.bind();

            ctrl.initPrPicker();
        }
    }

    ctrl.initMenu = function () {
        if (!privilegeModule.hasMenu('实时数据')) {
            $('#bt-rtdata').css('display', 'none');
        }
        if (!privilegeModule.hasMenu('历史数据')) {
            $('#bt-hisdata').css('display', 'none');
        }
        if (!privilegeModule.hasMenu('报警查询')) {
            $('#bt-alarm').css('display', 'none');
        }
        if (!privilegeModule.hasMenu('能耗分析')) {
            $('#bt-energy').css('display', 'none');
        }
        if (!privilegeModule.hasMenu('日志查询') ||
            (!privilegeModule.hasMenu('巡检日志(新版)')
                && !privilegeModule.hasMenu('抢修日志(旧版)')
                && !privilegeModule.hasMenu('抢修日志')
                && !privilegeModule.hasMenu('操作票')
                && !privilegeModule.hasMenu('工作票'))) {
            $('#bt-log').css('display', 'none');
        }
        if (!privilegeModule.hasMenu('巡检问题')) {
            $('#inspect-bug').css('display', 'none');
        }
        if (!privilegeModule.hasMenu('视频监控')) {
            $('#camera').css('display','none');
        }
        $('#inspection-arrange').css('display','none');
        $('#work-order').css('display','none');
    }
    ctrl.bind = function () {
        //用户点击了实时数据按钮
        $api.addEvt($api.dom("#bt-rtdata"), "touchend", function () {

            //侧边栏的宽度为250

            var edge = api.winWidth * 0.2;
            (edge < 0) && (edge = 60);
            api.openDrawerLayout({
                name: 'rtdata-page',
                url: '../feature/rtData.html',
                rightPane: {
                    edge: edge,
                    name: 'rtDataRightPanel',
                    url: 'widget://html/feature/rtDataRightPanel.html',
                }
            });
        });
        api.addEventListener({
            name: 'userTouched'
        }, function (ret, err) {
            if (ret) {
                $api.css($api.dom("#pr-picker-modal"), "display:none");
            } else {

            }
        });

        //用户点击了历史数据按钮
        $api.addEvt($api.dom("#bt-hisdata"), "touchend", function () {
            //侧边栏的宽度为250
            var edge = api.winWidth * 0.2;
            (edge < 0) && (edge = 60);
            api.openDrawerLayout({
                name: 'history-data-pag',
                url: '../feature/historyData.html',
                rightPane: {
                    edge: edge,
                    name: 'historyDataRightPanel',
                    url: 'widget://html/feature/historyDataRightPanel.html',
                },
                slidBackEnabled: true,
                slidBackType: "edge",
                vScrollBarEnabled: false,
                hScrollBarEnabled: false
            });
        });

        //用户点击了报警查询按钮
        $api.addEvt($api.dom("#bt-alarm"), "touchend", function () {
            //侧边栏的宽度为250
            var edge = api.winWidth * 0.2;
            (edge < 0) && (edge = 60);
            api.openDrawerLayout({
                name: 'alarm-query-page',
                url: '../feature/alarmQuery.html',
                rightPane: {
                    edge: edge,
                    name: 'alarmQueryRightPanel',
                    url: 'widget://html/feature/alarmQueryRightPanel.html',
                },
            });
        });

        //用户点击了能耗分析按钮
        $api.addEvt($api.dom("#bt-energy"), "touchend", function () {
            //默认横屏
            api.showProgress({
                title: '加载中',
                text: '请稍等...',
                modal: false
            });

            if (api.systemType == "ios") {
                api.setScreenOrientation({
                    orientation: 'landscape_left'
                });
            }

            var edge = 400;

            api.openDrawerLayout({
                name: 'rtdata-page',
                url: 'widget://html/feature/energy.html',
                rightPane: {
                    edge: edge,
                    name: 'rtDataRightPanel',
                    url: 'widget://html/feature/energyRightPanel.html',
                },
                slidBackEnabled: false,
                animation: {
                    type: "none",
                }
            });
        });

        //用户点击了日志查询按钮
        $api.addEvt($api.dom("#bt-log"), "touchend", function () {
            api.openWin({
                name: 'statistic-page',
                url: '../feature/logQuery/logQuery.html'
            });
        });

        //用户点击了设备缺陷按钮
        // 跳转到logQuery-main页面在logQuery-main中加载设备缺陷列表页（便于实现设备缺陷列表页刷新）
        $api.addEvt($api.dom("#inspect-bug"), "touchend", function () {
            // api.openWin({
            //     name: 'inspect-log',
            //     url: '../feature/logQuery/logQuery-main.html',
            //     pageParam: {
            //         page: 'inspectBug'
            //     }
            // });
            //侧边栏的宽度为250
            var edge = api.winWidth * 0.2;
            (edge < 0) && (edge = 60);
            api.openDrawerLayout({
                name: 'inspect-log',
                url: '../feature/logQuery/logQuery-main.html',
                pageParam: {
                    page: 'inspectBug'
                },
                rightPane: {
                    edge: edge,
                    name: 'inspectBugListRightPanel',
                    url: 'widget://html/feature/logQuery/inspectBugListRightPanel.html',
                },
            });
        });
        // 用户点击了视频监控按钮
        $api.addEvt($api.dom("#camera"), "touchend", function () {
            api.openWin({
                name: 'camera-page',
                url: '../feature/camera.html'
            });
        });
        // 用户点击了 “一次图” 按钮
        $api.addEvt($api.dom("#circuit"), "touchend", function () {
            api.openWin({
                name: 'circuit-page',
                url: '../feature/circuit.html'
            });
        });
        // 用户点击了 “巡检安排” 按钮
        $api.addEvt($api.dom("#inspection-arrange"), "touchend", function () {
            api.openWin({
                name: 'inspection-arrange-page',
                url: '../feature/inspectionArrange.html'
            });
        });
        // 用户点击了 “系统工单” 按钮
        $api.addEvt($api.dom("#work-order"), "touchend", function () {
            api.openWin({
                name: 'system-order-page',
                url: '../feature/systemOrder.html'
            });
        });
        //收到首页传来的requestChangePr事件  呼出选择配电室面板
        api.addEventListener({
            name: 'requestChangePr'
        }, function (ret, err) {
            if ($api.getStorage('cus')) {
                return;
            }
            ctrl.initPrPicker();
            if ($('#pr-picker-modal').css('display') == 'block') {
                $api.css($api.dom("#pr-picker-modal"), "display:none");
            } else {
                $api.css($api.dom("#pr-picker-modal"), "display:block");
            }
            
        });
        api.addEventListener({
            name: 'requestCloseModal'
        }, function (ret, err) {
            $api.css($api.dom("#pr-picker-modal"), "display:none");
        });
        //如果body收到touchend事件 隐藏掉modal
        $api.addEvt($api.dom("body"), "touchend", function () {
            $api.css($api.dom("#pr-picker-modal"), "display:none");
        }, false);

        //点击配电室选择器中的配电室
        $("#pr-list").on("touchend", ".pr", function () {
            var index = $(this).data("index");
            // var list = $api.getStorage("prList");
            var list = ctrl.prList;

            var selected = list[index];
            var powerRoom = {
                prId: selected.prId,
                prName: selected.prName,
                configId: selected.configId,
            }

            $api.setStorage("powerRoom", powerRoom);
            api.sendEvent({
                name: 'prChanged',
                extra: powerRoom
            });
        })

        //组织页面弹通
        // $("body").on("touchmove", function (e) {
        //     e.stopPropagation();
        //     return false;
        // })

        // add s
        // $("#pr-list").on("touchmove", function (e) {
        //     e.stopPropagation();
        //     return false;
        // })
        // add e
    }

    //初始化配电室选择器
    ctrl.initPrPicker = function () {

        var list = ctrl.prList
        list = list ? list : [];
        var tpl = '<div class="pr bg-touch" data-index="{index}"><div class="icon-con"><img src="../../image/black-pr-icon.png"></div><div>{prName}</div></div>';
        var length = list.length;
        var html = '';

        for (var i = 0; i < length; i++) {

            html += tpl.replace('{index}', i).replace('{prName}', list[i].prName);

        }

        $("#pr-list").html(html);
    }

    // add 员工获取配电室列表
    ctrl.getPrList = function () {
        var data = {
            employeeId: ctrl.employeeId,
            roleId: ctrl.roleId
        }

        /**
         * 注意在客户体验模式下要拦截此接口的数据，另外在最外层的index.html中也有此接口的调用，如果需要修改，则两处都要该，别忘了
         */
        $api.get("/ems/rest/power/room/list", data, function(ret, err) {

            if (ret && ret.code == 200) {

                ctrl.prList = ret.body;

                ctrl.initPrPicker();

            }
        });
    }

    ctrl.init();
}
