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

        prId: $api.getStorage('powerRoom')['prId'],
        circuitList: [],
        currentCircuitId: null,
    }

    ctrl.init = function() {
        CommonModel.fitInStatusBar();

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

        $('#circuit-selector').on('touchend', function () {
            $('.btngroup').css('display', 'block')
        });
    }

    ctrl.getCircuitList = function() {
        //显示载入动画
        api.showProgress({
            title: '载入中...',
            text: '请稍后',
            modal: false
        });
        var url = '/ems/rest/circuit/list';
        var args = {
            prId: this.prId
        }

        $api.get(url, args, function(data, err) {
            if (err) {
                api.hideProgress();
                toast("网络请求失败");
            } else {
                if (!data.code || data.code != "200") {
                    toast("服务器响应错误" + (data.code ? ("，错误码：" + data.code) : ""));
                    api.hideProgress();
                    $api.css($api.dom('#content'), 'display:none');
                    $api.css($api.dom('#circuit-empty-tips'), "display:block");
                } else {
                    api.hideProgress();
                    ctrl.circuitList = data.body;
                    // 判断当前配电室是否有监控视频
                    if (ctrl.circuitList.length && ctrl.circuitList.length == 0) {
                        $api.css($api.dom('#content'), 'display:none');
                        $api.css($api.dom('#circuit-empty-tips'), "display:block");
                        return false;
                    }

                    ctrl.currentCircuitId = ctrl.circuitList[0]['circuitId']
                    $('#circuit-selector span').text(ctrl.circuitList[0]['circuitName'])
                    // $('#circuit-frame').attr('src', `https://circuit.saas.dianwutong.com/run.html?circuitId=${ctrl.currentCircuitId}&prId=${ctrl.prId}&flag=2`)
                    $('#circuit-frame').attr('src', 'https://cdycircuit.saas.dianwutong.com/run.html?circuitId=' + ctrl.currentCircuitId + '&prId=' + ctrl.prId + '&flag=2')
                    ctrl.renderCircuitBtns()
                }
            }
        })
    }

    ctrl.renderCircuitBtns = function() {
        var domStr = '';
        ctrl.circuitList.forEach(function(item, index) {
            if (index == 0) {
                // domStr += `<input class="btn btn-highlight" type="button" value="${item.channelName}" ctvalue="${index}" />`;
                domStr += '<input class="btn btn-highlight" type="button" value="';
                domStr += item.circuitName;
                domStr += '" ctvalue="';
                domStr += item.circuitId;
                domStr += '" />';
            } else {
                // domStr += `<input class="btn" type="button" value="${item.channelName}" ctvalue="${index}" />`;
                domStr += '<input class="btn" type="button" value="';
                domStr += item.circuitName;
                domStr += '" ctvalue="';
                domStr += item.circuitId;
                domStr += '" />';
            }
        })
        $('#circuit-btns-group').html(domStr);
        ctrl.bindCircuitBtnsEvent();
    }

    ctrl.bindCircuitBtnsEvent = function() {
        var btnList = $('#circuit-btns-group').children();

        for (var i = 0; i < btnList.length; i++) {
            var btn = $(btnList[i]);
            btn.on('touchend', ctrl.switchButtonClicked);
        }
    }

    ctrl.switchButtonClicked = 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");

        ctrl.currentCircuitId = sender.attr('ctvalue');

        $('#circuit-selector span').text(sender.val())
        $('.btngroup').css('display', 'none')
        
        // $('#circuit-frame').attr('src', `https://circuit.saas.dianwutong.com/run.html?circuitId=${ctrl.currentCircuitId}&prId=${ctrl.prId}&flag=2`)
        $('#circuit-frame').attr('src', 'https://cdycircuit.saas.dianwutong.com/run.html?circuitId=' + ctrl.currentCircuitId + '&prId=' + ctrl.prId + '&flag=2')
    }

    ctrl.init();
}
