# Script untuk mengkonversi semua view dari @extends ke x-app-layout
$viewsPath = "resources/views"

# Daftar direktori yang perlu dikonversi
$directories = @(
    "kelas",
    "semesters", 
    "mata_pelajarans",
    "jadwals",
    "absensis",
    "nilai_santris",
    "prestasis",
    "pelanggarans",
    "catatan_kesehatans",
    "beritas",
    "pembayarans",
    "alumnis"
)

foreach ($dir in $directories) {
    $fullPath = Join-Path $viewsPath $dir
    if (Test-Path $fullPath) {
        Write-Host "Processing directory: $dir"
        
        # Proses semua file .blade.php dalam direktori
        $files = Get-ChildItem -Path $fullPath -Filter "*.blade.php"
        
        foreach ($file in $files) {
            Write-Host "  Converting: $($file.Name)"
            
            $content = Get-Content $file.FullName -Raw
            
            # Skip jika sudah menggunakan x-app-layout
            if ($content -match '<x-app-layout>') {
                Write-Host "    Skipped (already converted): $($file.Name)"
                continue
            }
            
            # Konversi @extends('layouts.app') ke <x-app-layout>
            $content = $content -replace '@extends\(''layouts\.app''\)', '<x-app-layout>'
            
            # Hapus @section('content')
            $content = $content -replace '@section\(''content''\)', ''
            
            # Ganti @endsection dengan </x-app-layout>
            $content = $content -replace '@endsection', '</x-app-layout>'
            
            # Tambah header slot setelah <x-app-layout>
            $title = ""
            if ($file.Name -match 'index\.blade\.php') {
                $title = "Daftar " + ($dir -replace '_', ' ' -replace 's$', '')
            } elseif ($file.Name -match 'create\.blade\.php') {
                $title = "Tambah " + ($dir -replace '_', ' ' -replace 's$', '')
            } elseif ($file.Name -match 'edit\.blade\.php') {
                $title = "Edit " + ($dir -replace '_', ' ' -replace 's$', '')
            } elseif ($file.Name -match 'show\.blade\.php') {
                $title = "Detail " + ($dir -replace '_', ' ' -replace 's$', '')
            } else {
                $title = "Manage " + ($dir -replace '_', ' ' -replace 's$', '')
            }
            
            # Tambah header slot
            $headerSlot = "`n    <x-slot name=`"header`">`n        <h2 class=`"font-semibold text-xl text-gray-800 leading-tight`">`n            {{ __('$title') }}`n        </h2>`n    </x-slot>`n`n    <div class=`"py-12`">`n        <div class=`"max-w-7xl mx-auto sm:px-6 lg:px-8`">`n            <div class=`"bg-white overflow-hidden shadow-sm sm:rounded-lg`">`n                <div class=`"p-6`">"
            
            $content = $content -replace '<x-app-layout>', "<x-app-layout>$headerSlot"
            
            # Tutup div wrapper
            $content = $content -replace '</x-app-layout>', "`n                </div>`n            </div>`n        </div>`n    </div>`n</x-app-layout>"
            
            # Update styling untuk container dan card
            $content = $content -replace '<div class="container mt-4">', ''
            $content = $content -replace '<div class="card">', ''
            $content = $content -replace '</div>\s*</div>\s*</div>', ''
            
            # Update button styling
            $content = $content -replace 'class="btn-primary mb-3 inline-block"', 'class="btn-primary"'
            
            # Update alert styling
            $content = $content -replace 'class="alert-success"', 'class="alert-success mb-4"'
            
            # Simpan file
            Set-Content -Path $file.FullName -Value $content -Encoding UTF8
        }
    }
}

Write-Host "Conversion completed!" 