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

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Request;
/**
 *
 */
class BannerController extends Controller {

    /**
     * 广告
     */
    function Index(Req $req) {

        $request=array();

        $request['banner_link'] = isset($req->banner_link) ? trim($req->banner_link) : null;
        $request['banner_name'] = isset($req->banner_name) ? trim($req->banner_name) : null;
        $request['banner_url'] = isset($req->banner_url) ? trim($req->banner_url) : null;
        $request['advert_id'] = isset($req->advert_id) ? trim($req->advert_id) : null;
        $request['add_time'] = isset($req->add_time) ? trim($req->add_time) : null;
        //$request['status'] = isset($req->status) ? trim($req->status) : null;
        $request['type'] = isset($req->type) ? trim($req->type) : null;
        $id = isset($req->id) ? trim($req->id) : null;

        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/Banner/getArticle?ids='.$id.'&type='.$request['type']);
        $dt->setLang('notice');
        $dt->addColsFields('banner_link');
        $dt->addColsFields('banner_name');
        $dt->addColsFields('banner_url');
        $dt->addColsFields('advert_id');
        $dt->addColsFields('add_time');
        $dt->addColsFields('sort');
        $dt->addColsFields('status');
        $dt->enableCheckBox();
        $dt->addColsFields('chakan', array('templet' => '#waterTool', 'sort' => false, 'width' => 110));
        return view('admin.Banner/arictle',$dt->render($request));
    }

    //获取信息
    //获取活动信息
    function getArticle(Req $req) {
        $limit = Request::has('limit') ? Request::get('limit') : 10;
        $banner_name = Request::has('banner_name') ? Request::get('banner_name') : '';
        $id = Request::has('ids') ? Request::get('ids') : '';
        $type = Request::has('type') ? Request::get('type') : '';
        $status = Request::has('status') ? Request::get('status') : '';

        $where = array();
        if (!empty($banner_name)) {
            $where[] = array('banner_name', 'like', '%' . $banner_name . '%');
        }
        if (!empty($type)) {
            $where[] = array('type', '=', $type);
        }
        if (!empty($status)) {
            $where[] = array('status', '=', $status);
        }
        //dump($where);exit;
        $db = new \App\Models\Banner();
        $data = $db->getlist($limit, $where);

        if (!($data < 0)) {
            $langinfo = trans('status.advert');
            $langstatus = trans('status.system_user.status');
            foreach ($data['data'] as $k => $v) {
                $data['data'][$k]['advert_id'] = $langinfo[$v['type']];
                //$data['data'][$k]['status'] = $langstatus[$v['status']];
            }
        }
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
    }
    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);
        }
        $db = new \App\Models\Banner;
        if (!$req->isMethod('post')) {
            $data = $db->getDmsg($id, 1);
            if (!$data) {
                return responseToJson($data);
            }
            return view('admin.Banner/editNotice', ['data' => $data]);
        } else {
            $data['banner_link'] = $req->input('banner_link');
            $data['banner_name'] = $req->input('banner_name');
            $data['banner_url'] = $req->input('banner_url');
            $data['sort'] = $req->input('sort');
            $data['status'] = $req->input('status');
            $data['type'] = $req->input('type');
            $res = $db->updateMsg($data, $id);
//            $log = array(
//                session('adminInfo.admin_name'),
//            );
//            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\Banner::whereIn('id', $ids)->delete();
        if (!$rows) {
            return responseToJson(-2003); //id����
        }
        return responseToJson(1); //id����
    }

    function addNotice(Req $req){

        if (!$req->isMethod('post')) {
            $type = Request::has('type') ? Request::get('type') : '';
            return view('admin.Banner/editNotice',['select'=>1,'type'=>$type]);
        }else{
            $data['banner_link'] = $req->input('banner_link');
            $data['identity'] = UUID();
            $data['banner_name'] = $req->input('banner_name');
            $data['banner_url'] = $req->input('banner_url');
            $data['status'] = $req->input('status');
            $data['sort'] = $req->input('sort');
            $data['type'] = $req->input('type');
            $data['advert_id'] = $req->input('id');
            $log = array(
                session('adminInfo.admin_name'),
                $data['banner_name']
            );
            $data['add_time']=date('Y-m-d H:i:s');

            $db = new \App\Models\Banner;

            $res = $db->addNotice($data);

            OperationLog(session('adminInfo.admin_id'), 'add_notice', $log);
            return responseToJson(1);
        }
    }


    function MessageView(){
        $id = Request::has('id') ? Request::get('id') : '';
        $admin_id=session('adminInfo.admin_id');
        $where=[
            'id',
            $id
        ];
        $dataWhwew=[
            'message_id'=>$id,
            'account_identity'=>$admin_id,
//            'add_time'=>time()
        ];
        $db = new \App\Models\Manager();
        $dbRead = new \App\Models\MessageRead;
        $data = $db->getlistd($where);
        $dataRead = $dbRead->Messagelist($id,$admin_id);
        if ($dataRead < 0){
            $dat = $dbRead->addMessage($dataWhwew);
        }
        return view('admin.Manager/view',['data'=>$data]);
    }
}

?>