<?php
/**
 * 系统设置
 */

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class NoticeController extends Controller{

    /**
     * 活动公告
     */
    function Index(Req $req){
        $request['title'] = isset($req->title) ? trim($req->title) : null;
        $request['type'] = isset($req->type) ? trim($req->type) : null;

        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/System/getArticle');
        $dt->setLang('notice');
         $dt->addColsFields('id',array('width' => 50));
        $dt->addColsFields('title', array('templet' => '#title', 'sort' => false, 'width' => 120));
        // $dt->addColsFields('content');
        $dt->addColsFields('time');
        $dt->addColsFields('type');
        $dt->addColsFields('typebind');
        $dt->addColsFields('sort',array('width' => 120));
        $dt->addColsFields('status', array('templet' => '#closeTool', 'sort' => false, 'width' => 120));
        $dt->enableCheckBox();
        $arr[] = 'view';
        if (checkRriv('/admin/Notice/Edit')) {
            $arr[] = 'edit';
        }
        $dt->setToolBar($arr, array('width' => 140));
        return view('admin.systemSet/arictle', $dt->render($request));
    }

    function view(Req $req){
        return $this->Edit($req);
    }

    function Edit(Req $req){
        $id = $req->id;
//        $id=Request::has('id')?Request::get('id'):'';
		if (intval($id) < 1) {
			abort(404);
		}
        $acticle_type   =   new  ArticleType();
		$db = new \App\Models\Article;
		if (!$req->isMethod('post')) {
			$data = $db->getDmsg($id, 1);
			if (!$data) {
				return responseToJson($data);
			}
			$datas = new \App\Models\LotteryMoney();
        	$arr = $datas->getlist();
//        	$admin = \App\Model\TypeName::getAllType();
        $selectdata = $acticle_type->type();
            $_selectdata =   $this->getTree($selectdata);
            $select =    $data["type"];
            $child_id   =   $data["child_id"];
            //查出优惠活动的子类
            $show_hui   =   $this->getTree($selectdata,3);
            $show_hui   =   array_column($show_hui,"id");
			return view('admin.systemSet/editNotice', ['select'=>intval($select),'data' => $data,'show_hui'=>json_encode($show_hui),/*'admin'=>$admin,*/'list'=>1,'arr'=>$arr['data'],"child_id"=>$child_id,"selectdata"=>$_selectdata]);
		} else {
            $data['child_id'] =$req->child_id;
			$data['title'] = $req->input('title');
			$data['author'] = $req->input('author');
			$data['sort'] = $req->input('sort');
			$data['type'] = $req->input('type');
			$data['img'] = $req->input('img');
			$data['typebind'] = $req->input('typebind');
            $data['mobilecontent'] = $req->mobilecontent;
            $data['mobileimg'] = $req->input('mobileimg');
			$data['content'] = $req->desc;
			$res = $db->updateMsg($data, $id);
			$log = array(
				session('adminInfo.admin_name'),
				$data['title']
			);
			OperationLog(session('adminInfo.admin_id'), 'edit_game', $log);
			return responseToJson($res);
		}
	}

	/**
	 * 批量删除
	 * [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\Article::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id����
		}
		return responseToJson(1); //id����
	}

	function addNotice(Req $req){
        $acticle_type = new ArticleType();
        if (!$req->isMethod('post')) {
        	$datas = new \App\Models\LotteryMoney();
        	$arr = $datas->getlist();

        //	$admin = \App\Model\TypeName::getAllType();
            $selectdata = $acticle_type->type();

            //查出优惠活动的子类
            $show_hui   =   $this->getTree($selectdata,3);
            $show_hui   =   array_column($show_hui,"id");

            return view('admin.systemSet/editNotice',['select'=>0,/*'admin'=>$admin,*/'show_hui'=>json_encode($show_hui),'arr'=>$arr['data'],"child_id"=>0,"selectdata" => $this->getTree($selectdata)]);
        }else{
            $data['title'] = $req->input('title');
            $data['img'] = $req->input('img');
            $data['sort'] = $req->input('sort');
            $data['typebind'] = $req->input('typebind');
            $data['mobilecontent'] = $req->mobilecontent;
            $data['mobileimg'] = $req->input('mobileimg');
            $data['type'] =$req->type;
            $data['child_id'] =$req->child_id;
            $data['content'] = $req->desc;
            $log = array(
                session('adminInfo.admin_name'),
                $data['title']
            );


            $data['time'] = date('Y-m-d H:i:s');
            $data['author'] = session('adminInfo.admin_name');
            $data['identity'] = UUID();
            $db = new \App\Models\Article;
            $res = $db->addNotice($data);
            OperationLog(session('adminInfo.admin_id'), 'add_notice', $log);
            return responseToJson(1);
        }
    }

    function upImg(Req $req){
        if (!$req->hasFile('file')) {
            return responseToJson(-5050010022);
        }
        $img = $req->file('file');
        $newFileName = md5(time() . rand(0, 10000)) . '.' . $img->getClientOriginalExtension();
        if (!$img->isValid()) {
            return responseToJson(-5050010122);
        }
        if (file_exists(public_path('/home/img') . $img->getClientOriginalName())) {
            return responseToJson(-5050012122);
        }
        $newPath = public_path('/home/img');
        $res = $img->move($newPath, $img->getClientOriginalName());
        if ($res) {
            $retarr = array(
                'code' => 0,
                'msg' => 'success',
                'data' => array(
                    'src' => $res->getPathname(),
                    'title' => $img->getClientOriginalName(),
                )
            );
            echo json_encode($retarr);
        } else {
            return responseToJson(-5050010122);
        }
    }

    private function getTree($array, $pid = 0, $cen = 0)
    {
        $arr = array();
        $cen = ++$cen;
        $str = "";
        foreach ($array as $k => $v) {
            if ($v['parent_id'] == $pid) {
                $str = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $cen - 1);
                $arr[] = ["id" => $v["id"], "name" => ($cen - 1 ? $str . "└" . $v["cate_name"] : $v["cate_name"])];
                unset($array[$k]);
                $arr = array_merge($arr, $this->getTree($array, $v['id'], $cen));
            }
        }
        return $arr;
    }

    /**
     * 关闭游戏接口
     */
    function activeClose(Req $req) {
        $status = $req->has('status') ? $req->get('status') : '';
        $active_id = $req->has('id') ? $req->get('id') : '';


        if (empty($active_id)) {
            return responseToJson(-5030002031);
        }
        if ($status < 0) {
            return responseToJson(-5030002032);
        }

        $lottery = new \App\Models\Article();
        $data = array(
            'status' => $status,
        );
        if ($status) {
            $data['status'] = 1;
        } else {
            $data['status'] = '0';
        }
        $res = $lottery->closeGame(["id"=>intval($active_id)],$data );
        return responseToJson($res);
    }

}

?>