<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;

use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ConfigController extends Controller
{
    function getConfig()
    {
        $konfigurasi = DB::table('configurasi')->get();
        return response()->json([
            'success' => true,
            'data' => $konfigurasi
        ]);
    }

    function getConfigPencandangan()
    {
        $konfigurasi = DB::table('configurasi')->where('kategori', 'pencadangan')->get();
        return response()->json([
            'success' => true,
            'data' => $konfigurasi
        ]);
    }

    function getConfigAnalisa()
    {
        $konfigurasi = DB::table('configurasi')->where('kategori', 'analisa')->get();
        return response()->json([
            'success' => true,
            'data' => $konfigurasi
        ]);
    }

    function simpanConfigPencadangan(Request $request)
    {
        try {
            foreach ($request->all() as $key => $value) {
                $nilai = DB::table('configurasi')
                    ->where('variable', $key)
                    ->where('kategori', 'pencadangan')
                    ->select(['nilai'])
                    ->first();
                if ($nilai == null) {
                    DB::table('configurasi')->insert([
                        'variable' => $key,
                        'kategori' => 'pencadangan',
                        'nilai' => $value,
                    ]);
                } else {
                    DB::table('configurasi')
                        ->where('kategori', 'pencadangan')
                        ->where('variable', $key)
                        ->update([
                            'variable' => $key,
                            'kategori' => 'pencadangan',
                            'nilai' => $value,
                        ]);
                }
            }
            return response()->json([
                'success' => true,
                'message' => "Data berhasil disimpan",
                'data' => $request->all()
            ]);
        } catch (Exception $error) {
            return response()->json([
                'success' => false,
                'error' => $error->getMessage()()
            ]);
        }
    }

    function simpanConfigAnalisa(Request $request)
    {
        try {
            foreach ($request->all() as $key => $value) {
                $nilai = DB::table('configurasi')
                    ->where('variable', $key)
                    ->where('kategori', 'analisa')
                    ->select(['nilai'])
                    ->first();
                if ($nilai == null) {
                    DB::table('configurasi')->insert([
                        'variable' => $key,
                        'kategori' => 'analisa',
                        'nilai' => $value,
                    ]);
                } else {
                    DB::table('configurasi')
                        ->where('kategori', 'analisa')
                        ->where('variable', $key)
                        ->update([
                            'variable' => $key,
                            'kategori' => 'analisa',
                            'nilai' => $value,
                        ]);
                }
            }
            return response()->json([
                'success' => true,
                'message' => "Data berhasil disimpan",
                'data' => $request->all()
            ]);
        } catch (Exception $error) {
            return response()->json([
                'success' => false,
                'error' => $error->getMessage()()
            ]);
        }
    }
}
