<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Repositories\ColourRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class ColourController extends Controller
{
    protected $colourRepo;
    public function __construct(colourRepository $colourRepository)
    {
        $this->colourRepo = $colourRepository;
    }


    public function index()
    {

        return $this->colourRepo->indexPage();
    }

    public function createPage()
    {
        return $this->colourRepo->createPage();

    }

    public function edit($id)
    {
        return $this->colourRepo->editPage($id);

    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|max:255',
        ]);
        try {
            $this->colourRepo->create($request->all());
            return redirect()->route('admin.colours.list')->with('success', 'Colour Successfully Added');
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }

    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => 'required|max:255',
        ]);
        try {
            $this->colourRepo->update($request->all(), $id);
            return redirect()->route('admin.colours.list')->with('success', 'Colour Successfully Updated');
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());

        }

    }

    public function delete(Request $request)
    {
        $this->colourRepo->delete($request->id);
        return response()->json(['success' => 'Colour and related products successfully deleted!']);

    }

}
