@extends('layouts.app')
@section('title', isset($article) ? 'Edit Knowledge Note' : 'Create Knowledge Note')
@section('content')
<h4 class="mb-3">{{ isset($article) ? 'Edit' : 'Create' }} Knowledge Note</h4>
<form method="POST" action="{{ isset($article) ? route('knowledge.update', $article) : route('knowledge.store') }}" class="card card-body">
@csrf @if(isset($article)) @method('PUT') @endif
<div class="row g-3">
<div class="col-md-6"><label class="form-label">Title</label><input class="form-control" name="title" value="{{ old('title', $article->title ?? '') }}" required></div>
<div class="col-md-3"><label class="form-label">Type</label><select class="form-select" name="type">@foreach(['auto_doc','module_note','api_note','bug_kb','deployment_note','developer_note'] as $t)<option value="{{ $t }}" @selected(old('type', $article->type ?? 'module_note')===$t)>{{ ucfirst(str_replace('_',' ',$t)) }}</option>@endforeach</select></div>
<div class="col-md-3"><label class="form-label">Published</label><select class="form-select" name="is_published"><option value="1">Yes</option><option value="0" @selected(old('is_published', $article->is_published ?? true)==false)>No</option></select></div>
<div class="col-md-6"><label class="form-label">Project</label><select class="form-select" name="project_id"><option value="">N/A</option>@foreach($projects as $project)<option value="{{ $project->id }}" @selected(old('project_id', $article->project_id ?? '')==$project->id)>{{ $project->name }}</option>@endforeach</select></div>
<div class="col-md-6"><label class="form-label">Module</label><select class="form-select" name="module_id"><option value="">N/A</option>@foreach($modules as $module)<option value="{{ $module->id }}" @selected(old('module_id', $article->module_id ?? '')==$module->id)>{{ $module->name }}</option>@endforeach</select></div>
<div class="col-md-12"><label class="form-label">Tags (comma separated)</label><input class="form-control" name="tags" value="{{ old('tags', isset($article) ? $article->tags->pluck('name')->join(', ') : '') }}"></div>
<div class="col-md-12"><label class="form-label">Content (Rich Text)</label><textarea id="editor" class="form-control" name="content" rows="12" required>{{ old('content', $article->content ?? '') }}</textarea><div class="small text-muted mt-1">Supports HTML content for rich documentation.</div></div>
</div>
<div class="mt-3"><button class="btn btn-success">Save</button></div>
</form>
<script>
const editor = document.getElementById('editor');
editor.addEventListener('keydown', function(e){
  if (e.key === 'Tab') { e.preventDefault(); const start=this.selectionStart; this.value=this.value.substring(0,start)+'    '+this.value.substring(this.selectionEnd); this.selectionStart=this.selectionEnd=start+4; }
});
</script>
@endsection
