<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AduanTps;
use Illuminate\Http\Request;

class AduanController extends Controller
{
    /**
     * Tampilkan semua aduan
     */
    public function index()
    {
        $aduan = AduanTps::with('lokasiTps')
            ->orderBy('tanggal_aduan', 'desc')
            ->get();

        return view('admin.aduan.index', compact('aduan'));
    }

    public function show($id)
    {
        $aduan = AduanTps::findOrFail($id);
        return view('admin.aduan.edit', compact('aduan'));
    }

    public function tanggapi(Request $request, $id)
    {
        $request->validate([
            'tanggapan_admin' => 'required'
        ]);

        $aduan = AduanTps::findOrFail($id);
        $aduan->tanggapan_admin = $request->tanggapan_admin;
        $aduan->tanggal_tanggapan = now();
        $aduan->save();

        return redirect()->route('admin.aduan.index')
            ->with('success', 'Tanggapan berhasil disimpan');
    }

    /**
     * Hapus tanggapan admin
     */
    public function destroy($id)
    {
        $aduan = AduanTps::findOrFail($id);
        $aduan->delete();

        return redirect()->route('admin.aduan.index')
            ->with('success', 'Aduan berhasil dihapus');
    }
}
