(function (app) {
    app.controller('carCtrl', ["$scope", "$ionicSideMenuDelegate", "$state", 'carService', 'msg', '$rootScope', function ($scope, $ionicSideMenuDelegate, $state, carService, msg, $rootScope) {
        $scope.filter = {
            hasMore: false,
            pageIndex: 0,
            pageSize: 10,
            type: 2,
            begin_address: '',
            end_address: '',
            midway_address: ''
        }
        if ($rootScope.location) {
            var city = $rootScope.location.city;
            if (city) {
                $scope.filter.begin_address = city.slice(0, city.length - 1);
            }
        }
        $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(erro.data.message);
            });
        }
        $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.goGoods = function () {
            $state.go('wl.goods');
        }
        $scope.$on('$ionicView.enter', function (viewResult) {
            $scope.load(true);
        });
        
    }]);
    app.controller('carAddCtrl', ["$scope", "$state", "carService", "msg", function ($scope, $state, carService, msg) {
        $scope.vm = { type: 1 };
        $scope.vm.begin_addressPick = {
          
        }
        $scope.vm.end_addressPick = {
        
        }
        $scope.vm.midway_addressPick = {
          
        }
        $scope.truckOpt = [];
        carService.listTrucks().then(function (result) {
            angular.forEach(result.data.data, function (item) {
                $scope.truckOpt.push({ id: item.id, name: item.name })
            });
        }, function (erro) {
            msg.error(erro.data.message);
        });
        $scope.save = function () {
            $scope.vm.type = 2;
            var price = new Number($scope.vm.price);
            if (!price) {
                msg.text('输入价格');
                return;
            }
            if (!$scope.vm.begin_addressPick.area) {
                msg.text('请输入出发地址');
                return;
            }
            if (!$scope.vm.end_addressPick.area) {
                msg.text('请输入到达地址');
                return;
            }
            if (!$scope.vm.midway_addressPick.area) {
                msg.text('请输入途经地址');
                return;
            }
            if (!$scope.vm.detail) {
                msg.text('请输入详情');
                return;
            }
            if (!$scope.vm.contact_name) {
                msg.text('请输入随车联系人');
                return;
            }
            if (!$scope.vm.contact_phone) {
                msg.text('请输入随车联系电话');
                return;
            }

            msg.loading('提交中...');
            $scope.vm.begin_address = getAdress($scope.vm.begin_addressPick);
            $scope.vm.end_address = getAdress($scope.vm.end_addressPick);
            $scope.vm.midway_address = getAdress($scope.vm.midway_addressPick);
            carService.addCar($scope.vm).then(function (result) {
                $state.go('wl.car');
                msg.hide();
            }, function (erro) {
                msg.hide();
                msg.error(erro.data.message);
            });
        }
        function getAdress(address) {
            return address.province + ',' + address.city + ',' + address.area;
        }
    }]);
    app.controller('carDetailCtrl', ["$scope", '$stateParams', 'msg', 'orderService', '$state', 'map',
        function ($scope, $stateParams, msg, orderService, $state, map) {
        $scope.vm = $stateParams.item;
        map.getMapPic($scope.vm.begin_address).then(function (result) {
            $scope.vm.picUrl = result;
        });
        //下单
        $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(erro.data.message);
                    })
                } else {

                }
            });
        }
    }]);
    app.controller('carSuccessCtrl', ['$scope', '$state', '$ionicHistory', '$stateParams', 'msg', function ($scope, $state, $ionicHistory, $stateParams, msg) {
        $scope.vm = $stateParams.item;
        //下单成功
        $scope.back = function () {
            $ionicHistory.clearHistory();
            $state.go('wl.car');
        }
        $scope.pay = function () {
            msg.text('支付尚未开通，敬请期待');
        }
    }]);
})(angular.module('app.controllers'));



