[update values]
[calc]
	$Tag->tmp('option_table');
	my $loc = $Config->{Options_repository}{Simple} || {};
	$Scratch->{option_table} = $loc->{table} || 'options';
	if($CGI->{item_id} and ! $CGI->{sku}) {
		$CGI->{sku} = $CGI->{item_id};
	}
	return;
[/calc]

<!-- flag write: [flag type=write table="[scratch option_table]"] -->

[if cgi ui_clone_options]
[and cgi ui_clone_id]
[perl tables="[scratch option_table]"]
#Debug("Cloning options for $CGI->{ui_clone_id}...");
	my $db = $Db{$Scratch->{option_table}}
		or return;
#Debug("Have a table=$db clone id=$CGI->{ui_clone_id}");
	my ($k,$v);
	$db->clone_row($CGI->{ui_clone_id}, $CGI->{sku});
	$db->clone_set('sku', $CGI->{ui_clone_id}, $CGI->{sku});
	return;
[/perl]
[/if]

[if cgi opt_groups]
[or cgi opt_group_]
    [perl tables="[scratch option_table]"]
        my $otab = $Scratch->{option_table};
        my $odb = $Db{$otab};

		my %existing;
		my @groups = split /\0/, $CGI->{opt_groups};
		for(@groups) {
			$existing{$_} = 1;
		}

        foreach(sort keys %{$CGI}) {
            next unless /^opt_group_(.*)/;
            my $key = $1 || '';
#Debug("looking at opt_group_$key");

            my $rec_key;
            my $name;
            my $wid;
			my $ref = {};
			# New option
			if(! $key) {
				$name = $CGI->{"opt_group_"}
					or next;
				if($name =~ /\W/) {
					$Tag->error({
						name => 'Set option groups',
						set => errmsg(qq{Group name "%s" contains non-word characters, will not set}, $name),
					});
					next;
				}
				if($existing{$name}) {
					$Tag->error({
						name => 'Set option groups',
						set => errmsg("Group %s already exists, will not set", $name),
					});
					next;
				}
				$rec_key = $CGI->{sku} . "-$name";
				$ref->{o_group} = $name;
				$ref->{o_widget} = 'select';
				$ref->{sku} = $CGI->{sku};
			}
			else {
				$name = $key;
				$rec_key = $CGI->{"opt_key_$key"};
			}

			$ref->{o_value} = $Tag->filter('line2options', $CGI->{"opt_value_$key"});

            $ref->{o_label} = $CGI->{"opt_label_$key"};

			if($name =~ /\W/) {
				$Tag->error({
					name => 'Set option groups',
					set => errmsg("o_group '%s', bad name, skipping", $name),
				});
				next;
			}
			elsif (! $ref->{o_value}) {
				$Tag->error({
					name => 'Set option groups',
					set => errmsg("o_group '%s' has no value, skipping", $name),
				});
				next;
			}

#::logDebug("doing set_slice for key=$rec_key, ref=" . uneval($ref));
            $odb->set_slice($rec_key,$ref);
        }

        return;
    [/perl]
[/if]


<FORM ACTION="[area @@MV_PAGE@@]" METHOD="POST">
[if scratch ui_failure]
<P>
<BLOCKQUOTE>
<FONT COLOR="__CONTRAST__">[scratch ui_failure][set ui_failure][/set]</FONT>
</BLOCKQUOTE>
<P>
&nbsp;
[/if]
[if scratch ui_message]
<P>
<BLOCKQUOTE>
<FONT COLOR="__CONTRAST__">[scratch ui_message][set ui_message][/set]</FONT>
</BLOCKQUOTE>
<P>
&nbsp;
[/if]
<INPUT TYPE=hidden NAME=sku              VALUE="[cgi sku]">
<INPUT TYPE=hidden NAME=ui_page_title    VALUE="[cgi ui_page_title]">
<INPUT TYPE=hidden NAME=ui_page_title    VALUE="[cgi ui_page_banner]">
<INPUT TYPE=hidden NAME=ui_return_to     VALUE="@@MV_PAGE@@">
<INPUT TYPE=hidden NAME=mv_action        VALUE=back>

<TABLE BORDER=0><TR><TD VALIGN=TOP>

[query list=1 table="[scratch option_table]" sql="select * from [scratch option_table] where sku='[filter op=sql interpolate=1][cgi sku][/filter]' and o_group is not null and o_group <> ''"]
[list]

<div style="border: 1px solid black; margin-bottom: 3px">
<table	border=0
		cellspacing=0 cellpadding=3
		class="[sql-alternate 2]__UI_T_ROW_EVEN__[else]__UI_T_ROW_ODD__[/else][/sql-alternate]"
		>
[if-sql-param o_group !~ /^\w+$/]
<tr>
	<td>
		Bad option group '[filter entities][sql-param o_group][/filter]'
	</td>
