<?php

namespace App\Http\Controllers\Admin;

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

/**
 *代理管理
 */
class WagentController extends Controller {

	//代理列表
	function index(Req $req) {
		$request=array();
		$request['home_team'] = isset($req->home_team) ? trim($req->home_team) : null;
		$request['status'] = isset($req->status) ? trim($req->status) : '-1';
		$request['sureblurs'] = isset($req->sureblurs) ? $req->sureblurs : 'on';
		$request['star_time'] = isset($req->star_time) ? trim($req->star_time) :trans('status.default_time.seven_day') ;
        $request['end_time'] = isset($req->end_time) ? trim($req->end_time) : trans('status.default_time.etime');

		$dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/wagent/info');
        $dt->setLang('wagent');
        $dt->addColsFields('agent_name', array('templet' => '#newtime', 'sort' => false, 'width' => 80));
        $dt->addColsFields('agent_key_secret', array('templet' => '#agent_key_secret', 'sort' => false, 'width' => 160));
        $dt->addColsFields('create_time', array('templet' => '#totime', 'sort' => false, 'width' => 120));
        $dt->addColsFields('frozen_money', array('templet' => '#frozen_money', 'sort' => false, 'width' => 120));
        $dt->addColsFields('money', array('templet' => '#totime', 'sort' => false, 'width' => 80));
        $dt->addColsFields('zjrecord_xxrecord', array('templet' => '#zjrecord_xxrecord', 'sort' => false, 'width' => 180));
        if (checkRriv('/admin/wagent/edit')) {
			$arr[] = 'edit';
		}
		$dt->setToolBar($arr, array('width' => 100));
        $dt->enableCheckBox();
		return view('admin.wagent/index', $dt->render($request));
	}

	//代理用户资金纪录
	function zjrecord(Req $req){
		$request=array();
		$request['home_team'] = isset($req->home_team) ? trim($req->home_team) : null;
		$request['status'] = isset($req->status) ? trim($req->status) : '-1';
		$request['sureblurs'] = isset($req->sureblurs) ? $req->sureblurs : 'on';
		$request['star_time'] = isset($req->star_time) ? trim($req->star_time) :trans('status.default_time.seven_day') ;
        $request['end_time'] = isset($req->end_time) ? trim($req->end_time) : trans('status.default_time.etime');

        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/wagent/zjrecordinfo');
        $dt->setLang('wagent');
        $dt->addColsFields('agent_name', array('templet' => '#newtime', 'sort' => false, 'width' => 80));
        $dt->addColsFields('ordernumber', array('templet' => '#newtime', 'sort' => false, 'width' => 80));
        $dt->addColsFields('credit', array('templet' => '#newtime', 'sort' => false, 'width' => 80));
        $dt->addColsFields('agent_money_before', array('templet' => '#newtime', 'sort' => false, 'width' => 100));
        $dt->addColsFields('agent_money_after', array('templet' => '#newtime', 'sort' => false, 'width' => 100));
        $dt->enableCheckBox();
		return view('admin.wagent/zjrecord', $dt->render($request));
	}

