<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\Models\{Cotizacion, CotizacionItem};
use PDF;

class CotizacionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $data = Cotizacion::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)
    {
        $item = new Cotizacion;

        $item->client_id = $request->cliente;
        $item->fecha_entrega_estimada = $request->fecha_entrega;
        $item->validez = $request->validez;
        $item->moneda = $request->moneda;
        $item->impuesto = $request->impuesto;
        $item->descuento = $request->descuento;
        $item->envio = $request->envio;
        $item->obs = $request->observacion;

        $item->status = 0;

        $item->user_id = auth()->user()->id;

        $item->save();

        $items = $request->items;
        $items = json_decode($items);

        foreach ($items as $citem) {

            $cit = new CotizacionItem;
            $cit->cotizacion_id = $item->id;
            $cit->pieza_id = $citem->id ?? null;
            $cit->name = $citem->name ?? null;
            $cit->qty = $citem->qty;
            $cit->valor = $citem->valor ?? null;
            $cit->subtotal = $citem->qty * $citem->valor ?? 0;
            $cit->descuento = $citem->descuento ?? 0;
            $cit->save();

        }
        


        return ['status' => 'succes', 'data' => $item];
    }

    /**
     * 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)
    {
        //
        $item = Cotizacion::find($id);

        $item->name = $request->name;
        $item->description = $request->description;
        $item->numero = $request->numero;
        $item->tipo = $request->tipo;

        $item->save();

        return ['status' => 'succes', 'data' => $item];
    }

    public function accept($id)
    {
        $item = Cotizacion::find($id);

        $item->status = 3;

        $item->save();

        return ['status' => 'succes', 'data' => $item];
    }

    public function reject($id)
    {
        $item = Cotizacion::find($id);

        $item->status = 4;

        $item->save();

        return ['status' => 'succes', 'data' => $item];
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $item = Cotizacion::find($id);
        $item->delete();
        return ['status' => 'succes', 'data' => $item];
    }

    public function pdf($id)
    {
 
        $data = Cotizacion::with('items')->find($id)->toArray();

        $pdf = PDF::loadView('pdfs.cotizacion' , compact('data'));
        return $pdf->download('cotizacion.pdf');

    }
}
