<?php

namespace App\Http\Controllers;

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

class PackageController extends Controller
{
    public function index()
    {
        $packages = Package::all();
        return view('packages.index', compact('packages'));
    }

    public function create()
    {
        return view('packages.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'type'=>'required',
            'duration'=>'required|integer',
            'price'=>'required|integer',
            'admission_fee'=>'nullable|integer',
            'personal_training_fee'=>'nullable|integer'
        ]);

        Package::create($data);

        return back()->with('success','Package added!');
    }

    public function edit(Package $package)
    {
        return view('packages.edit', compact('package'));
    }

    public function update(Request $request, Package $package)
    {
        $data = $request->validate([
            'type'=>'required',
            'duration'=>'required|integer',
            'price'=>'required|integer',
            'admission_fee'=>'nullable|integer',
            'personal_training_fee'=>'nullable|integer'
        ]);

        $package->update($data);

        return back()->with('success','Package updated!');
    }

    public function destroy(Package $package)
    {
        $package->delete();
        return back()->with('success','Package deleted!');
    }
}
