/**
 * Created by wh on 2015/7/17.
 * 报警
 */
define(['../testDrive', 'jquery','prevload'],function(module, x2, $){
    module.controller("alarmCtrl",function($scope,$resource,$location,$filter,$timeout, $routeParams,testDriveService){
        //initCopyRight(); //版权信息位置调整
        var pathname = 'testDrive';
        $scope.tdriveId = $routeParams.tdriveId; //试乘试驾Id
        $scope.currentPage = 1;//车辆主页默认当前页
        $scope.pageRecord = 10; //分页记录数

        //$scope.alarmTypeOption = [{id:0,name:"请选择"},{id:1,name:"故障报警"},{id:2,name:"碰撞报警"},{id:3,name:"震动报警"},{id:4,name:"水温报警"}
        //    ,{id:5,name:"充电电压报警"},{id:6,name:"超速报警"},{id:7,name:"急加速报警"},{id:8,name:"急减速报警"},{id:9,name:"急转弯报警"}
        //    ,{id:240,name:"设备拔下报警"}]; //报警类型

        $scope.alarmTypeOption = [{id:0,name:"请选择"},{id:1,name:"故障报警"},{id:2,name:"碰撞报警"},{id:4,name:"水温报警"},{id:9,name:"超速报警"},{id:7,name:"终端拔下报警"},{id:12,name:"越界报警"}]; //报警类型
        $scope.allVehicleOption = [{id:0,name:"请选择"}];

        $scope.alarmSelect = {type:"请选择",license:"请选择"};


        var apiAllAlarm = $resource('/'+pathname+'/api/testDrive/:tdriveId/alarm');  //获取报警信息
        var apiUpdateAlarm = $resource('/'+pathname+'/api/testDrive/:tdriveId/alarm/:alarmId',null,{'update': { method:'PUT' }});   //修改一条报警信息

        var apiAlarmTDriving = $resource('/'+pathname+'/api/testDrive/:tdriveId/alarm/:alarmId/alarmTDriving');
        var apiVehicle = $resource('/'+pathname+'/api/testDrive/:tdriveId/vehicle');  //获取试驾车辆


        testDriveService.delQueryUrl("page",function(data){ //当前页
            $scope.currentPage = data;
        });
        //testDriveService.delQueryUrl("_at",function(data){ //报警时间
        //    if(data)$scope.alarmSelect.alarmTime = data;
        //    else $scope.alarmSelect.alarmTime = "";
        //});
        testDriveService.delQueryUrl("_t",function(data){ //报警类型
            if(data)$scope.alarmSelect.type = data;
            else $scope.alarmSelect.type = "请选择";
        });
        testDriveService.delQueryUrl("_l",function(data){ //车牌号
            if(data)$scope.alarmSelect.license = data;
            else $scope.alarmSelect.license = "请选择";
        });

        //查询路径
        $scope.changePath = function(currentPage) {
            var hash = 'page='+currentPage;
            //if($scope.alarmSelect.alarmTime != "") hash += "&_at="+$scope.alarmSelect.alarmTime;
            if($scope.alarmSelect.type != "请选择") hash += "&_t="+$scope.alarmSelect.type;
            if($scope.alarmSelect.license != "请选择" ) hash += "&_l="+$scope.alarmSelect.license;
            $location.path($location.path()).search(hash);
        };


        $scope.alarmPaging = function(currentPage){

            if(!currentPage) $scope.currentPage = 1;
            else $scope.currentPage = currentPage;
            $scope.postData ={
                tdriveId:$scope.tdriveId,
                s4_id:$scope.tdriveId
            };

            //if($scope.alarmSelect.alarmTime != "" ){
            //    $scope.postData.locationTime = $scope.alarmSelect.alarmTime;
            //}
            if($scope.alarmSelect.type != "请选择" ){
                for(var i=0;i<$scope.alarmTypeOption.length;i++){
                    if($scope.alarmSelect.type == $scope.alarmTypeOption[i].name){
                        $scope.postData.alarmType = $scope.alarmTypeOption[i].id;
                    }
                }
            }
            if($scope.alarmSelect.license != "请选择" ){
                $scope.postData.license = $scope.alarmSelect.license;
            }
            var nextMarker = parseInt($scope.pageRecord * ($scope.currentPage - 1)) + 1;
            $scope.postData.pageSize = $scope.pageRecord;
            $scope.postData.nextMarker = nextMarker;


            //获取所有报警信息
            apiAllAlarm.get($scope.postData,function(data){
                $scope.pagingAlarm = {};
                if(data.status == 'ok'){
                    if(data.total >0){
                        PagingInfo(data.total);//分页
                        $scope.pagingAlarm = data.alarmData;
                        testDriveService.changeDatePicker("date",["datepicker"]);
                        angular.forEach($scope.pagingAlarm,function(alarm){
                            if(alarm.careStatus == null){
                                alarm.careStatus = 1;
                            }
                            apiAlarmTDriving.get({tdriveId:$scope.tdriveId,alarmId:alarm.id},function(alarmTDriving){
                                if(alarmTDriving.alarmTDriving.length >0){
                                    alarm.name = alarmTDriving.alarmTDriving[0].name;
                                    alarm.salesPerson = alarmTDriving.alarmTDriving[0].salesPerson;
                                }
                            })
                        });
                    }else{
                        alert("没有数据");
                    }
                }
            });
            $scope.currentPage =  (nextMarker - 1) / $scope.pageRecord + 1;
        };

        $scope.alarmPaging($scope.currentPage);  //报警信息初始化
        vehicleSelect();  //车辆搜索栏

        //车辆搜索栏
        function vehicleSelect(){
            //搜索全部车辆
            $scope.allVehicleOption = [{id:0,name:"请选择"}];
            //车辆
            apiVehicle.get({tdriveId:$scope.tdriveId}, function(data) {
                $scope.vehicle = data.vehicles;
                var vehicleHash = {};
                angular.forEach($scope.vehicle,function(vehicle){
                    vehicleHash[vehicle.id] = vehicle;
                });
                for(var i in vehicleHash){
                    $scope.allVehicleOption.push({id:vehicleHash[i].id,name:vehicleHash[i].license});
                }
            });
        }



       //报警基本操作
        $scope.operAlarm = function(operType,index){
            $scope.alarmDetail = {};
            $scope.alarmDetail = $scope.pagingAlarm[index];
            if(operType == 'update'){
                $scope.alarmDetail.careRemark = ""; //初始化处理方法
            }
        };

        //处理报警信息
        $scope.modifyAlarm = function(){
            $scope.modifyPostData = {
                alarmId:$scope.alarmDetail.alarmId,
                careStatus:2,
                careRemark:$scope.alarmDetail.careRemark
            };
            //修改报警信息
            apiUpdateAlarm.update({tdriveId:$scope.tdriveId,alarmId:$scope.alarmDetail.id},$scope.modifyPostData,function(data){
                if(data.status == 'ok'){
                    $scope.alarmPaging($scope.currentPage);
                }
            });
        };


        //分页数据处理方法
        function PagingInfo(totalCount) {
            $scope.totalCount = totalCount;
            $scope.totalPage = Math.ceil($scope.totalCount / $scope.pageRecord);
            $scope.totalOption = [{}];
            for (var i = 0; i < $scope.totalPage; i++) {
                $scope.totalOption[i] = {size: i + 1};
            }
        }
    });

});
