<?php

namespace App\Http\Controllers;

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

class CancelReasonController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $cancelReasons = CancelReason::get();
        return view('clients.newclient.cancelReasons.index', [
            'page_title' => 'Cancel Reasons',
            'cancelReasons' => $cancelReasons,
        ]);
    }

    /**
     * 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)
    {
        $validated = $request->validate([
            'reason' => 'required',
            'status' => 'required',
        ]);

        $cancelReason = CancelReason::create($validated);

        return redirect()->route('cancel-reasons.index')->with('success', 'Cancel Reason created successfully');
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\CancelReason  $cancelReason
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cancelReason = CancelReason::find($id);
        return view('clients.newclient.cancelReasons.edit', [
            'page_title' => 'Edit Cancel Reason',
            'cancelReason' => $cancelReason,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\CancelReason  $cancelReason
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validated = $request->validate([
            'reason' => 'required',
            'status' => 'required',
        ]);

        $cancelReason = CancelReason::find($id);
        $cancelReason->reason = $request->reason;
        $cancelReason->status = $request->status;
        $cancelReason->save();

        return redirect()->route('cancel-reasons.index')->with('success', 'Cancel Reason updated successfully');
    }

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