(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 () {
                common.chooseImage().then(function (img) { 
                    common.uploadFiles(img,1).then(function (result) {
                        var response = JSON.parse(result.response);
                        var file = config.imgServer + response.data.file;
                        $scope.imgs.push(file);
                    }, function (error) {
                        msg.error('图片上传失败');
                    });
                }, function (error) {
                    console.log('图片选择失败');
                });
            };
            $scope.addvideo = function () {
                common.chooseVideo().then(function (file) {
                    common.uploadFiles(file, 2).then(function (result) {
                        var response = JSON.parse(result.response);
                        $scope.video.server = response.data.file;
                        var file = config.imgServer+response.data.file;
                        console.log("file:"+file);
                        $scope.video.isOK = true;
                        $scope.video.file = file;
                        $scope.video.vpic = config.server+'upload/vpic/'+response.data.file+'.jpg';
                        console.log("vpic:"+$scope.video.vpic);
                    }, 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: ["https://www.ba/idu.com/img/bd_logo1.png"],
                    //pics: $scope.imgs,
                    video: $scope.video.server,
                    name: $scope.vm.name,
                    about: $scope.vm.about,
                    days: 21, //默认21
                    coin: $scope.vm.coin
                };
                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.imgs=[];
                $scope.video = {};
                $scope.vm={
                    name:'',
                    about:'',
                    coin:''
                };
                $ionicTabsDelegate.showBar(false);
            });
        }]);

    app.controller('addExplainCtrl', ["$scope","$ionicTabsDelegate", "$state", "dreamService", "msg","storage"
        , function ($scope,$ionicTabsDelegate, $state,dreamService, msg, storage) {

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