/**
 * Created by wh on 2015/7/22.
 */

define(['../testDrive', 'jquery','prevload'],function(module, $){
    module.controller("testSalesPersonCtrl",function($scope,$resource,$location,$filter,$timeout,$routeParams,testDriveService){

        //initCopyRight(); //版权信息位置调整

        var pathname = 'testDrive';
        $scope.tdriveId = $routeParams.tdriveId;
        $scope.currentPage = 1;//车辆主页默认当前页
        $scope.pageRecord = 10; //分页记录数
        $scope.addStaff = {};//添加销售员的信息
        $scope.staffSelect = {};

        var apiStaff = $resource('/'+pathname+'/api/testDrive/:tdriveId/staff');  //获取销售员
        var apiUpdateStaff =$resource('/'+pathname+'/api/testDrive/:tdriveId/staff/:staffId',null,{'update': { method:'PUT' }});  //修改销售员
        var apiStaffById =$resource('/'+pathname+'/api/testDrive/:tdriveId/staff/:staffId');  //删除销售员

        testDriveService.delQueryUrl("page",function(data){ //当前页
            $scope.currentPage = data;
        });
        testDriveService.delQueryUrl("_n",function(data){ //销售员
            if(data)$scope.staffSelect.name = data;
            else $scope.staffSelect.name = "";
        });
        testDriveService.delQueryUrl("_p",function(data){ //联系电话
            if(data)$scope.staffSelect.phone = data;
            else $scope.staffSelect.phone = "";
        });

        //查询路径
        $scope.changePath = function(currentPage) {
            var hash = 'page='+currentPage;
            if($scope.staffSelect.name != "") hash += "&_n="+$scope.staffSelect.name;
            if($scope.staffSelect.phone != "") hash += "&_p="+$scope.staffSelect.phone;
            $location.path($location.path()).search(hash);
        };



        $scope.staffPaging = function(currentPage){
            if(!currentPage) $scope.currentPage = 1;
            else $scope.currentPage = currentPage;
            $scope.postData = {
                tdriveId:$scope.tdriveId,
                userType:4,      //销售员
                sortBy:'id-'
            };

            if($scope.staffSelect.name != ""){
                $scope.postData.name = $scope.staffSelect.name;
            }
            if($scope.staffSelect.phone != ""){
                $scope.postData.phone = $scope.staffSelect.phone;
            }

            var nextMarker = parseInt($scope.pageRecord * ($scope.currentPage - 1)) + 1;
            apiStaff.get($scope.postData, function(data) {
                $scope.pagingStaffs = data.staffs;
                PagingInfo(data.total);
            });
            $scope.currentPage =  (nextMarker - 1) / $scope.pageRecord + 1;
        };


        $scope.staffPaging($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};
            }
        }


        //试驾员的基本操作
        $scope.operStaff = function(operType,index,id){
            $scope.staffDetail = $scope.pagingStaffs[index];
            if(operType == 'add'){
                $scope.addStaff ={};
            }else if(operType == 'delete'){
                if (confirm("是否确定删除销售员?")) {
                    apiStaffById.delete({tdriveId:$scope.tdriveId,staffId:$scope.staffDetail.id},function(data){
                        if(data.status == 'ok'){
                            alert("销售员删除成功");
                            $scope.staffPaging($scope.currentPage);
                        }
                    })
                }
            }else if(operType == 'update'){
                apiStaffById.get({tdriveId:$scope.tdriveId,staffId:$scope.staffDetail.id},function(data){
                    $scope.updateStaff =data
                })
            }
        };

        //添加销售员
        $scope.createStaff = function(){
            $scope.addStaff.nick = $scope.addStaff.name;
            $scope.addStaff.pwd = "0000000000000000000000000000000000000000";
            $scope.addStaff.userType = 4;
            apiStaff.save({tdriveId:$scope.tdriveId},$scope.addStaff).$promise.then(function(data){
                if(data.status == 'ok'){
                    $scope.staffPaging($scope.currentPage);
                }
            },function(error){
                if(error.data.indexOf("ER_DUP_ENTRY: Duplicate entry")!= -1){
                    alert("姓名为"+$scope.addStaff.name+"的职员已存在,请重填！");
                }
            })
        };


        //修改销售员
        $scope.modifyStaff = function(){
            apiUpdateStaff.update({tdriveId:$scope.tdriveId,staffId:$scope.staffDetail.id},$scope.updateStaff,function(data){
                if(data.status == 'ok'){
                    alert("修改销售员成功");
                    $scope.staffPaging($scope.currentPage);
                }
            })
        };
    });
});