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

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Request;
/**
 *
 */
class MessageReadController 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;
        $request['toname'] = isset($req->toname) ? trim($req->toname) : null;
        $request['fromname'] = isset($req->fromname) ? trim($req->fromname) : null;

        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/MessageRead/getArticle');
        $dt->setLang('notice');
        // $dt->addColsFields('id');
        $dt->addColsFields('title');
//        $dt->addColsFields('toname');
        $dt->addColsFields('formname');


         $dt->addColsFields('status',array('sort' => false, 'templet' => '#agent', 'width' => 80));
        $dt->addColsFields('add_time');
//        $dt->addColsFields('message_type');
//        $dt->addColsFields('type');
        $dt->enableCheckBox();
        $arr[] = 'view';
        $dt->setToolBar($arr, array('width' => 140));
        return view('admin.ManagerRead/arictle',$dt->render($request));

    }

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

        $title = Request::has('title') ? Request::get('title') : '';
//        $content = Request::has('content') ? Request::get('content') : '';
        $message_type = Request::has('message_type') ? Request::get('message_type') : '';
        $type = Request::has('type') ? Request::get('type') : '';
//        $forname = Request::has('forname') ? Request::get('forname') : '';
//        $toname = Request::has('toname') ? Request::get('toname') : '';
//        $add_time = Request::has('add_time') ? Request::get('add_time') : '';


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



        $db = new \App\Models\Manager();
        $data = $db->getlist($limit, $where);

        $admin_id=session('adminInfo.admin_id');
        $dbs = new \App\Models\MessageRead;
        $datas=$dbs->getlist($admin_id);
        if (!empty($datas)){

            foreach ($data['data'] as $k=>$v){
                foreach ($datas as $vv){
                    if($vv['type'] == '1' && $v['id'] == $vv['message_id']){

                        $data['data'][$k]['type']='1';
                    }

                }
            }

            foreach ($data['data'] as $k=>$v){
                if($v['type'] == '2'){
                    $dat[$k]=$v;
                }
            }

            if (!empty($dat)){
                $data['data']=  $dat;
            }else{
                $data['data']="";
            }
        }


//        var_dump($dat);

        if (!empty($data['data'])) {
            foreach ($data['data'] as $k=>$v){
                foreach ($datas as $val){
                    if ($v['id'] == $val['message_id']){
                        $data['data'][$k]['status']='1';
                    }

                }
            }
            $langinfo = trans('status.managerread');
            foreach ($data['data'] as $k => $v) {
                $data['data'][$k]['status'] = $langinfo[$v['status']];
            }
        }
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
    }
    function view(Req $req) {
        return $this->MessageView($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\Manager;
        if (!$req->isMethod('post')) {
            $data = $db->getDmsg($id, 1);
            if (!$data) {
                return responseToJson($data);
            }
            return view('admin.ManagerRead/editNotice', ['data' => $data]);
        } else {
            $data['title'] = $req->input('title');
            $data['content'] = $req->desc;
            $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');
        $admin_id=session('adminInfo.admin_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]);
                }
            }
        }

       $ids= explode(",",$id);

        foreach ($ids as $va){
        $rows = new \App\Models\MessageRead;
        $rtt=$rows->Messagelist($va,$admin_id);
        if($rtt > 0){
            $data=[
                'type'=>'1'
            ];
            $rowt=$rows->updateMsg($va,$admin_id,$data);
            if (!$rowt) {
                return responseToJson(-2003); //id
            }
        }else{
            $dataWhwew=[
                'message_id'=>$va,
                'account_identity'=>$admin_id,
                'type'=>'1',
                'add_time'=>date("Y-m-d H:i",time())
            ];
            $dat = $rows->addMessage($dataWhwew);
            if (!$dat) {
                return responseToJson(-2003); //id
            }
          }
        }

        return responseToJson(1); //id
    }

    function addNotice(Req $req){
//        $dataName=array();
        if (!$req->isMethod('post')) {
            return view('admin.Manager/editNotice',['select'=>1]);
        }else{
            $data['title'] = $req->input('title');
            $data['type'] =$req->type;
            $data['content'] = $req->desc;
            $log = array(
                session('adminInfo.admin_name'),
                $data['title']
            );
            $data['add_time']=date('Y-m-d H:i:s');
            $data['formname']=$req->input('formname');
            $data['toname']=$req->input('toname');
//            $data['message_type']=$req->input('message_type');
            $db = new \App\Models\Manager;
            $user = new \App\Models\Account;
            //普通消息
            if ($data['type'] == 2){
                    $dataName=explode(',',$data['toname']);
                    foreach ($dataName as $v){
                        $data['toname']=$v;
                        $res = $db->addNotice($data);
                    }
            }
            //系统消息
            if ($data['type'] == 1){
                $data['formname']='system';
                $data['toname']='all';
                $db->addNotice($data);
            }


            //系统通知
            if ($data['type'] == 3){
                $data['formname']='notice';
                $data['toname']='all';
                $db->addNotice($data);

                }

            //系统通知
            if ($data['type'] == 4){
                $data['formname']='admin';
                $data['toname']='admin_all';
                $db->addNotice($data);

            }


//            $res = $db->addNotice($data);
            OperationLog(session('adminInfo.admin_id'), 'add_notice', $log);
            return responseToJson(1);
        }
    }


    function MessageView(Req $req){
        $id = $req->input('id');
        $admin_id=session('adminInfo.admin_id');
        $where=[
            'id',
            $id
        ];
        $dataWhwew=[
            'message_id'=>$id,
            'account_identity'=>$admin_id,
            'add_time'=>date("Y-m-d H:i",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.ManagerRead/view',['data'=>$data]);
    }
}

?>