(function (app) {
    app.controller('loginCtrl', ["$scope", 'userService', "data", '$state', 'msg', function ($scope, userService, data, $state, msg) {
        $scope.vm = {
            mobile: '',
            password:''
        };
        $scope.login = function () {
            msg.loading('登录中...');
            userService.login($scope.vm.mobile, $scope.vm.password).then(function (result) {
                msg.hide();
                data.setObject('user', result.data.data);
                data.set('token', result.data.meta.token);
                if (result.data.data.type == 1) {//托运方
                    $state.go('wl.car');
                }
                if (result.data.data.type == 2) {//承运方
                    $state.go('wl.goods');
                }
            }, function (erro) {
                msg.hide();
                msg.error(JSON.stringify(erro.data));
            });
        }
    }]);
    app.controller('registerCtrl', ["$scope", 'msg', 'util', 'userService', '$timeout', '$state', 'data', function ($scope, msg, util, userService, $timeout, $state, data) {
        $scope.vm = {
            mobile: '',
            password: '',
            password1:'',
            verifyCode:'',
            waitSeconds: "获取验证码"
        };
        $scope.typeOpt = [
            { name: "托运方", value: 1 },
            { name: "承运方", value: 2 }
        ];
        $scope.idcard_typeOpt = [
           { name: "身份证", value: 1 },
           { name: "其它", value: 2 }
        ];

        $scope.save = function () {
            if (!$scope.vm.type) {
                msg.text('请选择类型');
                return;
            }
            if (!$scope.vm.idcard_type) {
                msg.text('请选择证件类型');
                return;
            }
            if (!$scope.vm.idcard_no) {
                msg.text('请输入证件号码');
                return;
            }
            if (!util.isMobile($scope.vm.mobile)) {
                msg.text('请输入正确的手机号', true);
                return;
            }
            if (!$scope.vm.verifyCode) {
                msg.text('请输入短信验证码');
                return;
            }
            if ($scope.vm.password !== $scope.vm.password1) {
                msg.text('密码前后输入不一致');
                return;
            }
            msg.loading('请稍等...');
            userService.register({
                phone: $scope.vm.mobile, password: $scope.vm.password,
                verifyCode: $scope.vm.verifyCode, idcard_type: $scope.vm.idcard_type, idcard_no: $scope.vm.idcard_no, type: $scope.vm.type
            }).then(function (result) {
                data.setObject('user', { mobile: $scope.vm.mobile, type: $scope.vm.type });
                data.set('token', result.data.token);
                msg.hide();
                if ($scope.vm.type == 1) {//托运方
                    $state.go('wl.car');
                }
                if ($scope.vm.type == 2) {//承运方
                    $state.go('wl.goods');
                }
             
            }, function (erro) {
                msg.hide();
                msg.error(JSON.stringify(erro.data));
            });
        }
        $scope.getVerifyCode = function () {
            if (!util.isMobile($scope.vm.mobile)) {
                msg.text('请输入正确的手机号',true);
                return;
            }
            $scope.vm.waitSeconds = "正在发送";
            userService.getVerifyCode($scope.vm.mobile).then(function (result) {
                wait(30);
            }, function (erro) {
                wait(0);
                msg.error(erro);
            });
        }
        var wait = function (seconds) {
            if (seconds > 0) {
                $scope.vm.waitSeconds = "" + seconds + "秒";
                $scope.vm.enableSend = false;
            } else {
                $scope.vm.waitSeconds = "获取验证码";
                $scope.vm.enableSend = true;
            }
            $timeout(function () {
                if (seconds >= 1)
                    wait(seconds - 1);
            }, 1000);
        };
    }]);
    app.controller('forgetPasswordCtrl', ["$scope", function ($scope) {

    }]);
})(angular.module('app.controllers'));