(function (app) {
    app.controller('addCtrl', ["$scope", "$state",  "dreamService", "msg", "$ionicTabsDelegate", "$ionicNavBarDelegate","$ionicHistory", "common", "config"
        , function ($scope, $state,  dreamService, msg, $ionicTabsDelegate, $ionicNavBarDelegate,$ionicHistory, common, config) {
            $scope.addpict = function () {
                $scope.imgs=new Array();
                common.chooseImage().then(function (img) { 
                    common.uploadFiles(img,1).then(function (result) {
                        var response = JSON.parse(result.response);
                        var file = response.data.file;
                        $scope.imgs.push(config.imgServer+file);
                    }, function (error) {
                        msg.error('图片上传失败');
                    });
                }, function (error) {
                    console.log('图片选择失败');
                });
            };
            $scope.video = {};
            $scope.addvideo = function () {
                common.chooseVideo().then(function (file) {
                    $scope.video.path = file;
                    common.uploadFiles(file, 2).then(function (result) {
                        var response = JSON.parse(result.response);
                        var file = response.data.file;
                        console.log("file:"+config.imgServer+file);
                        $scope.video.isOK = true;
                        $scope.video.server = config.imgServer+file;
                    }, function (error) {
                        msg.error('视频上传失败');
                    });
                }, function (erro) {
                    console.log('选择视频失败');
                });
            };
            $scope.deletefile = function (file) {
                var index = $scope.imgs.indexOf(file);
                $scope.imgs.splice(index, 1);
                common.deletefile(file).then(function () {
                })
            };
            $scope.add = function () {
                var data = {
                    pics: $scope.imgs,
                    video:$scope.video.server,
                    dream: $scope.vm.dream,
                    about: $scope.vm.about,
                    time: 21, //默认21
                    money: $scope.vm.money
                };
                msg.loading('保存中...');
                dreamService.addDream(data).then(function (result) {
                    msg.hide();
                    $state.go('app.home');
                }, function (error) {
                    msg.hide();
                    msg.error(error.data.message);
                });
            };
            $scope.$on('$ionicView.beforeEnter', function () {
                $scope.vm={
                    dream:'',
                    about:'',
                    money:""
                };
            });
            $scope.$on('$ionicView.leave', function () {
                $ionicTabsDelegate.showBar(true);
            });
        }]);
})(angular.module('app.controllers'));