=head
 NAME: 
 DESCRIPTION: 
 PARAMETER:
	 [
		
	]

 VERSION_HISTORY:
	V1.00 2013-2-21 Cody Yu
	   1.新版本
	V1.01 2020-7-15 许仕斌
	   1.内外层层名增加t/b标识
	V1.02 2020-8-6 许仕斌
	   1.增加了根据叠构信息自动排列T/B标识
	V1.03 2022-2-28 fsy
	   1.假层下一层带入时增加屏蔽条件  
		
 HELP:
	<html><body bgcolor="#DDECFE">
		<font size="3" color="#003DB2"><p>功能简介</p></font>
		  <p> 根据预审指示工具类型更改层名 </p>
		  <br>
		<font size="3" color="#003DB2"><p>参数配置</p></font>
		<font color="#008000"><p> ● </p></font>
		  <p> </p>
		<font color="#008000"><p> ● </p></font>
		 <p> </p>
		<font size="3" color="#003DB2"><p>注意事项</p></font>
		  <p> ● 无 </p>
		  <br>
	</body></html>
  
=cut

#use strict;
#my ($JOB,$GEN,$GUI,$DB,$JOB_ID,$PAR);
use utf8;
use JSON;
use Data::Dump 'dump';
my ($Job,$Step) = ($JOB,undef);
my $Return = 'finish';
my @Report;
my (%stack_rows,$n,$y);

