<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Datatables;
use App\Lead;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class LeadsController extends Controller
{
    public function index()
    {
        $users = User::all();
        return view('leads')->with([
            'users'=> $users
        ]);
    }
    public function leadsList()
    {
        $leads = DB::table('leads')->select('*')->where('status', '<>', 'REMALHO');
        return datatables()->of($leads)
            ->make(true);
    }

    public function edit(Lead $lead)
    {
        return view('leads.edit')->with('lead', $lead);
    }

    public function update(Request $request, Lead $lead)
    {
        $lead->nome = $request->nome;
        $lead->proposta = $request->proposta;
        $lead->venda = $request->venda;
        $lead->bolsao = $request->bolsao;
        $lead->num1assemb = $request->num1assemb;
        $lead->ndn = $request->ndn;
        $lead->nplano = $request->nplano;
        $lead->dddtelres = $request->dddtelres;
        $lead->telresidencial = $request->telresidencial;
        $lead->dddtelcom = $request->dddtelcom;
        $lead->telcom = $request->telcom;
        $lead->dddcel = $request->dddcel;
        $lead->celular = $request->celular;
        $lead->sexo = $request->sexo;
        $lead->estadocivil = $request->estadocivil;
        $lead->orgaoexp = $request->orgaoexp;
        $lead->profissao = $request->profissao;
        $lead->renda = $request->renda;
        $lead->informconta = $request->informconta;
        $lead->contatipo = $request->contatipo;
        $lead->banco = $request->banco;
        $lead->agencia = $request->agencia;
        $lead->conta = $request->conta;
        $lead->status = $request->status;
        $lead->observacoes = $request->observacoes;
        $lead->email = $request->email;
        $lead->bairro = $request->bairro;
        $lead->endereco = $request->endereco;
        $lead->numero = $request->numero;
        $lead->complemento = $request->complemento;
        $lead->divulgar = $request->divulgar;
        $lead->nomeresp1 = $request->nomeresp1;
        $lead->tiporesp1 = $request->tiporesp1;
        $lead->cpfresp1 = $request->cpfresp1;
        $lead->participacao1 = $request->participacao1;
        $lead->nomeresp2 = $request->nomeresp2;
        $lead->tiporesp2 = $request->tiporesp2;
        $lead->cpfresp2 = $request->cpfresp2;
        $lead->participacao2 = $request->participacao2;
        $lead->capitalsubs = $request->capitalsubs;
        $lead->socioben = $request->socioben;
        $lead->plano = $request->plano;
        $lead->segurodevida = $request->segurodevida;
        $lead->prazogrupo = $request->prazogrupo;
        $lead->contrimensal = $request->contrimensal;
        $lead->taxaantecipada = $request->taxaantecipada;
        $lead->taxadiferida = $request->taxadiferida;
        $lead->fundores = $request->fundores;
        $lead->valor1prest = $request->valor1prest;
        $lead->veiculo = $request->veiculo;
        $lead->marca = $request->marca;
        $lead->valorveiculo = $request->valorveiculo;
        $lead->planopadrao = $request->planopadrao;
        $lead->planoreduzido = $request->planoreduzido;
        $lead->produto = $request->produto;
        $lead->celular_prop = $request->celular_prop;
        $lead->telefone2 = $request->telefone2;
        $lead->cpf = $request->cpf;
        $lead->rg = $request->rg;
        $lead->nascimento = $request->nascimento;
        $lead->cep = $request->cep;
        $lead->uf = $request->uf;
        $lead->cidade = $request->cidade;
        $lead->save();

        return back()->with('success','Os dados foram salvos!');
    }

    public function remalho()
    {
        $users = User::all();
        return view('remalho')->with([
            'users'=> $users
        ]);
    }

    public function remalhoList()
    {
        $remalhoList = DB::table('leads')->where('status', 'REMALHO');
        return datatables()->of($remalhoList)
            ->make(true);
    }
    
    public function relatorios()
    {
        $users = User::all();
        $leads = DB::table('leads')->select('*')->where('status', '<>', 'REMALHO');
        
        $consultores = Lead::select('consultor')->distinct()->get();
        $consultores->values()->all();
    
        $statuses = Lead::select('status')->distinct()->get();
        $statuses->values()->all();
    
        $marcas = Lead::select('marca')->distinct()->get();
        $marcas->values()->all();
    
        return view('relatorio', [
            'leads' => $leads,
            'consultores' => $consultores,
            'statuses' => $statuses,
            'marcas' => $marcas,
            'users' => $users
        ]);
    }
    
    public function search(Request $request)
    {
        $consultor = $request->consultor;
        $marca = $request->marca;
        $status = $request->status;
        $periodo = $request->periodo;
    
    
    
        $dates = explode(" ~ ", $periodo );
    
        $date1=  $dates[0];
        $date2=  $dates[1];
    
    
        $leads = DB::table('leads')
            ->when($status, function ($query) use ($status) {
                return $query->where('status', $status);
            })
            ->when($consultor, function ($query) use ($consultor) {
                return $query->where('consultor', $consultor);
            })
            ->when($marca, function ($query) use ($marca) {
                return $query->where('marca', $marca);
            })
            ->when($periodo, function ($query) use ($date1, $date2) {
                return $query->whereBetween('created_at', [$date1, $date2]);
            })
            ->get();
    
        $leads = $leads->unique('nome');
        $leads->values()->all();
    
        return view('result', [
            'leads' => $leads,
            'consultor' => $consultor,
            'marca' => $marca,
            'status' => $status,
            'periodo' => $periodo,
            'date1' => $date1,
            'date2' => $date2
        ]);
    }

}
