<?php

namespace App\Http\Controllers\Backend;

use App\Helpers\CoreConstants;
use App\Http\Controllers\Controller;

use App\Models\StoreLocation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class StoreLocationController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['PageTitle'] = 'Store Location';
        $data['parentpageShowClass'] = 'show';
        $data['storeLocationActiveClass'] = 'active';
        $data['storeLocation'] = StoreLocation::all();
        return view('backend.store-location.index')->with($data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['PageTitle'] = 'Store Location';
        $data['parentpageShowClass'] = 'show';
        $data['storeLocationActiveClass'] = 'active';
        return view('backend.store-location.create')->with($data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'store_name' => 'required|string',
            'address' => 'required|string',
        ]);
        $store_location = new StoreLocation();
        $store_location->name = $request->store_name;
        $store_location->address = $request->address;

        if ($request->hasFile('image')) {
            $image = $request->file('image');
            $orginalName = time() . '.' . $image->getClientOriginalName();
            $fileName = pathinfo($orginalName, PATHINFO_FILENAME);
            $extension = pathinfo($orginalName, PATHINFO_EXTENSION);
            $imageName = preg_replace('/\s+/', '', $fileName);
            $file_name = preg_replace('/[^A-Za-z0-9\-]/', '', $imageName);
            $name = $file_name . '.' . $extension;
            $store = $image->storeAs('upload', $name, 'public');
            $store_location->image = $store;
        }

        $store_location->save();
        return redirect()->route('admin.store-location.index')->with('success', CoreConstants::CREATED_SUCCESSFULLY);
    }

    /**
     * Display the specified resource.
     */
    public function show(StoreLocation $storeLocation)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(StoreLocation $storeLocation)
    {
        $data['PageTitle'] = 'Store Location';
        $data['parentpageShowClass'] = 'show';
        $data['storeLocationActiveClass'] = 'active';
        $data['storeLocation'] = $storeLocation;
        return view('backend.store-location.edit')->with($data);

    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, StoreLocation $storeLocation)
    {
        $request->validate([
            'store_name' => 'required|string',
            'address' => 'required|string',
        ]);
        $storeLocation->name = $request->store_name;
        $storeLocation->address = $request->address;
        
        if ($request->hasFile('image')) {
            $image = $request->file('image');
            $orginalName = time() . '.' . $image->getClientOriginalName();
            $fileName = pathinfo($orginalName, PATHINFO_FILENAME);
            $extension = pathinfo($orginalName, PATHINFO_EXTENSION);
            $imageName = preg_replace('/\s+/', '', $fileName);
            $file_name = preg_replace('/[^A-Za-z0-9\-]/', '', $imageName);
            $name = $file_name . '.' . $extension;
            $store = $image->storeAs('upload', $name, 'public');
            if ($storeLocation->image && Storage::disk('public')->exists($storeLocation->image)) {
                Storage::disk('public')->delete($storeLocation->image);
            }
            $storeLocation->image = $store;
        }
        $storeLocation->save();

        return redirect()->route('admin.store-location.index')->with('success', CoreConstants::UPDATED_SUCCESSFULLY);

    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(StoreLocation $storeLocation)
    {
        $storeLocation->delete();
        return redirect()->route('admin.store-location.index')->with('success', CoreConstants::DELETED_SUCCESSFULLY);
    }
}
