<style>
    table {
        border-collapse: collapse;
    }

    table,
    th,
    td {
        border: 1px solid black;
    }

    td,
    th {
        padding: .5em;
    }
</style>

<h3 class="text-left">學生表現評語記錄 - <?php echo $_controller == 'comment_gro' ? 'GRO' : '教師' ?></h3>

<?php if ($this->Permission_model->readonly($_controller)) : ?>
    <!--Readonly-->
    <table datatables_existing filter="comment">
        <thead>
            <tr>
                <th>課程名稱</th>
                <th>學生名稱</th>
                <th>學生表現評語</th>
                <th>評語日期</th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($items as $_ => $item) {
                $item['date'] = empty($item['date']) || $item['date'] == '0000-00-00' ? '-' : $item['date'];
                if ($item['tailor_comment']) {
                    $_comments = $item['tailor_comment'];
                } else {
                    $_comments = '<ul>';
                    $strpc = '<ul>';
                    $dc = $item['dc'];
                    if (!empty($dc)) {
                        foreach ($dc as $_ => $v) {
                            $_comments .= "<li>" . $v['comment'] . "</li>";
                        }
                    }
                    $pc = $item['pc'];
                    if (!empty($pc)) {
                        foreach ($pc as $_ => $v) {
                            $_comments .= "<li>" . $v['comment'] . "</li>";
                        }
                    }
                    $_comments . "</ul>";
                }
                echo "<tr>
                    <td>{$item['course_name']}</td>
                    <td>{$item['student_chi_name']}</td>
                    <td>$_comments</td>
                    <td>{$item['date']}</td>
                </tr>";
            }
            ?>
        </tbody>
    </table>
<?php else : ?>
    <table datatables_existing filter="comment" <?php if ($this->Permission_model->can($_controller, 'add')) : ?>data-add="<?php echo $_controller; ?>/add" <?php endif; ?>>
        <thead>
            <tr>
                <th>課程名稱</th>
                <th>學生名稱</th>
                <th>學生表現評語</th>
                <th>評語日期</th>
                <th>動作</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $_actions = '';
            if ($this->Permission_model->can($_controller, 'edit')) {
                $_actions .= "<a href='/" . $_controller . "/edit?id={ID}" . "'>修改</a>";
            }
            if ($this->Permission_model->can($_controller, 'delete')) {
                $_actions .= "<a class='del' href='javascript:void(0);' onclick='javascript:deleteRecord(\"{ID}\");'>刪除</a>";
            }
            foreach ($items as $_ => $item) {
                $item['date'] = empty($item['date']) || $item['date'] == '0000-00-00' ? '-' : $item['date'];
                if ($item['tailor_comment']) {
                    $_comments = $item['tailor_comment'];
                } else {
                    $_comments = '<ul>';
                    $strpc = '<ul>';
                    $dc = $item['dc'];
                    if (!empty($dc)) {
                        foreach ($dc as $_ => $v) {
                            $_comments .= "<li>" . $v['comment'] . "</li>";
                        }
                    }
                    $pc = $item['pc'];
                    if (!empty($pc)) {
                        foreach ($pc as $_ => $v) {
                            $_comments .= "<li>" . $v['comment'] . "</li>";
                        }
                    }
                    $_comments . "</ul>";
                }

                $actions = $_actions;
                $actions = str_replace("{ID}", $item['id'], $actions);
                echo "<tr>
                    <td>{$item['course_name']}</td>
                    <td>{$item['student_chi_name']}</td>
                    <td>$_comments</td>
                    <td>{$item['date']}</td>
                    <td>$actions</td>
                </tr>";
            }
            ?>
        </tbody>
    </table>

    <?php if ($this->Permission_model->can($_controller, 'delete')) : ?>
        <!--Delete record Javascript-->
        <script>
            function deleteRecord(id) {
                var url = '/<?php echo $_controller; ?>/delete?id=' + id;
                //        alert('url:'+url);return;
                if (confirm('確認刪除記錄?')) {
                    $.ajax({
                        url: url,
                        method: 'get',
                        dataType: 'json',
                        success: function(response) {
                            if (response !== null) {
                                alert('刪除成功');
                                location.reload();
                            }
                        }
                    });
                }
            }
        </script>
    <?php endif; ?>
<?php endif; ?>


<h3>評語列表</h3>
<table datatables_existing>
    <thead>
        <tr>
            <th>編號</th>
            <th>代碼</th>
            <th>評語</th>
        </tr>
    </thead>
    <tbody>
        <?php
        foreach ($allcomments as $_ => $item) {
            echo "<tr>
                <td>{$item['id']}</td>
                <td>{$item['code']}</td>
                <td>{$item['comment']}</td>
            </tr>";
        }
        ?>
    </tbody>
</table>