(function (app) {
    app.controller('carCtrl', ["$scope", "$ionicSideMenuDelegate", "$state", 'carService', 'msg', function ($scope, $ionicSideMenuDelegate, $state, carService, 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 = [];
            }
            carService.listCars($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.car_detail', { item: item })
        }
        $scope.add = function () {
            $state.go('wl.car_add');
        }
        $scope.load(true);
    }]);
    app.controller('carAddCtrl', ["$scope", "$state", "carService", "msg", function ($scope, $state, carService, 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('提交中...');
            carService.addCar($scope.vm).then(function (result) {
                $state.go('wl.car');
                msg.hide();
            }, function (erro) {
                msg.hide();
                msg.error(JSON.stringify(erro.data));
            });
        }
    }]);
    app.controller('carDetailCtrl', ["$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.car_success', { item: $scope.vm });
                    }, function (erro) {
                        msg.hide();
                        msg.error(JSON.stringify(erro.data));
                    })
                } else {

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



