<?php
define('YUKEY', true);
require(str_replace('sicms','',dirname(__FILE__)) . '/includes/init.php');
check_login();
$active=!empty($_GET['active']) ? intval($_GET['active']) : 2;
//var_dump($act);exit;
if($act=='ready' || $act=='edit_report')
{
    if($id !='')
	{
	require(ROOT_PATH . 'includes/lib_reports.php');
	require(ROOT_PATH . 'includes/lib_instructs.php');
	$rep=rep_details($id);
	if($pic=get_row('','*','tbReportNumber',$rep['tbReportNumber'],'pics')){
	$pic['tbPackimg'] = $pic && $pic['tbPackimg'] != '' ? explode(',', $pic['tbPackimg']) : array();
	$pic['tbStyleimg'] = $pic && $pic['tbStyleimg'] != '' ? explode(',', $pic['tbStyleimg']) : array();
	$pic['tbAppearanceimg'] = $pic && $pic['tbAppearanceimg'] != '' ? explode(',', $pic['tbAppearanceimg']) : array();
	$pic['tbTestimg'] = $pic && $pic['tbTestimg'] != '' ? explode(',', $pic['tbTestimg']) : array();}
	$ver=get_rows("","tbWN,tbClientID,tbProductName,tbVersion ","`tbProductName`='".$rep['tbProductName']."' ","instructs","tbVersion");
	$wi=get_wi_summary($rep['tbProductName'],$rep['tbWI']);
		   $strt=$strm=$cpf=$cpm=$idt=$idm='';
		   $idt=explode(',',$wi['tbTest']);
		   $idm=explode(',',$wi['tbMeasure']);
		   foreach($idt as $value)
		   {
		   $cpf=get_row('si','tbCheckPoint','tbSN',$value,'items');
		   $strt .=$cpf['tbCheckPoint']."/";
		   }
		   $strt="Function testing(" . rtrim($strt,"/") . ")";
		   foreach($idm as $value)
		   {
		   $cpm=get_row('si','tbCheckPoint','tbSN',$value,'items');
		   $strm[]=[$value=>$cpm['tbCheckPoint']];
		   }
		   //var_dump($strm);exit;
	$defects=get_rows("","*","`tbReportNumber`='".$rep['tbReportNumber']."' ","find_defects","tbFN");
	$factory_user=get_users('tbCompanyID',$rep['tbFactoryID']);
	$tests=get_rows('si','*','tbSN<51','items','tbSN');
	$idf=$cp=$strf=$tested="";
	$tested=leftjoin('si',"a.*,b.*",'tbTestedID','tbSN','tbReportNo',$rep['tbReportNumber'],'tested','items','a.tbPosition');
	//var_dump($items);exit;
	if($tested!='')
	{
		   foreach($tested as $value)
		   {
		   		if(strpos($value['tbTestedID'],","))
				{
				  	$idf=explode(',',$value['tbTestedID']);
				  	foreach($idf as $v)
				   	{
				   	$cp=get_row('si','tbCheckPoint','tbSN',trim($v),'items');
				   	$strf .=$cp['tbCheckPoint']."/";
				   	}
				   	$strf ="Function testing(" . rtrim($strf,"/") . ")";
		   		}
			}
	}	
	$smarty->assign('aql',explode(",",$rep['tbAQL']));
	$smarty->assign('active',$active);
	$smarty->assign('id',$id);
	$smarty->assign('pre',$pre);
	$smarty->assign('tbTest',$wi['tbTest']);
	$smarty->assign('fuTest',$strt);
	$smarty->assign('meTest',$strm);
	$smarty->assign('pic',$pic);
	$smarty->assign('defects',$defects); 
	$smarty->assign('rep',$rep); 
	$smarty->assign('ver',$ver);
	$smarty->assign('wi_summary',$wi); 
	$smarty->assign('factory_user',$factory_user);
	$smarty->assign('tested',$tested);
	$smarty->assign('testedf',$strf);
	$smarty->assign('tests',$tests);
    if($act=='ready')$smarty->display('report_create.dwt');
	if($act=='edit_report')$smarty->display('report_edit.dwt');
	
	}else{
	$smarty->display('message.dwt');
	}
}
elseif ($act=='get_wi')
{
	$tbProductName=!empty($_POST['tbProductName']) ? $_POST['tbProductName'] : '';
	$tbWI=!empty($_POST['tbWI']) ? $_POST['tbWI'] : '';
	require(str_replace('sicms','',dirname(__FILE__)) . '/includes/lib_instructs.php');
    $wi=get_wi_summary($tbProductName,$tbWI);
	echo json_encode($wi);
}
elseif ($act=='get_topic')
{
    $tbCategory=!empty($_POST['tbCategory']) ? $_POST['tbCategory'] : '';
	$defects=get_rows("si","*","`tbCategory`='".$tbCategory."' ","defects","tbTopical");
	echo json_encode($defects);
}
elseif($act=='save_defect')
{
	require(str_replace('sicms','',dirname(__FILE__)) . '/includes/get_rdata.php');
    if($rep!='')
	{
		require(str_replace('sicms','',dirname(__FILE__)) . '/includes/in_ddata.php');
		if($GLOBALS['db']->query($sql))
		{
		   require(str_replace('sicms','',dirname(__FILE__)) . '/includes/show_ditem.php');
	       echo json_encode($str);
		 } 
		else
		{
		echo json_encode("Database query error!");
		}
	}
	else
	{
	echo json_encode("Report Number is not Exist,Error!");
	}
}
elseif($act=='getdefect')
{
    $id=!empty($_POST['id']) ? $_POST['id'] : '';
	$rep=!empty($_POST['rep']) ? $_POST['rep'] : '';
	$defect=get_row('',"*","tbFN",$id,"find_defects");
	echo json_encode($defect);
}
elseif($act=='del_defect')
{   $sql = $str = '';
	$id=!empty($_POST['id']) ? $_POST['id'] : '';
	$rep=!empty($_POST['rep']) ? $_POST['rep'] : '';
	$sql = "DELETE FROM " . $GLOBALS['prefix']->table('find_defects') . "WHERE tbFN='" . $id . "'";
	if($GLOBALS['db']->query($sql))
		{
   		   require(str_replace('sicms','',dirname(__FILE__)) . '/includes/show_ditem.php');
	       echo json_encode($str);
		}
		else
		{
		   echo "Database query error!";
		}
}
elseif($act=='save_point')
{
	require(str_replace('sicms','',dirname(__FILE__)) . '/includes/get_tdata.php');
	if($rep!='')
	{
		require(str_replace('sicms','',dirname(__FILE__)) . '/includes/in_tdata.php');
		if($exist==false){
			if($GLOBALS['db']->query($sql))
			  {
			   require(str_replace('sicms','',dirname(__FILE__)) . '/includes/show_titem.php');
			   echo json_encode($str);
			  }else{
			   echo json_encode("Database query error!");
			  }
		}	  
	}
	else
	{
	echo json_encode("Report Number is not Exist,Error!");
	}
}
elseif($act=='getpoint')
{
    $id=!empty($_POST['id']) ? $_POST['id'] : '';
	$rep=!empty($_POST['rep']) ? $_POST['rep'] : '';
	$point=leftjoin('si',"a.*,b.*",'tbTestedID','tbSN','a.tbTN',$id,'tested','items','');
	//var_dump($point);exit;
	echo json_encode($point);
}
elseif($act=='del_point')
{   $sql = $str = '';
	$id=!empty($_POST['id']) ? $_POST['id'] : '';
	$rep=!empty($_POST['rep']) ? $_POST['rep'] : '';
	$sql = "DELETE FROM " . $GLOBALS['prefix']->table('tested') . "WHERE tbTN='" . $id . "'";
	if($GLOBALS['db']->query($sql))
		{
   		   require(str_replace('sicms','',dirname(__FILE__)) . '/includes/show_titem.php');
	       echo json_encode($str);
		}
		else
		{
		   echo "Database query error!";
		}
}
elseif($act=='save_report')
{
	// var_dump($_POST);exit;
    require(ROOT_PATH . 'includes/lib_reports.php');
	$rep=rep_details($id);
    $tbWI=!empty($_POST['tbWI']) ? intval($_POST['tbWI']) : '';
	$tbInspectLevel=!empty($_POST['tbInspectLevel']) ? $_POST['tbInspectLevel'] : '';
	//$tbSampleSize=!empty($_POST['tbSampleSize']) ? $_POST['tbSampleSize'] : '';
	$Critical=!empty($_POST['Critical']) ? $_POST['Critical'] : '';
	$Major=!empty($_POST['Major']) ? $_POST['Major'] : '';
	$Minor=!empty($_POST['Minor']) ? $_POST['Minor'] : '';
	$tbAQL=$Critical .','. $Major .','. $Minor;
    
	//图片
    $packImg = (!empty($_POST['pack_upload'])) ? implode(',', $_POST['pack_upload']) : '';
    $testImg = (!empty($_POST['test_upload'])) ? implode(',', $_POST['test_upload']) : '';
	$styleImg = (!empty($_POST['style_upload'])) ? implode(',', $_POST['style_upload']) : '';
    $appearanceImg = (!empty($_POST['appearance_upload'])) ? implode(',', $_POST['appearance_upload']) : '';

	$tbCR=!empty($_POST['tbCR']) ? $_POST['tbCR'] : '0';
	$tbMA=!empty($_POST['tbMA']) ? $_POST['tbMA'] : '0';
	$tbMI=!empty($_POST['tbMI']) ? $_POST['tbMI'] : '0';
	$tbTestPlan=!empty($_POST['tbTestPlan']) ? $_POST['tbTestPlan'] : '';
	$tbKitBom=!empty($_POST['tbKitBom']) ? $_POST['tbKitBom'] : '';
	$tbPacking=!empty($_POST['tbPacking']) ? $_POST['tbPacking'] : '';
	$tbStyle=!empty($_POST['tbStyle']) ? $_POST['tbStyle'] : '';
	$tbMeasure=!empty($_POST['tbMeasure']) ? $_POST['tbMeasure'] : '';
	$tbSupplement=!empty($_POST['tbSupplement']) ? $_POST['tbSupplement'] : '';
	$tbCheckQuantity=!empty($_POST['tbCheckQuantity']) ? $_POST['tbCheckQuantity'] : '';
	$tbCheckPacking=!empty($_POST['tbCheckPacking']) ? $_POST['tbCheckPacking'] : '';
	$tbCheckSMC=!empty($_POST['tbCheckSMC']) ? $_POST['tbCheckSMC'] : '';
	$tbCheckAppearance=!empty($_POST['tbCheckAppearance']) ? $_POST['tbCheckAppearance'] : '';
	$tbCheckMeasure=!empty($_POST['tbCheckMeasure']) ? $_POST['tbCheckMeasure'] : '';
	$tbResult=!empty($_POST['tbResult']) ? $_POST['tbResult'] : '';
	$tbMS=!empty($_POST['tbMS']) ? $_POST['tbMS'] : '';
	$tbJudge=!empty($_POST['tbJudge']) ? $_POST['tbJudge'] : '';
	$count=substr_count($rep['tbModelNO'],'&');
    $tbLotQty=$tbCTNQty = $tbShortQty = $tbSerialNO='';
	if($count == 0)
	{
		$tbLotQty=!empty($_POST['tbShippingQty']) ? $_POST['tbShippingQty'] : '';
		$tbCTNQty=!empty($_POST['tbCTNQty']) ? $_POST['tbCTNQty'] : '';
		$tbShortQty=!empty($_POST['tbShortQty']) ? $_POST['tbShortQty'] : '0';
		$tbSerialNO=!empty($_POST['tbSerialNO']) ? $_POST['tbSerialNO'] : '';
	}
	else
	{
	    for($k=0;$k<=$count;$k++)
		{
		$tbLotQty += intval($_POST["ShipQty_$k"]);
		$tbCTNQty .= !empty($_POST["CTNQty_$k"]) ? $_POST["CTNQty_$k"] . ' & ' : ' & ';
		$tbShortQty .= !empty($_POST["Short_$k"]) ? $_POST["Short_$k"] . ' & ' : ' 0 & '; //如果输入的数字为0就POST不到数据,empty函数？
		$tbSerialNO .= !empty($_POST["SerialNO_$k"]) ? $_POST["SerialNO_$k"] . ' & ' : ' & ';
		}
		$tbCTNQty=substr(trim($tbCTNQty),-1)=='&' ? substr(trim($tbCTNQty),0,-1) : trim($tbCTNQty);
		$tbShortQty=substr(trim($tbShortQty),-1)=='&' ? substr(trim($tbShortQty),0,-1) : trim($tbShortQty);
		$tbSerialNO=substr(trim($tbSerialNO),-1)=='&' ? substr(trim($tbSerialNO),0,-1) : trim($tbSerialNO);
	}
	$Sr="UPDATE " .$GLOBALS['prefix']->table('reports'). " SET". "  tbLotQty='$tbLotQty',tbWI='$tbWI',tbInspectLevel='$tbInspectLevel',tbAQL='$tbAQL',tbSerialNO ='$tbSerialNO',tbCTNQty='$tbCTNQty',tbShortQty='$tbShortQty',tbTestPlan='$tbTestPlan',tbKitBom='$tbKitBom',tbPacking='$tbPacking',tbStyle='$tbStyle',tbMeasure='$tbMeasure',tbSupplement='$tbSupplement',tbCheckQuantity='$tbCheckQuantity',tbCheckPacking='$tbCheckPacking',tbCheckSMC='$tbCheckSMC',tbCheckAppearance='$tbCheckAppearance',tbCheckMeasure='$tbCheckMeasure',tbResult='$tbResult',tbMS='$tbMS',tbJudge='$tbJudge',tbReport='1',tbCR='$tbCR',tbMA='$tbMA',tbMI='$tbMI' WHERE tbPN='$id'";
	if(get_row('','tbPN','tbReportNumber',$rep['tbReportNumber'],'pics')==false){
	$Sp = "INSERT INTO ".$GLOBALS['prefix']->table('pics')." (tbReportNumber,tbPackimg,tbStyleimg,tbAppearanceimg,tbTestimg) VALUES ('".$rep['tbReportNumber']."','$packImg','$styleImg','$appearanceImg','$testImg')";
	}else{
	$Sp="UPDATE " .$GLOBALS['prefix']->table('pics'). " SET ". "tbPackimg='$packImg',tbStyleimg='$styleImg',tbAppearanceimg='$appearanceImg',tbTestimg='$testImg' WHERE tbReportNumber='".$rep['tbReportNumber']."'";
	}
	
	if($GLOBALS['db']->query($Sr))
	{
		if($GLOBALS['db']->query($Sp)){
		ecs_header("Location:report_page.php?id=$id&active=2&client=$pre");
		}else{
			$GLOBALS['err'] ->add($GLOBALS['_LANG']['pic_update_fail']);
			$GLOBALS['err'] ->show();
		}
	}else{
			$GLOBALS['err'] ->add($GLOBALS['_LANG']['rep_update_fail']);
			$GLOBALS['err'] ->show();
	}
}
elseif($act=='add_serial')
{   
     $str='';
	 $rep=!empty($_POST['rep']) ? $_POST['rep'] : '';
	 $tbSerial=!empty($_POST['tbSerial']) ? $_POST['tbSerial'] : '';
	 $count=!empty($_POST['count']) ? $_POST['count'] : '';
	 $start=substr($tbSerial,0,-5);
	 $end=substr($tbSerial,-5);
		for($i=0;$i<$count;$i++)
		{  
		  		  $sql="INSERT INTO ". $GLOBALS['prefix']->table('serial2') ."(tbReportNumber,tbSerialNumber) VALUES ('$rep','$tbSerial')";
			   if($GLOBALS['db']->query($sql)){
			   	  $str.="<li>" . $tbSerial . "</li>";
				  $end=sprintf("%05d",$end+1);
		  		  $tbSerial=$start.$end;		   
			   }else{
			   	  echo json_encode("Write Database Error!");
			   }
		}
		  $string="<div class='b_line'>" . $str . "</div>";
		  echo json_encode($string);	  
}