try {
	my $json = JSON->new->allow_nonref;
	###检查Genesis料号是否存在并打开
	if (! $GEN->isJobExists(job=>$Job) ){
        $GUI->msgbox(-icon=>'error',-text=>"料号 $Job 在Genesis中不存在, 请检查!");
        return 'Error';
    }
	$GEN->openJob(job=>$Job) unless ($GEN->isJobOpen(job=>$Job));
	my %matrix  = $GEN->getMatrix(job=>$Job,type=>'hash');
	
	my $inn_film_type = $IKM->get_jobinfo(-jobname=>$JOB,-jobcategory=>'work',-jobinfo=>'JNS_inn_tooling_type');
	my $out_film_type = $IKM->get_jobinfo(-jobname=>$JOB,-jobcategory=>'work',-jobinfo=>'JNS_out_tooling_type');
	my $silk_type = $IKM->get_jobinfo(-jobname=>$JOB,-jobcategory=>'work',-jobinfo=>'JNS_ss_procss_type');
	my $layer_count = $GEN->getLayerCount(job=>$Job);
	my $lamination_method = $IKM->get_jobinfo(-jobname=>$JOB,-jobcategory=>'work',-jobinfo=>'JNS_lamination_method');
	my $spec3 = $IKM->get_jobinfo(-jobname=>$JOB,-jobcategory=>'work',-jobinfo=>'JNS_pcb_speciality3');
	if ($layer_count > 16){
		$y = 16*31+120;
	}
	else{
		$y = $layer_count*31+120;
	}
	my $nameJOB = substr($Job,0,9);
	
	$GEN->openMatrix(job=>$Job);
	foreach my $layer (sort {$matrix{$a}{row} <=> $matrix{$b}{row}}  keys %matrix) {
		if ( ($matrix{$layer}{context} eq 'board')and ($matrix{$layer}{layer_type} eq 'silk_screen')  ){
			unless( $layer =~ /^(p|n)/ ){
				my $tmp;
				if( $silk_type =~ /^spray$/i ){
					$tmp = 'p-';
				}
				elsif( $silk_type =~ /^silkscreen$/i ){
					$tmp = 'n-';
				}
				else{
					next;
				}
				$GEN->COM( 'matrix_rename_layer',job=>$Job,matrix=>'matrix',layer=>$layer,new_name=>$tmp.$layer);
			}
		}
		elsif ( $matrix{$layer}{context} eq 'board' and ($matrix{$layer}{tl_name} =~ /^(top|bottom)$/ )  ){
			if ($layer =~ m/(t|b)/){
				$GUI->msgbox(-icon=>'info',-text=>"层已经被定义了上、下面，如需变更，请手动修正！",-gen => $GEN);
				# return 'Cancel'
			}
			push @stackup_layers,$layer;
		}
		elsif ( $matrix{$layer}{context} eq 'board' and ($matrix{$layer}{tl_name} =~ /^l\d+$/ )  ){
			if ($layer =~ m/(t|b)/){
				$GUI->msgbox(-icon=>'info',-text=>"层已经被定义了上、下面，如需变更，请手动修正！",-gen => $GEN);
				# return 'Cancel'
			}
			push @stackup_layers,$layer;
		}
	}
	my $toplinker_layers = $IKM->select_arrayhash(-table=>'pdm_job_stack_layers',-where=>{job_id=>$JOB_ID});
	my @cust_layer;

	foreach my $item (sort{$a->{layer_num} <=> $b->{layer_num}} @$toplinker_layers) {
		if($item->{attr_data} && $item->{layer_type} ne "Silk Screen" && $item->{layer_type} ne "Solder Mask"){
			my $json_attr = $json->decode($item->{attr_data});
			my $cust_ly = $json_attr->{customized_layer_name};
			if($cust_ly && $cust_ly ne "") {
				if($cust_ly =~ /jia/) {
					push @cust_layer, $cust_ly;
				} else {
					if($cust_ly =~ /(\d+)/) {
						push @cust_layer, ($1).($item->{layer_side} eq "Top"? "t" : "b");
					} else {
						push @cust_layer, $cust_ly;
					}
				}
			}
		}
	}
	my $test;
	foreach my $layer (@stackup_layers){
		my ($worklyrn) = $layer =~ /(\d+)/;
		$ope_boolean = 1 if ($layer =~ m/^ope-/);
		
		$n ++;
		$stack_rows{$layer}{sequence}   = $n;
		$stack_rows{$layer}{gen_name}   = $layer;
		#enum_tl_side

		if (scalar(@cust_layer) > 1 ){
			foreach $l (@cust_layer) {
				my ($lyrn) = $l =~ /(\d+)/;
				# $GEN->PAUSE($l);
				$l =~ s/-//;
				#$GUI->msgbox(-icon=>'info',-text=>"$l   $layer  ");
				if ($l =~ /^$layer/ ){
					$lm = substr($l ,-2,1);
					if ( $lm eq 't'){
						$stack_rows{$layer}{enum_tl_side} =  'top';
						last;
					}
					else{
						$stack_rows{$layer}{enum_tl_side} =  'bottom';
						last;
					}
				}
				else{
					if($worklyrn == $lyrn and $layer !~ /^jia/ and $l !~ /^jia/){
						if ($l =~ /([a-zA-Z]+)/){
							if ($1 eq 't'){
								$stack_rows{$layer}{enum_tl_side} =  'top';
								last;
							}
							else{
								$stack_rows{$layer}{enum_tl_side} =  'bottom';
								last;
							}
							
						}
					}
				}
			}
			$test = 'auto';
		}
		else{
			if ( defined $matrix{$layer}{enum_tl_side} ){
			$stack_rows{$layer}{enum_tl_side} =  $matrix{$layer}{enum_tl_side};
			}
			else{
				if(defined $matrix{$layer}{tl_type} and $matrix{$layer}{tl_type} eq 'outer'){#外层
					if ( defined $matrix{$layer}{tl_num} and $matrix{$layer}{tl_num}%2 == 1 ){
						$stack_rows{$layer}{enum_tl_side} =  'top';
					}else{
						$stack_rows{$layer}{enum_tl_side} =  'bottom';
					}
				}else{#内层
					if ($spec3 eq 'through_board'){#通孔板
						if ( defined $matrix{$layer}{tl_num} and $matrix{$layer}{tl_num}%2 == 0 ){
							if ($lamination_method eq 'cu'){#覆箔法
								$stack_rows{$layer}{enum_tl_side} =  'top';
							}elsif ($lamination_method eq 'core'){#覆板法
								if ($ope_boolean){#包含ope层
									$stack_rows{$layer}{enum_tl_side} =  'top';
								}else{
									$stack_rows{$layer}{enum_tl_side} =  'bottom';
								}
							}
						}else{
							if ($lamination_method eq 'cu'){#覆箔法
								$stack_rows{$layer}{enum_tl_side} =  'bottom';
							}elsif ($lamination_method eq 'core'){#覆板法
								if ($ope_boolean){#包含ope层
									$stack_rows{$layer}{enum_tl_side} =  'bottom';
								}else{
									$stack_rows{$layer}{enum_tl_side} =  'top';
								}
							}
						}
					}else{
						$stack_rows{$layer}{enum_tl_side} =  '';
					}
				}
			}
			$test = 'work';
		}
		my $facefile = "E:/$nameJOB.txt";
		# if (-e $facefile){
		# 	open (LAYERMYFILE,$facefile);
		# 	while (<LAYERMYFILE>) {
		# 		chomp;
		# 		my ($lyrn) = $_ =~ /(\d+)/;
		# 		$_ =~ s/-//;
		# 		#$GUI->msgbox(-icon=>'info',-text=>"$_   $layer  ");
		# 		if ($_ =~ /^$layer/ ){
		# 			$lm = substr($_ ,-2,1);
		# 			if ( $lm eq 't'){
		# 				$stack_rows{$layer}{enum_tl_side} =  'top';
		# 				last;
		# 			}
		# 			else{
		# 				$stack_rows{$layer}{enum_tl_side} =  'bottom';
		# 				last;
		# 			}
		# 		}
		# 		else{
		# 			if($worklyrn == $lyrn and $layer !~ /^jia/ and $_ !~ /^jia/){
		# 				if ($_ =~ /([a-zA-Z]+)/){
		# 					if ($1 eq 't'){
		# 						$stack_rows{$layer}{enum_tl_side} =  'top';
		# 						last;
		# 					}
		# 					else{
		# 						$stack_rows{$layer}{enum_tl_side} =  'bottom';
		# 						last;
		# 					}
							
		# 				}
		# 			}
		# 		}
		# 	}
		# 	close LAYERMYFILE;
		# 	$test = 'auto';
		# }
		# else{
		# 	if ( defined $matrix{$layer}{enum_tl_side} ){
		# 	$stack_rows{$layer}{enum_tl_side} =  $matrix{$layer}{enum_tl_side};
		# 	}
		# 	else{
		# 		if(defined $matrix{$layer}{tl_type} and $matrix{$layer}{tl_type} eq 'outer'){#外层
		# 			if ( defined $matrix{$layer}{tl_num} and $matrix{$layer}{tl_num}%2 == 1 ){
		# 				$stack_rows{$layer}{enum_tl_side} =  'top';
		# 			}else{
		# 				$stack_rows{$layer}{enum_tl_side} =  'bottom';
		# 			}
		# 		}else{#内层
		# 			if ($spec3 eq 'through_board'){#通孔板
		# 				if ( defined $matrix{$layer}{tl_num} and $matrix{$layer}{tl_num}%2 == 0 ){
		# 					if ($lamination_method eq 'cu'){#覆箔法
		# 						$stack_rows{$layer}{enum_tl_side} =  'top';
		# 					}elsif ($lamination_method eq 'core'){#覆板法
		# 						if ($ope_boolean){#包含ope层
		# 							$stack_rows{$layer}{enum_tl_side} =  'top';
		# 						}else{
		# 							$stack_rows{$layer}{enum_tl_side} =  'bottom';
		# 						}
		# 					}
		# 				}else{
		# 					if ($lamination_method eq 'cu'){#覆箔法
		# 						$stack_rows{$layer}{enum_tl_side} =  'bottom';
		# 					}elsif ($lamination_method eq 'core'){#覆板法
		# 						if ($ope_boolean){#包含ope层
		# 							$stack_rows{$layer}{enum_tl_side} =  'bottom';
		# 						}else{
		# 							$stack_rows{$layer}{enum_tl_side} =  'top';
		# 						}
		# 					}
		# 				}
		# 			}else{
		# 				$stack_rows{$layer}{enum_tl_side} =  '';
		# 			}
		# 		}
		# 	}
		# 	$test = 'work';
		# }
		#gen_ldi
		if(defined $matrix{$layer}{tl_type} and $matrix{$layer}{tl_type} eq 'outer'){#外层
			if( $out_film_type =~ /^ldi$/i){
				$stack_rows{$layer}{gen_ldi}   = 'ldi';
			}
			else{
				$stack_rows{$layer}{gen_ldi}   = 'film';
			}
		}
		else{
			if( $inn_film_type =~ /^ldi$/i ){
				$stack_rows{$layer}{gen_ldi}   = 'ldi';
			}
			else{
				$stack_rows{$layer}{gen_ldi}   = 'film';
			}
		}
	}	
	if ($test eq 'work'){
		$GUI->msgbox(-icon=>'error',-text=>"没有检测到叠构信息，请注意人工核对信息!");
	}
	my $stackup_new = $GUI->show_tableform (
			-defaultsize=>[450,$y],
			-title => "请确认料号的叠构信息--------------" ,
			-rows => \%stack_rows,
			-showcheck => 1,-gen => $GEN,
			-columns => [
				{
					column_name=>'gen_name',
					label=>'层别名称',
					width=>150,
					type=>'string',
					validate_func=>sub{
						my %par = @_;
						my $layer = $par{formpanel}->get_value($par{row},'gen_name');
						$par{formpanel}->get_widget($par{row},'gen_name')->set_editable(0);
						if ($matrix{$layer}{context} eq 'board' and $matrix{$layer}{layer_type} eq 'signal' and $matrix{$layer}{polarity} eq 'positive' and $layer !~ m/jia/){
							return {
								bgcolor => '#FDC74D',
							};
						}
						elsif ($matrix{$layer}{context} eq 'board' and $matrix{$layer}{layer_type} eq 'power_ground' and ($matrix{$layer}{polarity} eq 'negative' or $matrix{$layer}{polarity} eq 'positive') and $layer !~ m/jia/){
							return {
								bgcolor => '#DBAD00',
							};
						}
						return {};
					}
				},
				{
					column_name=>'enum_tl_side',
					label=>'层别面次',
					type=>'enum',
					must_field=>1,
					width=>120,
					#sensitive =>0, 
					property=>{
						tl_field=>[name=>'scalar',display_name=>'text'],
						tl_value_field=>'name',
						tl_data=>[
							{name=>'top',display_name=>'top'},
							{name=>'bottom',display_name=>'bottom'},
						]
					},
					validate_func=>sub{
						my %par = @_;
						my $layer = $par{formpanel}->get_value($par{row},'gen_name');
						my $tl_side = $par{formpanel}->get_value($par{row},'enum_tl_side');
						if ($matrix{$layer}{tl_name} eq 'top' and $tl_side eq 'bottom' ){
							return {
								bgcolor => 'red',
								error_msg=>'外层层别面次选择错误',
							};
						}
						elsif ($matrix{$layer}{tl_name} eq 'bottom' and $tl_side eq 'top' ){
							return {
								bgcolor => 'red',
								error_msg=>'外层层别面次选择错误',
							};
						}
						if ($tl_side eq 'top'){
							return {
								#bgcolor => '#CDDD00',
								bgcolor => 'light green',
							};
						}
						return {};
					}
				},
				{
					column_name=>'gen_ldi',
					label=>'曝光方式',
					type=>'enum',
					must_field=>1,
					width=>120,
					#sensitive =>0, 
					property=>{
						tl_field=>[name=>'scalar',display_name=>'text'],
						tl_value_field=>'name',
						tl_data=>[
							{name=>'ldi',display_name=>'ldi'},
							{name=>'film',display_name=>'film'},
						]
					},
					validate_func=>sub{
						my %par = @_;
						my $tl_ldi = $par{formpanel}->get_value($par{row},'gen_ldi');
						if ($tl_ldi eq 'film'){
							return {
								#bgcolor => '#0DBA00',
								bgcolor => '#88777B',
							};
						}
						return {};
					}
				},
			],
		);
		return 'Cancel' unless ($stackup_new);
		
	foreach my $layer (sort {$matrix{$a}{row} <=> $matrix{$b}{row}} keys %{$stackup_new}){
		if($stackup_new->{$layer}{enum_tl_side} eq "top"){
			if ($stackup_new->{$layer}{gen_ldi} eq "ldi"){
				if ($layer =~ /ldi$/i){
					$layerold = $layer;
					$layer =~ s/-ldi/t-ldi/;
					$GEN->COM( 'matrix_rename_layer',job=>$Job,matrix=>'matrix',layer=>$layerold,new_name=>$layer);
				}
				else{
					$GEN->COM( 'matrix_rename_layer',job=>$Job,matrix=>'matrix',layer=>$layer,new_name=>$layer.'t-ldi');
				}
			}
			else{
				if ($layer =~ /ldi$/i){
					$layerold = $layer;
					$layer =~ s/-ldi/t/;
					$GEN->COM( 'matrix_rename_layer',job=>$Job,matrix=>'matrix',layer=>$layerold,new_name=>$layer);
				}
				else{
					$GEN->COM( 'matrix_rename_layer',job=>$Job,matrix=>'matrix',layer=>$layer,new_name=>$layer.'t');
				}
			}
		}
		else{
			if ($stackup_new->{$layer}{gen_ldi} eq "ldi"){
				if ($layer =~ /ldi$/i){
					$layerold = $layer;
					$layer =~ s/-ldi/b-ldi/;
					$GEN->COM( 'matrix_rename_layer',job=>$Job,matrix=>'matrix',layer=>$layerold,new_name=>$layer);
				}
				else{
					$GEN->COM( 'matrix_rename_layer',job=>$Job,matrix=>'matrix',layer=>$layer,new_name=>$layer.'b-ldi');
				}
			}
			else{
				if ($layer =~ /ldi$/i){
					$layerold = $layer;
					$layer =~ s/-ldi/b/;
					$GEN->COM( 'matrix_rename_layer',job=>$Job,matrix=>'matrix',layer=>$layerold,new_name=>$layer);
				}
				else{
					$GEN->COM( 'matrix_rename_layer',job=>$Job,matrix=>'matrix',layer=>$layer,new_name=>$layer.'b');
				}
			}
		}
	}
		
	$GEN->COM( 'matrix_page_close',job=>$Job,matrix=>'matrix');
	
	#$GUI->msgbox(-icon=>'info',-text=>"$layer - 上",-gen => $GEN);
    ###output and return status, if genesis error, it will output genesis error command
	unless ($GEN->{STATUS}){
		return $Return;
	}
	else{
		$GUI->msgbox(-icon=>'error',-text=>join("\n",@{$GEN->{STATUS}}));
		$IKM->update_job_workflow_log(-notes=>"   Genesis Error:\n ".join("\n   ",@{$GEN->{STATUS}}));
		return 'Error';
	}
}

catch Error::Simple with {
	my $error = shift;
	$GUI->msgbox(-icon=>'error',-text=>$error);
	return 'Error';
}

finally{

};
