<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Request;

/**
 *
 */
class SpecialReportController extends Controller {
	/**
	 * @return 六合彩特码报表
	 */
	function index(Req $req) {
		$request=array();
		$request['game']=isset($req->game)?trim($req->game):'sixlottery';
		$request['game_no']=isset($req->game_no)?trim($req->game_no):null;
		$gamelist = \App\Models\GameType::select('table_name','name')->whereIn('table_name',array('sixlottery','sfsixlottery'))->get();
        if($gamelist){
			$request['gameList'] = $gamelist->toArray();
		}else{
			$request['gameList'] = array('table_name'=>'sixlottery','name'=>'香港六合彩');
		}
		
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/SpecialReport/ReportList');
		$dt->setLang('sixlottery');
		$dt->setPriKey('identity');
		$dt->addColsFields('name', array('sort' => false));
		$dt->addColsFields('specialcode', array('sort' => false,'width'=>'80'));
		$dt->addColsFields('codea_summoney', array('sort' => false,'templet' => '#userrecharge','width'=>'150'));
		$dt->addColsFields('codeb_summoney', array('sort' => false,'templet' => '#orderid_time','width'=>'150'));
		$dt->addColsFields('code_summoney', array('sort' => false,'width'=>'150'));
		$dt->addColsFields('code1_summoney', array('sort' => false,'width'=>'150'));
		$dt->addColsFields('code2_summoney', array('sort' => false,'width'=>'150'));
		$dt->addColsFields('code3_summoney', array('sort' => false,'width'=>'150'));
		$dt->addColsFields('code4_summoney', array('sort' => false,'width'=>'150'));
		$dt->addColsFields('code5_summoney', array('sort' => false,'width'=>'150'));
		$dt->addColsFields('code6_summoney', array('sort' => false,'width'=>'150'));
		$dt->addColsFields('codee_summoney', array('sort' => false));
		$gameno = $this->getAllno($request['game']);
		$request['noinfo'] = $gameno;
		//print_r($gameno);exit;
		return view('admin.SpecialReport/index', $dt->render($request));
	}
	
	//获取特码报表
	function ReportList() {
		$where = array();
		$info_no = Request::has('game_no') ? Request::get('game_no') : '';
		$game = Request::has('game') ? Request::get('game') : 'sixlottery';
		$where[] = array('delstatus','=',1);
		$cls = '\App\Models\Game' . ucfirst($game);
		$clsbuy = '\\App\\Model\\' . ucfirst($game) . 'Buy';
		if(empty($info_no)){
			$gameno =new $cls;
			$gamenofirst = $gameno->getGameno();
			if($gamenofirst<0){
				return responseToJson($gamenofirst);
			}
			$info_no = $gamenofirst['info_no'];
		}
		$where[] = array('no','=',$info_no);
		
		$moneybuy =new $clsbuy;
		$data = $moneybuy->getBetInfo($where);
		//$data[0]['codes'] = '{"special_code_a":{"1":{"0":"1","settings":{"multiple":"42.5","back":"0"}},"2":{"0":"1","settings":{"multiple":"42.5","back":"0"}}}}';
		if($data<0){
			return responseToJson($data);
		}
		$newdata = array();
		for($i=1;$i<50;$i++){
			$newdata[] = array(
				'name' => trans($game . '.' . $game),
				'specialcode' => $i,
				'codea_summoney' => 0,
				'codeb_summoney' => 0,
				'code_summoney' => 0,
				'code1_summoney' => 0,
				'code2_summoney' => 0,
				'code3_summoney' => 0,
				'code4_summoney' => 0,
				'code5_summoney' => 0,
				'code6_summoney' => 0,
				'codee_summoney' => 0,
			);
		}

		foreach($data as $k=>$v){
			$codes = json_decode($v['codes'],1);
			//print_r($codes);
            if(!empty($codes)){
                foreach($codes as $sk=>$sv){
                    if($sk=='special_code_a'||$sk=='special_code_b'){
                        foreach($sv as $ck=>$cv){
                            $j = $ck-1;
                            $codename = ($sk=='special_code_a')?'codea_summoney':'codeb_summoney';
                            $newdata[$j]['code_summoney'] += $cv[0];
                            $newdata[$j][$codename] += $cv[0];
                        }
                    }else if ($sk == 'general_special'){
                    	foreach($sv as $ck=>$cv){
                    		$gk = explode('_', $ck);
                            $j = $gk[1]-1;
                            $codename = 'code' . $gk[0] . '_summoney';
                            $newdata[$j]['codee_summoney'] += $cv[0];
                            $newdata[$j][$codename] += $cv[0];
                        }
                    }
                }
            }

		}
		
		return \App\Lib\DataTable\DataTable::init()->toJson($newdata, 49);
	}
	
	function getAllno($game){
		$cls = '\App\Models\Game' . ucfirst($game);
		$gameno =new $cls;
		$gamenoAll = $gameno->getGameAllno();
		if($gamenoAll<0){
			return 1;
		}else{
			return $gamenoAll;
		}
	}

}
?>