<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\Models\Proveedor;

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


class ProveedorController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $data = Proveedor::all();

        return $data;

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //

        $cliente = new Proveedor();

        $cliente->name = $request->name;
        $cliente->cuit = $request->cuit;
        $cliente->iibb = $request->iibb; 
        $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];
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $cliente = Proveedor::find($id);

        $cliente->name = $request->name;
        $cliente->cuit = $request->cuit;
        $cliente->iibb = $request->iibb; 
        $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 = Proveedor::find($id);
        $cliente->delete();

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

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

        return $proveedores;            
    }
}
