<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Datatables\Admin\Shop\InvoicePaymentsDataTable;
use App\Http\Controllers\Controller;
use App\Repositories\Shop\InvoicePayments;
use App\Repositories\Shop\Invoices;
use Illuminate\Http\Request;

class InvoicePaymentController extends Controller
{
    public function index(InvoicePaymentsDataTable $dataTable)
    {
        return $dataTable->render('Admin.Shop.InvoicePayments.list');
    }

    public function create($invoice_id)
    {
        $data = InvoicePayments::init();
        $data['invoice_id'] = $invoice_id;

        return view('Admin.Shop.InvoicePayments.form', $data);
    }

    public function store(Request $request)
    {
        $data = $request->all();
        $ret = InvoicePayments::store($data);
        Invoices::checkPayments($data['invoice_id']);

        return redirect()->route('Admin.Shop.Invoices.edit', ['id' => $request->input('invoice_id')]);
    }

    public function show($id)
    {
        $data = [];
        $data['invoice_payment'] = InvoicePayments::getArray($id);

        return view('Admin.Shop.InvoicePayments.view', $data);
    }

    public function edit($id)
    {
        $data = InvoicePayments::init();
        $data['invoice_payment'] = InvoicePayments::getArray($id);
        $data['invoice_id'] = $data['invoice_payment']['invoice_id'];

        return view('Admin.Shop.InvoicePayments.form', $data);
    }

    public function destroy($id)
    {
        $payment = InvoicePayments::get($id);
        InvoicePayments::destroy($id);
        Invoices::checkPayments($payment->invoice_id);
    }
}
