<?php
/**
 * 系统设置
 */
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ArticleType;
use App\Models\LotteryMoney;
use Illuminate\Http\Request as Req;
use Request;

/**
 *
 */
class SystemController extends Controller {
	/**
	 *
	 */
	function index() {
		return view('admin.systemSet/index');
	}

	/**
	 * 活动公告
	 */
	function Article() {
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/System/getArticle');
		$dt->setLang('notice');
		$dt->addColsFields('id');
		$dt->addColsFields('title');
//        $dt->addColsFields('content');
		$dt->addColsFields('time');
		$dt->addColsFields('type');
		$dt->setToolBar(array('edit'));
		return view('admin.systemSet/arictle', $dt->render());
	}

	/**
	 * 公告添加
	 * [addNotice description]
	 */
	function addNotice() {
		return view('admin.menu/addNotice');
	}

//	//基础设置
	//	function BaseSet() {
	//		$dt = \App\Lib\DataTable\DataTable::init();
	//		$dt->setDataSource('/admin/System/getBaseSet');
	//		$dt->setLang('notice');
	//		$dt->addColsFields('id', array('fixed' => 'left'));
	//		$dt->addColsFields('infoname');
	//		$dt->addColsFields('infocontent', array('edit' => 'password'));
	//		$dt->addColsFields('status');
	//		$dt->setToolBar(array('save'));
	//		return view('admin.systemSet/arictle', $dt->render());
	//
	//	}
	//获取基础设置
	function getBaseSet() {
		$infoname = Request::has('infoname') ? Request::get('infoname') : '';
		$infocontent = Request::has('infocontent') ? Request::get('infocontent') : '';
		$status = Request::has('status') ? Request::get('status') : '';

		$where = array();
		if (!empty($infoname)) {
			$where[] = array('infoname', 'like', '%' . $infoname . '%');
		}
		if (!empty($infocontent)) {
			$where[] = array('infocontent', 'like', '%' . $infocontent . '%');
		}
		if (!empty($status)) {
			if ($status == 2) {
				$where[] = array('status', '=', '0');
			} else {
				$where[] = array('status', '=', $status);
			}
		}

		$db = new \App\Models\Setinfo();
		$data = $db->getAllSet($where);
		return \App\Lib\DataTable\DataTable::init()->toJson($data, count($data));
	}
	/**
	 * 菜单管理
	 */
	function Menu() {
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/System/getMenu');
		$dt->setLang('menu');
		$dt->addColsFields('id', array('fixed' => 'left'));
		$dt->addColsFields('name');
		$dt->addColsFields('icon');
		$dt->addColsFields('href');
		$dt->addColsFields('type');
		$dt->addColsFields('sort');
		$dt->addColsFields('parent_id');
		$dt->addColsFields('target');
		$dt->setToolBar(array('edit'));
//        return  view('vip.menu',$dt->render());
		return view('admin.systemSet/menu', $dt->render());
	}
	//获取菜单列表
	function getMenu() {
		$limit = Request::has('limit') ? Request::get('limit') : '';
		$page = Request::has('page') ? Request::get('page') : '';
		$field = Request::has('field') ? Request::get('field') : '';
		$order = Request::has('order') ? Request::get('order') : '';
		$model = \App\Models\NavsModel::offset(($page - 1) * $limit)
			->limit($limit);

		$name = Request::has('name') ? Request::get('name') : '';
		$href = Request::has('href') ? Request::get('href') : '';
		$parent_id = Request::has('parent_id') ? Request::get('parent_id') : '';
		$type = Request::has('type') ? Request::get('type') : '';
		$where = array();
		if (!empty($name)) {
			$where[] = array('name', 'like', '%' . $name . '%');
		}
		if (!empty($href)) {
			$where[] = array('href', 'like', '%' . $href . '%');
		}
		if (!empty($parent_id)) {
			$where[] = array('parent_id', '=', $parent_id);
		}
		if (!empty($type)) {
			if ($type == -1) {
				$where[] = array('type', '=', '0');
			} else {
				$where[] = array('type', '=', $type);
			}
		}
		if (!empty($where) && is_array($where)) {
			$model = $model->where($where);
		}
		if (!empty($field) && !empty($order)) {
			$model = $model->orderBy($field, $order);
		}
		$data = $model->get();
		$data = $data->toArray();
		if (!empty($data) && is_array($data)) {
			$langinfo = trans('status.dc_navs.type');
			foreach ($data as $k => $v) {
				$data[$k]['type'] = ($v['type'] == 0) ? $langinfo['-1'] : $langinfo[$v['type']];
			}
		}
		$count = \App\Models\NavsModel::count();
		return \App\Lib\DataTable\DataTable::init()->toJson($data, $count);
	}

