<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

use DataTables;
use GuzzleHttp\Client;
use Carbon\Carbon;

use App\Models\Riwayat;

class RiwayatController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $data = Riwayat::select('*');
            // Convert the Eloquent Collection to a regular PHP array
            $data->each(function ($item, $key) {
                $item->rowIndex = $key + 1;
            });

            return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('title-post', function($row){
                    $text = '
                    <p class="mb-0">' . $row->gejala . '</p>
                    ';
                    return $text;
                })
                ->addColumn('jenis_gejala', function($row){
                    return $row->gejala;
                })
                ->rawColumns(['title-post', 'jenis_gejala'])
                ->make(true);
        }

        $data = [
            'subtitle' => 'Riwayat'
        ];

        return view('admin.app.content.riwayat.index', compact('data'));
    }

    public function delete($id)
    {
        $riwayat = Riwayat::find($id);
        if($riwayat) {
            if($riwayat->delete()) {
                return redirect()->to(app_url('riwayat'))->with('success', 'You have successfully deleted data');
            } else {
                return redirect()->to(app_url('riwayat'))->with('error', 'An error occurred in the query');
            }
        } else {
            return redirect()->to(app_url('riwayat'))->with('error', 'An error occurred in the query');
        }
    }
}
