/**
 * Created by MyPC on 2019/11/5.
 */
const hash=require('../class/hash');
const cloudService=require('../service/cloudEditionService');
const operateLogService=require('../service/operateLogService');
const url=require("url");
var models=require('../models');


var cloudEditionAjaxController={
		
		//------------用户列表界面相关----------------
		/**
		 * 养护用户-筛选-条件已升级编办和最近使用编办----废弃
		 */
		getCuringByCompilationId: async function(req, res){
//		    var compilation=req.params.compilation;
			
			//get获得方式
		    var params = url.parse(req.url, true).query;
			
		    var curingList= await cloudService.getCuringList(params);

		    res.json({ 'status': 1,'curingList':curingList.curingList,'pageData':curingList.pageData });
		},

		/**
		 * 养护用户分页
		 */
		getCuringPage: async function(req, res){
		    var currentPage=req.params.currentPage;
		    if(!hash.isNotANumber(currentPage)){
		        res.json({ 'status': 2,'msg':'currentPage error' });return ;
		    }
		    //get获得方式
		    var params = url.parse(req.url, true).query;

		    var curingList= await cloudService.getCuringList(req, res);

		    res.json({ 'status': 1,'curingList':curingList.curingList,'pageData':curingList.pageData });
		},	
		
		/**
		 * 用户列表刷新
		 */
		refreshCuringPage: async function(req, res){
			//var params = url.parse(req.url, true).query;
			
//			var mAge=1000*60*60*24*28;
//			if(hash.isExistence(params.sortField)){//保存到cookie中
//				res.cookie('sortField','create_time', {maxAge: mAge});
//        		if(params.sortField=='latest_login'){
//        			res.cookie('sortField','latest_login', {maxAge: mAge});
//        		}
//        	}
//        	if(hash.isExistence(params.sortField)){//保存到cookie中
//        		res.cookie('sort','desc', {maxAge: mAge});
//        		if(params.sortField=='asc'){
//        			res.cookie('sort','asc', {maxAge: mAge});
//        		}
//        	}
//			
//        	
//        	if(hash.isExistence(req.cookies.sortField)){
//        		console.log(req.cookies.sortField);
//        	}
        	
        	
		    var curingList= await cloudService.getCuringList(req, res);

		    res.json({ 'status': 1,'curingList':curingList.curingList,'pageData':curingList.pageData });
		},	
		

    //获得养护信息
    getCuringInfo: async function(req, res){
        if(!hash.isNotANumber(req.params.mobile)){
            res.json({ 'status': 0,'msg':'error mobile' });
            return ;
        }
        //获得本地绑定CLD客户用户
        var bcDetail= await cloudService.getBindCuringByMobile(req.params.mobile);
        var cDetail= await cloudService.getCuringByMobile(req.params.mobile);
        //数据组合
        /*var detail={};
        if(hash.isExistence(bcDetail)){
            detail=bcDetail;
        }
        if(hash.isExistence(cDetail)){
            detail['mobile']=cDetail['userInfo']['mobile'];
            detail['email']=cDetail['userInfo']['email'];
            detail['username']=cDetail['userInfo']['username'];
            detail['create_time']=cDetail['userInfo']['create_time'];
            detail['company']=cDetail['userInfo']['company'];
            detail['province']=cDetail['userInfo']['province'];
            detail['upgrade_list']=cDetail['userInfo']['upgrade_list'];
            detail['curing']=cDetail;
        }*/
        //console.log(detail);

        if(!hash.isExistence(bcDetail)){//没有绑定CLD客户
            if(!hash.isExistence(cDetail)){
                res.header("Content-Type", "application/json; charset=utf-8")
                res.json({ 'status': 3,'msg':'该号码未注册养护项目' });
                return ;
            }
            res.json({ 'status': 2,'msg':'已升级用户列表不存在' });//未绑定CLD客户
        }else{
            res.json({ 'status': 1,'detail':bcDetail });
        }
    },



//搜索手机号，防止由于改号码导致数据不全问题
checkMobile: async function(req, res){
    if(!hash.isNotANumber(req.params.mobile)){
        res.json({ 'status': 0,'msg':'error mobile' });
        return ;
    }
    var data= await cloudService.getCuringByMobile(req.params.mobile);

    if(hash.isExistence(data)) {
        var curingDetail=await models.cloud_curing.getCuringBySsoid(data['userInfo']['ssoId']);
        var client_id='';
        if(hash.isExistence(curingDetail)&&hash.isExistence(curingDetail['client_id'])){
            client_id=curingDetail['client_id'];
        }
        res.json({ 'status': 1,'data':{'id':data['userInfo']['ssoId'],'client_id':client_id} });
    }else{
        res.json({ 'status': 3,'msg':'该号码未注册养护项目' });
    }
},

/**
 * 异步获得养护用户信息详情
 */
getCuringBySsoid: async function(req, res){
    var params = url.parse(req.url, true).query;

    if(!hash.isExistence(params.ssoid)){
        res.json({ 'status': 0,'msg':'error ssoid' });
        return ;
    }

    var detail= await cloudService.getCuringBySsoid(params.ssoid,params.client_id);
    //console.log(detail);
    if(hash.isExistence(detail)) {
        res.json({ 'status': 1,'detail':detail });
    }else{
        res.json({ 'status': 3,'msg':'该号码未注册养护项目' });
    }
},

upCuringDo: async function(req, res){
    //post数据获得
    var ssoid=req.body.ssoid;//通行证ID
    var compilationId=req.body.compilationId;//编办ID

    if(hash.isExistence(ssoid)&&hash.isExistence(compilationId)) {
        var flag= await cloudService.upCuringDo(req.body);

        if(flag){
            res.json({ 'status': 1,'msg':'升级成功' });
        }else{
            res.json({ 'status': 3,'msg':'升级失败' });
        }
    }else{
        res.json({ 'status': 3,'msg':'数据不正确' });
    }
},
//养护绑定CLD客户
relevanceClientDo: async function(req, res){
    //post数据获得
    var cidKey=req.body.cidKey;//客户ID
    var id=req.body.id;//养护用户ID

    if(hash.isExistence(cidKey)&&hash.isExistence(id)) {
        var flag= await cloudService.relevanceClientDo(req.body);
        if(flag){
            //获得绑定的CLD客户信息
            var detail= await models.CLD_client.findById(cidKey);
            var operateLogList=await models.operate_log.findByCondition(1,hash.hashDecode(id));


            res.json({ 'status': 1,'detail':detail,'operateLogList':operateLogList,'msg':'绑定CLD客户成功' });
        }else{
            res.json({ 'status': 2,'msg':'绑定失败' });
        }
    }else{
        res.json({ 'status': 3,'msg':'参数不正确' });
    }
},
//养护移除CLD客户
relieveClientDo: async function(req, res){
    //post数据获得
    var id=req.body.id;//养护用户ID

    if(hash.isExistence(id)) {
        var flag= await cloudService.relieveClientDo(req.body);
        if(flag){
            var operateLogList=await models.operate_log.findByCondition(1,hash.hashDecode(id));
            //获得绑定的CLD客户信息
            res.json({ 'status': 1,'operateLogList':operateLogList,'msg':'解绑CLD客户成功' });
        }else{
            res.json({ 'status': 2,'msg':'解绑失败' });
        }
    }else{
        res.json({ 'status': 3,'msg':'参数不正确' });
    }
},

//养护详情页 数据
getCuringById: async function(req, res){

    if(!hash.isExistence(req.params.id)){
        res.json({ 'status': 0,'msg':'error id' });
        return ;
    }
    var id=hash.hashDecode(req.params.id);
    if(!hash.isNotANumber(id)){
        res.json({ 'status': 0,'msg':'error id' });
        return ;
    }

    res.json({ 'status': 3,'msg':'数据不正确' });
},

}
module.exports = cloudEditionAjaxController;
