<?php
namespace App\Http\Controllers;
use App\Models\Teacher;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class TeacherController extends Controller {
 public function index(): View { $teachers=Teacher::latest()->get(); return view('teachers.index',compact('teachers')); }
 public function create(): View { return view('teachers.create'); }
 public function store(Request $request): RedirectResponse {
  $request->validate(['code'=>'required|max:50|unique:teachers,code','full_name'=>'required|max:255']);
  Teacher::create($request->only(['code','full_name','gender','date_of_birth','academic_title','degree','specialty','organization','position','phone','email','address','bio','status']));
  return redirect()->route('teachers.index')->with('success','Đã thêm giảng viên thành công.');
 }
 public function show(Teacher $teacher): View { return view('teachers.show',compact('teacher')); }
 public function edit(Teacher $teacher): View { return view('teachers.edit',compact('teacher')); }
 public function update(Request $request, Teacher $teacher): RedirectResponse {
  $request->validate(['code'=>'required|max:50|unique:teachers,code,'.$teacher->id,'full_name'=>'required|max:255']);
  $teacher->update($request->only(['code','full_name','gender','date_of_birth','academic_title','degree','specialty','organization','position','phone','email','address','bio','status']));
  return redirect()->route('teachers.index')->with('success','Đã cập nhật giảng viên thành công.');
 }
 public function destroy(Teacher $teacher): RedirectResponse {
  $teacher->delete(); return redirect()->route('teachers.index')->with('success','Đã xóa giảng viên thành công.');
 }
}
