<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Models\Transaction;
use App\Models\WarehouseStock;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use LaravelDaily\Invoices\Invoice;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;

class InvoiceSalesTransactionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function invoice($id)
    {
        $response = Transaction::with('transactionproduct')->where('id', $id)->get();
        // dd($response);
        
        $customer = new Buyer([
            'name' => $response[0]['customer'],
            'custom_fields' => [
                'address' => $response[0]['address'],
            ],
        ]);
        
        $items = $response[0]['transactionproduct'];
        $storage = [];
        $storageDiskon = [];
        for($i=0; $i<count($items); $i++) {
            $product = WarehouseStock::with('product')->where('id', $items[$i]['id'])->get();
            // dd($product[0]['product']);
            $item[$i] = (new InvoiceItem())->title($product[0]['product']['nama'])->quantity($items[0]['qty'])->pricePerUnit($product[0]['product']['harga_jual']);
            $diskon[$i] = $items[$i]['diskon'];
            // dd($diskon[$i]);
            array_push($storage, $item[$i]);
            array_push($storageDiskon, $diskon[$i]);
        }
        // dd($storageDiskon);

        // dd($response[0]['created_at']);
        $invoice = Invoice::make('Invoice')
        ->filename($response[0]['customer'] . '-GF-000' . $id)
        ->serialNumberFormat('GF-000' . $response[0]['id'])
        ->buyer($customer)
        ->totalDiscount(array_sum($storageDiskon))
            ->taxRate(0)
            ->shipping($response[0]['biaya_tambahan'])
            ->addItems($storage)
            ->save('public_uploads_invoice');
        
        return $invoice->url();
    }

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

    /**
     * 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)
    {
        //
    }

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