[perl]
	my $loc = $Config->{Options_repository}{Matrix} || {};
	$CGI->{mv_data_table} = $loc->{table} || 'options';
	$CGI->{mv_variant_table} = $loc->{variant_table} || 'variants';
	return;
[/perl]

<div class=titlebox>
[tmp o_display_type][data table="[cgi mv_data_table]" col=display_type key="[cgi item_id]"][/tmp]
Display type:
	[either][scratch o_display_type][or]default[/either]
	&nbsp;
	&nbsp;
	&nbsp;
	[page 
		href="admin/flex_editor"
		form="
			mv_data_table=[cgi mv_data_table]
			item_id=[cgi mv_item_id]
			ui_return_to=@@MV_PAGE@@
			ui_return_to=item_id=[cgi item_id]
			ui_te_widget:display_type=select
			ui_te_widget:code=hidden_text
			ui_te_label:display_type=Display type
			ui_te_options:display_type=one=One widget,separate=Separate widgets
			ui_te_default:display_type=[scratch o_display_type]
			ui_te_override:code=[cgi item_id]
			page_title=Change Matrix display type for [cgi item_id]
			page_banner=Change Matrix display type for [cgi item_id]
			ui_data_fields=code display_type
			ui_meta_view=matrix_display
		"
		]Change</A>
</div>

[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

					se=1
					sf=o_simple
					op=ne

					tf=[calcn]$Config->{Options_repository}{$Scratch->{setting}}{o_sort}[/calcn]

					rf=o_group,o_value,price,o_widget
		"][loop-param o_group]	[loop-param o_value]	[loop-param price]	[loop-param o_widget]
[/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] [cgi mv_variant_table] __UI_ITEM_TABLES__"]
#Debug("opts: $Scratch->{all_opts}");
	my $vtab = $CGI->{mv_variant_table};
	my $vdb = $Db{$vtab};
	unless ($vdb) {
		Log("can't open option table '%s'.", $vtab);
		return undef;
	}
	my $id = $CGI->{item_id};
	my %default = (
		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, $p, $w) = split /\t/, $_;
		push @optnames, $g;
		my @ary;
		my (@o) = grep /\S/, split /\s*,\s*/, $o;
		my (@p) = grep /\S/, split /\s*,\s*/, $p;
		my %p;
		my $multi = $Tag->widget_info($w, 'Multiple');
		for(@p) {
			my ($k, $v) = split /\s*=\s*/, $_;
			next unless $k;
			if($v =~ s/\s*%\s*$//) {
				next unless $v;
				$p{$k} = ($v / 100) * $Scratch->{tmp_price};
			}
			else {
				$p{$k} = $v;
			}
			$p{$k} = $v;
		}
		
		for(@o) {
			my ($k, $v) = split /\s*=\s*/, $_;
			$k = '__MV_VARIANT_JOINER__' || '0' if ! length($k);
			$v =~ s/\*$//;
			if ($multi) {
				push @dimensions, [[undef, undef, 0], [$k, $v, $p{$k}]];
			}
			else {
				push @ary, [ $k, $v, $p{$k} ];
			}
		}
		push @dimensions, \@ary unless $multi;
	}

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

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

	sub dosum {
		my $tot = 0;
		for(@_) {
			next unless $_;
			$tot += $_;
		}
		return $tot;
	}

	sub recurse_dim {
		my $dim = $_[0] || 0;
		return if $dim > $#dimensions;
		for (0 .. @{$dimensions[$dim]}-1) {
			$pointers[$dim] = $_;
			if ($dim == $#dimensions) {
				my $o = join('-', grep { defined } map { $dimensions[$_][$pointers[$_]][0] } (0..$#dimensions));
				next unless length $o;
				push @options, $o;
				push @descriptions, join(', ', grep { defined } map { $dimensions[$_][$pointers[$_]][1] } (0..$#dimensions));
				push @prices, dosum(map { $dimensions[$_][$pointers[$_]][2] } (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 } ) );
	my $tname = $vdb->name();
	for( my $i = 0; $i < @options; $i++) {
		my $key = "$id-$options[$i]";
		die ::errmsg("No key for create!") unless $key;
#Debug( "creating variant $key in table $tname for item=$id price=$prices[$i]");
		$hashes[$i]->{code} = $id;
		$hashes[$i]->{noformat} = 1;
		$hashes[$i]->{quantity} = 1;
		$price = $Scratch->{tmp_price};
		$price += $prices[$i];
		my $hash = { 
			code => $key,
			sku => $id,
			price => $price,
			description => "$Scratch->{tmp_desc}, $descriptions[$i]",
		};
		for(keys %default) {
			$hash->{$_} = $default{$_}
				if $vdb->test_column($_);
		}
		$vdb->set_slice($key, $hash);
	}

	return;

[/perl]
[/if]

<TABLE CELLSPACING=0 cellpadding=2 cellmargin=3 width="100%">
<TR class=rhead>
<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 
	more=1
	form=|
		item_id=[cgi item_id]
		ui_sequence_edit=[cgi ui_sequence_edit]
		item_id_left=[cgi item_id_left]
	|
	prefix=variant
	search="
				fi=[cgi mv_variant_table]
				st=db
				co=yes

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

				rf=code,sku,description,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_variant_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]
						[if type=data term="mv_metadata::code::variant::[cgi mv_variant_table]"]
						ui_meta_view=variant
						[else]
						ui_data_fields=code sku description price wholesale inventory:quantity weight
						[/else]
						[/if]
						ui_return_to=@@MV_PAGE@@
						ui_return_to=item_id=[cgi item_id]
						item_id=[variant-code]
					|][variant-code]</A></TD>
<TD>
	[variant-param description]
</td>
<TD ALIGN=right>
	[variant-filter currency][variant-param 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_variant_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">[more-list][more][/more-list]</td></tr>
<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=titlebox 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]

[loop prefix=item
		search="
				fi=[cgi mv_data_table]
				st=db
				co=yes

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

				tf=o_sort

				rf=*
				"]
[calc]
	$Scratch->{built_js} =~ s/_NEXT_[A-Z]+_//g;
	return;
[/calc]
[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
			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>
<SELECT NAME=ui_clone_id>
<OPTION VALUE=""> --
[query
	table="[cgi mv_data_table]"
	list=1
	prefix=clone
	db=1
	sql="SELECT DISTINCT sku
		 FROM  [cgi mv_data_table]
		 WHERE   sku      <> '[cgi item_id]'
		"
	more=1]
<OPTION VALUE="[clone-code]">[clone-filter 30.][clone-description][/clone-filter]
[/query]
</SELECT>
[button text="[L]Clone options[/L]"]
ui_clone_options=1
mv_todo=back
mv_nextpage=@@MV_PAGE@@
[/button]

</FORM>

</TD><TD VALIGN=TOP>

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

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