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

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

    td,
    th {
        padding: .5em;
    }
</style>
<h3 class='text-left'>即時訊息</h3>

<?php if ($this->Permission_model->readonly($_controller)) : ?>
    <table datatables_existing filter="messaging">
        <thead>
            <tr>
                <th>標題</th>
                <th>內容</th>
                <th>發佈日期</th>
                <th>發佈狀態</th>
                <th>推播狀態</th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($items as $_ => $item) {
                $pushStatus = $item['push'] == 1 ? '有推播' : '沒有推播';
                $publishedStatus = $item['published'] >= 1 ? '已發佈' : '未發佈';
                echo "<tr>
                    <td>{$item['title']}</td>
                    <td>{$item['content']}</td>
                    <td>{$item['issued_date']}</td>
                    <td>$publishedStatus</td>
                    <td>$pushStatus</td>
                </tr>";
            }
            ?>
        </tbody>
    </table>
<?php else : ?>
    <table datatables_existing filter="messaging" <?php if ($this->Permission_model->can($_controller, 'add')) : ?>data-add="/messaging/add" <?php endif; ?>>
        <thead>
            <tr>
                <th>標題</th>
                <th>內容</th>
                <th>發佈日期</th>
                <th>發佈狀態</th>
                <th>推播狀態</th>
                <th>動作</th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($items as $_ => $item) {
                $pushStatus = $item['push'] == 1 ? '有推播' : '沒有推播';
                $publishedStatus = $item['published'] >= 1 ? '已發佈' : '未發佈';
                $actions = '';
                if ($this->Permission_model->can($_controller, 'edit')) {
                    $actions .= "<a href='/messaging/edit?id={ID}" . "'>修改</a>";
                }
                if ($this->Permission_model->can($_controller, 'delete')) {
                    $actions .= "<a class='del' href='javascript:void(0);' onclick='javascript:deleteRecord(\"{ID}\");'>刪除</a>";
                }
                $actions = str_replace("{ID}", $item['id'], $actions);
                echo "<tr>
                    <td>{$item['title']}</td>
                    <td>{$item['content']}</td>
                    <td>{$item['issued_date']}</td>
                    <td>$publishedStatus</td>
                    <td>$pushStatus</td>
                    <td>$actions</td>
                </tr>";
            }
            ?>
        </tbody>
    </table>
    <?php if ($this->Permission_model->can($_controller, 'delete')) : ?>
        <script>
            function deleteRecord(id) {
                var url = '<?php echo  base_url('messaging') ?>/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; ?>