<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\SimpleType\JcTable;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\Table;
use PhpOffice\PhpWord\ComplexType\TblWidth;
use PhpOffice\PhpWord\SimpleType\Paper;
use Illuminate\Support\Facades\Storage;
use App\Models\BorangUlasanIk;

class CetakanPenjajaController extends Controller
{
    //
    public function cetak_pemeriksaan_lepas(Request $request, $id, $borang_ik_id)
    {
        $headText = ['name' => 'Arial', 'size' => 10];
            
        $borang_ik = BorangUlasanIk::findOrFail($request->borang_ik_id);
        
        $phpWord = new PhpWord();

        $phpWord->setDefaultFontName('Arial');   // Jenis font
        $phpWord->setDefaultFontSize(8);         // Saiz font kecil

        $phpWord->addTitleStyle(
            1, // heading level (1 = Heading1)
            NULL,
            ['alignment' => 'center']
        );

        $section = $phpWord->addSection([
                'orientation' => 'portrait',  
                'marginLeft' => 1000,
                'marginRight' => 1000,  
                'marginTop' => 500,
            ]);

        $footer = $section->addFooter();
        $footer->addText(
            'Rujukan Fail : MBIP(L)(PS) '.$borang_ik->lesen_penjaja->no_fail_lesen,
            ['size' => 8, 'italic' => true],
            ['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::START]
        );

        $table = $section->addTable();

        $table->addRow();
        $cell1 = $table->addCell(2000);
        $cell1->addImage(public_path('img/mbip-crown-png45x45.png'), ['width' => 45, 'height' => 45]);
        
        $cell2 = $table->addCell(6000);
        $cell2->addText('JABATAN PELESENAN', ['bold' => true, 'size' => 12], ['spaceAfter' => 0]);
        $cell2->addText('MAJLIS BANDARAYA ISKANDAR PUTERI', ['bold' => true, 'size' => 12]);
        
        $cell3 = $table->addCell(2000);
        $cell3->addText(' L02-03 ', ['size' => 8, 'bgColor'=>'CCCCCC'], ['alignment' => 'right', 'spaceAfter' => 0]);

        $textRun = $section->addTextRun();
        $textRun->addText(
            'Ruj. Fail : ',
            ['name' => 'Arial', 'size' => 8]
        );
        
        $textRun->addText(
            'MBIP(L)(PS) '.$borang_ik->lesen_penjaja->no_fail_lesen,
            ['name' => 'Arial', 'size' => 10, 'bold' => true]
        );

        
        $section->addText('Pegawai Tadbir N41', [], ['spaceAfter'=> 0]);
        $section->addText('Jabatan Pelesenan');

        $section->addText('LAPORAN PEMERIKSAAN KE ATAS PENJAJA', ['size' => 9, 'bold' => true], ['alignment' => 'center', 'spaceAfter' => 0]);
        $section->addText('(PERMOHONAN / PEMBAHARUAN / PEMANTAUAN)', ['size' => 9, 'bold' => true], ['alignment' => 'center', 'spaceAfter' => 0]);

        $section->addText(' ' );

        $section->addText('STATUS PENJAJA', ['size' => 9, 'bold' => true, 'underline'=>'single']);
        $section->addText('[ / ] '.ucwords($borang_ik->lesen_penjaja->jenis_penjaja->jenis), ['size' => 10, 'bold' => true]);
        $section->addText('1) PERIHAL PENJAJA' );
        
        $tableStyle = new Table();
        $tableStyle->setWidth(new TblWidth(10000));
        
        $tableStyle->setBorderSize(0);
        $tableStyle->setBorderColor('FFFFFF');
        $tableStyle->setCellMargin(80);
        $tableStyle->setAlignment(JcTable::START);
        
        // cara betul set indent
        $tableStyle->setIndent(new TblWidth(720)); // 720 twip = 0.5 inch
        // Create table with 2 columns
        $table = $section->addTable($tableStyle);

        $table->addRow();
        $table->addCell(3000)->addText('a) Nama Pemohon :', [], ['spaceAfter' => 0]);
        $table->addCell(700)->addText($borang_ik->lesen_penjaja->user->name, ['bold' => true], ['spaceAfter' => 0]);
        
        // Row 2: Lokasi Tempat Berniaga
        $table->addRow();
        $table->addCell(3000)->addText('b) Lokasi Tempat Berniaga :', [], ['spaceAfter' => 0]);
        $table->addCell(7000)->addText($borang_ik->lesen_penjaja->lokasi_penjajaan(), ['bold' => true], ['spaceAfter' => 0]);

        $table->addRow();
        $table->addCell(3000)->addText('c) Jenis Jualan :', [], ['spaceAfter' => 0]);
        $table->addCell(7000)->addText($borang_ik->lesen_penjaja->jenis_jualan->nama, ['bold' => true], ['spaceAfter' => 0]);

        $section->addLine(['weight' => 1, 'width' => 500, 'height' => 0,  'color' => '000000']);

        $section->addText('Tarikh Pemeriksaan : '.$borang_ik->dt_periksa);
        
        $section->addText('2) ULASAN' );
        
        $tableStyle = new Table();
        $tableStyle->setWidth(new TblWidth(10000));
        $tableStyle->setBorderSize(0);
        $tableStyle->setBorderColor('FFFFFF');
        $tableStyle->setCellMargin(80);
        $tableStyle->setAlignment(JcTable::START);
        
        // cara betul set indent
        $tableStyle->setIndent(new TblWidth(720)); // 720 twip = 0.5 inch
        // Create table with 3 columns
        $table = $section->addTable($tableStyle);

        $table->addRow();
        $table->addCell(4500)->addText('a) Lokasi Tempat Berniaga ', [], ['spaceAfter' => 0]);
        $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);
        $table->addCell(5300)->addText($borang_ik->kesesuaian_lokasi." [ / ]", ['bold' => true], ['spaceAfter' => 0]);

        if($borang_ik->kesesuaian_lokasi == 'tidak sesuai'){
            $table->addRow();
            $table->addCell(4500)->addText('Jika tidak sesuai', [], ['alignment' => 'right', 'spaceAfter' => 0]);
            $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);
            $table->addCell(5300)->addText('sebab '.$borang_ik->kesesuaian_lokasi_sebab, ['bold' => true], ['spaceAfter' => 0]);
        }

