public function index()
 {
     if (Auth::user()->typeuser_id == 1) {
         $messages = AdvertMessage::orderBy('created_at', 'desc')->paginate(30);
         return view('admin.mensagens.index', compact('messages'));
     } else {
         $user = Auth::user();
         $messages = AdvertMessage::where('id_user', $user->id)->orderBy('visto', 'asc')->paginate(30);
         return view('admin.mensagens.index', compact('messages'));
     }
 }
 public function dadosPainelAdm()
 {
     /*
                     $qntVerAnunciosTodos = Advert::sum('advert_count');
                     $qntVerAnunciosImoveis = Advert::select('adverts.advert_count')->join('subcategories','adverts.subcategories_id','=','subcategories.id')
                                              ->where('subcategories.category_id','=','1')->sum('advert_count');
                     $qntVerAnunciosVeiculos = Advert::select('adverts.advert_count')->join('subcategories','adverts.subcategories_id','=','subcategories.id')
                                               ->where('subcategories.category_id','=','2')->sum('advert_count');
                     $qntVerTelefone = Advert::sum('tel_count');
                     $qntFinanciamento = Advert::sum('fin_count');
                     $qntEmailContato = AdvertMessage::count();
                     $qntAnunciosGratis = Advert::where('destaque', '=', '0')->count();
                     $qntAnunciosPagos = Advert::where('destaque', '!=', '0')->count();
             
                     return view('admin.principal.adm',compact('qntVerAnunciosTodos','qntVerAnunciosImoveis', 'qntVerAnunciosVeiculos',
                                                               'qntVerTelefone', 'qntFinanciamento', 'qntEmailContato', 'qntAnunciosGratis',
                                                               'qntAnunciosPagos')); */
     $timezone = new DateTime('now');
     $dia = $timezone->format("d");
     $mes = $timezone->format("m");
     $ano = $timezone->format("Y");
     $dados = DadosAdmin::where('ano', '=', $ano)->where('mes', '=', $mes)->where('dia', '=', $dia)->first();
     if (!empty($dados)) {
         $qntVerAnunciosTodos = $dados->totalCliques;
         $qntVerAnunciosImoveis = $dados->cliquesImoveis;
         $qntVerAnunciosVeiculos = $dados->cliquesVeiculos;
         $qntVerTelefone = $dados->cliquesVerTelefone;
         $qntFinanciamento = $dados->cliquesFinanciamento;
         $qntEmailContato = $dados->emailsContato;
         $qntAnunciosGratis = $dados->anunciosGratis;
         $qntAnunciosPagos = $dados->anunciosPagos;
         return view('admin.principal.adm', compact('qntVerAnunciosTodos', 'qntVerAnunciosImoveis', 'qntVerAnunciosVeiculos', 'qntVerTelefone', 'qntFinanciamento', 'qntEmailContato', 'qntAnunciosGratis', 'qntAnunciosPagos'));
     } else {
         $qntVerAnunciosTodos = Advert::sum('advert_count');
         $qntVerAnunciosImoveis = Advert::select('adverts.advert_count')->join('subcategories', 'adverts.subcategories_id', '=', 'subcategories.id')->where('subcategories.category_id', '=', '1')->sum('advert_count');
         $qntVerAnunciosVeiculos = Advert::select('adverts.advert_count')->join('subcategories', 'adverts.subcategories_id', '=', 'subcategories.id')->where('subcategories.category_id', '=', '2')->sum('advert_count');
         $qntVerTelefone = Advert::sum('tel_count');
         $qntFinanciamento = Advert::sum('fin_count');
         $qntEmailContato = AdvertMessage::count();
         $qntAnunciosGratis = Advert::where('destaque', '=', '0')->count();
         $qntAnunciosPagos = Advert::where('destaque', '!=', '0')->count();
         DadosAdmin::create(['dia' => $dia, 'mes' => $mes, 'ano' => $ano, 'totalCliques' => $qntVerAnunciosTodos, 'cliquesImoveis' => $qntVerAnunciosImoveis, 'cliquesVeiculos' => $qntVerAnunciosVeiculos, 'cliquesVerTelefone' => $qntVerTelefone, 'cliquesFinanciamento' => $qntFinanciamento, 'emailsContato' => $qntEmailContato, 'anunciosGratis' => $qntAnunciosGratis, 'anunciosPagos' => $qntAnunciosPagos]);
         return view('admin.principal.adm', compact('qntVerAnunciosTodos', 'qntVerAnunciosImoveis', 'qntVerAnunciosVeiculos', 'qntVerTelefone', 'qntFinanciamento', 'qntEmailContato', 'qntAnunciosGratis', 'qntAnunciosPagos'));
     }
 }
 public function formContato()
 {
     $inputData = Input::get('formData');
     parse_str($inputData, $formFields);
     $userData = array('id_user' => $formFields['id_user'], 'url_site' => $formFields['url_site'], 'nome_usuario' => $formFields['nome_usuario'], 'email_usuario' => $formFields['email_usuario'], 'telefone_usuario' => $formFields['telefone_usuario'], 'nome' => $formFields['nome'], 'email' => $formFields['email'], 'codigo_area' => $formFields['codigo_area'], 'telefone' => $formFields['telefone'], 'mensagem' => $formFields['mensagem']);
     if (empty($userData['email_usuario'])) {
         $userData['email_usuario'] = "*****@*****.**";
     }
     $rules = array('email_usuario' => 'required', 'mensagem' => 'required');
     $validator = Validator::make($userData, $rules);
     if ($validator->fails()) {
         return Response::json(array('fail' => true, 'errors' => $validator->getMessageBag()->toArray()));
     } else {
         \Mail::send('emails.contactAnunciante', $userData, function ($message) use($userData) {
             $message->from('*****@*****.**', 'Sempre da Negócio');
             $message->to($userData['email_usuario']);
             $message->subject($userData['nome'] . ', enviou uma mensagem para você ');
             $message->replyTo($userData['email_usuario'], $userData['nome']);
         });
         if (AdvertMessage::create($userData)) {
             //return success  message
             return Response::json(array('success' => true));
         }
     }
 }