[if cgi ui_clone_options]
[and cgi ui_clone_id]
[perl tables="[cgi mv_data_table]"]
	my $db = $Db{$CGI->{mv_data_table}}
		or return;
	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]
<!--
				fi=[cgi mv_data_table]
				st=db
				co=yes

				se=[cgi item_id]
				sf=sku
				op=eq
				ac=0

				sf=o_matrix
				se=1
				op=ne
				ac=0

				sf=o_master
				se=.
				op=rn
				ac=0

				rf=*

-->
[if cgi explode]
[tmp all_opts][loop
	search="
					fi=[cgi mv_data_table]
					st=db
					co=yes

					se=[cgi item_id]
					sf=sku
					op=eq
					ac=0

					sf=o_matrix
					se=1
					op=ne
					ac=0

					rf=o_group,o_value
		"][loop-param o_group]	[loop-param o_value]
[/loop][/tmp]

[tmp tmp_price][price noformat=1 code="[cgi item_id]"][/tmp]
[tmp tmp_whole][field field=wholesale code="[cgi item_id]"][/tmp]
[tmp tmp_desc][description code="[cgi item_id]"][/tmp]
[perl tables="[cgi mv_data_table] __UI_ITEM_TABLES__"]
#Debug("opts: $Scratch->{all_opts}");
	my $otab = $CGI->{mv_data_table};
	my $odb = $Db{$otab};
	unless ($odb) {
		::logError("can't open option table '%s'.", $otab);
		return undef;
	}
	my $id = $CGI->{item_id};
	my %default = (
		price		=> $Scratch->{tmp_price},
		wholesale	=> $Scratch->{tmp_whole},
	);
#Debug("opts: $Scratch->{all_opts}");
	my @opts = grep /\S/, split /\n/, $Scratch->{all_opts};
#Debug( "options raw: " . $Tag->uneval( { ref => \@opts } ) );
	return unless @opts;

	my @dimensions;
	for(@opts) {
		my($g, $o) = split /\t/, $_;
		push @optnames, $g;
		my @ary;
		my (@o) = grep /\S/, split /\s*,\s*/, $o;
		for(@o) {
			my ($k, $v) = split /\s*=\s*/, $_;
			$k = '__MV_VARIANT_JOINER__' || '0' if ! length($k);
			$v =~ s/\*$//;
			push @ary, [ $k, $v ];
		}
		push @dimensions, \@ary;
	}

#Debug( "dimensions: " . $Tag->uneval( { ref => \@dimensions } ) );

	my @pointers;
	my @options;
	my @descriptions;
	my @hashes;

	sub recurse_dim {
			my $dim = $_[0] || 0;
			return if $dim > $#dimensions;
			for (0 .. @{$dimensions[$dim]}-1) {
				$pointers[$dim] = $_;
				if ($dim == $#dimensions) {

					push @descriptions,
					join( ', ',
	map { $dimensions[$_]->[$pointers[$_]]->[1] } (0..$#dimensions)
						)
					;
					push @options,
					join( '-',
	map { $dimensions[$_]->[$pointers[$_]]->[0] } (0..$#dimensions)
						)
					;

					my %opthash;
					for (my $i = 0; $i < @optnames; $i++) {
						$opthash{$optnames[$i]} =
							$dimensions[$i]->[$pointers[$i]]->[0];
					}
					push @hashes, \%opthash;

				} else {
					recurse_dim($dim + 1);
				}
			}
	}

	recurse_dim();

#Debug( "options: "  . $Tag->uneval( { ref => \@options } ) );
#Debug( "hashes: "  . $Tag->uneval( { ref => \@hashes } ) );
	$odb->set_field($id, 'o_matrix', 0);
	for( my $i = 0; $i < @options; $i++) {
		my $key = "$id-$options[$i]";
		die ::errmsg("No key for create!") unless $key;
		$odb->set_field($key, 'description', "$Scratch->{tmp_desc}, $descriptions[$i]");
		$hashes[$i]->{code} = $id;
		$hashes[$i]->{noformat} = 1;
		$hashes[$i]->{quantity} = 1;
		$price = $Tag->price( $id, $hashes[$i] );
		for(keys %default) {
			$odb->set_field($key, $_, $default{$_})
				if $odb->test_column($_);
		}
		$odb->set_field($key, 'price', $price);
		$odb->set_field($key, 'sku', $id);
		$odb->set_field($key, 'o_matrix', 1);
		$odb->set_field($key, 'o_enable', 1);
	}
	$odb->set_field($id, 'o_matrix', 1);

	return;

[/perl]
[/if]

[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]
<TABLE CELLSPACING=0 cellpadding=2 cellmargin=3 width="100%">
<TR class=rmarq>
<TH ALIGN=LEFT>[L]SKU[/L]</TH>
<TH ALIGN=LEFT>[L]Description[/L]</TH>
<TH ALIGN=RIGHT>[L]Price[/L]</TH>
<TH ALIGN=CENTER>&nbsp;</TH>
</tr>
[loop prefix=variant
		search="
				fi=[cgi mv_data_table]
				st=db
				co=yes

				sf=sku
				se=[cgi item_id]
				op=eq

				sf=o_matrix
				se=1
				op=eq

				rf=code,sku,description,o_value,price
				"]
[list]
<TR [variant-alternate 2]BGCOLOR="__UI_T_ROW_EVEN__"[else]BGCOLOR="__UI_T_ROW_ODD__"[/else][/variant-alternate]>

<TD>[page href=__UI_BASE__/flex_editor form=|
						mv_data_table=[cgi mv_data_table]
						ui_page_title=[cgi ui_page_title]
						ui_page_title=[cgi ui_page_banner]
						ui_class=Items
						ui_meta_specific=[cgi ui_meta_specific]
						ui_meta_view=variant
						ui_data_fields=code sku description price wholesale inventory:quantity weight o_widget o_width o_height
						ui_return_to=@@MV_PAGE@@
						ui_return_to=ui_return_table=[cgi mv_data_table]
						ui_return_to=ui_text_qualification=sku=[cgi item_id]
						item_id=[variant-code]
					|][variant-code]</A></TD>
<TD>
	[variant-data options description]
</td>
<TD ALIGN=right>
	[variant-filter currency][variant-data options price][/variant-filter]
</td>
<TD ALIGN=CENTER WIDTH="1"><A
onClick="return confirm('Are you sure you want to delete the [variant-code] variant?')"
HREF="[area
			href='@@MV_PAGE@@'
			form='
				deleterecords=1
				ui_delete_id=[variant-code]
				sku=[cgi item_id]
				mv_data_table=[cgi mv_data_table]
				mv_click=db_maintenance
				mv_action=back
				mv_nextpage=@@MV_PAGE@@
			'
]"><IMG SRC="delete.gif" ALT="Delete [variant-code] variant" BORDER=0></A></TD>
</tr>
[/list]
[no-match]
<tr>
<td colspan=6 align=center>
<B>No Variants.</B>
</td>
</tr>
[/no-match]
<tr>
<td colspan=3>
	<form action="[area @@MV_PAGE@@]">
	<INPUT TYPE=hidden NAME=debug VALUE=0>
	<INPUT TYPE=hidden NAME=mv_data_table VALUE="[cgi mv_data_table]">
	<INPUT TYPE=hidden NAME=item_id VALUE="[cgi item_id]">
	<INPUT TYPE=hidden NAME=explode VALUE="1">
	<INPUT TYPE=submit VALUE="[L]Create all possible combinations[/L]">
	</form>
</td>
</tr>
</table>
[/loop]

[tmp desc_value][calc]
	$f = qq{[field column=__DescriptionField__ key="[cgi item_id]"]};
	$f =~ s/'/\\'/g;
	return $f;
	[/calc][/tmp]
<FORM ACTION="[area @@MV_PAGE@@]" METHOD=post NAME=simple_options>
<INPUT TYPE=hidden NAME=sku              VALUE="[cgi item_id]">
<INPUT TYPE=hidden NAME=mv_data_table    VALUE="[cgi mv_data_table]">
<INPUT TYPE=hidden NAME=ui_class         VALUE="Items">
<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_meta_specific VALUE="[cgi ui_meta_specific]">
<INPUT TYPE=hidden NAME=ui_return_to     VALUE="@@MV_PAGE@@">
<INPUT TYPE=hidden NAME=ui_return_to     VALUE="mv_data_table=[cgi mv_data_table]">
<INPUT TYPE=hidden NAME=ui_return_to VALUE="ui_text_qualification=sku=[cgi item_id]">
<INPUT TYPE=hidden NAME=mv_action        VALUE=back>

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

<table __UI_T_PROPERTIES__ WIDTH=100%>
<tr class=rtitle>
	<td class=ctitle colspan=3>
		<H2>[L]Add Variant[/L]</H2>
	</td>
</tr>
<tr class=rtitle>
	<td class=ctitle>[L]Option name[/L]</td>
	<td class=ctitle>[L]Choices[/L]</td>
	<td class=ctitle>&nbsp;</td>
</tr>
[tmp built_js]
<SCRIPT LANGUAGE="JavaScript">
	function set_description(this_form, other_form) {
		other_form.description.value = '[scratch desc_value] - ';
		var joiner = '';
		other_form.code.value = '[cgi item_id]';
		if(! other_form.price.value) {
			other_form.price.value = '[field code="[cgi item_id]" column=price]';
		}
		if(! other_form.wholesale.value) {
			other_form.wholesale.value = '[field code="[cgi item_id]" column=wholesale]';
		}
		if(! other_form.weight.value) {
			other_form.weight.value = '[field weight code="[cgi item_id]"]';
		}
		_NEXT_DESC_
		return true;
	}
</SCRIPT>
[/tmp]
[tmp do_clone]1[/tmp]
[loop prefix=item
		search="
				fi=[cgi mv_data_table]
				st=db
				co=yes

				se=[cgi item_id]
				sf=sku
				op=eq
				ac=0

				sf=o_matrix
				se=1
				op=ne
				ac=0

				sf=o_master
				se=.
				op=rn
				ac=0

				tf=o_sort

				rf=*
				"]
[calc]
	$Scratch->{built_js} =~ s/_NEXT_[A-Z]+_//g;
	return;
[/calc]
[on-match][if type=data term="[cgi mv_data_table]::o_matrix::[cgi item_id]" op=eq compare=2][tmp do_clone][/tmp][/if][/on-match]
[scratch built_js]
[list]
[item-sub do_js]
	my $parm = shift;
	$parm =~ s/'/\\'/g;

	my $line = shift;
	my $piece = <<EOF;
 	if(this_form.$parm\[this_form.$parm.selectedIndex].value != '') {
		other_form.description.value =
							other_form.description.value
							+ joiner
							+ this_form.$parm\[this_form.$parm.selectedIndex].text;
		joiner = ', ';
	}

	other_form.code.value =
			other_form.code.value
			+ '-'
			+ (this_form.$parm\[this_form.$parm.selectedIndex].value || '__MV_VARIANT_JOINER__' || '0');
	_NEXT_DESC_
EOF
	$Scratch->{built_js} =~ s/_NEXT_DESC_/$piece/;
	return;
[/item-sub]
<TR [item-alternate 2]BGCOLOR="__UI_T_ROW_EVEN__"[else]BGCOLOR="__UI_T_ROW_ODD__"[/else][/item-alternate]>

<TD>&nbsp;[page href=__UI_BASE__/flex_editor form=|
									mv_data_table=[cgi mv_data_table]
									ui_page_title=[cgi ui_page_title]
									ui_page_title=[cgi ui_page_banner]
									ui_meta_view=matrix_options
									ui_hide_key=1
									ui_class=Items
									ui_meta_specific=[cgi ui_meta_specific]
									ui_return_to=@@MV_PAGE@@
									item_id=[item-code]
						|][item-param o_group]</A></TD>
<TD>
	[item-exec do_js][item-param o_group][/item-exec]
	[accessories
				js=| onChange="set_description(simple_options, variant)"; |
				type=select
				name="[item-param o_group]"
				passed="=--choose--,[item-param o_value]"
	] </td>
<TD ALIGN=CENTER WIDTH="1"><A
onClick="return confirm('Are you sure you want to delete the [item-param o_group] ([item-code]) option?')"
HREF="[area
			href='@@MV_PAGE@@'
			form='
				deleterecords=1
				ui_delete_id=[item-code]
				sku=[cgi item_id]
				mv_data_table=[cgi mv_data_table]
				mv_click=db_maintenance
				mv_action=back
				mv_nextpage=@@MV_PAGE@@
			'
]"><IMG SRC="delete.gif" ALT="Delete [item-param o_group] option" BORDER=0></A></TD>
</tr>
[/list]
[no-match]
<tr>
<td colspan=6 align=center>
<B>No options.</B>
</td>
</tr>
[/no-match]
</table>
[/loop]

[page href=__UI_BASE__/flex_editor
		form="
			ui_new_item=1
			item_id=new
			sku=[cgi item_id]
			mv_data_table=[cgi mv_data_table]
			ui_return_to=@@MV_PAGE@@
			ui_return_to=ignore_sku=1
			ui_return_to=item_id=[cgi item_id]
			ui_te_override:sku=[cgi item_id]
			ui_data_fields=code sku o_group o_label o_value price
			ui_te_widget:sku=hidden_text
		"]<IMG SRC="plus.gif" ALT="[L]Add new option[/L]" BORDER=0 ALIGN=MIDDLE></A> &nbsp;<font size=2>[L]Add new option[/L]</font>
<br>
[if scratch do_clone]
[query
	list=1
	prefix=clone
	db=1
	table="[cgi mv_data_table]"
	sql="SELECT DISTINCT code
		 FROM  [cgi mv_data_table]
		 WHERE o_matrix  >= '1'
		 AND   o_enable  =  '1'
		 AND   o_master  =  '1'
		 AND   code      <> '[cgi item_id]'
		"
	more=1]<SELECT NAME=ui_clone_id>
<OPTION VALUE=""> --
[list]<OPTION VALUE="[clone-code]">[clone-filter 30][clone-description][/clone-filter][/list]
</SELECT>[more-list]<BR>[more]<BR>[/more-list][/query]<br>[button text="[L]Clone options[/L]"]
[flag type=write table="[cgi mv_data_table]"]
ui_clone_options=1
mv_todo=back
mv_nextpage=@@MV_PAGE@@
[/button]
[/if]

</FORM>

</TD><TD VALIGN=TOP>

[table-editor
	table_width=400
	default.o_matrix=1
	default.o_enable=1
	default.quantity=1
	override.sku=`$CGI->{item_id}`
	extra.description="wrap=auto"
	form_name=variant
	key=""
	table=options
	ui_data_fields="code sku description price wholesale inventory:quantity weight o_enable o_matrix"
	ui_display_only=products:description:sku
	ui_meta_view="variant"
	ui_new_item=1
	widget.o_matrix=hidden
	widget.o_enable=hidden
	widget.sku="hidden_text"
]

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