<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Log;
use App\Segmento;
use App\Cliente;

class ClienteSegmentoController extends Controller
{
    /* public function __construct()
    {   
         $this->middleware('auth');
    } */

    //remover relacionamento de cliente com segmento
    public function delete(Request $request )
    {
        
        $segmentos = Segmento::find($request['segmento']);
        $cliente = Cliente::find($request['cliente']);
        $segmentos->removeCliente($cliente);
        $obj = Segmento::with('clientes')->find($request['segmento']);
        
        $output = '<table class="table table-striped">
                    <thead>
                        <tr>
                            <th> Id </th>
                            <th> Nome </th>
                            <th> Ações </th>
                        </tr>
                    </thead>
                    <tbody>';
        if(count($obj->clientes) > 0) {
            foreach($obj->clientes as $row) {
                    $output .= '
                    
                    <tr>
                    <td>'.$row->id.'</td>
                    <td>'.$row->name.'</td>
                    <td><form action='. route('segmento.cliente.del') .'
                    method="post" class=" ">
                    '. method_field('DELETE') .'
                    '. csrf_field() .'
                    <button title="Deletar" class="btn btn-icons btn-inverse-danger clienteSegmentoDel" data-token="'. csrf_token() .'" 
                        data-segmento="'.$request['segmento'].'" data-cliente="'.$row->id.'">
                        <i class="mdi mdi-delete "  ></i></button>
                </form></td>
                    </tr>
                    ';
            }

        } else {
            $output .= '<tr>
                            <td colspan="6">Nenhum registro encontrado.</td>
                        </tr>';
        }
        $output .= '</tbody></table>';


        $response = array(
            'status' => 'success',
            'msg' => 'Setting created successfully',
            'output' => $output,
        ); 
        return $response;

    }    

    
}
