<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class DemoController extends Controller {

	function index() {
		# code...
	}

	/**
	 * 试玩用户列表
	 * [demos description]
	 * @return [type] [description]
	 */
	function demos(Req $req) {
		$request=array();
		$request['account']=isset($req->account)?trim($req->account):null;
		$request['register_ip'] = isset($req->register_ip) ? trim($req->register_ip) : null;
		$request['last_ip'] = isset($req->last_ip) ? trim($req->last_ip) : null;
		$request['register_url'] = isset($req->register_url) ? trim($req->register_url) : null;
		$request['last_url'] = isset($req->last_url) ? trim($req->last_url) : null;
		$request['statuss'] = isset($req->statuss) ? trim($req->statuss) : null;
		$request['cash_small'] = isset($req->cash_small) ? trim($req->cash_small) : null;
		$request['cash_big'] = isset($req->cash_big) ? trim($req->cash_big) : null;
		
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/demo/demo');
		$dt->setLang('demo');
		$dt->addColsFields('id', array('width' => 80));
		$dt->addColsFields('account', array('templet' => '#userdetail', 'width' => 120));
		$dt->addColsFields('register_time', array('width' => 180));
		$dt->addColsFields('cash', array('templet' => '#moneyrecord', 'width' => 110));
		$dt->addColsFields('register_log_ip', array('templet' => '#selectip', 'width' => 150, 'sort' => false));
		$dt->addColsFields('register_log_url', array('templet' => '#selecturl', 'sort' => false));
		$dt->addColsFields('statuss', array('width' => 100, 'sort' => false));
		$dt->addColsFields('action_user', array('templet' => '#waterTool', 'sort' => false,'width' => 110));
		$dt->setPriKey('id');
		return view('admin.demo/demos', $dt->render($request));
	}

	/**
	 * 注册ip列表
	 * [detail description]
	 * @return [type] [description]
	 */
	function registipDetail() {
		$register_ip = Request::has('register_ip') ? Request::get('register_ip') : '';
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/demo/demo?register_ip=' . $register_ip);
		$dt->setLang('demo');
		$dt->addColsFields('id');
		$dt->addColsFields('account');
		$dt->addColsFields('register_time');
		$dt->addColsFields('cash');
		$dt->addColsFields('register_log_ip', array('sort' => false));
		$dt->addColsFields('register_log_url', array('sort' => false));
		$dt->addColsFields('statuss', array('sort' => false));
		$dt->setPriKey('id');
		$dt->setToolBar(array('view'));
		return view('admin.user/viewip', $dt->render());
	}

	/**
	 * 最后登录ip列表
	 * [detail description]
	 * @return [type] [description]
	 */
	function lastipDetail() {
		$last_ip = Request::has('last_ip') ? Request::get('last_ip') : '';
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/demo/demo?last_ip=' . $last_ip);
		$dt->setLang('demo');
		$dt->addColsFields('id');
		$dt->addColsFields('account');
		$dt->addColsFields('register_time');
		$dt->addColsFields('cash');
		$dt->addColsFields('register_log_ip', array('sort' => false));
		$dt->addColsFields('register_log_url', array('sort' => false));
		$dt->addColsFields('statuss', array('sort' => false));
		$dt->setToolBar(array('view'));
		$dt->setPriKey('id');
		return view('admin.user/viewip', $dt->render());
	}

	/**
	 * 注册网址列表
	 * [detail description]
	 * @return [type] [description]
	 */
	function registurlDetail() {
		$register_url = Request::has('register_url') ? Request::get('register_url') : '';
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/demo/demo?register_url=' . $register_url);
		$dt->setLang('demo');
		$dt->addColsFields('id');
		$dt->addColsFields('account');
		$dt->addColsFields('register_time');
		$dt->addColsFields('cash');
		$dt->addColsFields('register_log_ip', array('sort' => false));
		$dt->addColsFields('register_log_url', array('sort' => false));
		$dt->addColsFields('statuss', array('sort' => false));
		$dt->setPriKey('id');
		$dt->setToolBar(array('view'));
		return view('admin.user/viewip', $dt->render());
	}

	/**
	 * 最后登录网址列表
	 * [detail description]
	 * @return [type] [description]
	 */
	function lasturlDetail() {
		$last_url = Request::has('last_url') ? Request::get('last_url') : '';
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/demo/demo?last_url=' . $last_url);
		$dt->setLang('demo');
		$dt->addColsFields('id');
		$dt->addColsFields('account');
		$dt->addColsFields('register_time');
		$dt->addColsFields('cash');
		$dt->addColsFields('register_log_ip', array('sort' => false));
		$dt->addColsFields('register_log_url', array('sort' => false));
		$dt->addColsFields('statuss', array('sort' => false));
		$dt->setPriKey('id');
		$dt->setToolBar(array('view'));
		return view('admin.user/viewip', $dt->render());
	}

	/**
	 * 试玩列表
	 * 'demo description'
	 * @return 'type' 'description'
	 */
	function demo() {
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : 10;
		$account = Request::has('account') ? Request::get('account') : '';
		$register_ip = Request::has('register_ip') ? Request::get('register_ip') : '';
		$last_ip = Request::has('last_ip') ? Request::get('last_ip') : '';
		$regist_startime = Request::get('regist_startime') ? Request::get('regist_startime').' 00:00:00' : '';
		$regist_endtime = Request::get('regist_endtime') ? Request::get('regist_endtime').' 23:59:59' : '';
		$statuss = Request::has('statuss') ? Request::get('statuss') : '';
		$cash_small = Request::has('cash_small') ? Request::get('cash_small') : '';
		$cash_big = Request::has('cash_big') ? Request::get('cash_big') : '';
		$sureblur = Request::has('sureblur') ? Request::get('sureblur') : '';
		$where = array();
		if (!empty($account)) {
			if (empty($sureblur) || $sureblur == '模糊') {
				$where[] = array('account', 'like', '%' . $account . '%');
			} else {
				$where[] = array('account', '=', $account);
			}
		}
		if (!empty($register_ip)) {
			$where[] = array('register_ip', '=', $register_ip);
		}
		if (!empty($last_ip)) {
			$where[] = array('last_ip', '=', $last_ip);
		}
		if (!empty($regist_startime)) {
			$regist_startime = date('Y-m-d H:i:s', strtotime($regist_startime));
			$where[] = array('register_time', '>=', $regist_startime);
		}
		if (!empty($regist_endtime)) {
			$regist_endtime = date('Y-m-d H:i:s', strtotime($regist_endtime));
			$where[] = array('register_time', '<=', $regist_endtime);
		}
		if (!empty($statuss)) {
			$where[] = array('statuss', '=', $statuss);
		}
		if (!empty($cash_small)) {
			$where[] = array('cash', '>=', $cash_small);
		}
		if (!empty($cash_big)) {
			$where[] = array('cash', '<=', $cash_big);
		}

		$db = new \App\Models\Account;
		$data = $db->demo_account($list, $page, $where);
		if (!($data < 0)&&is_array($data)&&count($data)>0&&is_array($data['data'])&&count($data['data'])>0) {
			$langinfo = trans('status.account_detailed.statuss');
			foreach ($data['data'] as $k => $v) {
				$data['data'][$k]['statuss'] = ($v['statuss']==0)?$langinfo[2]:$langinfo[$v['statuss']];
				$data['data'][$k]['register_url'] = (empty($v['register_url'])) ? '' : $v['register_url'];
				$data['data'][$k]['last_url'] = (empty($v['last_url'])) ? '' : $v['last_url'];
				$data['data'][$k]['register_log_ip'] = $v['register_ip'] . '<br>' . $v['last_ip'];
			}
		}
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

	/**
	 * 批量删除
	 * [delete description]
	 * @return [type] [description]
	 */
	function delete(Req $req) {
		$id = $req->input('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\Account::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id����
		}
		return responseToJson(1, trans('menu.delete_success')); //id����
	}

	function View(Req $req) {
//	    $this->Edit($req);
		$id = $req->id;
		if (empty($id)) {
			abort(404);
		}
		$u_db = new \App\Models\Account();
		$u_details = new \App\Models\Account_detailed();
		$bank = new \App\Models\Account_bank();
		$data = $u_db::where('id', $id)->first();
		if (!$data) {
			abort(404);
		}
		$data = $data->toArray();
		$data_details = $u_details->getInfoBy($data['identity']);
		$data_details['account'] = $data['account'];
		$data_details['remark'] = $data['remark'];
		$bank_data = $bank->getInfoByUid($data['identity']);
		$data_details['bank_name'] = isset($bank_data['bank_name']) ? $bank_data['bank_name'] : '';
		$data_details['bank_address'] = isset($bank_data['bank_address']) ? $bank_data['bank_address'] : '';
		$data_details['bank_number'] = isset($bank_data['bank_number']) ? $bank_data['bank_number'] : '';
		unset($data_details['password']);
		return view('admin.menu/userAdd', ['data' => $data_details, 'look' => '12']);
	}
	//修改会员信息
	function Edit(Req $req) {
		$id = $req->id;
		if (empty($id)) {
			abort(404);
		}
		$u_db = new \App\Models\Account();
		$u_details = new \App\Models\Account_detailed();
		$bank = new \App\Models\Account_bank();
		if (!$req->isMethod('post')) {
			$data = $u_db::where('id', $id)->first();
			if (!$data) {
				abort(404);
			}
			$data = $data->toArray();
			$data_details = $u_details->getInfoBy($data['identity']);
			$data_details['account'] = $data['account'];
			$data_details['remark'] = $data['remark'];
			$bank_data = $bank->getInfoByUid($data['identity']);
			$data_details['bank_name'] = isset($bank_data['bank_name']) ? $bank_data['bank_name'] : '';
			$data_details['bank_address'] = isset($bank_data['bank_address']) ? $bank_data['bank_address'] : '';
			$data_details['bank_number'] = isset($bank_data['bank_number']) ? $bank_data['bank_number'] : '';
			unset($data_details['password']);
			return view('admin.menu/userAdd', ['data' => $data_details]);
		} else {
			$account = $u_db::where('id', $id)->first();
			if (!$account) {
				abort(404);
			}
			$account = $account->toArray();
			$data = array(
				'name' => $req->input('name'),
				'email' => $req->input('email'),
				'phone' => $req->input('phone'),
				'grade' => $req->input('grade'),
			);
			$res = $u_details->updateInfo($data, $account['identity']);
			return responseToJson(1);
		}
	}

	function demoAdd() {
		return view('admin.menu/demoAdd');
	}

}