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

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

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

        $request=array();

        $request['title'] = isset($req->title) ? trim($req->title) : null;
        $request['type'] = isset($req->type) ? trim($req->type) : null;
        $request['status'] = isset($req->status) ? trim($req->status) : null;


        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/Advert/getArticle');
        $dt->setLang('notice');
        $dt->addColsFields('title');
        $dt->addColsFields('type');
        $dt->addColsFields('status');
        $dt->enableCheckBox();
        $dt->addColsFields('chakan', array('templet' => '#waterTool', 'sort' => false, 'width' => 155));
        return view('admin.Advert/arictle',$dt->render($request));
    }

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

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

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

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

        $db = new \App\Models\Advert();
        $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]['type'] = $langinfo[$v['type']];
                $data['data'][$k]['status'] = $langstatus[$v['status']];
                $data['data'][$k]['typeid'] = $v['type'];
            }
        }
        $result=\App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);

        if (empty($result)){
            return '';
        }
        return $result;

    }
    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\Advert;
        if (!$req->isMethod('post')) {
            $data = $db->getDmsg($id, 1);
            if (!$data) {
                return responseToJson($data);
            }
            return view('admin.Advert/editNotice', ['data' => $data]);
        } else {
            $data['title'] = $req->input('title');
            $data['type'] = $req->input('type');
            $data['status'] = $req->input('status');
            $res = $db->updateMsg($data, $id);
           $log = array(
               session('adminInfo.admin_name'),
               $data['title']
           );
           OperationLog(session('adminInfo.admin_id'), 'edit_adv', $log);
            return responseToJson(1);
        }
    }

    /**
     * 批量删除
     * [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\Advert::whereIn('id', $ids)->delete();
        if (!$rows) {
            return responseToJson(-2003); //id����
        }
        $log = array(
            session('adminInfo.admin_name'),
        );
        OperationLog(session('adminInfo.admin_id'), 'de_notice', $log);
        return responseToJson(1); //id����
    }

    function addNotice(Req $req){

        if (!$req->isMethod('post')) {
            return view('admin.Advert/editNotice',['select'=>1]);
        }else{
            $data['title'] = $req->input('title');
            $data['type'] =$req->type;
            $data['status'] =$req->status;
            $log = array(
                session('adminInfo.admin_name'),
                $data['title']
            );
            $data['add_time']=date('Y-m-d H:i:s');

            $db = new \App\Models\Advert;

            $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]);
    }
}

?>
