/**
 * Created by Administrator on 2016/6/2.
 */

myAdminApp.controller('loginAdminCtrl', ['$scope', '$http', '$state', function($scope, $http, $state) {
    var height = $(window).height();
    $('.reg-content').css("height", height);
    $('.reg-input-container').css("height", height-120);
    $('.reg-input-container').css("overflow-y", "auto");
    $(window).resize(function() {
        var height = $(window).height();
        $('.reg-content').css("height", height);
        $('.reg-input-container').css("height", height-120);
        $('.reg-input-container').css("overflow-y", "auto");
    });

    // 下拉框显示隐藏
    $('[name="nice-select"]').click(function(e){
        $('[name="nice-select"]').find('ul').hide();
        $(this).find('ul').show();
        e.stopPropagation();
    });
    $('[name="nice-select"] li').hover(function(e){
        $(this).toggleClass('on');
        e.stopPropagation();
    });
    $('[name="nice-select"] li').click(function(e){
        var val = $(this).text();
        var type = $(this).attr("data-value");
        $(this).parents('[name="nice-select"]').find('input').val(val);
        $(this).parents('[name="nice-select"]').find('input').attr("admin-type", type);
        $('[name="nice-select"] ul').hide();
        e.stopPropagation();
    });
    $(document).click(function(){
        $('[name="nice-select"] ul').hide();
    });
    if (typeof(getMsg) != "undefined") {
        clearInterval(getMsg);
    }

    // bind here all data from the form
    $scope.account = {};
    // place the message if something goes wrong
    $scope.authMsg = '';

    $scope.sendAdminLogin = function() {
        $scope.authMsg = '';

        if($scope.loginAdminForm.$valid) {

            var transform = function (data) {
                return $.param(data);
            }
            var adminType = $('#_logAdminMember').attr("admin-type");
            

            var requestData = {"adminName": $scope.loginAdminAccount, "adminPass": $scope.loginAdminPwd, "adminType": adminType};

            $http.post("/trans/base/admin/login", requestData, {
                headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                transformRequest: transform
            }).success(function(data) {
                if (typeof data == "string") {
                    data = $.parseJSON(data);
                }
                if(data.success){
                    $.cookie('adminCode',data.adminCode);
                    $.cookie('token',data.token);
                    $.cookie('authType',data.authType);
                    $.cookie('nickName',data.nickName);
                    $.cookie('roleTypeForNotice',data.roleTypeForNotice);
                    sessionStorage.setItem('menuAdminList', JSON.stringify(data.authorities));
                    if (data.authorities.length > 0) {
                        $state.go('mainAdmin.'+data.authorities[0].htmlUrl);
                    } else {
                        $state.go('mainAdmin');
                    }
                }else{
                    errorAdminMsgHint(data.errorCode, data.errorMsg);
                }
            });
        }
        else {
            showAlertDialog(1, "用户名或密码不能为空！");
        }
    };

}]);
