<?php

namespace Rvsitebuilder\Wysiwyg\Http\Controllers\Backend;

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Lib\Util\RVLog;
use Rvsitebuilder\Core\Models\CoreSetting;
use Rvsitebuilder\Wysiwyg\Http\Requests\SaveBacktotop;
use Rvsitebuilder\Wysiwyg\Http\Requests\SaveBacktotopRequest;
use Rvsitebuilder\Wysiwyg\Http\Requests\SaveBrandRequest;
use Rvsitebuilder\Wysiwyg\Http\Requests\SetHideMenu;
use Rvsitebuilder\Wysiwyg\Http\Requests\SetHideMenuRequest;

class SettingController extends Controller
{
    public function saveBrand(SaveBrandRequest $request)
    {
        try {
            DB::beginTransaction();

            CoreSetting::updateOrCreate(['key' => 'tel'], ['value' => $request->input('tel')]);
            CoreSetting::updateOrCreate(['key' => 'logo'], ['value' => $request->input('logo')]);
            CoreSetting::updateOrCreate(['key' => 'logo_mobile'], ['value' => $request->input('logo_mobile')]);
            CoreSetting::updateOrCreate(['key' => 'logo_linkto_homepage'], ['value' => $request->input('linktohomepage')]);
            CoreSetting::updateOrCreate(['key' => 'site_brand'], ['value' => $request->input('sitebrand')]);
            CoreSetting::updateOrCreate(['key' => 'email'], ['value' => $request->input('email')]);
            CoreSetting::updateOrCreate(['key' => 'title_contact'], ['value' => $request->input('titlecontact')]);
            CoreSetting::updateOrCreate(['key' => 'address'], ['value' => $request->input('address')]);
            //  query process
            DB::commit();

            $response = [
                'status' => 'success',
                'messages' => 'Save Brand Success',
                //'debug_msg' => $debug_msg
            ];

            return response()->json($response);
        } catch (Exception $e) {
            DB::rollBack();
            $errorMsg = 'false:'.$e->getMessage().' at line:'.$e->getLine();
            RVLog::error(sprintf('Error Msg %s at %s :: LINE %s ::  %s', $errorMsg, __FILE__, __LINE__, __METHOD__));
            exit;
        }
    }

    public function saveBacktotop(SaveBacktotopRequest $request)
    {
        try {
            DB::beginTransaction();
            CoreSetting::updateOrCreate(['key' => 'backtotop'], ['value' => $request->input('backtotop')]);

            // query process
            DB::commit();

            return 'Success Backtotop';
        } catch (Exception $e) {
            DB::rollBack();
            $errorMsg = 'false:'.$e->getMessage().' at line:'.$e->getLine();
            RVLog::error(sprintf('Error Msg %s at %s :: LINE %s ::  %s', $errorMsg, __FILE__, __LINE__, __METHOD__));
            exit;
        }
    }

    public function setHideMenu(SetHideMenuRequest $request)
    {
        try {
            DB::beginTransaction();
            CoreSetting::updateOrCreate(['key' => 'show_loginMenu'], ['value' => $request->input('show_menu')]);

            // query process
            DB::commit();

            return 'Save Success';
        } catch (Exception $e) {
            DB::rollBack();
            $errorMsg = 'false:'.$e->getMessage().' at line:'.$e->getLine();
            RVLog::error(sprintf('Error Msg %s at %s :: LINE %s ::  %s', $errorMsg, __FILE__, __LINE__, __METHOD__));
            exit;
        }
    }
}