elseif($act=='del_serial')
{   
	$id=!empty($_POST['id']) ? $_POST['id'] : '';
	$rep=!empty($_POST['rep']) ? $_POST['rep'] : '';
	$sql= "DELETE FROM " . $GLOBALS['prefix']->table('serial2') . " WHERE tbSN='" . $id . "'";
	if($GLOBALS['db']->query($sql))
		{
		   $serials=get_rows("","*","`tbReportNumber`='".$rep."' ","serial2","tbSerialNumber");
		   $i=0;
		   $str="<table width='98%'  border='0' cellpadding='1' cellspacing='1'><tr>";
		   foreach($serials as $value)
		   {
		    $i++;
			$str.="<td width='25%'><img src='pics/remove.png' onclick='delserial(\"" . $value['tbSN'] . "\",\"" . $value['tbReportNumber']."\");' title='Remove' style='cursor:pointer;' width='12' height='12' />" . $value['tbSerialNumber'];
				if(($i%4) != 0){
					$str .= "</td>";
				}else{
				 	$str .= "</td></tr><tr>";
				}
		   }
		   $str .="</tr></table>";
		echo json_encode($str);
		}else{
		echo json_encode("Database query error!");
		}
}

elseif($act=='c_status')
{
        $sql_r='';
	    $sql_r="UPDATE " .$GLOBALS['prefix']->table('reports'). " SET". " tbReport='2' WHERE tbPN='$id'";
		if($GLOBALS['db']->query($sql_r))
		{
		ecs_header("Location:reports.php?active=2&client=$pre");
		}
		else
		{
				$GLOBALS['err'] ->add($GLOBALS['_LANG']['rep_update_fail']);
				$GLOBALS['err'] ->show();
		}
}
?>