<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class SettingsController extends Controller
{
    public function settingCreate()
    {
        $content = Setting::query()->first();
        return view('admin.pages.setting', compact('content'));
    }


    public function settingStore(Request $request)
    {
        try {
            $content = Setting::find(1);
            $content->site_title = $request->site_title;
            $content->email = $request->email;
            $content->phone_num = $request->phone_num;
            $content->footer_description = $request->footer_description;
            $content->footer_btm_description = $request->footer_btm_description;
            if ($request->hasFile('header_logo')) {
                $content->clearMediaCollection('header_logo');
                $content->addMediaFromRequest('header_logo')->toMediaCollection('header_logo');
            }

            if ($request->hasFile('footer_logo')) {
                $content->clearMediaCollection('footer_logo');
                $content->addMediaFromRequest('footer_logo')->toMediaCollection('footer_logo');
            }

            if ($request->hasFile('fav_icon')) {
                $content->clearMediaCollection('fav_icon');
                $content->addMediaFromRequest('fav_icon')->toMediaCollection('fav_icon');
            }

            if ($request->hasFile('loader')) {
                $content->clearMediaCollection('loader');
                $content->addMediaFromRequest('loader')->toMediaCollection('loader');
            }
            $content->update();
            return redirect()->back()->with('success', 'Record Successfully Update');
        } catch (\Exception $e) {
            return back()->with('error', $e->getMessage());
        }
    }
}
