<%perl>
# Show the story summary if needed.
if ($show_summary) {
    my $type  = $element->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 => $lang->maketext('Bulk Edit [_1]', $element->get_name),
   id      => 'bulkedit',
   number  => ++$i,
&>
    <div style="float: right; padding: 5px;">
        <button title="<% $lang->maketext('Open the search and replace dialog box') . ' ' . $lang->maketext('(Access Key: [_1])', 'f') %>" accesskey="f" onclick="return openFindDialog(document.getElementById('finddialog'), event);">Search</button>
        <button title="<% $lang->maketext('Show cheat sheet') . ' ' . $lang->maketext('(Access Key: [_1])', 'c') %>" accesskey="c" onclick="return openDialog(document.getElementById('cheat_sheet'), event);">Cheat Sheet</button>
    </div>
    
    <dl class="viewmeta">
        <dt>Element:</dt>
        <dd><% $et->get_name %> <% $show_element_flags->($et) %></dd>
        <dt>Default Field:</dt>
        <dd>
<%perl>;
my $opts = [
    [ '' =>  '' ],
    map  {   [ $_ => $_ ]   } sort { $a cmp $b } keys %seen_fields
];
</%perl>
            <& '/widgets/profile/select.mc',
                    name     => $widget.'|default_field',
                    options  => $opts,
                    value    =>  $def_field,
                    useTable => 0,
            &>
            <& '/widgets/buttons/submit.mc',
                disp    => "Change",
                widget  => $widget,
                cb      => "change_default_field_cb",
                button  => "change_red",
                value   => "Change",
                useTable => 0,
            &>
        </dd>
        <dt><% $lang->maketext('Words') %>:</dt>
        <dd><span id="words"></span></dd>
        <dt><% $lang->maketext('Characters') %>:</dt>
        <dd><span id="chars"></span></dd>
    </dl>
% my $textarea_id = "$widget|text";
% $textarea_id =~ s/[\||_]//g;
<& '/widgets/profile/textarea.mc',
    name     => "$widget|text",
    value    => $args->{__use_text__} ? $args->{"$widget|text"} : $element->serialize_to_pod($def_field),
    rows     => 40,
    cols     => 80,
    useTable => 0,
    js       => qq{onchange="wordCount('$textarea_id', 'words', 'chars');" onkeyup="wordCount('$textarea_id', 'words', 'chars');"},
&>
<& /widgets/dialog_box/find.mc,
    field_id  => "$textarea_id",
    dialog_id => 'finddialog',
&>
<&| /widgets/dialog_box/dialog_box.mc,
   id          => 'cheat_sheet',
   title       => 'Cheat Sheet',
   close_label => 'Close',
&>
<div class="cheat_sheet">
<% $cheat_sheet %>
</div>
</&>
<script>wordCount('<% $textarea_id %>', 'words', 'chars')</script>
<& '/widgets/wrappers/sharky/table_bottom.mc' &>
<& '/widgets/buttons/submit.mc',
    disp    => 'Save',
    widget  => $widget,
    cb      => 'bulk_save_and_up_cb',
    button  => 'save_red',
    useTable    => 0,
&>
<& '/widgets/buttons/submit.mc',
    disp    => 'Save and Stay',
    widget  => $widget,
    cb      => 'bulk_save_cb',
    button  => 'save_and_stay_lgreen',
    useTable    => 0,
&>
<& '/widgets/buttons/submit.mc',
    disp    => 'Return without Saving',
    widget  => $widget,
    cb      => 'bulk_up_cb',
    button  => 'cancel_red',
    useTable    => 0,
&>

<%args>
$widget
$show_summary
$args
</%args>
<%init>;
# the element object
my $element       = get_state_data($widget, 'element');
#my $preserve   = get_state_data($widget, 'preserve');
my $i          = 0;
my $et         = $element->get_element_type;
my $def_field  = get_state_data(
    '_tmp_prefs',
    'container_prof.' . $et->get_id . '.def_field'
);
my $cheat_sheet = $m->scomp('.gen_cheat_sheet', $et, \my %seen_fields, {});
</%init>
<%once>;
my $show_element_flags = sub {
    my $et = shift;
    my @flags;
    push @flags, 'paginated'     if $et->is_paginated;
    push @flags, 'top level'     if $et->is_top_level;
    push @flags, 'fixed uri'     if $et->is_fixed_uri;
    push @flags, 'media'         if $et->is_media;
    push @flags, 'related media' if $et->is_related_media;
    push @flags, 'related story' if $et->is_related_story;

    return '' unless @flags;
    return ' <span class="orangeLink">(' . join(', ', @flags) . ')</span>';
};

my $show_data_flags = sub {
    my $dt = shift;
    my @flags;

    push @flags, 'required'   if $dt->get_required;
    push @flags, 'repeatable' if $dt->get_quantifier;
    return '' unless @flags;
    return ' <span class="orangeLink">(' . join(', ', @flags) . ')</span>';
};
</%once>
<%def .gen_cheat_sheet>
<%perl>;
    my ($et, $seen_fields, $seen_elems) = @_;
    my $fields = $et->get_field_types;
    my $subs   = $et->get_containers;
    return unless @$fields || @$subs;
    $m->print("<ul>\n");
    for my $field (@$fields) {
        my $kn = $field->get_key_name;
        $seen_fields->{$kn} ||= 1;
        $m->print("<li>=$kn", $show_data_flags->($field), "</li>\n");
    }

    for my $cont (@$subs) {
        my $kn = $cont->get_key_name;
        next if $seen_elems->{$kn} > 1;
        ++$seen_elems->{$kn};
        $m->print(
            qq{<li class="elem"><strong>=begin $kn</strong>},
            $show_element_flags->($cont),
        );
        $m->comp('.gen_cheat_sheet', $cont, $seen_fields, $seen_elems);
        $m->print("<strong>=end $kn</strong></li>\n");
        $seen_elems->{$kn}--;
    }

    $m->print("</ul>\n");
</%perl>
</%def>
