<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\Models\{Cliente, Contacto, LugarEntrega};

use App\Imports\ClientsImport;
use Maatwebsite\Excel\Facades\Excel;


class ClienteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $clientes = Cliente::with('tipo')->get();

        return $clientes;

    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {
        // Validar Campos Obligatorios Faltantes
        $cliente = Cliente::create($request->all());
        
        if ($request->delivery_data) {
            $this->saveDeliveryData($cliente, $request->delivery_data);
        }

        if ($request->contacts) {
            $this->saveContactData($cliente, $request->contacts);
        }

        
        /*
        $cliente = new Cliente();
        $cliente->name = $request->name;
        $cliente->cuit = $request->cuit;
        $cliente->iibb = $request->iibb;
        $cliente->type_client = $request->type_client;
        $cliente->email = $request->email;
        $cliente->address = $request->address;
        $cliente->location = $request->location;
        $cliente->postalcode = $request->postalcode;
        $cliente->province = $request->province;
        $cliente->web = $request->web;
        $cliente->phone = $request->phone;
        $cliente->contact = $request->contact;
        $cliente->save();
        */

        return ['status' => 'success', 'cliente' => $cliente];
    }

    public function show($id)
    {
        $cliente = Cliente::with('tipo')->with('lugares_entrega')->with('contactos')->find($id);

        return $cliente;
    }

    public function edit($id)
    {
    }

    private function saveDeliveryData ($cliente, $data)
    {
        $deliveries = new LugarEntrega();
        $deliveries->where('cliente_id', $cliente->id)->delete();
        foreach ($data as $item) {
            $deliveries->create([
                'cliente_id' => $cliente->id,
                'code' => $item['code'],
                'description' => $item['description'],
                'place' => $item['place'],
                'locality' => $item['locality'],
                'province' => $item['province'],
                'zipcode' => $item['zipcode'],
                'schedule' => $item['schedule']
            ]);
        }
    }

    private function saveContactData ($cliente, $data)
    {
        $contacts = new Contacto();
        $contacts->where('cliente_id', $cliente->id)->delete();
        foreach ($data as $item) {
            $contacts->create([
                'cliente_id' => $cliente->id,
                'name' => $item['name'],
                'email' => $item['email'],
                'phone' => $item['phone'],
                'cell' => $item['cell'],
                'role' => $item['role'],
                'other' => $item['other'],
            ]);
        }
    }

    public function update(Request $request, $id)
    {
        $cliente = (new Cliente)->find($id);
        if (!$cliente) {
            return ['status' => 'error', 'message' => 'Cliente no existe!'];
        }

        $cliente->fill($request->all())->save();

        if ($request->delivery_data) {
            $this->saveDeliveryData($cliente, $request->delivery_data);
        }

        if ($request->contacts) {
            $this->saveContactData($cliente, $request->contacts);
        }

        /*
        $cliente = Cliente::find($id);
        $cliente->name = $request->name;
        $cliente->cuit = $request->cuit;
        $cliente->iibb = $request->iibb;
        $cliente->type_client = $request->type_client;
        $cliente->email = $request->email;
        $cliente->address = $request->address;
        $cliente->location = $request->location;
        $cliente->postalcode = $request->postalcode;
        $cliente->province = $request->province;
        $cliente->web = $request->web;
        $cliente->phone = $request->phone;
        $cliente->contact = $request->contact;
        $cliente->save();
        */

        return ['status' => 'success', 'cliente' => $cliente];
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //

        $cliente = Cliente::find($id);
        $cliente->delete();

        $deliveries = (new LugarEntrega)->where('cliente_id', $id)->delete();
        $contacts = (new Contacto)->where('cliente_id', $id)->delete();

        return ['status' => 'success', 'data' => $cliente];
    }

    public function importClient(Request $request) 
    {
        if (! $request->file) {
            return ['status' => 'error', 'message' => 'Debe indicar el archivo a importar'];
        }
        Excel::import(new ClientsImport, $request->file); 
        $clientes = Cliente::all();

        return $clientes;            
    }
}
