<?php
use Illuminate\Database\Capsule\Manager as Capsule;
	$rsClientId = $_SESSION['uid'];	
	$resellerData = Capsule::table("ResellersCenter_Resellers")->select('id')->where('client_id',$rsClientId)->first();
	$rsId = $resellerData->id;

	//This code use for update client credit from reseller area.
	if(isset($_POST['wgs_reseller_custom_ajax']) && $_POST['wgs_reseller_custom_ajax']=='savecredit'){
		$customerId = $_POST['wgsclientid'];
		$amount = $_POST['wgscreditAmount'];		
		$count = Capsule::table("ResellersCenter_ResellersClients")->select('id')->where('reseller_id',$rsId)->where('client_id',$customerId)->count();
		if($count != '0'){
			Capsule::table("tblclients")->where('id',$customerId)->update(array('credit'=>$amount));
			$log = 'Reseller (id #'.$rsId.') has update the credit of client #'.$customerId;
			Capsule::table("ResellersCenter_Logs")->insert(array('reseller_id'=>$rsId,'client_id'=>$customerId,'type'=>'info','description'=>$log,'created_at'=>date('Y-m-d H:i:s')));
		}
		$res = Capsule::table("tblclients")->where('id',$customerId)->select('credit')->first();
		$res = (array) $res;
		$result = array('type'=>'success','message'=>'','data'=>$res);
		echo json_encode($result);die();
	}

	//This code use for transfer domain with reseller client from reseller area.
	if(isset($_POST['wgs_reseller_custom_ajax']) && $_POST['wgs_reseller_custom_ajax']=='transferdomain'){
		$wgsdomainid = $_POST['wgsdomainid'];
		$newdomainuserid = $_POST['newdomainuser'];
		$wgsuDomain = $_POST['wgsuDomain'];		
		$count = Capsule::table("ResellersCenter_ResellersClients")->select('id')->where('reseller_id',$rsId)->where('client_id',$newdomainuserid)->count();
		if($count != '0' || $rsClientId==$newdomainuserid){

			$pData = Capsule::table("tblproducts")->select('id')->first();
			$pid = $pData->id;

			$dData = Capsule::table("tbldomains")->select('paymentmethod','userid','domain')->where('id',$wgsdomainid)->first();
			if($wgsuDomain==$dData->domain)
			{	
				$command = 'AddOrder';
	            $postData = array(
	                'clientid' => $newdomainuserid,
	                'paymentmethod' => $dData->paymentmethod ,
	                'noinvoice' => true,
	                'pid' => array($pid)
	            );
	            $adminUsername = ''; // Optional for WHMCS 7.2 and later
	            $results = localAPI($command, $postData, $adminUsername);

	            if($results['result']=='success'){
	                $orderid = $results['orderid'];                          
	                $serviceid = $results['productids'];
	                Capsule::table('tblhosting')->where('id', $serviceid)->delete();
	                Capsule::table('tbldomains')->where('id', $wgsdomainid)->update(array('orderid'=>$orderid,'userid'=>$newdomainuserid));
	                Capsule::table('tblorders')->where('id', $orderid)->update(array('status'=>'Active'));

	                $log = 'Reseller (id #'.$rsId.') has transfer domain('.$dData->domain.') with his clients #'.$dData->userid.' to #'.$newdomainuserid;
					Capsule::table("ResellersCenter_Logs")->insert(array('reseller_id'=>$rsId,'client_id'=>$newdomainuserid,'type'=>'info','description'=>$log,'created_at'=>date('Y-m-d H:i:s')));
	            }
	        }
		}

		$res = Capsule::table("tblclients")->join('tbldomains', 'tbldomains.userid', '=', 'tblclients.id')->where('tbldomains.id',$wgsdomainid)->select('tblclients.id','tblclients.firstname','tblclients.lastname','tblclients.email')->first();
		$res = (array) $res;
		$result = array('type'=>'success','message'=>'','data'=>$res);
		echo json_encode($result);die();
	}
	