<?php

namespace App\Http\Controllers;

use App\Http\Requests\create_apply_request;
use App\Models\Apply;
use Illuminate\Support\Facades\Auth;

class ApplyController extends Controller
{
    public function addapply(create_apply_request $request)
    {
        $ap = new Apply();
        $ap->name = $request->name;
        $ap->number = $request->number;
        $ap->email = $request->email;
        $ap->address = $request->address;
        $ap->date = $request->date;
        $ap->time = $request->time;
        $ap->paymethod = $request->paymethod;
        $ap->category_id = $request->category_id;
        $ap->user_id = Auth::id();
        $ap->customcategory = $request->customcategory;
        $ap->save();
        return redirect()->route('myapply');
    }

    public function addapplyAPI(create_apply_request $request)
    {
        $ap = new Apply();
        $ap->name = $request->name;
        $ap->number = $request->number;
        $ap->email = $request->email;
        $ap->address = $request->address;
        $ap->date = $request->date;
        $ap->time = $request->time;
        $ap->paymethod = $request->paymethod;
        $ap->category_id = $request->category_id;
        $ap->user_id = Auth::id();
        $ap->customcategory = $request->customcategory;
        $ap->save();
        return ['success' => 'Заявка создана'];
    }

    public function myapply()
    {
        $myaps = Apply::with('category')->where('user_id', Auth::id())->orderBy('created_at', 'desc')->get();

        return view('myApply', ['myaps' => $myaps]);
    }

    public function myapplyAPI()
    {
        $myaps = Apply::with('category')->where('user_id', Auth::id())->orderBy('created_at', 'desc')->get();

        return ['myaps' => $myaps];
    }
}