	function zjrecordinfo(){
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : 10;
		$home_team = Request::has('home_team') ? Request::get('home_team') : '';
		$sureblur = Request::has('sureblurs') ? Request::get('sureblurs') : 'off';
		$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();
		if (!empty($home_team)) {
			if (empty($sureblur) || $sureblur == 'off') {
				$where[] = array('wagent_log.agent_name', 'like', '%' . $home_team . '%');
			} else {
				$where[] = array('wagent_log.agent_name', '=', $home_team);
			}
		}

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

	//代理用户客户资金纪录
	function xxrecord(Req $req){
		$request=array();
		$request['home_team'] = isset($req->home_team) ? trim($req->home_team) : null;
		$request['status'] = isset($req->status) ? trim($req->status) : '-1';
		$request['sureblurs'] = isset($req->sureblurs) ? $req->sureblurs : 'on';
		$request['star_time'] = isset($req->star_time) ? trim($req->star_time) :trans('status.default_time.seven_day') ;
        $request['end_time'] = isset($req->end_time) ? trim($req->end_time) : trans('status.default_time.etime');
        
        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/wagent/xxrecordinfo');
        $dt->setLang('wagent');
        $dt->addColsFields('ordernumber', array('templet' => '#newtime', 'sort' => false, 'width' => 300));
        $dt->addColsFields('tradeamount', array('templet' => '#newtime', 'sort' => false, 'width' => 80));
        $dt->addColsFields('blance', array('templet' => '#newtime', 'sort' => false, 'width' => 80));
        $dt->addColsFields('addtime', array('templet' => '#newtime', 'sort' => false, 'width' => 160));
        $dt->addColsFields('agent_name', array('templet' => '#newtime', 'sort' => false, 'width' => 80));
        $dt->enableCheckBox();
		return view('admin.wagent/xxrecord', $dt->render($request));
	}

	function xxrecordinfo(){
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : 10;
		$home_team = Request::has('home_team') ? Request::get('home_team') : '';
		$sureblur = Request::has('sureblurs') ? Request::get('sureblurs') : 'off';
		$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();
		if (!empty($home_team)) {
			if (empty($sureblur) || $sureblur == 'off') {
				$where[] = array('money_transfer.agent_name', 'like', '%' . $home_team . '%');
			} else {
				$where[] = array('money_transfer.agent_name', '=', $home_team);
			}
		}

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

	function info(){
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : 10;
		$home_team = Request::has('home_team') ? Request::get('home_team') : '';
		$sureblur = Request::has('sureblurs') ? Request::get('sureblurs') : 'off';
		$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();
		if (!empty($home_team)) {
			if (empty($sureblur) || $sureblur == 'off') {
				$where[] = array('wagent.agent_name', 'like', '%' . $home_team . '%');
			} else {
				$where[] = array('wagent.agent_name', '=', $home_team);
			}
		}

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

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

			$data = \App\Models\Wagent::where('id', $id)->first();
			if (!$data) {
				return -2;
			}
			$data = $data->toArray();

			return view('admin.wagent/edit', $data);
		} else {
			$newapp = new \App\Models\Wagentlog();
			$model = \App\Models\Wagent::where('id', $id)->first();
			$newapp->agent_money_before = $model['money'];//交易前金额
			$upmoney = $req->input('upmoney');
			$type = $req->input('type');

			if($type==1){
				$model->money = $model['money']+$upmoney;
			}
			if($type==2){
				if($model['money']>=$upmoney){
					$model->money = $model['money']-$upmoney;
				}
			}
			$model->status = $req->input('status');
			$model->save();

			//资金纪录日志
			$newapp->agent_name = $model['agent_name'];//代理名称
			$newapp->account_identity = 0;//默认值
			$newapp->ordernumber = 'ST'.date("Ymdhis").mt_rand(100000, 999999);//订单号
			$newapp->credit = $upmoney;//改变金额
			$newapp->agent_money_after = $model->money;//交易后金额
			$newapp->uid = session('adminInfo.admin_id');//操作员id
			$newapp->save();
			return responseToJson(1);
		}
	}	

	function add(Req $req) {
		if (!$req->isMethod('post')) {
            $lange = trans('menu');

            return view('admin.wagent/add');
        } else {
        	$agent_name = trim($req->input('agent_name'));//唯一代理名称
        	$agent_pre = trim($req->input('agent_pre'));//唯一代理用户前缀

            $model = new \App\Models\Wagent();
            $only = $model->onlywagent($agent_name,$agent_pre);
            if($only==1){
				$model->agent_name = $agent_name;//唯一代理名称
				$model->money = trim($req->input('money'));
				$model->frozen_money = trim($req->input('frozen_money'));
				$model->agent_key = md5($this->randomnum(6));
				$model->auth_expire = time();
				$model->agent_secret = md5($this->randomnum(6));
				$model->create_time = date("Y-m-d");
				$model->agent_pre = $agent_pre.'_';//唯一代理用户前缀
				$model->status = trim($req->input('status'));
			}

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

	//随机数
	function randomnum($num){
		$str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
		$randStr = str_shuffle($str);//打乱字符串
		$rands= substr($randStr,0,$num);
		return $rands;
	}

}