(function (app) {
    app.controller('goodsCtrl', ["$scope", "$state", 'goodsService', '$ionicSideMenuDelegate', 'msg', function ($scope, $state, goodsService, $ionicSideMenuDelegate, msg) {
        $scope.filter = {
            hasMore: false,
            pageIndex: 0,
            pageSize: 10,
            type:1,
            begin_address: '',
            end_address: '',
            midway_address:''
        }
        $scope.items = [];
        $scope.load = function (init) {
            if (init) {
                $scope.filter.pageIndex = 1;
                $scope.items = [];
            }
            goodsService.listGoods($scope.filter).then(function (result) {
                 
                $scope.filter.pageIndex++;
                var more = (result.data.data.length >= $scope.filter.pageSize);
                $scope.filter.hasMore = more;
                $scope.items = $scope.items.concat(result.data.data);
                if (init) {
                    $scope.$broadcast('scroll.refreshComplete');
                } else {
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                }
            }, function (erro) {
                msg.error(JSON.stringify(erro.data));
            });
        }
        $scope.toggleRight = function () {
            $scope.load(true);
            $ionicSideMenuDelegate.toggleRight();
        };
        $scope.detail = function (item) {
            $state.go('wl.goods_detail', {item:item})
        }
        $scope.add = function () {
            $state.go('wl.goods_add');
        }
        $scope.load(true);
    }]);
    app.controller('goodsAddCtrl', ["$scope", "$state", "goodsService", "msg", function ($scope, $state, goodsService, msg) {
        $scope.vm = {type:1};
        $scope.save = function () {
            var price=new Number($scope.vm.price);
            if (!price) {
                msg.text('输入价格');
                return;
            }
            if (!$scope.vm.begin_address) {
                msg.text('请输入出发地址');
                return;
            }
            if (!$scope.vm.end_address) {
                msg.text('请输入到达地址');
                return;
            }
            if (!$scope.vm.midway_address) {
                msg.text('请输入途经地址');
                return;
            }
            if (!$scope.vm.detail) {
                msg.text('请输入详情');
                return;
            }
            msg.loading('提交中...');
            goodsService.addGoods($scope.vm).then(function (result) {
                $state.go('wl.goods');
                msg.hide();
            }, function (erro) {
                msg.hide();
                msg.error(JSON.stringify(erro.data));
            });
        }
    }]);
    app.controller('goodsDetailCtrl', ["$scope", '$stateParams', 'msg', 'orderService', '$state', function ($scope, $stateParams, msg, orderService, $state) {
        $scope.vm = $stateParams.item;
        //下单
        $scope.bill = function () {
            msg.confirm('下单提示', '确定下单？').then(function (res) {
                if (res) {
                    msg.loading('下单中...');
                    orderService.addOrder({ message_id: $scope.vm.id }).then(function (result) {
                        msg.hide();
                        $state.go('wl.goods_success', {item:$scope.vm});
                    }, function (erro) {
                        msg.hide();
                        msg.error(JSON.stringify(erro.data));
                    })
                } else {

                }
            });
        }
    }]);
    app.controller('goodsSuccessCtrl', ['$scope', '$state', '$ionicHistory', '$stateParams', function ($scope, $state, $ionicHistory, $stateParams) {
        $scope.vm = $stateParams.item;
        //下单成功
        $scope.back = function () {
            $ionicHistory.clearHistory();
            $state.go('wl.goods');
        }
    }]);
})(angular.module('app.controllers'));