44b1d847bc
Co-authored-by: ISMAIL MASSERAN <topaz@ISMAILs-Macbook.local> Co-authored-by: nurafrinaalimi16 <nurafrinaalimi16@gmail.com> Co-authored-by: ISMAIL MASSERAN <topaz@Mac.dlinkrouter.local> Reviewed-on: #17 Co-authored-by: ismailmasseran <ismail@koppkb.com> Co-committed-by: ismailmasseran <ismail@koppkb.com>
70 lines
2.3 KiB
PHP
70 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class AdminFeatureController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware(['auth', 'admin']);
|
|
}
|
|
|
|
protected function api()
|
|
{
|
|
return Http::withOptions(['verify' => config('app.api_verify')]);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$api_url = config('api.url');
|
|
|
|
$metaRes = $this->api()->get($api_url . '/api/features');
|
|
$meta = $metaRes->successful() ? $metaRes->json() : [];
|
|
$keys = is_array($meta) && !empty($meta['keys']) ? $meta['keys'] : ['pembiayaan_online'];
|
|
$labels = is_array($meta) && !empty($meta['labels']) ? $meta['labels'] : [
|
|
'pembiayaan_online' => 'Pembiayaan Online',
|
|
];
|
|
|
|
$listRes = $this->api()->get($api_url . '/api/features/cawangan');
|
|
$cawangan = ($listRes->successful() && is_array($listRes->json())) ? $listRes->json() : [];
|
|
|
|
if (!$listRes->successful()) {
|
|
session()->now('error', 'Gagal muat senarai ciri cawangan.');
|
|
}
|
|
|
|
return view('admin.features', compact('api_url', 'keys', 'labels', 'cawangan'));
|
|
}
|
|
|
|
public function update(Request $request)
|
|
{
|
|
$kodcaw = trim((string) $request->input('kodcaw', ''));
|
|
$feature = trim((string) $request->input('feature', ''));
|
|
$enabled = (int) $request->input('enabled') ? 1 : 0;
|
|
|
|
if ($kodcaw === '' || $feature === '') {
|
|
return redirect()->route('admin.features')->with('error', 'Kod cawangan dan ciri diperlukan.');
|
|
}
|
|
|
|
$res = $this->api()
|
|
->put(config('api.url') . '/api/features/' . $kodcaw . '/' . $feature, [
|
|
'enabled' => $enabled,
|
|
]);
|
|
|
|
if (!$res->successful()) {
|
|
$body = $res->json();
|
|
$msg = is_array($body) && isset($body['message'])
|
|
? $body['message']
|
|
: 'Gagal kemaskini ciri cawangan.';
|
|
return redirect()->route('admin.features')->with('error', $msg);
|
|
}
|
|
|
|
$label = $enabled ? 'diaktifkan' : 'dinyahaktifkan';
|
|
return redirect()
|
|
->route('admin.features')
|
|
->with('message', $feature . ' ' . $label . ' untuk ' . $kodcaw . '.');
|
|
}
|
|
}
|