	//获取活动信息
	function getArticle() {
		$limit = Request::has('limit') ? Request::get('limit') : 10;

		$title = Request::has('title') ? Request::get('title') : '';
		$type = Request::has('type') ? Request::get('type') : '';
		$add_startime = Request::has('add_startime') ? Request::get('add_startime') : '';
		$add_endtime = Request::has('add_endtime') ? Request::get('add_endtime') : '';

		$where = array();
		if (!empty($title)) {
			$where[] = array('title', 'like', '%' . $title . '%');
		}
		if (!empty($type)) {
			$where[] = array('type', '=', $type);
		}
		if (!empty($add_startime)) {
			$add_startime = date('Y-m-d H:i:s', strtotime($add_startime));
			$where[] = array('time', '>=', $add_startime);
		}
		if (!empty($add_endtime)) {
			$add_endtime = date('Y-m-d H:i:s', strtotime($add_endtime));
			$where[] = array('time', '<=', $add_endtime);
		}
       
		$db = new \App\Models\Article();
		$data = $db->getlist($limit, $where);

		$Lottery=   new LotteryMoney();
		$activity_list  =   $Lottery->getLotteryList();
       $activity_list   =   is_array($activity_list)&&count($activity_list) >0?array_column($Lottery->getLotteryList(),"name","id"):[];

		if (count($data)>0) {
			//$langinfo = trans('status.article.type');
            //2018-09-26 修改无限级分类

			$_type   =   (new  ArticleType())->type();
            $type   =   array_column($_type,"parent_path","id");
            $type_name  =   array_column($_type,"cate_name","id");
			foreach ($data['data'] as $k => $v) {
			//	$data['data'][$k]['type'] = isset($langinfo[$v['type']])?$langinfo[$v['type']]:null;
                $act    =   isset($activity_list[$v["child_id"]])&&!empty($activity_list[$v["child_id"]])?"<b>【活动:".$activity_list[$v["child_id"]]."】</b>":"";
                $_name= isset($type_name[$v['type']])?">".$type_name[$v['type']]:"";
				$data['data'][$k]['type'] = isset($type[$v['type']])?$type[$v['type']].$_name.$act:$_name;

			}
		}

		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

    //获取活动信息
    function getArticleType() {

	    $cen    =   [
	        "1"=>"一级分类",
	        "2"=>"二级分类",
	        "3"=>"三级分类",
	        "4"=>"四级分类",
	        "5"=>"五级分类",
	        "6"=>"六级分类",
	        "7"=>"七级分类",
	        "8"=>"八级分类",
	        "9"=>"九级分类",
        ];

        $limit = Request::has('limit') ? Request::get('limit') : 10;

        $title = Request::has('title') ? Request::get('title') : '';
        $type = Request::has('type') ? Request::get('type') : '';
        $add_startime = Request::has('add_startime') ? Request::get('add_startime') : '';
        $add_endtime = Request::has('add_endtime') ? Request::get('add_endtime') : '';

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

        if (!empty($add_startime)) {
            $add_startime = date('Y-m-d H:i:s', strtotime($add_startime));
            $where[] = array('create_time', '>=', $add_startime);
        }
        if (!empty($add_endtime)) {
            $add_endtime = date('Y-m-d H:i:s', strtotime($add_endtime));
            $where[] = array('create_time', '<=', $add_endtime);
        }

        $db = new ArticleType();
        $data = $db->getlist($where,$limit );
        $c  =   count($data);
        if ($c>0) {
            foreach ($data as $k=>$v){
                $data[$k]["parent_path"]=empty($v["parent_path"])?$cen[1]:$v["parent_path"];
            }
            return \App\Lib\DataTable\DataTable::init()->toJson($data, $c);
        }
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data']=[], $data['total']=null);
    }


	//添加菜单
	function AddMenu() {
		$id = Request::has('id') ? Request::get('id') : '';
		$data = array();
		$db = new \App\Models\NewMenu();
		if (!empty($id)) {
			$data = $db->getMenuById($id);
		}
		$parent = $db->getMenuParent();
		return view('admin.menu/add', ['data' => $data, 'parents' => $parent]);
	}
	function Edit(Req $req) {
		$id = $req->id;
		if (empty($id)) {
			abort(404);
		}
		$db = new \App\Models\NewMenu();
		if (!$req->isMethod('post')) {
			$data = $db::where('id', $id)->first();
			if (!$data) {
				abort(404);
			}
			$data = $data->toArray();
			$parent = $db->getMenuParent();
			return view('admin.menu/add', ['data' => $data, 'parents' => $parent]);
		} 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 add() {
		$name = Request::get('name');
		echo '<pre>';
		print_r($_GET);
		print_r($_POST);
	}
	//玩法简介
	function Playdesc() {
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/System/active');
		$dt->setLang('notice');
		$dt->addColsFields('id', array('fixed' => 'left'));
		$dt->addColsFields('name');
//        $dt->addColsFields('play_desc');
		$dt->addColsFields('table_name');
		$dt->setToolBar(array('view', 'edit'));
		return view('admin.systemSet/arictle', $dt->render());
	}
	/**
	 * 公告显示
	 */
	function Notice() {
		$db = new \App\Models\Setinfo();
		$data = $db->getNotice();
		return responseToJson($data);
	}

	/**
	 * 公告设置
	 */
	function NoticeSet() {

	}

	/**
	 * 弹框
	 */
	function OutBox() {
		$db = new \App\Models\Setinfo();
		$data = $db->getBoxOut();
		return responseToJson($data);
	}

	/**
	 * 手机弹框内容设置
	 */
	function OutBoxSet() {

	}

	/**
	 * 客服QQ设置
	 */
	function ServerQQ() {
//        $data=DB::table('setinfo')->where('infotype',14)->get();
		//        dump($data->toArray());
		$db = new \App\Models\Setinfo();
		$data = $db->getQQnumber();
		return responseToJson($data);
	}
	/**
	 * 客服QQ设置修改
	 */
	function ServerQQSet() {

	}
	/**
	 * 代理设置
	 */
	function Agent() {
		$db = new \App\Models\Setinfo();
		$data = $db->getAgentSet();
		return responseToJson($data);
	}

	/**
	 * 代理模式设置
	 */
	function AgentSet() {

	}

	/**
	 * 平台维护
	 */
	function PlatFrom() {
		$db = new \App\Models\Setinfo();
		$data = $db->getPlatSet();
		return responseToJson($data);
	}

	/**
	 * 平台维护设置
	 */
	function PlatFromSet() {

	}
	//虚拟游戏设置显示
	function Flaseprize() {
		$db = new \App\Models\Setinfo();
		$data = $db->getPrizeSet();
		return responseToJson($data);
	}
	//转账银行卡信息
	function BankInfo() {
		$db = new \App\Models\Setinfo();
		$data = $db->getBankInfo();
		return responseToJson($data);
	}
	//跑马灯公告
	function RunNotice() {
		$db = new \App\Models\Setinfo();
		$data = $db->getRunNotice();
		return responseToJson($data);
	}
	//注册设置信息
	function RegisterSet() {
		$db = new \App\Models\Settings();
		$data = $db->getSet();
		return responseToJson($data);
	}
	/**
	 * 游戏玩法说明消息
	 */
	function Active() {
		$list = Request::has('limit') ? Request::get('limit') : 10;
		$db = new \App\Models\GameType();
		$data = $db->getDescList($list);
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
//        return responseToJson($data);
	}

	/**
	 * 修改游戏玩法内容
	 */
	function ActiveSet() {
//        $data=DB::table('article')->limit(3)->get();
		//        dump($data->toArray());

	}

	/**
	 * 系统菜单列表
	 */
	function SystemMenu() {
		$db = new \App\Models\Menu();
		$data = $db->getMenuAll();
		return responseToJson($data);
	}

	/**
	 * 菜单
	 */
	function MenuList() {
		$data = config('menu');
		return responseToJson($data);
	}

	function cceshi(){
        dump('start');
        $db=\App\Models\Role::hasRoot('11');
	    dump($db);
    }

}

?>