<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Project;
use App\Models\Region;
use Illuminate\Http\Request;

class ProjectController extends Controller
{
    public function index()
    {
        $projects = Project::orderBy('id', 'desc')->get();

        return view('admin.project.index', compact('projects'));
    }

    public function create()
    {
        $regions = Region::all();

        return view('admin.project.create', compact('regions'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'region_id' => 'required',
            'apartments' => 'required',
            'floors' => 'required',
            'card_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
            'background_image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg',
            'logo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg',
            'yard_image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg',
            'hall_image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg',
            'slug' => 'required|unique:projects',
        ]);
        $project = new Project();
        $project->fill($request->post());
        if ($request->post('status') == null) {
            $project->status = 1;
        }
        $project->card_image = $this->uploadImage('card_image', $request);
        $project->background_image = $this->uploadImage('background_image', $request);
        $project->logo = $this->uploadImage('logo', $request);
        $project->yard_image = $this->uploadImage('yard_image', $request);
        $project->hall_image = $this->uploadImage('hall_image', $request);

        $project->save();

        return redirect()->route('admin.projects.index')->with('success', 'project has been created successfully.');
    }

    public function edit(Project $project)
    {
        $regions = Region::all();

        return view('admin.project.edit', compact('project', 'regions'));
    }

    public function update(Request $request, Project $project)
    {
        $request->validate([
            'region_id' => 'required',
            'apartments' => 'required',
            'floors' => 'required',
            'card_image' => 'image|mimes:jpeg,png,jpg,gif,svg',
            'background_image' => 'image|mimes:jpeg,png,jpg,gif,svg',
            'logo' => 'image|mimes:jpeg,png,jpg,gif,svg',
            'yard_image' => 'image|mimes:jpeg,png,jpg,gif,svg',
            'hall_image' => 'image|mimes:jpeg,png,jpg,gif,svg',
        ]);

        $project->fill($request->post());
        if ($request->post('status') == null) {
            $project->status = 1;
        }
        $project->card_image = $this->uploadImage('card_image', $request);
        $project->background_image = $this->uploadImage('background_image', $request);
        $project->logo = $this->uploadImage('logo', $request);
        $project->yard_image = $this->uploadImage('yard_image', $request);
        $project->hall_image = $this->uploadImage('hall_image', $request);

        $project->save();

        return redirect()->route('admin.projects.index')->with('success', 'project Has Been updated successfully');
    }

    public function destroy(Project $project)
    {
        $project->delete();
        return redirect()->route('admin.projects.index')->with('success', 'project has been deleted successfully');
    }

    public function uploadImage($attribute, $request)
    {
        if ($request->file($attribute)) {
            $request->file($attribute)->move(public_path() . '/uploads/images/', $request->file($attribute)->getClientOriginalName());

            return $request->file($attribute)->getClientOriginalName();
        }
    }
}