        $table->addRow();
        $table->addCell(4500)->addText('b) Perniagaan dijalankan oleh ', [], ['spaceAfter' => 0]);
        $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);
        $table->addCell(5300)->addText(ucwords($borang_ik->dijalankan_oleh)." [ / ]", ['bold' => true], ['spaceAfter' => 0]);

        if($borang_ik->dijalankan_oleh == 'orang lain'){
            $table->addRow();
            $table->addCell(10000)->addText('Notis memberhentikan perniagaan dikeluarkan.', [], ['alignment' => 'right', 'spaceAfter' => 0]);
        }

        $table->addRow();
        $table->addCell(4500)->addText('c) Pematuhan syarat ', [], ['spaceAfter' => 0]);
        $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);
        $table->addCell(5300)->addText($borang_ik->pematuhan_syarat." [ / ]", ['bold' => true], ['spaceAfter' => 0]);

        if($borang_ik->pematuhan_syarat == 'belum dipatuhi'){
            $table->addRow();
            $table->addCell(4500)->addText('Jika belum', [], ['alignment' => 'right', 'spaceAfter' => 0]);
            $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);
            $table->addCell(5300)->addText('Perkara-perkara yang perlu dipatuhi :- ', [], ['spaceAfter' => 0]);

            if($borang_ik->perkara_perlu_dipatuhi != ''){
                $perlu_patuhi = explode("\n", $borang_ik->perkara_perlu_dipatuhi);
                $i = 0;

                foreach($perlu_patuhi as $pp){
                    $i++;
                    $table->addRow();
                    $table->addCell(4500)->addText('', [], ['spaceAfter' => 0]);
                    $table->addCell(200)->addText('', [], ['spaceAfter' => 0]);
                    $table->addCell(5300)->addText($i.") ".$pp, [], ['spaceAfter' => 0]);

                }
            }
        }

        $table->addRow();
        $table->addCell(4500)->addText('d) Pemprosesan makanan di tempat berniaga ', [], ['spaceAfter' => 0]);
        $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);
        if(!is_null($borang_ik->pemprosesan_makanan)){
            $table->addCell(5300)->addText(ucwords($borang_ik->pemprosesan_makanan)." [ / ]", ['bold' => true], ['spaceAfter' => 0]);
        }
        else{
            $table->addCell(5300)->addText('', ['bold' => true], ['spaceAfter' => 0]);
        }

        if($borang_ik->pemprosesan_makanan == 'ada'){
            if($borang_ik->pemprosesan_makanan_memasak == '1'){
                $table->addRow();
                $table->addCell(4500)->addText('', [], ['alignment' => 'right', 'spaceAfter' => 0]);
                $table->addCell(200)->addText('', [], ['spaceAfter' => 0]);
                $table->addCell(5300)->addText('[ / ] Memasak makanan', ['bold' => true], ['spaceAfter' => 0]);
            }
            if($borang_ik->pemprosesan_makanan_merebus == '1'){
                $table->addRow();
                $table->addCell(4500)->addText('', [], ['alignment' => 'right', 'spaceAfter' => 0]);
                $table->addCell(200)->addText('', [], ['spaceAfter' => 0]);
                $table->addCell(5300)->addText('[ / ] Merebus makanan', ['bold' => true], ['spaceAfter' => 0]);
            }
            if($borang_ik->pemprosesan_makanan_menggoreng == '1'){
                $table->addRow();
                $table->addCell(4500)->addText('', [], ['alignment' => 'right', 'spaceAfter' => 0]);
                $table->addCell(200)->addText('', [], ['spaceAfter' => 0]);
                $table->addCell(5300)->addText('[ / ] Menggoreng makanan', ['bold' => true], ['spaceAfter' => 0]);
            }
            if($borang_ik->pemprosesan_makanan_lain == '1'){
                $table->addRow();
                $table->addCell(4500)->addText('', [], ['alignment' => 'right', 'spaceAfter' => 0]);
                $table->addCell(200)->addText('', [], ['spaceAfter' => 0]);
                $table->addCell(5300)->addText('[ / ] Lain-lain '.$borang_ik->pemprosesan_makanan_lain_txt, ['bold' => true], ['spaceAfter' => 0]);
            }
        }

        $table->addRow();
        $table->addCell(4500)->addText('e) Mendirikan bangunan/payung ', [], ['spaceAfter' => 0]);
        $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);

        if($borang_ik->mendirikan_bangunan == 'tiada'){
            $txt_bangunan = $borang_ik->mendirikan_bangunan." [ / ] (hanya menggunakan payung/meja sahaja)";
        }
        else{
            $txt_bangunan = $borang_ik->mendirikan_bangunan." [ / ] (keluarkan notis)";
        }
        $table->addCell(5300)->addText(ucwords($borang_ik->pemprosesan_makanan), ['bold' => true], ['spaceAfter' => 0]);

        $table->addRow();
        $table->addCell(4500)->addText('(Jika bukan Penjaja Gerai Statik)', [], ['spaceAfter' => 0]);
        $table->addCell(200)->addText('', [], ['spaceAfter' => 0]);
        $table->addCell(5300)->addText('', ['bold' => true], ['spaceAfter' => 0]);

        $table->addRow();
        $table->addCell(4500)->addText('f) Jika berniaga menggunakan kenderaan ', [], ['spaceAfter' => 0]);
        $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);
        if((!is_null($borang_ik->jika_guna_kenderaan))&&($borang_ik->jika_guna_kenderaan != 'lain-lain')){
            $table->addCell(5300)->addText($borang_ik->jika_guna_kenderaan, ['bold' => true], ['spaceAfter' => 0]);
        }
        else{
            $table->addCell(5300)->addText($borang_ik->jenis_kenderaan_lain, ['bold' => true], ['spaceAfter' => 0]);
        }

        if(!is_null($borang_ik->jika_guna_kenderaan)){
            $table->addRow();
            $table->addCell(4500)->addText('  No. Pendaftaran Kenderaan ', [], ['spaceAfter' => 0]);
            $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);
            $table->addCell(5300)->addText($borang_ik->no_plate_kenderaan, ['bold' => true], ['spaceAfter' => 0]);
        }

        $table->addRow();
        $table->addCell(4500)->addText('g) Tarikh mula berniaga ', [], ['spaceAfter' => 0]);
        $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);
        $table->addCell(5300)->addText($borang_ik->dt_mula_niaga, ['bold' => true], ['spaceAfter' => 0]);

        $table->addRow();
        $table->addCell(4500)->addText('   Masa berniaga ', [], ['spaceAfter' => 0]);
        $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);
        $table->addCell(5300)->addText('Dari jam : '.$borang_ik->masa_berniaga_mula, ['bold' => true], ['spaceAfter' => 0]);

        $table->addRow();
        $table->addCell(4500)->addText('h) Kebersihan semasa berniaga ', [], ['spaceAfter' => 0]);
        $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);
        $table->addCell(5300)->addText('Kebersihan diri : '.$borang_ik->kebersihan_diri, ['bold' => true], ['spaceAfter' => 0]);

        $table->addRow();
        $table->addCell(4700, ['gridSpan' => 2])->addText('', [], ['spaceAfter' => 0]);
        $table->addCell(5300)->addText('Kebersihan makanan : '.$borang_ik->kebersihan_makanan, ['bold' => true], ['spaceAfter' => 0]);

        $table->addRow();
        $table->addCell(4700, ['gridSpan' => 2])->addText('', [], ['spaceAfter' => 0]);
        $table->addCell(5300)->addText('Kebersihan kawasan : '.$borang_ik->kebersihan_kawasan, ['bold' => true], ['spaceAfter' => 0]);

        $table->addRow();
        $table->addCell(4700, ['gridSpan' => 2])->addText('', [], ['spaceAfter' => 0]);
        $table->addCell(5300)->addText('(jika tidak: Keluarkan notis) ', ['bold' => true], ['spaceAfter' => 0]);

        $table->addRow();
        $table->addCell(4500)->addText('i) Laporan kebersihan kawasan/tapak selepas berniaga ', [], ['spaceAfter' => 0]);
        $table->addCell(200)->addText(':', [], ['spaceAfter' => 0]);

        if(!is_null($borang_ik->laporan_kebersihan_selepas_niaga)){
            $table->addCell(5300)->addText('', ['bold' => true], ['spaceAfter' => 0]);

            $text = htmlspecialchars($borang_ik->tiada_halangan_syarat, ENT_QUOTES | ENT_XML1, 'UTF-8');
            $table->addRow();
            $table->addCell(10000, ['gridSpan' => 3])->addText($text, ['bold' => true], ['spaceAfter' => 0]);
        }
        else{
            $table->addCell(5300)->addText('', ['bold' => true], ['spaceAfter' => 0]);
        }


        $table = $section->addTable();
        $table->addRow();
        
        $cell1 = $table->addCell(3300);
        $cell2 = $table->addCell(3300);
        $cell3 = $table->addCell(3300);
        
        $baseDir = "dokumens/penjaja/{$borang_ik->lesen_penjaja_id}/ik/";
        
        $addImg = function ($cell, $filename) use ($baseDir) {
            $filename = trim((string) $filename);
            if ($filename === '') return; // skip kalau kosong
        
            $rel = $baseDir . $filename;
        
            // penting: exists sahaja tak cukup sebab folder pun exists
            if (!Storage::exists($rel)) return;
        
            $full = Storage::path($rel);
            if (!is_file($full)) return; // elak folder/invalid
        
            $cell->addImage($full, [
                'width'  => 200,
                'height' => 140,
            ]);
        };
        
        $addImg($cell1, $borang_ik->gambar1);
        $addImg($cell2, $borang_ik->gambar2);
        $addImg($cell3, $borang_ik->gambar3);

        
        $section->addLine(['weight' => 1, 'width' => 500, 'height' => 0,  'color' => '000000']);

        $section->addText('3) CADANGAN' );
        
        $tableStyle = new Table();
        $tableStyle->setWidth(new TblWidth(10000));
        $tableStyle->setBorderSize(0);
        $tableStyle->setBorderColor('FFFFFF');
        $tableStyle->setCellMargin(80);
        $tableStyle->setAlignment(JcTable::START);
        
        // cara betul set indent
        $tableStyle->setIndent(new TblWidth(720)); // 720 twip = 0.5 inch
        // Create table with 3 columns
        $table = $section->addTable($tableStyle);

        if($borang_ik->cadangan == 'tiada halangan'){
            $table->addRow();
            $table->addCell(1000)->addText('[ / ] ', [], ['spaceAfter' => 0]);
            $table->addCell(9000, ['gridSpan' => 2])->addText('Tiada halangan dengan syarat :', [], ['spaceAfter' => 0]);

            $text = htmlspecialchars($borang_ik->tiada_halangan_syarat, ENT_QUOTES | ENT_XML1, 'UTF-8');
            $table->addRow();
            $table->addCell(10000, ['gridSpan' => 2])->addText($text, [], ['spaceAfter' => 0]);

            if($borang_ik->tiada_halangan_notis_dipatuhi == '1'){
                $table->addRow();
                $table->addCell(1000)->addText('', [], ['spaceAfter' => 0]);
                $table->addCell(9000, ['gridSpan' => 2])->addText('[ / ] Notis yang telah dikeluarkan perlu dipatuhi', [], ['spaceAfter' => 0]);
            }

        }
        elseif($borang_ik->cadangan == 'tidak menyokong'){
            $table->addRow();
            $table->addCell(1000)->addText('[ / ] ', [], ['spaceAfter' => 0]);
            $table->addCell(9000, ['gridSpan' => 2])->addText('Tidak menyokong atas sebab :', [], ['spaceAfter' => 0]);

            $table->addRow();
            $table->addCell(10000, ['gridSpan' => 2])->addText($borang_ik->tidak_menyokong_atas_sebab, [], ['spaceAfter' => 0]);

            if($borang_ik->dirujuk_unit_penguatkuasa == '1'){
                $table->addRow();
                $table->addCell(1000)->addText('', [], ['spaceAfter' => 0]);
                $table->addCell(9000, ['gridSpan' => 2])->addText('[ / ] Dirujuk Unit Penguatkuasa untuk tindakan : ', [], ['spaceAfter' => 0]);

                $table->addRow();
                $table->addCell(2000, ['gridSpan' => 2])->addText('', [], ['spaceAfter' => 0]);
                $table->addCell(8000)->addText($borang_ik->dirujuk_unit_penguatkuasa_txt, [], ['spaceAfter' => 0]);
            }

        }
        elseif($borang_ik->cadangan == 'permohonan / lesen dibatalkan kerana tamat perniagaan'){
            $table->addRow();
            $table->addCell(1000)->addText('[ / ] ', [], ['spaceAfter' => 0]);
            $table->addCell(9000, ['gridSpan' => 2])->addText('Permohonan / Lesen dibatalkan kerana tamat perniagaan', [], ['spaceAfter' => 0]);
        }

        
        $section->addText('');
        $section->addText('Dirujuk ulasan dan cadangan untuk tindakan tuan selanjutnya.' );
        $section->addText('Sekian, dimaklumkan. Terima kasih.' );

        
        $section->addText('');        
        $section->addText('');    
        $section->addText("(".$borang_ik->pegawai->name.")");
        $section->addText("Penolong Pegawai Kesihatan Persekitaran U5");
        $section->addText("Jabatan Pelesenan");
        $section->addText("Majlis Bandaraya Iskandar Puteri");


        $filename = 'laporan_tapak-' . $borang_ik->id . '.docx';
        $path = storage_path('app/public/' . $filename);
        
        $writer = IOFactory::createWriter($phpWord, 'Word2007');
        $writer->save($path);


        return response()->download($path)->deleteFileAfterSend(true);
    }

    
    public function cetak_cadangan_pegawai(Request $request, $id, $borang_ik_id)
    {
        $headText = ['name' => 'Arial', 'size' => 10];
            
        $borang_ik = BorangUlasanIk::findOrFail($request->borang_ik_id);
        
        $phpWord = new PhpWord();

        $phpWord->setDefaultFontName('Arial');   // Jenis font
        $phpWord->setDefaultFontSize(8);         // Saiz font kecil

        $phpWord->addTitleStyle(
            1, // heading level (1 = Heading1)
            NULL,
            ['alignment' => 'center']
        );

        $section = $phpWord->addSection([
                'orientation' => 'portrait',  
                'marginLeft' => 1000,
                'marginRight' => 1000,  
                'marginTop' => 500,
            ]);

        $footer = $section->addFooter();
        $footer->addText(
            'Rujukan Fail : MBIP(L)(PS) '.$borang_ik->lesen_penjaja->no_fail_lesen,
            ['size' => 8, 'italic' => true],
            ['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::START]
        );

        $table = $section->addTable();

        $table->addRow();
        $cell1 = $table->addCell(2000);
        $cell1->addImage(public_path('img/mbip-crown-png45x45.png'), ['width' => 45, 'height' => 45]);
        
        $cell2 = $table->addCell(6000);
        $cell2->addText('JABATAN PELESENAN', ['bold' => true, 'size' => 12], ['spaceAfter' => 0]);
        $cell2->addText('MAJLIS BANDARAYA ISKANDAR PUTERI', ['bold' => true, 'size' => 12]);
        
        $cell3 = $table->addCell(2000);
        $cell3->addText(" L02-07 \n(PIN-7)", ['size' => 8, 'bgColor'=>'CCCCCC'], ['alignment' => 'right', 'spaceAfter' => 0]);
        
        $tableStyle = new Table();
        $tableStyle->setWidth(new TblWidth(10000));
        $tableStyle->setBorderSize(0);
        $tableStyle->setBorderColor('FFFFFF');
        $tableStyle->setCellMargin(80);
        $tableStyle->setAlignment(JcTable::START);
        
        // Create table with 3 columns
        $table = $section->addTable($tableStyle);

        $row = $table->addRow();

        $cell = $table->addCell(7000);
        $textRun = $cell->addTextRun();
        $textRun->addText(
            'Ruj. Fail : ',
            ['name' => 'Arial', 'size' => 8]
        );
        
        $textRun->addText(
            'MBIP(L)(PS) '.$borang_ik->lesen_penjaja->no_fail_lesen,
            ['name' => 'Arial', 'size' => 10, 'bold' => true]
        );

        $cell = $table->addCell(3000);
        $textRun = $cell->addTextRun();

        $textRun->addText('Tarikh : ', [], ['alignment' => 'right', 'spaceAfter' => 0]);
        $textRun->addText($borang_ik->ulasanPegawai->dt_pegawai_ulas, ['bold' => true], ['alignment' => 'right', 'spaceAfter' => 0]);
        

        $section->addText('Pengarah Pelesenan', ['name' => 'Arial', 'size' => 7], ['spaceAfter'=> 0]);
        $section->addText('Jabatan Pelesenan', ['name' => 'Arial', 'size' => 7], ['spaceAfter'=> 1]);
        $section->addText(' ' );

        $section->addText('SEMAKAN LAPORAN DAN CADANGAN KE ATAS PERMOHONAN LESEN PENJAJA /GERAI / ARKED /PASAR', ['size' => 9, 'bold' => true], ['alignment' => 'left', 'spaceAfter' => 1]);
        

        if($borang_ik->ulasanPegawai->pegawai_cadangan == 'tiada halangan'){
            $textRun = $section->addTextRun();
            $textRun->addText('[ / ] ');
            $textRun->addText(ucwords($borang_ik->ulasanPegawai->pegawai_cadangan), ['bold' => true]);
            $textRun->addText(' dengan syarat : ');
            $textRun->addText($borang_ik->ulasanPegawai->pegawai_syarat);    

            if($borang_ik->ulasanPegawai->pegawai_notis_perlu_dipatuhi){
                $section->addText('[ / ] Notis yang telah dikeluarkan perlu dipatuhi');
            }        
        }
        elseif($borang_ik->ulasanPegawai->pegawai_cadangan == 'tidak menyokong'){
            
            $textRun = $section->addTextRun();
            $textRun->addText('[ / ] ');
            $textRun->addText(ucwords($borang_ik->ulasanPegawai->pegawai_cadangan), ['bold' => true]);
            $textRun->addText(' atas sebab : ');
            $textRun->addText($borang_ik->ulasanPegawai->pegawai_tidak_menyokong_sebab);  
  
            if($borang_ik->ulasanPegawai->pegawai_dirujuk_unit_penguatkuasa){ 
                $textRun = $section->addTextRun();
                $textRun->addText('[ / ] Dirujuk untuk ');   
                $textRun->addText(' tindakan Unit Penguatkuasaan', ['bold' => true]);
            }        
        }
        elseif($borang_ik->ulasanPegawai->pegawai_cadangan == 'ditangguhkan untuk tindakan/siasatan semula'){
            $textRun = $section->addTextRun();
            $textRun->addText('[ / ] Permohonan ini ');   
            $textRun->addText(ucwords($borang_ik->ulasanPegawai->pegawai_cadangan), ['bold' => true]);         
        }

        if($borang_ik->ulasanPegawai->pegawai_catatan != ''){
            $section->addText('');
            $section->addText('Catitan: ');
            $section->addText($borang_ik->ulasanPegawai->pegawai_catatan);
        }

        $section->addText('Sekian dimaklumkan, terima kasih.');
        $section->addText(' ');
        $section->addText(' ');

        $section->addText("(".strtoupper($borang_ik->ulasanPegawai->pegawai->name).")", ['bold'=>true]);
        $section->addText('Penolong Pengarah Pelesenan');
        $section->addText('Majlis Bandaraya Iskandar Puteri');

        $section->addLine(['weight' => 1, 'width' => 500, 'height' => 0,  'color' => '000000']);

        $section->addText('Tarikh : '.$borang_ik->ulasanPegawai->dt_pengarah_ulas, [], ['alignment' => 'right']);
        

        $section->addText('Tuan Setiausaha', ['name' => 'Arial', 'size' => 7], ['spaceAfter'=> 0]);
        $section->addText('Majlis Bandaraya Iskandar Puteri', ['name' => 'Arial', 'size' => 7], ['spaceAfter'=> 0]);

        $section->addText('');        
        $section->addText('ULASAN KE ATAS CADANGAN PERMOHONAN LESEN PENJAJA / GERAI / ARKED / PASAR', ['size' => 9, 'bold' => true], ['alignment' => 'left']);
        
        $section->addText('Hasil dari semakan dan cadangan ke atas laporan Pegawai Pemeriksa, saya mencadangkan bahawa permohonan ini : ');
        $section->addText('[ / ] Dibawa ke Mesyuarat Jawatankuasa Kesihatan dan Lesen untuk : ');
        
        $tableStyle = new Table();
        $tableStyle->setWidth(new TblWidth(10000));
        $tableStyle->setBorderSize(0);
        $tableStyle->setBorderColor('FFFFFF');
        $tableStyle->setCellMargin(80);
        $tableStyle->setAlignment(JcTable::START);
        
        // cara betul set indent
        $tableStyle->setIndent(new TblWidth(720)); // 720 twip = 0.5 inch
        // Create table with 3 columns
        $table = $section->addTable($tableStyle);

        if($borang_ik->ulasanPegawai->pengarah_ulasan == 'dipertimbangkan'){
            $row = $table->addRow();
            $table->addCell(2500)->addText('[ / ] Dipertimbangkan ', ['bold' => true], ['spaceAfter' => 0]);
            $table->addCell(7500)->addText(' / diperakukan kelulusan lesen dengan bersyarat.', [], ['spaceAfter' => 0]);
        }
        elseif($borang_ik->ulasanPegawai->pengarah_ulasan == 'ditolak'){
            $row = $table->addRow();
            $table->addCell(2500)->addText('[ / ] Ditolak ', ['bold' => true], ['spaceAfter' => 0]);
            $table->addCell(7500)->addText(' kerana ', [], ['spaceAfter' => 0]);

            if($borang_ik->ulasanPegawai->pengarah_ditolak_sebab == 'pemohon gagal mematuhi syarat'){
                $row = $table->addRow();
                $table->addCell(2500)->addText('', ['bold' => true], ['spaceAfter' => 0]);
                $table->addCell(7500)->addText(' [ / ] pemohon gagal mematuhi syarat.', [], ['spaceAfter' => 0]);
            }
            elseif($borang_ik->ulasanPegawai->pengarah_ditolak_sebab == 'perniagaan dijalankan bukan seperti dipohon'){
                $row = $table->addRow();
                $table->addCell(2500)->addText('', ['bold' => true], ['spaceAfter' => 0]);
                $table->addCell(7500)->addText(' [ / ] perniagaan dijalankan bukan seperti dipohon.', [], ['spaceAfter' => 0]);
            }

        }        
        elseif($borang_ik->ulasanPegawai->pengarah_ulasan == 'ditangguhkan'){
            $row = $table->addRow();
            $table->addCell(2500)->addText('[ / ] Ditangguhkan ', ['bold' => true], ['spaceAfter' => 0]);
            $table->addCell(7500)->addText(' sehingga pihak pemohon mematuhi syarat.', [], ['spaceAfter' => 0]);
        }

        if($borang_ik->ulasanPegawai->pengarah_catatan != ''){
            $section->addText('');
            $section->addText('Catitan: ');
            $section->addText($borang_ik->ulasanPegawai->pengarah_catatan);
        }
        
        $section->addText('');
        $section->addText('Sekian, dimaklumkan. Terima kasih.' );

        
        $section->addText('');        
        $section->addText('');    
        $section->addText("(".$borang_ik->ulasanPegawai->pengarah->name.")", ['bold' => true]);
        $section->addText("Pengarah Pelesenan");
        $section->addText("Majlis Bandaraya Iskandar Puteri");


        $filename = 'ulasan_pegawai-' . $borang_ik->id . '.docx';
        $path = storage_path('app/public/' . $filename);
        
        $writer = IOFactory::createWriter($phpWord, 'Word2007');
        $writer->save($path);


        return response()->download($path)->deleteFileAfterSend(true);
    }


}
