<?php
$this->css($this->assetModule('script/admin.css'));
$this->jQuery();
$this->Backbone();
?>

<div id="pi-js-list">
    <table class="table table-striped">
        <tbody>
        <tr>
            <th><?php echo _a('Image'); ?></th>
            <th style="width: 250px"><?php echo _a('Title'); ?></th>
            <th><?php echo _a('Article Number'); ?></th>
            <th><?php echo _a('Template'); ?></th>
            <th><?php echo _a('Description'); ?></th>
        </tr>
        <?php foreach ($topics as $topic) { ?>
            <tr>
                <td>
                    <?php if ($topic['image']) {
                        $image = Pi::url($topic['image']);
                        ?>
                        <img src="<?php echo $this->escape($image); ?>"
                             alt="<?php echo $this->escape($topic['title']); ?>" style="width: 40px">
                        <?php
                    } else {
                        ?>
                        <div class="list-image"><i class="icon-book"></i></div>
                        <?php
                    } ?>
                </td>
                <td><a target="_blank" href="<?php echo $this->url($route, [
                        'controller' => 'topic',
                        'action'     => 'index',
                        'topic'      => $topic['slug'] ?: $topic['id']])
                    ?>"><?php echo $this->escape($topic['title']); ?></a>
                    <?php if (!$topic['active']) { ?>
                        - <strong class="text-warning"><?php echo _a('Deactive'); ?></strong>
                    <?php } ?>
                    <div class="row-actions">
                        <a href="<?php echo $this->url('', ['action' => 'list-article', 'topic' => $topic['id']]); ?>">
                            <?php echo _a('Manage'); ?>
                        </a> |
                        <a href="<?php echo $this->url('', ['action' => 'pull', 'topic' => $topic['id']]); ?>">
                            <?php echo _a('Pull'); ?>
                        </a> |
                        <a href="<?php echo $this->url('', [
                            'action' => 'edit',
                            'id'     => $topic['id'],
                        ]); ?>"><?php echo _a('Edit'); ?></a> |
                        <?php if ($topic['active']) { ?>
                            <a class="one-action"
                               data-id="<?php echo $this->escape($topic['id']); ?>"
                               data-value="active"
                               data-status="0"
                               href="javascript:void(0)"><?php echo _a('Deactivate'); ?></a>
                        <?php } else { ?>
                            <a class="one-action"
                               data-id="<?php echo $this->escape($topic['id']); ?>"
                               data-value="active"
                               data-status="1"
                               href="javascript:void(0)"><?php echo _a('Active'); ?></a>
                        <?php } ?> |
                        <a href="<?php echo $this->url('', [
                            'action' => 'delete',
                            'id'     => $topic['id'],
                        ]); ?>" onclick="return window.confirm('Really delete?');"><?php echo _a('Delete'); ?></a>
                    </div>
                </td>
                <td><?php echo isset($count[$topic['id']]) ? $this->escape($count[$topic['id']]) : 0 ?></td>
                <td><?php echo $this->escape($topic['template']); ?></td>
                <td><?php
                    echo $topic['description']
                        ? $this->escape($topic['description'])
                        : '<span class="muted">' . _a('No description') . '</span>'
                    ?>
                </td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
</div>

<script>
    (function ($) {
        var page = {
            url: "<?php echo $this->url('', [
                'controller' => 'topic',
                'action'     => '',
            ]); ?>".replace(/list-topic\/$/, ""),
            el: $("#pi-js-list"),
            $: function (selector) {
                return this.el.find(selector);
            },
            init: function () {
                _.bindAll(this);
                this.$(".one-action").click(this.oneAction);
            },
            oneAction: function (e) {
                var el = $(e.target),
                    id = el.attr("data-id"),
                    val = el.attr("data-value"),
                    status = el.attr("data-status");
                if (status) {
                    location.href = this.url + val + "?id="
                        + id + "&status="
                        + status + "&from="
                        + this.encodeUrl();
                } else {
                    location.href = this.url + val + "?id=" + id + "&from="
                        + this.encodeUrl();
                }
            },
            encodeUrl: function () {
                return encodeURIComponent(location.href);
            }
        }
        page.init();
    })(jQuery)
</script>
