Files

127 lines
6.8 KiB
PHP

@extends('layouts.app')
@section('content')
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<style>
/* Menyesuaikan tampilan Select2 agar menyatu dengan desain Tailwind */
.select2-container .select2-selection--single { height: 46px; border-radius: 0.75rem; border-color: #e2e8f0; background-color: #f8fafc; }
.select2-container--default .select2-selection--single .select2-selection__rendered { line-height: 46px; font-size: 0.875rem; color: #334155; padding-left: 1rem; }
.select2-container--default .select2-selection--single .select2-selection__arrow { height: 44px; right: 10px; }
.select2-container--default .select2-search--dropdown .select2-search__field { border-radius: 0.5rem; border-color: #cbd5e1; }
</style>
<div class="max-w-2xl mx-auto px-4 py-8">
<div class="flex items-center space-x-3 mb-6">
<a href="{{ route('admin.subjects.index') }}" class="text-slate-400 hover:text-blue-600 transition-colors">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
</a>
<h2 class="text-2xl font-bold text-slate-800 tracking-tight">Edit Materi</h2>
</div>
<div class="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden">
<form action="{{ route('admin.subjects.update', $subject->id) }}" method="POST" class="p-6 sm:p-8 space-y-6">
@csrf
@method('PUT')
@php
// 1. Ambil nama materi saat ini dari database
$currentName = old('name', $subject->name) ?? '';
$isSopMatch = false;
$matchedSopValue = '';
$originalManualValue = $currentName;
// 2. Logika Smart Auto-Sync: Cocokkan dengan data Master SOP
if ($currentName) {
// Pastikan variabel $sops sudah dikirim dari SubjectController@edit
if(isset($sops)) {
foreach($sops as $sop) {
$fullname = $sop->nomor . ' - ' . $sop->judul;
// Jika persis sama ATAU nama lama mengandung unsur nomor SOP
if ($currentName === $fullname || strpos($currentName, $sop->nomor) !== false) {
$isSopMatch = true;
$matchedSopValue = $fullname; // Arahkan ke nama resmi
$originalManualValue = ''; // Kosongkan form manual
break;
}
}
}
}
// 3. Tentukan mode radio button saat halaman dimuat
$defaultType = ($currentName && !$isSopMatch) ? 'manual' : 'sop';
@endphp
<div x-data="{ inputType: '{{ $defaultType }}' }">
<label class="block text-sm font-bold text-slate-700 mb-2">Sumber Materi</label>
<div class="flex space-x-6 mb-4 p-3 bg-slate-50 rounded-xl border border-slate-200">
<label class="flex items-center cursor-pointer">
<input type="radio" x-model="inputType" value="sop" class="w-4 h-4 text-blue-600 focus:ring-blue-500 border-slate-300">
<span class="ml-2 text-sm font-bold text-slate-700">Dari Master SOP</span>
</label>
<label class="flex items-center cursor-pointer">
<input type="radio" x-model="inputType" value="manual" class="w-4 h-4 text-blue-600 focus:ring-blue-500 border-slate-300">
<span class="ml-2 text-sm font-bold text-slate-700">Input Manual (Non-Plant)</span>
</label>
</div>
<label class="block text-sm font-bold text-slate-700 mb-2">Nama Materi <span class="text-red-500">*</span></label>
<div x-show="inputType === 'sop'">
<select name="name" x-bind:disabled="inputType !== 'sop'" class="select2-sop w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:ring-blue-500" required>
<option value="">-- Cari dan Pilih Dokumen SOP --</option>
@if(isset($sops))
@foreach($sops as $sop)
@php $fullname = $sop->nomor . ' - ' . $sop->judul; @endphp
<option value="{{ $fullname }}" {{ $matchedSopValue === $fullname ? 'selected' : '' }}>
{{ $fullname }}
</option>
@endforeach
@endif
</select>
</div>
<div x-show="inputType === 'manual'" x-cloak>
<input type="text" name="name" x-bind:disabled="inputType !== 'manual'" value="{{ $originalManualValue }}"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:bg-white focus:ring-2 focus:ring-blue-500/20 focus:border-blue-600 transition-all"
placeholder="Contoh: Modul Marketing Advance" required>
</div>
@error('name')
<p class="text-red-500 text-xs mt-2 font-semibold">{{ $message }}</p>
@enderror
</div>
<div class="pt-4 flex justify-end space-x-3 border-t border-slate-100">
<a href="{{ route('admin.subjects.index') }}" class="px-5 py-2.5 text-sm font-semibold text-slate-600 hover:bg-slate-100 rounded-xl transition-colors">Batal</a>
<button type="submit" class="px-6 py-2.5 text-sm font-bold text-white bg-blue-600 hover:bg-blue-700 rounded-xl shadow-sm transition-colors">Update Materi</button>
</div>
</form>
</div>
</div>
<script>
$(document).ready(function() {
// Inisialisasi Select2
$('.select2-sop').select2({
placeholder: "-- Cari dan Pilih Dokumen SOP --",
width: '100%'
});
// Memaksa Select2 merender ulang ukurannya ketika user berpindah dari mode Manual ke SOP
$('input[type=radio][value=sop]').change(function() {
setTimeout(function() {
$('.select2-sop').select2('destroy').select2({
placeholder: "-- Cari dan Pilih Dokumen SOP --",
width: '100%'
});
}, 50);
});
});
</script>
@endsection