<?php

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

class WithController extends Controller {

	function index() {
		# code...
	}

	/**
	 * 提现明细
	 * [detail description]
	 * @return [type] [description]
	 */
	function detail(Req $req) {

        $request=array();
        $request['betnum_small']=isset($req->betnum_small)?trim($req->betnum_small):null;
        $request['betnum_big']=isset($req->betnum_big)?trim($req->betnum_big):null;
        $request['betmoney_small']=isset($req->betmoney_small)?trim($req->betmoney_small):null;
        $request['betmoney_big']=isset($req->betmoney_big)?trim($req->betmoney_big):null;
        $request['name']=isset($req->name)?trim($req->name):null;
        $request['status']=isset($req->status)?trim($req->status):null;
        $request['account_name']=isset($req->account_name)?trim($req->account_name):null;
        $request['star_time']=isset($req->star_time)?trim($req->star_time):null;
        $request['end_time']=isset($req->end_time)?trim($req->end_time):null;

		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/With/state');
		$dt->setLang('state');
		$dt->addColsFields('account_name', array('sort' => false, 'toolbar' => '#usertake'));
		$dt->addColsFields('name', array('sort' => false));
		$dt->addColsFields('betnum');
		$dt->addColsFields('betmoney');
		$dt->addColsFields('star_time');
		$dt->addColsFields('end_time');
		$dt->addColsFields('status', array('sort' => false, 'toolbar' => '#userStatus'));
		$dt->setPriKey('account_identity');
		$dt->setToolBar(array('view'), array('toolbar' => '#query_status','align' => 'left'));
		// $dt->setPriKey('statuss');
		return view('admin.with/detail', $dt->render($request));
	}

	//报表查看
	function View(Req $req) {
		$id = $req->identity;
		$status = $req->status;
		$star_time = !empty($req->star_time) ? $req->star_time : '';
		$end_time = !empty($req->end_time) ? $req->end_time : '';
		if (empty($id)) {
			abort(404);
		}
		$dt = \App\Lib\DataTable\DataTable::init();
		$url = '/admin/With/withDetail?id=' . $id . '&statuss=' . $status . '&star_time=' . $star_time . '&end_time=' . $end_time;
		$dt->setDataSource($url);
		$dt->setLang('with');
		$dt->addColsFields('account_name', array('sort' => false, 'width' => 150));
		$dt->addColsFields('id', array('width' => 120));
		// $dt->addColsFields('order_id', array('width' => 210));
		$dt->addColsFields('old_money', array('width' => 135)); //提现前
		$dt->addColsFields('money', array('width' => 135));
		$dt->addColsFields('money_cash', array('width' => 135)); //提现后
		// $dt->addColsFields('bank_no', array('width' => 170));
		// $dt->addColsFields('bank_user', array('width' => 80));
		$dt->addColsFields('bank_no_bank_info', array('sort' => false, 'width' => 200));
		$dt->addColsFields('bank_user_bank_address', array('sort' => false));
		$dt->addColsFields('order_id_apply_time');
		$dt->addColsFields('pass_time', array('width' => 190));
		$dt->setPriKey('identity');
		return view('admin.with/view', $dt->render());
	}

	/**
	 * 提现明细
	 * 'with description'
	 * @return 'type' 'description'
	 */
	function withDetail(Req $req) {
		$id = $req->id;
		$tem = 'statuss';
		$status = $req->$tem;
		if (empty($id)) {
			abort(404);
		}
		$star_time = !empty($req->star_time) ? $req->star_time : '';
		$end_time = !empty($req->end_time) ? $req->end_time : '';
		$where = array();
		$where[] = array('account_identity', '=', $id);
		$where[] = array('status', '=', $status);
		if (!empty($star_time)) {
			$where[] = array('apply_time', '>=', $star_time);
		}
		if (!empty($end_time)) {
			$where[] = array('apply_time', '<=', $end_time);
		}
		$db = new \App\Models\Money_take();
		$data = $db::where($where)->orderBy('apply_time', 'desc')->get();
		if (!$data) {
			abort(404);
		}
		$data = $data->toArray();
		foreach ($data as $k => $v) {
			$data[$k]['old_money'] = sprintf("%.1f", $v['money_cash'] + $v['money']);
			$data[$k]['bank_no_bank_info'] = $v['bank_info'] . '<br>' . $v['bank_no'];
			$data[$k]['bank_user_bank_address'] = $v['bank_user'] . '<br>' . $v['bank_address'];
			$data[$k]['order_id_apply_time'] = $v['order_id'] . '<br>' . $v['apply_time'];
		}
		// $where = array();
		// $data = $db->getTotalDetails($where);
		return \App\Lib\DataTable\DataTable::init()->toJson($data, count($data));
	}

	/**
	 *提现账单
	 * [state description]
	 * @return [type] [description]
	 */
	function state() {
		$page = Request::has('page') ? Request::get('page') : 1;
		$list = Request::has('limit') ? Request::get('limit') : 10;
		$field = Request::has('field') ? Request::get('field') : 6;
		$order = Request::has('order') ? Request::get('order') : 'desc';

		$account_name = Request::has('account_name') ? Request::get('account_name') : '';
		$name = Request::has('name') ? Request::get('name') : '';
		$status = Request::has('status') ? Request::get('status') : '';
		$betnum_small = Request::has('betnum_small') ? Request::get('betnum_small') : '';
		$betnum_big = Request::has('betnum_big') ? Request::get('betnum_big') : '';
		$betmoney_small = Request::has('betmoney_small') ? Request::get('betmoney_small') : '';
		$betmoney_big = Request::has('betmoney_big') ? Request::get('betmoney_big') : '';
		$sureblur = Request::has('sureblur') ? Request::get('sureblur') : '';
		$star_time = Request::get('star_time') ? Request::get('star_time').' 00:00:00' : '';
		$end_time = Request::get('end_time') ? Request::get('end_time').' 23:59:59' : '';
		$where = array();
		$having = array();

		if (!empty($account_name)) {
			if (empty($sureblur) || $sureblur == '模糊') {
				$where[] = array('account_name', 'like', '%' . $account_name . '%');
			} else {
				$where[] = array('account_name', '=', $account_name);
			}
		}
		if (!empty($name)) {
			$where[] = array('name', 'like', '%' . $name . '%');
		}
		if (!empty($status)) {
			if ($status == 3) {
				$where[] = array('status', '=', '0');
			} else {
				$where[] = array('status', '=', $status);
			}
		}
		if (!empty($star_time)) {
			$where[] = array('apply_time', '>=', $star_time);
		}
		if (!empty($end_time)) {
			$where[] = array('apply_time', '<=', $end_time);
		}
		if (!empty($betnum_small)) {
			$having[] = 'count("order_id")>=' . $betnum_small;
		}
		if (!empty($betnum_big)) {
			$having[] = 'count("order_id")<=' . $betnum_big;
		}
		if (!empty($betmoney_small)) {
			$having[] = 'sum("money")>=' . $betmoney_small;
		}
		if (!empty($betmoney_big)) {
			$having[] = 'sum("money")<=' . $betmoney_big;
		}
		$field = ($field==6||$field=='star_time')?'star_time':(($field=='end_time')?'end_time':$field);

		$db = new \App\Models\Money_take;
		$data = $db->getTotal($where, 1, $list, $field, $order, 1, $having);
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}
}