<%perl>
# Show the story summary if needed.
if ($show_summary) {
    my $type = $tile->get_object_type;

    # Nab the story from the story_prof widget 
    my $asset = get_state_data($type.'_prof', $type);
    $m->comp('/widgets/summary/summary.mc', asset => $asset, number => ++$i);
}
</%perl>

<& '/widgets/wrappers/sharky/table_top.mc',
   caption => "All Fields Text",
   id      => 'containerprof',
   number  => ++$i,
&>

% if ($at) {
  <table border=0 cellpadding=0 cellspacing=0 width=578>
    <tr>
      <td width=1 height=20 class=medHeader><img src="/media/images/spacer.gif" width=1 height=20 /></td>
      <td width=<% FIELD_INDENT %> align="right" class=medHeader><span class=label>Element:&nbsp;</span></td>
      <td width=5 height=20><img src="/media/images/spacer.gif" width=5 height=20 /></td>
      <td width=<% 577 - FIELD_INDENT %>>
        <% $at->get_name %>
        <% $show_element_flags->($at) %>
      </td>
    </tr>

    <tr><td colspan=4><img src="/media/images/spacer.gif" width=578 height=1 /></td></tr>

% if (@{$at->get_data}) {
    <tr>
      <td width=1 height=20 class=medHeader><img src="/media/images/spacer.gif" width=1 height=20 /></td>
      <td align="right" class=medHeader><span class=label>Data Elements:&nbsp;</span></td>
      <td width=5 height=20><img src="/media/images/spacer.gif" width=5 height=20 /></td>
      <td>
% my $comma = '';
% foreach my $d ($at->get_data) {
        <% $comma %>
        <% $fmt_name->($d->get_key_name) %>
        <% $show_data_flags->($d) %>
%     $comma = ', ';
% }
      </td>
    </tr>

    <tr><td colspan=4><img src="/media/images/spacer.gif" width=578 height=1 /></td></tr>

% }
% if (@{$at->get_containers}) {
    <tr>
      <td width=1 height=20 class=medHeader><img src="/media/images/spacer.gif" width=1 height=20 /></td>
      <td align="right" class=medHeader><span class=label>Container Elements:&nbsp;</span></td>
      <td width=5 height=20><img src="/media/images/spacer.gif" width=5 height=20 /></td>
      <td>
% my $comma = '';
% foreach my $c ($at->get_containers) {
        <% $comma %>
        <% $fmt_name->($c->get_name) %>
        <% $show_subelement_flags->($at, $c) %>
%     $comma = ', ';
% }
      </td>
    </tr>
% }

   <tr><td colspan=4><img src="/media/images/spacer.gif" width=578 height=1 /></td></tr>

    <tr>
      <td width=1 height=20 class=medHeader><img src="/media/images/spacer.gif" width=1 height=20 /></td>
      <td align="right" class=medHeader><span class=label>Default Element:&nbsp;</span></td>
      <td width=5 height=20><img src="/media/images/spacer.gif" width=5 height=20 /></td>
      <td>
% my $opts = [map { my $n = lc($_->get_key_name);
%                   $n=~y/a-z0-9/_/cs;
%                   [$n, $_->get_key_name]
%                 } grep {$_->get_quantifier} $tile->get_possible_data()];
% unshift @$opts, ['', '- None -'];
<& '/widgets/profile/select.mc',
        name     => $widget.'|default_field',
        options  => $opts,
        value    =>  $default_field,
        useTable => 0,
&>
<input type="image" src="/media/images/<% $lang_key %>/change_red.gif" border="0"  name="<% $widget %>|change_default_field_cb" value="Change">
      </td>
    </tr>

  <tr><td colspan=4 class=medHeader><img src="/media/images/spacer.gif" width=578 height=1 /></td></tr>
  </table>
% }

<& '/widgets/profile/textarea.mc', 'name'  => $widget.'|text',
                                   'rows'  => $rows,
                                   'cols'  => $cols,
                                   'value' => $text &>
<table>
<tr>
<td>
<img src="/media/images/spacer.gif" width=10 height=1>
</td>
<td>
<& '/widgets/profile/text.mc', 'name'    => $widget.'|cols',
                               'value'   => "$cols",
                               'size'     => 3,
                               'useTable' => 0 &>