</tr>
[else]
<tr>
	<td valign=center class=titlebox>
		[L]Option[/L]: [sql-param o_group]
		<INPUT TYPE=hidden NAME="opt_key_[sql-param o_group]" VALUE="[sql-code]">
		<INPUT TYPE=hidden NAME="opt_group_[sql-param o_group]" VALUE="[sql-param o_group]">
		<INPUT TYPE=hidden NAME="opt_groups" VALUE="[sql-param o_group]">
	</td>
</tr>
<tr>
	<td valign=center>
		[L]Label[/L]:
			<INPUT TYPE=text SIZE=20 NAME="opt_label_[sql-param o_group]" VALUE="[filter entities][sql-param o_label][/filter]">
<A HREF="[area href='@@MV_PAGE@@'
               form='deleterecords=1
                     ui_delete_id=[sql-code]
                     item_id=[cgi sku]
                     mv_data_table=options
                     mv_click=db_maintenance
                     mv_action=back
                     mv_nextpage=@@MV_PAGE@@
                    '
         ]"
		 title="Delete [sql-param o_group] option"
		 ><IMG SRC="delete.gif" ALT="[L]Delete[/L]" ALIGN=CENTER BORDER=0></A>
	</td>
</tr>
[/else]
[/if-sql-param]


<TR><TD>
<TEXTAREA ROWS=5 COLS=30 NAME="opt_value_[sql-param o_group]">[filter options2line][sql-param o_value][/filter]</TEXTAREA><br>
<span style="font-size: smaller">
[page href="admin/flex_editor"
		form="
			mv_data_table=options
			nodelete=1
			ui_return_to=@@MV_PAGE@@
			ui_return_to=item_id=[cgi sku]
			item_id=[sql-code]
			ui_data_fields=code sku o_widget o_width o_height
			no_meta=1
			ui_te_widget:sku=hidden
		"][L]edit[/L]</A>
[L]Widget[/L]: [sql-param o_widget]
<br>
[page href="admin/flex_editor"
		form="
			mv_data_table=options
			nodelete=1
			item_id=[sql-code]
			ui_return_to=@@MV_PAGE@@
			ui_return_to=item_id=[cgi sku]
			ui_data_fields=code sku o_value price weight
			ui_te_widget:sku=hidden
			ui_meta_view=simple_price
			no_meta=1
			ui_te_widget:price=textarea_8_20
			ui_te_widget:weight=textarea_8_20
			ui_te_widget:o_value=hidden_text
			ui_te_filter:price=line2options
			ui_te_filter:weight=line2options
			ui_te_pre_filter:price=options2line
			ui_te_pre_filter:weight=options2line
			ui_te_help:price=Standard VALUE=N.NN format, one per line
			ui_te_help:weight=Standard VALUE=N.NN format, one per line
			ui_te_help:o_value=Values in this option
		"][L]edit[/L]</A>
[L]Price differential:[/L] [sql-param price]
</span>
<br>
</TD></TR>
</TABLE>
</div>
[/list]
[/query]

<BR><BR><BR>
[button text="[L]Commit Changes[/L]"]
mv_nextpage=@@MV_PAGE@@
ui_return_to=
mv_todo=return
[/button]

</TD><TD><PRE>                          </PRE></TD><TD VALIGN=TOP>

<div class=titlebox style="margin-bottom: 4px">[L]Create a new option[/L]</div>
[L]Name[/L]: <INPUT TYPE=text SIZE=20 NAME="opt_group_" VALUE="">
<br>[L]Label[/L]: <INPUT TYPE=text SIZE=20 NAME="opt_label_">
<script><!--
document.write('<br><INPUT TYPE=checkbox\n' +
'	onClick="if (this.checked) { this.form.opt_label_.value = this.form.opt_group_.value; } else { this.form.opt_label_.value = \'\'; }">\n' +
'<font size=2>[L]Set label to name[/L]</font>');
// -->
</script>
<BR>
<TEXTAREA ROWS=5 COLS=30 NAME="opt_value_"></TEXTAREA>
<BR>
[button text="[L]Create option[/L]"][/button]
<BR>
<BR>

<div class=titlebox style="margin-bottom: 4px">[L]Clone an existing option set[/L]</div>

[query
	table="[scratch option_table]"
	list=1
	prefix=clone
	sql="select DISTINCT sku from [scratch option_table] where o_group is not null and o_group <> ''"
	more=1]
<SELECT NAME=ui_clone_id>
<OPTION VALUE=""> --
[list]
<OPTION VALUE="[clone-code]">[either][clone-filter 20.][clone-description][/clone-filter][or][clone-code][/either]
[/list]
</SELECT>[more-list]<BR>[more]<BR>[/more-list][/query]&nbsp;[button text="[L]Clone options[/L]"]ui_clone_options=1[/button]
<BR>
</FORM>

</TD></TR></TABLE>

