<?php

namespace App\Http\Controllers\Admin;

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

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

    //remover relacionamento de segmento com solucao
    public function delete(Request $request )
    {
        
        $solucaos = Solucao::find($request['solucao']);
        $segmento = Segmento::find($request['segmento']);
        $solucaos->removeSegmento($segmento);
        $obj = Solucao::with('segmentos')->find($request['solucao']);
        
        $output = '<table class="table table-striped">
                    <thead>
                        <tr>
                            <th> Id </th>
                            <th> Título </th>
                            <th> Ações </th>
                        </tr>
                    </thead>
                    <tbody>';
        if(count($obj->segmentos) > 0) {
            foreach($obj->segmentos as $row) {
                    $output .= '
                    
                    <tr>
                    <td>'.$row->id.'</td>
                    <td>'.$row->name.'</td>
                    <td><form action='. route('solucao.segmento.del') .'
                    method="post" class="">
                    '. method_field('DELETE') .'
                    '. csrf_field() .'
                    <button title="Deletar" class="btn btn-icons btn-inverse-danger  segmentoSolucaoDel" 
                        data-token="'. csrf_token() .'" data-solucao="'.$request['solucao'].'" data-segmento="'.$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 delete successfully',
            'output' => $output,
        ); 
        return $response;

    }    

    
   
    
}
