<?php

namespace Modules\Admin\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

use Modules\Main\Models\Design;

class AdminDesignController extends Controller
{
    public function index(Request $request)
    {
        return view('admin::design.index', [
            'parameters' => Design::getParameters()
        ]);
    }

    public function update(Request $request)
    {
        
        $validated = $request->validate([
            'logo' => 'mimes:jpeg,png,jpg,gif,svg'
        ]);

        if ($request->file('logo')) {
            $logoPath = $request->file('logo')->store('design', ['disk' => 'public']);
            $request->logo = $logoPath;
        }

        foreach (Design::parameters as $parameter) {
            if ( !isset($request->$parameter) || !$request->$parameter ) {
                if ($parameter != 'logo') {
                    Design::where('parameter', $parameter)->delete();
                }
            } else {
                Design::updateOrCreate([
                    'parameter' => $parameter,
                    'value' => $request->$parameter
                ]);
            }
        }
        return to_route('admin.design');
    }

}
