<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class LogController extends Controller {

	function index() {
		# code...
	}

	/**
	 *会员登录日志
	 * [Colse description]
	 */
	function logs(Req $req) {
		$request=array();
		$request['account']=isset($req->account)?trim($req->account):null;
		$request['type'] = isset($req->type) ? trim($req->type) : null;
		$request['register_ip'] = isset($req->register_ip) ? trim($req->register_ip) : null;
		$request['url'] = isset($req->url) ? trim($req->url) : null;
		$request['last_ip'] = isset($req->last_ip) ? trim($req->last_ip) : null;
		$request['log_startime'] = isset($req->log_startime) ? trim($req->log_startime) : null;
		$request['log_endtime'] = isset($req->log_endtime) ? trim($req->log_endtime) : null;
		
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : '';
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/Log/log?page=' . $page . '&limit=' . $list);
		$dt->setLang('user');
		$dt->addColsFields('account', array('sort' => false));
		$dt->addColsFields('type', array('sort' => false));
		$dt->addColsFields('ip', array('templet' => '#selectip', 'sort' => false));
		$dt->addColsFields('url', array('templet' => '#selecturl', 'sort' => false));
		$dt->addColsFields('register_ip', array('templet' => '#selectregistip', 'sort' => false));
		$dt->addColsFields('add_time');
		return view('admin.log/logs', $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/Log/log?register_ip=' . $register_ip);
		$dt->setLang('user');
		$dt->addColsFields('account');
		$dt->addColsFields('type', array('sort' => false));
		$dt->addColsFields('ip', array('sort' => false));
		$dt->addColsFields('url', array('sort' => false));
		$dt->addColsFields('register_ip', array('sort' => false));
		$dt->addColsFields('add_time');
		return view('admin.user/viewip', $dt->render());
	}

	/**
	 * 登录ip列表
	 * [detail description]
	 * @return [type] [description]
	 */
	function logipDetail() {
		$ip = Request::has('ip') ? Request::get('ip') : '';
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/Log/log?last_ip=' . $ip);
		$dt->setLang('user');
		$dt->addColsFields('account');
		$dt->addColsFields('type', array('sort' => false));
		$dt->addColsFields('ip', array('sort' => false));
		$dt->addColsFields('url', array('sort' => false));
		$dt->addColsFields('register_ip', array('sort' => false));
		$dt->addColsFields('add_time');
		return view('admin.user/viewip', $dt->render());
	}

	/**
	 * 登录网址列表
	 * [detail description]
	 * @return [type] [description]
	 */
	function logurlDetail() {
		$url = Request::has('url') ? Request::get('url') : '';
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/Log/log?url=' . $url);
		$dt->setLang('user');
		$dt->addColsFields('account');
		$dt->addColsFields('type', array('sort' => false));
		$dt->addColsFields('ip', array('sort' => false));
		$dt->addColsFields('url', array('sort' => false));
		$dt->addColsFields('register_ip', array('sort' => false));
		$dt->addColsFields('add_time');
		return view('admin.user/viewip', $dt->render());
	}

	/**
	 * 会员登录日志
	 * 'log description'
	 * @return 'type' 'description'
	 */
	function log() {
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : '';

		$account = Request::has('account') ? Request::get('account') : '';
		$type = Request::has('type') ? Request::get('type') : '';
		$register_ip = Request::has('register_ip') ? Request::get('register_ip') : '';
		$last_ip = Request::has('last_ip') ? Request::get('last_ip') : '';
		$url = Request::has('url') ? Request::get('url') : '';
		$log_startime = Request::get('log_startime') ? Request::get('log_startime').' 00:00:00' : '';
		$log_endtime = Request::get('log_endtime') ? Request::get('log_endtime').' 23:59:59' : '';
		$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($type)) {
			$where[] = array('type', '=', $type);
		}
		if (!empty($register_ip)) {
			$where[] = array('register_ip', '=', $register_ip);
		}
		if (!empty($last_ip)) {
			$where[] = array('ip', '=', $last_ip);
		}
		if (!empty($url)) {
			$where[] = array('url', 'like', '%' . $url . '%');
		}
		if (!empty($log_startime)) {
			$log_startime = date('Y-m-d H:i:s', strtotime($log_startime));
			$where[] = array('add_time', '>=', $log_startime);
		}
		if (!empty($log_endtime)) {
			$log_endtime = date('Y-m-d H:i:s', strtotime($log_endtime));
			$where[] = array('add_time', '<=', $log_endtime);
		}

		$db = new \App\Models\Logfile;
		$data = $db->log($list, $page, $where);
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

	//查看会员信息
	function View(Req $req) {
		$id = $req->id;
		if (empty($id)) {
			abort(404);
		}
		$db = new \App\Models\Account_detailed();
		if (!$req->isMethod('post')) {
			$data = $db::where('id', $id)->first();
			if (!$data) {
				abort(404);
			}
			$data = $data->toArray();
			return view('admin.menu/userView', ['data' => $data]);
		} else {
			// $model = $db::where('id', $id)->first();
			// $model->name = $req->input('menu_name');
			// $model->href = $req->input('menu_url');
			// $model->icon = $req->input('menu_icon');
			// $model->sort = $req->input('menu_sort');
			// $model->type = $req->input('type');
			$model->save();
			return responseToJson(1);
		}
	}
	//修改会员信息
	function Edit(Req $req) {
		$id = $req->id;
		if (empty($id)) {
			abort(404);
		}
		$db = new \App\Models\Account_detailed();
		if (!$req->isMethod('post')) {
			$data = $db::where('id', $id)->first();
			if (!$data) {
				abort(404);
			}
			$data = $data->toArray();
			return view('admin.menu/userAdd', ['data' => $data]);
		} else {
			// $model = $db::where('id', $id)->first();
			// $model->name = $req->input('menu_name');
			// $model->href = $req->input('menu_url');
			// $model->icon = $req->input('menu_icon');
			// $model->sort = $req->input('menu_sort');
			// $model->type = $req->input('type');
			$model->save();
			return responseToJson(1);
		}
	}

}