<?= $this->extend('layouts/window.php') ?>

<?= $this->section('content') ?>

<div class="container-fluid mb-3">

    <div class="row page-titles">
        <div class="col-sm-12 align-self-center ">
            <h2 class="text-themecolor">Bugs Report</h2>
        </div>

    </div>

    <?php
        foreach ($bugs as $data) {
            $reportdate = date_create($data['reportdate']);
    ?>

    <div class="row">
        <div class="col-12">

        <?php

            // Fitur Untuk Edit dan Hapus BUGS
            if ( ($data['userid'] == $_SESSION['userid']) && ($data['bugstatus'] == 'O') ) {
                // Untuk Edit Post
                echo "<a href='/bugs/edit/".$data['bugid']."' class='btn btn-primary btn-sm mt-1 mb-1'>
                        <i class='fa-solid fa-bug'></i> &nbsp;Edit Post
                      </a>&nbsp;";
            }

            // Bila Kondisi Status CLOSE
            if ($data['bugstatus'] == 'C') {
                if ($data['userid_closer'] == $_SESSION['userid'] || $data['userid'] == $_SESSION['userid']) {

                    // Untuk Edit ReOpen Bugs
                    echo "<a href='/bugs/toggle_reopen/".$data['bugid']."' class='btn btn-outline-danger btn-sm mt-1 mb-1'>
                            <i class='fa-solid fa-bug'></i> &nbsp;ReOpen Bugs
                          </a>&nbsp;";
                }
            }

            // Bila Kondisi Status OPEN
            if ($data['bugstatus'] == 'O') {
                // Untuk Mark As Down 
                echo "<a class='btn btn-outline-success btn-sm mt-1 mb-1 me-2' id='markAsDone' href='/bugs/toggle_close/".$data['bugid']."'>
                        <i class='fa-solid fa-check'></i> &nbsp;Mark as Done
                      </a>";

                echo "<a class='btn btn-outline-warning btn-sm mt-1 mb-1 me-2 ' id='markAsDone' href='/bugs/toggle_pending/".$data['bugid']."'>
                        <i class='fa-regular fa-clock'></i> &nbsp;Mark as Pending
                      </a>";

                echo "<a class='btn btn-outline-dark btn-sm mt-1 mb-1 me-2' id='markAsDone' href='/bugs/toggle_suspend/".$data['bugid']."'>
                      <i class='fa-regular fa-clock'></i> &nbsp;Suspend Bugs
                    </a>";
            }

            // Bila Kondisi Status Pending
            if ($data['bugstatus'] == 'P') {
                echo "<a href='/bugs/toggle_reopen/".$data['bugid']."' class='btn btn-outline-dark btn-sm mt-1 mb-1'>
                        <i class='fa-solid fa-bug'></i> &nbsp;ReOpen Bugs
                      </a>&nbsp;";
            }

            // Bila Kondisi Status Suspend
            if ($data['bugstatus'] == 'S') {
                // Untuk Mark As Down 
                echo "<a href='/bugs/toggle_reopen/".$data['bugid']."' class='btn btn-outline-dark btn-sm mt-1 mb-1'>
                      <i class='fa-solid fa-bug'></i> &nbsp;ReOpen Bugs
                    </a>&nbsp;";
            }

        ?>

            <div class="card shadow">

                <?php 
                    if($data['bugstatus'] == 'C') {
                        echo ("<h3 class='card-header border bg-success text-white'>
                                   #Closed
                               </h3>");
                    } else if($data['bugstatus'] == 'O') {
                        echo ("<h3 class='card-header border bg-danger text-white'>
                                    #Still Open
                               </h3>");
                    } else if($data['bugstatus'] == 'S') {
                        echo ("<h3 class='card-header border bg-dark text-white'>
                                    #Suspend
                               </h3>");
                    } else {
                        echo ("<h3 class='card-header border bg-warning text-dark'>
                                    #Pending
                               </h3>");
                    }
                ?>

                <div class="card-body">
                    <h2 class="card-title"><?= $data['bugtitle'];?> <span
                            class='text-muted'><em>#<?= $data['bugid'];?></em></span></h2>
                    <h5 class="card-subtitle mt-3 mb-3 text-muted">
                        creator <span class='fw-bold text-dark'><?= $data['firstname'];?>
                            <?= $data['lastname'];?></span>
                        on
                        <u class='fw-light text-dark'><?= date_format($reportdate,"D, d M Y");?></u>
                    </h5>
                  
                    <hr>

                    <div class="card-text"><?= $data['bugdetail'];?></div>
                </div>
            </div>
        </div>
    </div>

    <?php
        }

        // For Comments
        if ( $bugcomment != null ) {
            foreach ($bugcomment as $data) {
                $logdate = date_create($data['logdate']);
    ?>

    <div class="row">
        <div class="col-12">
            <div class="card shadow">
            <?php
                if($data['userid'] == $_SESSION['userid']) {
                    echo ("<h5 class='card-header bg-primary text-white'>
                                <div class='row'>
                                    <div class='col-8'>
                                        <span class='fw-bold'>
                                            " .$data['firstname']. " " .$data['lastname']. "
                                        </span>
                                        on <u class='fw-light'>" .date_format($logdate,'D, d M Y - H:i'). "</u>
                                    </div>
                                    <div class='col-4 text-end'>
                                        <a class='text-light' href='/bugcomment/edit/" .$data['bugcommentid']. "'><i class='fa-solid fa-pen-to-square'></i></a>
                                            &nbsp;
                                        <a class='text-light' href='/bugcomment/delete/" .$data['bugcommentid']."/". $data['bugid']. "' 
                                        ><i class='fa-regular fa-trash-can'></i></a> 
                                    </div>
                                </div>
                            </h5>
                        ");           
                } else {
                    echo ("<h5 class='card-header'>
                                <span class='fw-bold'>".$data['firstname']." ".$data['lastname']."</span>
                                on <u class='fw-light'>".date_format($logdate,'D, d M Y - H:i')."</u>
                            </h5>");
                }       
            ?>

                <div class="card-body">
                    <h5 class="card-text"><?= $data['bugcommenttext'] ?></h5>
                </div>
            </div>
        </div>
    </div>
    
    <?php
            }
        } 
    ?>

    <hr>

    <?php 
        foreach ($bugs as $data) {
            if ($data['bugstatus'] == 'O') {
    ?>

    <form method='post' class='mt-4'>
        <div class="form-group row">
            <div class="col-12">
                <textarea class="form-control mb-1 editor" id="comment_textarea" name="bugcommenttext" placeholder="Silahkan berkomentar"></textarea>
            </div>
        </div>
        <input type="hidden" name='bugid' value='<?= $data['bugid']; ?>'>
        <input type="hidden" name='userid' value='<?= $_SESSION['userid']; ?>'>

        <button type="button" class="btn btn-dark float-start me-3" onclick="window.close()">Cancel</button>
        <button type="submit" name="send_comment" value="send_comment" class="btn btn-success text-white me-3">Send Comment!</button>
        <button type="submit" name="send_comment_and_done" value="send_comment_and_done" class=" btn btn-outline-success ">Send Comment and Mark As Done!</button>
    </form>

    <?php
            }
        }
    ?>

</div>

<?= $this->endSection() ?>

<?= $this->section('script') ?>
<script src="<?=base_url();?>/assets/tinymce/tinymce.min.js"></script>
<script>
    // config = {
    //     selector: '#comment_textarea',
    //     promotion: false,
    //     menubar: false,
    //     forced_root_block: 'pre',
    //     plugins: 'lists',
    //     // plugins: 'emoticons',
    //     // relative_urls: true,
    //     toolbar: 'insertfile undo redo styles styleselect | bold italic | forecolor backcolor emoticons | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
	
    // };
    // tinymce.init(config);
    
    // config
    config = {
        selector:'textarea',
        height:'350',
        promotion:false,
        // forced_root_block : 'pre',
        paste_data_images:false,
        content_style: "p { margin: 0; }",
        plugins: 'preview importcss searchreplace autolink autosave save code visualblocks visualchars fullscreen link template codesample table charmap pagebreak nonbreaking anchor insertdatetime advlist lists wordcount charmap emoticons',
        menubar: 'file edit view insert format tools table',
        toolbar: 'undo redo | bold italic underline strikethrough | fontfamily fontsize blocks | alignleft aligncenter alignright alignjustify | outdent indent |  numlist bullist | forecolor backcolor removeformat | pagebreak | charmap emoticons | fullscreen  preview save print | template link anchor codesample',
        autosave_ask_before_unload: true,
        autosave_interval: '30s',
        automatic_uploads: false,
    };
    tinymce.init(config);
</script>

<?= $this->endSection() ?>