<?php
namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class UservipController extends Controller
{
	/**
     *
     * [Colse description]
     */
    public function index(Req $req)
    {
    	$request = array();
    	$request['account'] = isset($req->account) ? trim($req->account) : null;
    	$request['sysetem_user'] = isset($req->sysetem_user) ? trim($req->sysetem_user) : null;
    	$request['addtime_start'] = isset($req->addtime_start) ? trim($req->addtime_start) : null;
    	$request['addtime_end'] = isset($req->addtime_end) ? trim($req->addtime_end) : null;
    	$request['status'] = isset($req->status) ? trim($req->status) : null;
    	$request['remark'] = isset($req->remark) ? trim($req->remark) : null;
    	$limit = Request::has('limit') ? Request::get('limit') : '';
        $dt = \App\Lib\DataTable\DataTable::init();
    	$dt->setDataSource('/admin/uservip/info?limit='.$limit);
		$dt->setLang('uservip');
		$dt->addColsFields('account_name');
		$dt->addColsFields('sysetem_user');
		$dt->addColsFields('addtime');
        $dt->addColsFields('audittime');
		//$dt->addColsFields('remark');
		$dt->addColsFields('status');
		$dt->addColsFields ('tools', array('templet' => '#caozuo'));
		$dt->enableCheckBox();
		$dt->setToolBar ();
    	return view('admin.user/uservip', $dt->render($request));
    }

    function info() {
    	$list = Request::has ('limit') ? Request::get ('limit') : '';
		$db = new \App\Models\Uservip();
		$account = Request::has('account') ? Request::get('account') : '';
		$sysetem_user = Request::has('sysetem_user') ? Request::get('sysetem_user') : '';
		$addtime_start = Request::has('addtime_start') ? Request::get('addtime_start') : '';
		$addtime_end = Request::has('addtime_end') ? Request::get('addtime_end') : '';
		$status = Request::has('status') ? Request::get('status') : '';
		$remark = Request::has('remark') ? Request::get('remark') : '';
		$where = array();
		if(!empty($account)){
			$where[] =  array('account_name','=',$account);
		}
		if(!empty($sysetem_user)){
			$where[] = array('sysetem_user','=',$sysetem_user);
		}
		if(!empty($addtime_start)){
			$where[] = array('addtime','>=',$addtime_start.' 00:00:00');
		}
		if(!empty($addtime_end)){
			$where[] = array('addtime','<=',$addtime_end.' 23:59:59');
		}
		if(!empty($status)){
			$where[] = array('status','=',$status);
		}
		if(!empty($remark)){
			$where[] = array('remark','like','%'.$remark.'%');
		}
		$data = $db->getlist($list,$where);
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

	function upvip(Req $req){
		$id = isset($req->id) ? trim($req->id) : 0;
		$status = isset($req->status) ? trim($req->status) : 1;
		$remark = isset($req->remark) ? trim($req->remark) : '';
        if (empty($id)) {
            return -1;
        }
        $db = new \App\Models\Uservip();
        if (!$req->isMethod('post')) {
        	$data = $db->where('id', $id)->first();
            if (!$data) {
                return -2;
            }
            $data = $data->toArray();
        	return view('admin.user/editvip', array('data' => $data));
        }else{

	        $Account_detailed = new \App\Models\Account_detailed();
	        $info = $db->where('id',$id)->first();
	        if(!empty($info)){
	        	$info = $info->toArray();
	        	$data['sysetem_user_id'] = session('adminInfo.admin_id');
	        	$data['sysetem_user'] = session('adminInfo.admin_name');
	        	$data['status'] = $status;
	        	$data['remark'] = $remark;
	        	$data['audittime'] = date('Y-m-d H:i:s');
	        	$res = $db->where('id',$id)->update($data);
		        if($res){ 
		        	$tong ="";
		        	if($status == 2){
		        		$tong ="通过";
		        		$re=$Account_detailed->where('account_identity',$info['account_identity'])->update(['grade'=>-2]);
		        	}elseif($status == 3){
		        		$tong ="未通过";
		        		$re=$Account_detailed->where('account_identity',$info['account_identity'])->update(['grade'=>0]);
		        	}
		        	$log = array(
			            session('adminInfo.admin_name'),
			            $tong,
			            $info['account_name'],
			            session('adminInfo.admin_name'),
			        );

			        OperationLog(session('adminInfo.admin_id'), 'Uservip', $log);
			        
			        return responseToJson ($re);
		        }else{
		        	return responseToJson(-1);
		        }
	        }else{
	        	return responseToJson(-1);
	        }
        }
		
	}

}

?>