﻿file = r'app\Exports\PanelTestExport.php'
with open(file, 'r', encoding='utf-8', errors='replace') as f:
    c = f.read()

# Tambah header group KESIMPULAN sebelum loop pertanyaan kesimpulan
old_kesimpulan = "        foreach ($panelTest->questions as $q) {\n            $isText = $q->question_type === 'text';\n            $c      = $this->col($colIdx);"

new_kesimpulan = """        // Header group KESIMPULAN di row 4
        if ($panelTest->questions->count() > 0) {
            $kesimpulanStart = $this->col($colIdx);
            $kesimpulanCount = 0;
            foreach ($panelTest->questions as $q) {
                $kesimpulanCount += $q->question_type === 'text' ? 1 : 2;
            }
            $kesimpulanEnd = $this->col($colIdx + $kesimpulanCount - 1);
            $sheet->mergeCells("{$kesimpulanStart}4:{$kesimpulanEnd}4");
            $sheet->setCellValue("{$kesimpulanStart}4", 'KESIMPULAN');
            $this->fill($sheet, "{$kesimpulanStart}4:{$kesimpulanEnd}4", 'FF7C3AED');
            $this->font($sheet, "{$kesimpulanStart}4", ['bold' => true, 'size' => 10, 'color' => 'FFFFFFFF']);
            $this->align($sheet, "{$kesimpulanStart}4", 'center', 'center');
            $sheet->getRowDimension(4)->setRowHeight(20);
        }

        foreach ($panelTest->questions as $q) {
            $isText = $q->question_type === 'text';
            $c      = $this->col($colIdx);"""

# Tambah header group nama sampel sebelum loop sampel
old_sampel = "        foreach ($panelTest->samples as $si => $sample) {\n            $sc = $this->palette[$si % 5];\n            foreach ($sample->questions as $q) {"

new_sampel = """        foreach ($panelTest->samples as $si => $sample) {
            $sc = $this->palette[$si % 5];

            // Header group nama sampel di row 4
            if ($sample->questions->count() > 0) {
                $sampelStart = $this->col($colIdx);
                $sampelCount = 0;
                foreach ($sample->questions as $q) {
                    $sampelCount += $q->question_type === 'text' ? 1 : 2;
                }
                $sampelEnd = $this->col($colIdx + $sampelCount - 1);
                $sheet->mergeCells("{$sampelStart}4:{$sampelEnd}4");
                $sheet->setCellValue("{$sampelStart}4", strtoupper($sample->name) . ($sample->code ? ' (' . $sample->code . ')' : ''));
                $this->fill($sheet, "{$sampelStart}4:{$sampelEnd}4", $sc['dark']);
                $this->font($sheet, "{$sampelStart}4", ['bold' => true, 'size' => 10, 'color' => 'FFFFFFFF']);
                $this->align($sheet, "{$sampelStart}4", 'center', 'center');
                $sheet->getRowDimension(4)->setRowHeight(20);
            }

            foreach ($sample->questions as $q) {"""

if old_kesimpulan in c:
    c = c.replace(old_kesimpulan, new_kesimpulan)
    print("kesimpulan header: OK")
else:
    print("GAGAL kesimpulan")

if old_sampel in c:
    c = c.replace(old_sampel, new_sampel)
    print("sampel header: OK")
else:
    print("GAGAL sampel")

with open(file, 'w', encoding='utf-8') as f:
    f.write(c)