</td>
<td>
Columns&nbsp;
</td>
<td>
<& '/widgets/profile/text.mc', 'name'    => $widget.'|rows',
                               'value'   => "$rows",
                               'size'     => 3,
                               'useTable' => 0 &>
</td>
<td>
Rows 
</td>
<td>
<input type="image" src="/media/images/<% $lang_key %>/resize_lgreen.gif" border=0 name="<% $widget %>|resize_cb" value="Resize">
</td>
</tr>
</table>

<& '/widgets/wrappers/sharky/table_bottom.mc' &>

<& '/widgets/wrappers/sharky/table_top.mc',
   caption => 'Statistics',
   number  => ++$i,
&>
<table>
<tr>
<td>
<img src="/media/images/spacer.gif" width=10 height=1>
</td>
<td>Words<br /><input type="text" name="words" size=5 value="<% $words %>" /></td>
<td>Characters<br /><input type="text" name="chars" size=5 value="<% $bytes %>" /></td>
  <td><a href="#" onClick="return wordCount(document.theForm, '<% $widget . "|text" %>', 'words', 'chars')"><img src="/media/images/<% $lang_key %>/recount_lgreen.gif" border=0></a></td>
</tr>
</table>
<& '/widgets/wrappers/sharky/table_bottom.mc' &>

<input type="image" src="/media/images/<% $lang_key %>/save_red.gif" border=0 name="<% $widget %>|bulk_save_and_up_cb">
<input type="image" src="/media/images/<% $lang_key %>/save_and_stay_lgreen.gif" border=0 name="<% $widget %>|bulk_save_cb">
<input type="image" src="/media/images/<% $lang_key %>/cancel_red.gif" border=0 name="<% $widget %>|bulk_up_cb" value="Return without Saving">

<%args>
$widget
$show_summary
</%args>

<%init>
# the tile object
my $tile = get_state_data($widget, 'tile');
my $at   = $tile->get_element;

my $dtiles        = get_state_data($widget, 'dtiles');
my $data          = get_state_data($widget, 'data');
my $cols          = get_state_data($widget, 'cols');
my $rows          = get_state_data($widget, 'rows');
my $default_field = get_state_data('_tmp_prefs', 'container_prof.' .
                                   $at->get_id . '.def_field');

# Create the text blob
my $text = '';

foreach my $t (@$data) {
    my ($name, $data) = @$t;
    $name = $simple_name->($name);
    $text .= "=$name\n\n" unless $name eq $default_field;
    $text .= "$data\n" if $data;
    $text .= "\n" unless $tile->get_container($name);
}

my ($i, $words, $bytes, @tmp);

foreach (@$data) {
    $words += scalar(@tmp = /\S+/g);
    $bytes += length;
}

</%init>
<%once>
my $simple_name = sub {
    my ($name) = @_;
    $name = lc($name);
    $name =~ y/a-z0-9/_/cs;

    return $name;
};

my $fmt_name = sub {
    my ($name) = @_;

    # Lowercase the name and replace non-alphanumeric characters
    ($name = lc $name) =~ y/a-z0-9/_/cs;
    return $name;
};

my $show_element_flags = sub {
    my ($at) = @_;
    my @flags;

    push @flags, 'paginated' if $at->get_paginated;
    push @flags, 'top level' if $at->get_top_level;
    push @flags, 'fixed url' if $at->get_fixed_url;
    push @flags, 'media'     if $at->is_media;
    push @flags, 'related media' if $at->is_related_media;
    push @flags, 'related story' if $at->is_related_story;

    if (scalar @flags) {
        return '&nbsp;<span class="orangeLink">('.join(', ', @flags).')</span>';
    } else {
        return;
    }
};

my $show_data_flags = sub {
    my ($d) = @_;
    my @flags;

    push @flags, 'required' if $d->get_required;
    push @flags, 'repeatable' if $d->get_quantifier;
  
    if (scalar @flags) {
        return '&nbsp;<span class="orangeLink">('.join(', ', @flags).')</span>';
    } else {
        return;
    }
};

my $show_subelement_flags = sub {
    my ($at, $c) = @_;
    my @flags;

    push @flags, 'repeatable' if $at->is_repeatable($c);

    if (scalar @flags) {
        return '&nbsp;<span class="orangeLink">('.join(', ', @flags).')</span>';
    } else {
        return;
    }
};
</%once>
