<?php

namespace App\Http\Controllers\Webprofile\Backend;

use App\Models\Webprofile\Setting;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Validator;
use Uuid;
use Alert;
use Session;
use Crypt;
use App\Helpers\InseoHelper;
use App\Http\Requests\SettingRequest;
use Auth;

class SettingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Setting::orderBy('name_setting', 'ASC')->get();

        $data = [
            'data' => $data,
        ];

        return view('webprofile.backend.setting.index', $data)->withTitle(trans('feature.setting'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('webprofile.backend.setting.create')->withTitle(trans('feature.create_setting'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(SettingRequest $request)
    {
        $data = $request->except('_token');

        $data['userid_created'] = Auth::user()->id;
        $data['userid_updated'] = Auth::user()->id;

        Setting::create($data);

        Alert::success('Data berhasil disimpan')->persistent('Ok');

        $successmessage = "Proses Tambah Pengaturan Berhasil !!";
        return redirect()->route('settings.index')->with('successMessage', $successmessage);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Setting  $setting
     * @return \Illuminate\Http\Response
     */
    public function show(Setting $setting)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Setting  $setting
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        try {
            $data = Setting::find(Crypt::decrypt($id));
            return view('webprofile.backend.setting.edit', compact('data'))->withTitle(trans('feature.edit_setting'));
        } catch (\Exception $id) {
            return redirect()->route('settings.index');
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Setting  $setting
     * @return \Illuminate\Http\Response
     */
    public function update(SettingRequest $request, $id)
    {
        $setting = Setting::findOrFail($id);

        $data = $request->except('_token');

        $data['userid_updated'] = Auth::user()->id;
        $setting->update($data);

        Alert::success('Data berhasil diubah')->persistent('Ok');
        return redirect()->route('settings.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Setting  $setting
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
