<?php
/**
 * 作者: theworld
 * 时间: 2021-09-02
 * 下游设置      
 */
class downreseller{
	
	//代理商
	public function reseller(){
		$post=_httpRequest('post');
		$get=_httpRequest('get');		
		$pagesize=30;
		$count=_pdoQuery("SELECT COUNT(*) AS count from `idcsmart_reseller_clients`");
		$count=$count[0]['count'];
		$page=isset($get['page'])?intval($get['page']):1;
		$offset=$pagesize*($page - 1);
		$search = trim($get['search']);
		
		$where="";
		if(!empty($search)){
			$_tblclients = _pdoSelect("tblclients","id","`email`='{$search}'");
			$where = " WHERE `userid`='{$_tblclients['id']}'";
		}
		$addons_clients = _pdoQuery("SELECT `userid`,`apikey`,`disabled` FROM `idcsmart_reseller_clients` $where ORDER BY `userid` DESC limit $offset,$pagesize");
		$userid=array_column($addons_clients,"userid");	
		
		if(count($userid)>0){		
			$userid=implode(",",$userid);
			$tblclients = _pdoQuery("SELECT `id`,`lastname`,`firstname`,`email`,`credit` FROM `tblclients` WHERE `id` IN ({$userid})");
			$tblaccounts = _pdoQuery("SELECT `userid`,sum(`amountin`-`amountout`) as total FROM `tblaccounts` WHERE `userid` IN ({$userid})");
			foreach($tblclients as $v){
				$clients[$v['id']]=$v;
			}
			foreach($tblaccounts as $v){
				$accounts[$v['userid']]=$v['total'];
			}
			unset($tblclients);
			unset($tblaccounts);
			
			foreach($clients as $k=>$v){
				$_clients[$k]=$v;
				$_clients[$k]['total']=$accounts[$v['id']];
			}
			
			foreach($addons_clients as $k=>$v){
				$addonsclients[]=array_merge($_clients[$v['userid']],$v);
			}
		}		
		$_page=[
			'url'=>'addonmodules.php?module=idcsmart_reseller&module_addons=downreseller_reseller',
			'pagecount'=>$pagesize,
			'count'=>$count,
			'page'=>$page,
			'num'=>10,
		];
		$data['title']="代理商管理";
		$data['listing']=$addonsclients;
		$data['search']=$search;		
		$data['page']=_page($_page);
		idcsmart_reseller_tpl("downreseller_reseller",$data);
	}
	//添加代理商
	public function resellerAdd(){
		$post=_httpRequest('post');
		if(empty($post['email'])){
			$resultMsg['status']=400;
			$resultMsg['msg']="邮箱不能为空";
			_json($resultMsg);
		}
		$_tblclients = _pdoSelect("tblclients","id","`email`='{$post['email']}'");
		if(!empty($_tblclients['id'])){			
			$reseller_addons_clients = _pdoSelect("idcsmart_reseller_clients","userid","`userid`='{$_tblclients['id']}'");
			if(empty($reseller_addons_clients['userid'])){
				$array=[
					'userid'=>$_tblclients['id'],
					'apikey'=>_strRandom(20,"lower_upper_number"),
					'disabled'=>0,
				];
				$result=_pdoInsert("idcsmart_reseller_clients",$array);
				if($result){
					$resultMsg['status']=200;
					$resultMsg['msg']="添加成功";
				}else{
					$resultMsg['status']=400;
					$resultMsg['msg']="添加失败";
				}
			}else{
				$resultMsg['status']=400;
				$resultMsg['msg']="该客户已经是代理";
				$resultMsg['data']=$reseller_addons_clients;
			}
		}else{
			$resultMsg['status']=400;
			$resultMsg['msg']="邮箱不存在";
		}
		_json($resultMsg);
	}
	//修改代理商
	public function resellerEdit(){
		$post=_httpRequest('post');	
		if(strlen($post['apikey'])>32){
			$resultMsg['status']=400;
			$resultMsg['msg']="API密钥长度超过32个字符";
			_json($resultMsg);
		}
		$array=[
			'apikey'=>$post['apikey'],
			'disabled'=>$post['disabled'],
		];
		$result=_pdoUpdate("idcsmart_reseller_clients",$array,"`userid`='{$post['userid']}'");
		if($result){
			$resultMsg['status']=200;
			$resultMsg['msg']="修改成功";
		}else{
			$resultMsg['status']=400;
			$resultMsg['msg']="数据无变动";
		}
		
		_json($resultMsg);
	}
	//删除代理商
	public function resellerDel(){
		$post=_httpRequest('post');	
		$result=_pdoQuery("delete from `idcsmart_reseller_clients` where `userid`='{$post['userid']}'");
		if($result){
			$resultMsg['status']=200;
			$resultMsg['msg']="删除成功";
		}else{
			$resultMsg['status']=400;
			$resultMsg['msg']="删除失败";
		}	
		_json($resultMsg);		
	}	
}
