<?php

namespace App\Http\Controllers\Admin;

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

class AllowipController extends Controller {

	
    public function __construct(){
        $set = new \App\Models\Rgame_setting();
        $where = 'kygame';
        $set_obj  = $set->getGames($where);
		if(empty($set_obj)){
			return;
		}
        $this->agent = $set_obj->agent_name;
        $this->game_type = $set_obj->game_type;
        $this->pwd = $set_obj->agent_pwd;
        $this->user_pre = $set_obj->user_prefix;
        $this->lineCode = $set_obj->lineCode;
         //解析扩展字段
        $json = json_decode($set_obj->extend, true);
        $this->host = $json['host1'];
        $this->pay_host = $json['host2'];
        $this->desKey = $json['desKey'];
        $this->md5key = $json['md5key'];
    }

	public function index()	{
		$request['ip'] = isset($req->ip) ? trim($req->ip) : null;
		$request['operator'] = isset($req->operator) ? trim($req->operator) : null;
		$request['time'] = isset($req->time) ? trim($req->time) : null;
		$request['remark'] = isset($req->remark) ? trim($req->remark) : null;
		$request['type'] = isset($req->type) ? trim($req->type) : null;
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/Allowip/data');
		$dt->setLang('allowip');
		$dt->addColsFields('ip', array('sort' => false));
		$dt->addColsFields('operator',array('sort' => false));
		$dt->addColsFields('time');
		$dt->addColsFields('remark',array('sort' => false));
		$dt->addColsFields('type',array('sort' => false));
		$dt->setToolBar(array('edit'),array('sort' => false));
		$dt->enableCheckBox();

		return view('admin.allowip/index', $dt->render($request));

	}

	function getEditColsModel() {
		return 'dc_allowip';
	}

	function add(Request $req) {
		$model = new \App\Models\Allowip();
		if (!$req->isMethod('post')) {
			$data['type'] = 2;
			return view('admin.allowip/add', array('data' => $data));
		} else {
			$model = new \App\Models\Allowip();
			$model->ip = trim($req->input('ip'));
			$model->operator = session('adminInfo.admin_name');
			$model->time = date('Y-m-d H:i:s');
			$model->remark = trim($req->input('remark'));
			$model->type = trim($req->input('type'));
			$model->save();
			$log = array(
	            session('adminInfo.admin_name'),
	            $model->ip,
	            $model->remark,
	        );
	        OperationLog(session('adminInfo.admin_id'), 'addallowip', $log);
			return responseToJson(1);
		}
	}

	function data() {
		$db = new \App\Models\Allowip();
		$data =  $db->getCode();
		$langinfo = trans('status.allowip');
		foreach ($data as $k => $v) {
			$data[$k]['type'] = ($v['type'] == 0) ? $langinfo['type']['1'] : $langinfo['type'][$v['type']];
		}
		return \App\Lib\DataTable\DataTable::init()->toJson($data);

	}

	public function delete(Request $req) {
		$id = $req->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\Allowip::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id错误
		}
		$log = array(
            session('adminInfo.admin_name'),
        );

        OperationLog(session('adminInfo.admin_id'), 'del_allowip', $log);
		return responseToJson(1, trans('priv_code.delete_success')); //id错误

	}

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

			$data = \App\Models\Allowip::where('id', $id)->first();
			if (!$data) {
				return -2;
			}
			$data = $data->toArray();
			return view('admin.allowip/edit', array('data' => $data));
		} else {
			$model = \App\Models\Allowip::where('id', $id)->first();
			$model->ip = trim($req->input('ip'));
			$model->operator = session('adminInfo.admin_name');
			$model->time = date('Y-m-d H:i:s');
			$model->remark = trim($req->input('remark'));
			$model->type = trim($req->input('type'));
			$model->save();
			return responseToJson(1);
		}

	}


    function getC(){
        $agent_money = \App\Models\Rgame_setting::where('game_type','kygame')->select('agent_money')->first()->toArray();
        return responseToJson ($agent_money);
    }


}