<?php

namespace Wave\Http\Controllers\CMS;

use App\Http\Controllers\Controller;
use App\Models\SiteSetting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class TermsAndConditionsController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $siteSetting = SiteSetting::latest()->first();
        return view('theme::cms.terms-and-conditions.index', [
            'siteSetting' => $siteSetting,
        ]);
    }

    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'terms_and_conditions_english' => 'required',
            'terms_and_conditions_chinese' => 'required',
        ]);
        if ($validator->fails()) {
            return redirect()->back()->with(['message' => __('Fill up all required fields!'), 'message_type' => 'danger', 'show_toast' => true]);
        }
        else {
            $siteSetting = SiteSetting::latest()->first();
            if ($siteSetting) {
                $siteSetting->update($request->all());
            }
            else {
                SiteSetting::create($request->all());
            }
    
            return redirect()->back()->with(['message' => __('Terms and conditions has successfully updated!'), 'message_type' => 'success', 'show_toast' => true]);
        }
    }
}
