<?php

namespace App\Http\Controllers;

use App\Models\Payment;
use \App\Http\Requests\StorePaymentRequest;
use Illuminate\Http\Request;

class PaymentController extends Controller
{
    protected $LogController;
    public function __construct(LogController $LogController)
    {
        $this->LogController = $LogController;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $payments = Payment::all();

        return view('payment.index')->with([
            'page_title' => 'การจัดการค่าจ้าง',
            'payments' => $payments
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('payment.create')->with([
            'page_title' => 'เพิ่มค่าจ้าง'
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StorePaymentRequest $request)
    {
        $payment = new Payment;
        $payment->drive = $request->drive;
        $payment->type = $request->type;
        $payment->gear = $request->gear;
        $payment->cost = $request->cost;
        $payment->save();

        // Log
        $description = "เพิ่มค่าจ้าง: ". $request->type.", เกียร์: ".$request->gear. ", จำนวน: ".$request->cost."/ชม.";
        $this->LogController->log('การจัดการค่าจ้าง', 'เพิ่มข้อมูล', $description);

        return redirect()->route('payment.index')->with('feedback','เพิ่มค่าจ้างเรียบร้อย');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Payment  $payment
     * @return \Illuminate\Http\Response
     */
    public function show(Payment $payment)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Payment  $payment
     * @return \Illuminate\Http\Response
     */
    public function edit(Payment $payment)
    {
        return view('payment.edit', [
            'page_title' => 'แก้ไขค่าจ้าง',
            'payment' => $payment
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Payment  $payment
     * @return \Illuminate\Http\Response
     */
    public function update(StorePaymentRequest $request, Payment $payment)
    {
        $payment->drive = $request->drive;
        $payment->type = $request->type;
        $payment->gear = $request->gear;
        $payment->cost = $request->cost;
        $payment->save();

        // Log
        $description = "แก้ไขค่าจ้าง: ". $request->type.", เกียร์: ".$request->gear. ", จำนวน: ".$request->cost."/ชม.";
        $this->LogController->log('การจัดการค่าจ้าง', 'แก้ไขข้อมูล', $description);

        return redirect()->route('payment.index')->with('feedback','แก้ไขค่าจ้างเรียบร้อย');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Payment  $payment
     * @return \Illuminate\Http\Response
     */
    public function destroy(Payment $payment)
    {
        // Log
        $description = "ลบค่าจ้าง: ". $payment->type.", เกียร์: ".$payment->gear. ", จำนวน: ".$payment->cost."/ชม.";
        $this->LogController->log('การจัดการค่าจ้าง', 'ลบข้อมูล', $description);

        $payment->delete();

        return redirect()->route('payment.index')->with('feedback','ลบค่าจ้างเรียบร้อย');
    }
}
