<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class User_buyController extends Controller {

	function add(Request $req) {
		if (!$req->isMethod('post')) {
			return view('admin.user_buy_form');
		} else {
			$model = new \App\Models\User_buyModel();
			return responseToJson(1);
		}
	}

	function view(Request $req) {
		return $this->edit($req);
	}
	function edit(Request $req) {
		$id = $req->id;
		if (intval($id) < 1) {
			return -1;
		}
		if (!$req->isMethod('post')) {

			$data = \App\Models\User_buyModel::where('id', $id)->first();
			if (!$data) {
				return -2;
			}
			$data = $data->toArray();
			return view('admin.user_buy_form', array('user_buy' => $data));
		} else {
			$model = \App\Models\User_buyModel::where('id', $id)->first();

			$model->save();
			return responseToJson(1);
		}

	}

	public function delete(Request $req) {
		$id = $req->id;
		if (empty($id)) {
			return responseToJson(-2001); //id不能为空
		}
		$ids = explode(',', $id);
		if (!is_array($ids) && intval($ids) < 0) {
			return responseToJson(-2002); //id错误
		}
		if (is_array($ids) && count($ids) > 0) {
			foreach ($ids as $k => $v) {
				if (intval($v) < 1) {
					unset($ids[$k]);
				}
			}
		}
		$rows = \App\Models\User_buyModel::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id错误
		}
		return responseToJson(1, trans('user_buy.delete_success')); //id错误

	}

	public function dataSource(Request $req) {
		$limit = intval($req->limit) > 0 ? $req->limit : 10;
		$page = intval($req->page) > 0 ? $req->page : 1;
		$field = !empty($req->field) ? $req->field : '';
		$order = !empty($req->order) ? $req->order : 'desc';

		$model = new \App\Models\User_buyModel;

		$type = $req->type ? $req->type : '';
		$type == '-1' ? $type = '' : null;
		$where = array();
        $model = $model->whereIn('status', array(1, 2));
        $where['in'] = array(1, 2);
		if (!empty($type)) {
			switch ($type) {
			case 'unclear':
//				$model = $model->whereIn('status', array(1, 2));
				$model = $model->where('game_status', 0);
//				$where['in'] = array(1, 2);
				$where[] = array('game_status', '=', 0);
				break;
			case 'cleared':
//                $model = $model->whereIn('status', array(1, 2));
				$model = $model->where('game_status', '<>', '0');
//				$where['in'] = array(1, 2);
				$where[] = array('game_status', '<>', '0');
				break;
			// case 'rubish':
			// 	$model = $model->where('status', 4);
			// 	$where[] = array('status', '=', 4);
			// 	break;
			default:
				break;
			}
		}

		$time_start = $req->time_start ? $req->time_start.' 00:00:00' : '';
		if (!empty($time_start)) {
			$model = $model->where('money_time', '>', $time_start);
			$where[] = array('money_time', '>', $time_start);
		}
		$time_end = $req->time_end ? $req->time_end.' 23:59:59': '';
		if (!empty($time_end)) {
			$model = $model->where('money_time', '<', $time_end);
			$where[] = array('money_time', '<', $time_end);
		}
		$account = $req->account ? $req->account : '';
		if (!empty($account)) {
			$model = $model->where('account_name', $account);
			$where[] = array('account_name', '=', $account);
		}
		$game_no = $req->game_no ? $req->game_no : '';
		if (!empty($game_no)) {
			$model = $model->where('game_no', $game_no);
			$where[] = array('game_no', '=', $game_no);
		}
		$game = $req->game ? $req->game : '';
		if (!empty($game)) {
			$model = $model->where('game_name', $game);
			$where[] = array('game_name', '=', $game);
		}
		$model = $model->select(DB::raw('count(id) as num,account_name,sum(money) as money,sum(prize_money) as prize_money,sum(get_money) as get_money'));
		$model->groupBy('account_name');
		if (!empty($field) && !empty($order)) {
			$model = $model->orderBy($field, $order);
		}
		$model->offset(($page - 1) * $limit);
		$model->limit($limit);
		$data = $model->get();
		$data = $data->toArray();
		foreach ($data as $k => $v) {
			if (empty($game)) {
				$data[$k]['game_name'] = '全部游戏';
			} else {
				$data[$k]['game_name'] = trans('common.' . $game);
			}
			if (empty($type)) {
				$data[$k]['type'] = '全部注单';
			} else {
				$data[$k]['type'] = trans('user_buy.' . $type);
			}
			// $data[$k]['prize_money'] = floatval($v['prize_money']);
		}
		$count = new \App\Models\User_buyModel;
		$num = $count->getAllBetAccount($where);
		$data = arrayToNumber($data, array('money', 'prize_money', 'get_money'));
		return \App\Lib\DataTable\DataTable::init()->toJson($data, count($num), 0, $where);
	}

	public function index(Request $req) {
        $request=array();
        $request['time_start'] = isset($req->time_start) ? $req->time_start : trans('status.default_time.seven_day');
        $request['time_end'] = isset($req->time_end) ? $req->time_end : trans('status.default_time.etime');
        $request['account'] = $req->account ? $req->account : '';
        $request['game_no'] = $req->game_no ? $req->game_no : '';
        $request['game'] = $req->game ? $req->game : '';
        $request['type'] = $req->type ? $req->type : '';
        $request['gameList']=\App\Models\GameType::getGameType();
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setLang('user_buy');
		$dt->addColsFields('type', array('sort' => false, 'width' => 140));
		$dt->addColsFields('account_name', array('sort' => false, 'width' => 140, 'templet' => '#userdetail'));
		$dt->addColsFields('game_name', array('sort' => false, 'width' => 140));
		$dt->addColsFields('num', array('width' => 190));
		$dt->addColsFields('money', array('width' => 165));
		$dt->addColsFields('prize_money', array('width' => 165));
		$dt->addColsFields('get_money', array('templet' => '#usergetmoney', 'width' => 180));

		$data = array(
			'options' => array('type' => \App\Models\User_buyModel::getType()),
		);
        $request['options']=array('type' => \App\Models\User_buyModel::getType());
		return view('admin.user_buy_list', $dt->render($request));
	}
	//统计用户总投注
	function Total(Request $req) {
		$where = $req->where;
		$model = new \App\Models\MoneyBuy;
		$model = $model->select(DB::raw('count(id) as num,sum(money) as money,sum(prize_money) as prize_money,sum(get_money) as get_money'));

		if (isset($where['in'])) {
			$model = $model->whereIn('status', $where['in']);
			unset($where['in']);
		}
		if (!empty($where)) {
			$model = $model->where($where);
		}

		$data = $model->get();
		//处理数据
		$array = array();
		foreach ($data as $v) {
			$array['num'] = round($v['num'], 2);
			$array['money'] = round($v['money'], 2);
			$array['prize_money'] = round($v['prize_money'], 2);
			$array['get_money'] = -round($v['get_money'], 2);
		}
		return responseToJson($array);
	}

}
