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

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

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

<h3 class='text-left'>即時訊息</h3>
<table datatables_existing data-add="/messaging/add" filter="messaging_approval">
    <thead>
        <tr>
            <th>標題</th>
            <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 = "<a href='/messaging/edit?id={ID}" . "'>修改</a>&nbsp;&nbsp;<a class='del' href='javascript:void(0);' onclick='javascript:deleteRecord(\"{ID}\");'>刪除</a>";
            switch ($item['approval']) {
                case 0:
                    $actions = "<a href='javascript:void(0);' onclick='javascript:approval({ID},2);'>接受審批</a><a href='javascript:void(0);' onclick='javascript:approval({ID},1);'>不接受審批</a><a href='/messaging/edit?id={ID}" . "'>修改</a><a class='del' href='javascript:void(0);' onclick='javascript:deleteRecord(\"{ID}\");'>刪除</a>";
                    $actions = str_replace("{ID}", $item['id'], $actions);
                    // $approvalStatus = '-';
                    $approvalStatus = '等待審批';
                    break;
                case 1:
                    $actions = "<a href='/messaging/edit?id={ID}" . "'>修改</a><a class='del' href='javascript:void(0);' onclick='javascript:deleteRecord(\"{ID}\");'>刪除</a>";
                    // $approvalStatus = '<i class="fa fa-times"></i>';
                    $approvalStatus = '不接受審批';
                    break;
                case 2:
                    $actions = "<a href='/messaging/edit?id={ID}" . "'>修改</a><a class='del' href='javascript:void(0);' onclick='javascript:deleteRecord(\"{ID}\");'>刪除</a>";
                    // $approvalStatus = '<i class="fa fa-check"></i>';
                    $approvalStatus = '已接受審批';
                case 3:
                    $actions = "<a href='/messaging/edit?id={ID}" . "'>修改</a><a class='del' href='javascript:void(0);' onclick='javascript:deleteRecord(\"{ID}\");'>刪除</a>";
                    // $approvalStatus = '<i class="fa fa-check"></i>';
                    $approvalStatus = '已接受審批';
                    break;
            }
            
            $actions = str_replace("{ID}", $item['id'], $actions);
            echo "<tr>
                <td>{$item['title']}</td>
                <td>{$item['content']}</td>
                <td>$approvalStatus</td>
                <td>{$item['issued_date']}</td>
                <td>$publishedStatus</td>
                <td>$pushStatus</td>
                <td>$actions</td>
            </tr>";
        }
        ?>
    </tbody>
</table>

<script>
    function approval(id, ar) {
        var url = '/messaging/approval?id=' + id + '&ar=' + ar;
        var action = ar === 1 ? 'approved' : 'rejected';
        //        alert('url:'+url);return;
        $.ajax({
            url: url,
            method: 'get',
            dataType: 'json',
            success: function(response) {
                alert('儲存成功');
                location.reload();
            }
        });
    }

    function deleteRecord(id) {
        var url = '/messaging/delete?id=' + id;
        //        alert('url:'+url);return;
        if (confirm('確認刪除記錄?')) {
            $.ajax({
                url: url,
                method: 'get',
                dataType: 'json',
                success: function(response) {
                    alert('刪除成功');
                    location.reload();
                }
            });
        }
    }
</script>