<?php

if (!class_exists('nc_core')) { die; }

?>
<div class="nc_admin_fieldset_head"><?= NETCAT_MODULE_SERVICES_DIRECT_CAMPAIGNS; ?></div>
<?php

$table = $ui->table()->wide()->striped()->bordered()->hovered();

$thead = $table->thead(); // chaining produces invalid code

$thead->th(NETCAT_MODULE_SERVICES_DIRECT_CAMPAIGN_ID)->compact()->text_center();
$thead->th(NETCAT_MODULE_SERVICES_DIRECT_CAMPAIGN_NAME);
$thead->th(NETCAT_MODULE_SERVICES_DIRECT_CAMPAIGN_START)->compact()->text_center();
$thead->th(NETCAT_MODULE_SERVICES_DIRECT_CAMPAIGN_SHOWS)->compact()->text_center();
$thead->th(NETCAT_MODULE_SERVICES_DIRECT_CAMPAIGN_CLICKS)->compact()->text_center();
$thead->th(NETCAT_MODULE_SERVICES_DIRECT_CAMPAIGN_STATUS)->compact()->text_center();
$thead->th(NETCAT_MODULE_SERVICES_DIRECT_CAMPAIGN_REST)->compact()->text_center();
$thead->th()->compact();
$thead->th()->compact();
$thead->th()->compact();
$thead->th()->compact();

$tr = $table->row();
$tr->id = $tr->td();
$tr->name = $tr->td();
$tr->start = $tr->td();
$tr->shows = $tr->td()->text_center();
$tr->clicks = $tr->td()->text_center();
$tr->status = $tr->td();
$tr->rest = $tr->td()->text_center();
$tr->edit_banners = $tr->td()->text_center();
$tr->action_links = $tr->td()->text_center();
$tr->delete_button = $tr->td()->text_center();

foreach ($campaigns as $row) {
    $campaign_id = $row['CampaignID'];
    $edit_link = "$link_prefix.campaign.edit($campaign_id)";

    $post_actions_params = array('controller' => "direct", 'campaign_id' => $campaign_id);

    $tr->id->text($campaign_id);
    $tr->name->text(
        "<a href='$edit_link' target='_top' class='nc-netshop-list-item-title'>" . $row['Name'] . "</a>"
    );
    
    $tr->start->text($row['StartDate']);
    $tr->shows->text($row['Shows']);
    $tr->clicks->text($row['Clicks']);
    $tr->status->text($row['Status']);

    $addfunds_link =  "$link_prefix.campaign.add_funds($campaign_id)";

    $tr->rest->text($row['Rest']."<br><a target='_top' href='".$addfunds_link."'>".NETCAT_MODULE_SERVICES_ACTION_ADDFUNDS."</a>");
    
    $edit_banners_link = "$link_prefix.banner.list($campaign_id)";
    $tr->edit_banners->text( "<a href='$edit_banners_link' target='_top' class='nc-netshop-list-item-title'>" . NETCAT_MODULE_SERVICES_DIRECT_EDIT_BANNERS . "</a>");

    $archive_link =  $current_url . "archive_campaign&campaign_id=$campaign_id";
    $unarchive_link =  $current_url . "unarchive_campaign&campaign_id=$campaign_id";
    $resume_link =  $current_url . "resume_campaign&campaign_id=$campaign_id";
    $stop_link =  $current_url . "stop_campaign&campaign_id=$campaign_id";
    $moderate_link =  $current_url . "moderate_campaign&campaign_id=$campaign_id";
    
    $tr->action_links->text(
      (!empty($row['ShowArchiveAction']) ? "<a href='".$archive_link."'>".NETCAT_MODULE_SERVICES_ACTION_ARCHIVE."</a><br>" : "")
      . (!empty($row['ShowUnarchiveAction']) ? "<a href='".$unarchive_link."'>".NETCAT_MODULE_SERVICES_ACTION_UNARCHIVE."</a><br>" : "")
      . (!empty($row['ShowResumeAction']) ? "<a href='".$resume_link."'>".NETCAT_MODULE_SERVICES_ACTION_RESUME."</a><br>" : "")
      . (!empty($row['ShowStopAction']) ? "<a href='".$stop_link."'>".NETCAT_MODULE_SERVICES_ACTION_STOP."</a><br>" : "")
      . (!empty($row['ShowModerateAction']) ? "<a href='".$moderate_link."'>".NETCAT_MODULE_SERVICES_ACTION_MODERATE."</a><br>" : "")
    );
    
    $tr->delete_button->text(
      $ui->controls->delete_button(
        sprintf(NETCAT_MODULE_SERVICES_CONFIRM_DELETE, $row['Name']),
        $post_actions_params
    ));

    $table->add_row($tr);
}


echo $table, "<br>";
