<?php

defined('_MAILING') or die('Restricted access');

$i = 0;
$tags_head = '';
$tags_body = '';

$result = $db->query("
    SELECT
        rt.id,
        rt.name,
        rt.description,
        (
            SELECT count(id_tag) FROM ".PREF."_recipient_tag_conn AS rtc where rtc.id_tag = rt.id
        ) AS used
    FROM 
        ".PREF."_recipient_tag AS rt
");

$tags_head .= "<tr><th>#</th><th>".TAGS_NAME."</th><th>".TAGS_USED."</th><th>".TAGS_DESCRIPTION."</th><th></th></tr>";

foreach ($result as $row) {
    $i++;
    $tags_body .= "<tr><td>".$i."</td><td><a href='index.php?manage=tags&action=edit&id=".$row['id']."'>".$row['name']."</a></td><td>".$row['used']."</td><td>".$row['description']."</td><td><button type='button' class='btn btn-danger btn-xs pull-right' data-toggle='modal' data-target='#tagDelete' data-tagname='".$row['name']."' data-tagid='".$row['id']."'>".DELETE."</button><a class='btn btn-warning btn-xs pull-right' href='index.php?manage=tags&action=edit&id=".$row['id']."'>".EDIT."</a></td></tr>";
}

?>

<div class="modal fade" id="tagDelete" tabindex="-1" role="dialog" aria-labelledby="tagDeleteLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="tagDeleteLabel"><?php echo DELETING; ?></h4>
          </div>
            <form action="index.php?manage=tags&data=modify&action=delete" method="post">
                <input type="hidden" name="csrf_token" value="<?php echo $app->getToken();?>">
                <div class="modal-body">
                    <input type="hidden" name="tagid" class="tagid" value="">
                    <div class="form-group">
                        <label for="tagname" class="control-label"><?php echo DELETING_CONFIRM_QUESTION; ?></label>
                        <input type="text" class="form-control tagname" disabled>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo CANCEL;?></button>
                    <button type="submit" class="btn btn-danger"><?php echo DELETE;?></button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="page-title">
    <div class="title_left">
        <h3><?php echo EMAIL_ADRESSES;?> <small>(<?php echo TAGS_LIST;?>)</small></h3>
    </div>
</div>
<div class="clearfix"></div>
<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
            <div class="x_title">
                <h2><?php echo TAGS_INFO;?></h2>
                <div class="clearfix"></div>
            </div>
            <div class="x_content">
                <table class="table table-striped responsive-utilities jambo_table" id="tags">
                    <thead>
                        <?php echo $tags_head; ?>
                    </thead>
                    <tbody>
                        <?php echo $tags_body; ?>
                    </tbody>
                </table>
            </div>
            <a href="index.php?manage=tags&action=add" class="btn btn-primary"><?php echo MENU_ADD_TAGS;?></a>
        </div>
    </div>
</div>
<p>&nbsp;</p>

<script src="components/tags/view.tags.list.js"></script>
