define(function (require, exports, module) {
    var global = require("../../js/global");
    var deviceManage = require("../../deviceManage/js/deviceManage");

    function getAndShowDevInfo() {
        function queryDevInfoCallback(error, result) {
            if (error == null) {
                addDevInfoToTable(result);
            } else {
                alert(undefined, "读取设备数据失败！");
            }
        }
        global.devicesIns.getDeviceConfigInfoAll(queryDevInfoCallback);
    }

    function addDevInfoToTable(devInfo) {
        //console.log(devInfo);
        for (var i = 0; i < devInfo.length; ++i) {
            if (devInfo[i].protocal == "serial") {
                devInfo[i].conne_type = devInfo[i].protocal;
                devInfo[i].host_com = devInfo[i].comNum;
                devInfo[i].port = devInfo[i].baudrate;
            } else {
                devInfo[i].conne_type = devInfo[i].protocal;
                devInfo[i].host_com = devInfo[i].host;
                devInfo[i].port = devInfo[i].port;
            }

            if (devInfo[i].groupName === null || devInfo[i].groupName === undefined || devInfo[i].groupName === '') {
                devInfo[i].groupName = "未分组";
            }
        }

        $('#selectDeviceTable').bootstrapTable('load', devInfo);
        return;
    }

    function operateStyle(value) {
        return '<div class="table-btn launch" style="width: 50px;">开局</div>';
    }

    function launchBtnClick(row, index) {
        var params = global.devicesIns.getDeviceParams(row.deviceName);
        // 强制改成GB2312,否则中文乱码
        params.encoding = 'GB2312';
        var info = {};
        $("#launchingModal").modal('show');
        var maccHost = global.jsUtil.getPkgInfo().spec.info.maccHost;
        global.hzLauncher.launch(params, maccHost, info)
            .then(function (result) {
                $("#launchingModal").modal('hide');
                $("#launchSuccessModal").modal('show');
                setTimeout(function () {
                    $("#launchSuccessModal").modal('hide');
                }, 3000);
            })
            .catch(function (err) {
                $("#launchingModal").modal('hide');
                $('#launchResultTxt').text('开局失败!' + err.toString());
                $("#launchFailedModal").modal('show');
                setTimeout(function () {
                    $("#launchFailedModal").modal('hide');
                }, 5000);
            });
    }

    function init() {
        initWindowEvent();
        initPageStatistic();
        initVariable();
        initTable();
        initEvent();
        initDom();
        resize();

        function initWindowEvent() {
            window.operateStyle = operateStyle;
            window.operateEvents = {
                'click .launch': function (e, value, row, index) {
                    launchBtnClick(row, index);
                }
            };
        }

        function initPageStatistic() {
            statRecordEle("faultDiag#selectDevice");
        }


        function initVariable() {
            //全局变量初始化

            // 测试代码初始化
            var user = global.userMgrIns.getRealUser();
            var maccHost = global.jsUtil.getPkgInfo().spec.info.maccHost;
            console.log('initEg, user:', user, ' maccHost:', maccHost);
            global.hzLauncher.setConfig(maccHost, user.userName, user.password);
        }

        function initTable() {
            $('#selectDeviceTable').bootstrapTable({
                data: []
            });

            $(".fixed-table-body").addClass('custom-scroll');
            $("#selectDeviceTable").show();

            getAndShowDevInfo();
        }

        function initEvent() {
            $("#selectGroup").change(function () {
                var groupId = $('#selectGroup').val();
                groupId = groupId.split(':')[0];
                if (groupId == "null") {
                    groupId = null;
                }
                addDevInfoToTable(global.devicesIns.queryLinkInfoByGroupIdFromMem(groupId));
            });

        }

        function initDom() {
            $('#connectDevModal').modal({
                backdrop: 'static',
                show: false
            });

            //查询当前分组
            var select_id = "selectGroup";
            $("#" + select_id).children().remove();
            var $option = $("<option>");
            $option.val("null:全部分组");
            $option.text("全部分组");
            $("#" + select_id).append($option);
            global.devicesIns.queryAllSubGroups("selectGroup", deviceManage.queryAllSubGroupsCbk);
        }

        function resize() {
            var client_h = document.documentElement.clientHeight;
            var head_h = $(".main_menu").height();
            var body_h = client_h - head_h;

            $('#selectDeviceTable').bootstrapTable('resetView', {
                height: body_h - 55
            });
        }

        function initGlobalEvent() {
            global.exitPageCallback.exitFun = function () {
                removeGlobalEvent();
                return true;
            }
        }
    }
    exports.init = init;
});