<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class BankInfoController extends Controller {

	function index() {
		# code...
	}
function selectBetList(Req $req){
	$game = $req->game;
	$no = $req->no;
	$list = $req->list;
        if($game=='sixlottery'){
            return \App\Models\SixMoneyBuy::BetList($game,$no,$list);
        }
        return \App\Models\MoneyBuy::BetList($game,$no,$list);
    }
	/**
	 *银行卡信息
	 * [Colse description]
	 */
	function infos(Req $req) {
		$request=array();
		$request['account']=isset($req->account)?trim($req->account):null;
		$request['bank_name'] = isset($req->bank_name) ? trim($req->bank_name) : null;
		$request['bank_number'] = isset($req->bank_number) ? trim($req->bank_number) : null;
		$request['bank_address'] = isset($req->bank_address) ? trim($req->bank_address) : null;
		$request['account_name'] = isset($req->account_name) ? trim($req->account_name) : null;
		$request['startime'] = isset($req->startime) ? trim($req->startime) : null;
		$request['endtime'] = isset($req->endtime) ? trim($req->endtime) : null;
		$request['status'] = isset($req->status) ? trim($req->status) : null;

		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/BankInfo/info');
		$dt->setLang('bank_info');//'templet' => 'userdetail',
		$dt->addColsFields('account', array('sort' => false, 'width' => 130));
		$dt->addColsFields('bank_name', array( 'sort' => false));
		$dt->addColsFields('bank_number', array('sort' => false));
		$dt->addColsFields('bank_address', array( 'sort' => false));
		$dt->addColsFields('band_time');
		$dt->addColsFields('name', array('sort' => false, 'width' => 100));
		$dt->addColsFields('status', array('templet' => '#closeTool', 'sort' => false, 'width' => 120));
		$dt->setPriKey('identity');
		if (checkRriv('/admin/BankInfo/Edit')) {
			$dt->setToolBar(array('edit'), array('width' => 100,'align' => 'left'));
		}
		return view('admin.bankinfo/infos', $dt->render($request));
	}

	function getEditColsModel() {
		return 'Account_bank';
	}
	function EditColsValidate($field, $value) {
		$db = new \App\Models\Account_bank;
		if ($field == "bank_number") {
			if (count($value) > 20) {
				return -8505012212; //银行卡长度有误
			}
		}
		if ($re = $db->checkBank($field, $value)) {
			return 1;
		}
		return -40255022;
	}

	/**
	 * 关闭游戏接口
	 */
	function GameClose() {
		$status = Request::has('status') ? Request::get('status') : '';
		$game = Request::has('bank_number') ? Request::get('bank_number') : '';
		if (empty($game)) {
			return responseToJson(-5030002031);
		}
		if ($status < 0) {
			return responseToJson(-5030002032);
		}

		$db = new \App\Models\Account_bank;
		$data = array(
			'status' => $status,
		);
		if ($status) {
			$data['status'] = 1;
		} else {
			$data['status'] = '0';
		}
		$res = $db->closeGame($data, $game);
		return responseToJson($res);
	}
	/**
	 * 银行卡信息
	 * 'FunctionName description'
	 * @param string $value 'description'
	 */
	function info() {
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : '';

		$account = Request::has('account') ? Request::get('account') : '';
		$bank_name = Request::has('bank_name') ? Request::get('bank_name') : '';
		$bank_number = Request::has('bank_number') ? Request::get('bank_number') : '';
		$bank_address = Request::has('bank_address') ? Request::get('bank_address') : '';
		$account_name = Request::has('account_name') ? Request::get('account_name') : '';
		$startime = Request::get('startime') ? Request::get('startime').' 00:00:00' : '';
		$endtime = Request::get('endtime') ? Request::get('endtime').' 23:59:59' : '';
		$status = Request::has('status') ? Request::get('status') : '';
		$sureblur = Request::has('sureblur') ? Request::get('sureblur') : '';
		$where = array();


		if (!empty($bank_name)) {
			$where[] = array('bank_name', 'like', '%' . $bank_name . '%');
		}

		if (!empty($bank_number)) {
			$where[] = array('bank_number', '=', $bank_number);
		}

		if (!empty($bank_address)) {
			$where[] = array('bank_address', 'like', '%' . $bank_address . '%');
		}

		if (!empty($account_name)) {
			$where[] = array('account_name', 'like', '%' . $account_name . '%');
		}

		if (!empty($startime)) {
			$startime = date('Y-m-d H:i:s', strtotime($startime));
			$where[] = array('band_time', '>=', $startime);
		}
		if (!empty($endtime)) {
			$endtime = date('Y-m-d H:i:s', strtotime($endtime));
			$where[] = array('band_time', '<=', $endtime);
		}
		if (!empty($status)) {
			if ($status == -1) {
				$where[] = array('account_bank.status', '=', '0');
			} else {
				$where[] = array('account_bank.status', '=', $status);
			}
		}
		if (!empty($account)) {
			if (empty($sureblur) || $sureblur == '模糊') {
				$where[] = array('account', 'like', '%' . $account . '%');
			} else {
				$where = array(array('account', '=', $account));
			}
		}
		$db = new \App\Models\Account_bank;
		$data = $db->account_bank($list, $page, $where);
		$has_root = 0;
		if(\App\Models\Role::hasRoot('/enable/show/bankcard') < 0){
			$has_root = 1;
		}
		if(\App\Models\Role::hasRoot('/enable/show/name') < 0){
			$has_root = 1;
		}
		if (!($data < 0)) {
			foreach ($data['data'] as $k => $v) {
				$has_root == 1 ? $data['data'][$k]['bank_number'] = bankHide($v['bank_number']) : null;
				$has_root == 1 ? $data['data'][$k]['account'] = userHide($v['account']) : null;
			}
		}
		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_bank::whereIn('account_identity', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id����
		}
		return responseToJson(1, trans('menu.delete_success')); //id����
	}

	function Edit(Req $req) {
		$id = $req->identity;
		if (empty($id)) {
			abort(404);
		}
		$u_db = new \App\Models\Account();
		$bank = new \App\Models\Account_bank();
		if (!$req->isMethod('post')) {
			$data = $u_db::where('identity', $id)->first();
			if (!$data) {
				abort(404);
			}
			$data = $data->toArray();
			$data_details['account'] = $data['account'];
			$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'] : '';
			$data_details['name'] = isset($bank_data['name']) ? $bank_data['name'] : '';
			$data_details['status'] = isset($bank_data['status']) ? $bank_data['status'] : '';
			unset($data_details['password']);
			return view('admin.menu/infoAdd', ['data' => $data_details]);
		} else {
			$data = $u_db::where('identity', $id)->first();
			if (!$data) {
				abort(404);
			}
			$data = $data->toArray();
			$data_details = $bank->getInfoByUid($data['identity']);
			$data['identity'] = $data_details['account_identity'];
			$data = array(
				'bank_name' => $req->input('bank_name'),
				'bank_address' => $req->input('bank_address'),
				'account_name' => $req->input('name'),
				'bank_number' => $req->input('bank_number'),
				'status' => $req->input('status'),
			);
			// print_r($data);die;
			$res = $bank->updateOne($data, $id);
			if($res<0){
				return responseToJson($res);
			}

			$u_details = new \App\Models\Account_detailed();
			$res = $u_details->updateInfo(array('name'=>$data['account_name']), $id);
			$log = array(
	            session('adminInfo.admin_name'),
	            $req->input('account'),
	            $req->input('bank_number'),
	            $req->input('name'),
	        );
	        OperationLog(session('adminInfo.admin_id'), 'EditBank', $log);
			return responseToJson($res);
		}
	}

	/**
	 * 银行卡信息添加
	 * [demoAdd description]
	 * @return [type] [description]
	 */
	function infoAdd() {
		return view('admin.menu/infoAdd');
	}

}