<?php
define('YUKEY', true);
require(str_replace('sicms','',dirname(__FILE__)) . '/includes/init.php');
//check_user("yukey","2133335");//测试用
//update_user_info();//测试用
check_login();
require(ROOT_PATH . 'includes/lib_instructs.php');
$active=!empty($_GET['active']) ? intval($_GET['active']) : 3;
if($act=='ready')
{
	$total=count_item('','','instructs',''); //总记录条数
	$page_num=20;  //每页显示的记录数
	$allpage=ceil($total/$page_num); //总页数
	$current_page=!empty($_GET['nowpage']) ? intval($_GET['nowpage']) : 1; //目前页数 
	$start_num=$page_num*($current_page-1); //$start_num 起始笔数
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('list_wi',get_wi_list($start_num,$page_num));
	$smarty->assign('all_page',$allpage);
	$smarty->assign('all_pages',listpage($allpage));
	$smarty->assign('now_page',$current_page);
	$smarty->display('instructs.dwt');
}
elseif ($act=='get_wi' || $act=='edit' || $act=='upgrade' || $act=='create')
{
	if(isset($id) && $id!='')
	{
		$wi=get_wi($id);//这里不能用get_row函数代替
		if(($_SESSION['tbRank']>='50') || ($_SESSION['tbCompanyID']==$wi['tbClientID']) || ($_SESSION['tbCompanyID']==$wi['tbFactoryID']))
		{
		   $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']."/";
		   }
		   foreach($idm as $key=>$value)
		   {
		   $key=$key+2;
		   $cpm=get_row('si','tbCheckPoint','tbSN',$value,'items');
		   $strm .=$key.": ".$cpm['tbCheckPoint']."<br>";
		   }
		   $smarty->assign('active',$active);
		   $smarty->assign('wi',$wi);
		   $smarty->assign('pre',$pre);
		   $smarty->assign('tbTest',rtrim($strt,"/"));
		   $smarty->assign('tbMeasure',rtrim($strm,"<br>"));
		if($act=='get_wi'){
		   $smarty->display('instructs_page.dwt');}
		if($act=='edit' || $act=='create' || $act=='upgrade'){
		   $tests=get_rows('si','*','tbSN<47','items','tbCheckPoint');
		   $ftest=get_rows('si','*','tbSN>100','items','tbCheckPoint');
		   $factory_list=get_rows('si','tbCompanyID,tbType,tbNickName','tbType=2','companys','tbNickName');
		   if($act=='upgrade'){
		      $max_ver=get_max_ver($wi['tbProductName']);
			  $max_ver++;
			  $smarty->assign('max_ver',$max_ver);
			  }
		   $smarty->assign('factory_list',$factory_list);
		   $smarty->assign('tests',$tests);
		   $smarty->assign('ftest',$ftest);
		   $smarty->assign('idt',$idt);
		   $smarty->assign('stbTest',$wi['tbTest']);
		   $smarty->assign('act',$act);
		   $smarty->assign('id',$id);
		   $smarty->display('instructs_edit.dwt');
		   }
		}
		else
		{
		$GLOBALS['err'] ->add($GLOBALS['_LANG']['permission']);
		$GLOBALS['err'] ->show();
		}
	}
}
elseif ($act=='save_edit' || $act=='save_upgrade' || $act=='save_create')
{
	$tbProductName=!empty($_POST['tbProductName']) ? $_POST['tbProductName'] : '';
	$tbClientID=!empty($_POST['tbClientID']) ? $_POST['tbClientID'] : '';
	$tbVersion=!empty($_POST['tbVersion']) ? $_POST['tbVersion'] : '';
	$tbIssueDate=!empty($_POST['tbIssueDate']) ? $_POST['tbIssueDate'] : '';
	$tbDescript=!empty($_POST['tbDescript']) ? $_POST['tbDescript'] : '';
	$tbEffect=!empty($_POST['tbEffect']) ? $_POST['tbEffect'] : '';
	$tbModel=!empty($_POST['tbModel']) ? $_POST['tbModel'] : '';
	$tbFactoryID=!empty($_POST['tbFactoryID']) ? $_POST['tbFactoryID'] : '';
	$tbPlan=!empty($_POST['tbPlan']) ? $_POST['tbPlan'] : '';
	$tbInspectLevel=!empty($_POST['tbInspectLevel']) ? $_POST['tbInspectLevel'] : '';
	$tbCritical=!empty($_POST['tbCritical']) ? $_POST['tbCritical'] : '';
	$tbMajor=!empty($_POST['tbMajor']) ? $_POST['tbMajor'] : '';
	$tbMinor=!empty($_POST['tbMinor']) ? $_POST['tbMinor'] : '';
	$tbPacking=!empty($_POST['tbPacking']) ? $_POST['tbPacking'] : '';
	$tbStyle=!empty($_POST['tbStyle']) ? $_POST['tbStyle'] : '';
	$tbMeasure=!empty($_POST['tbMeasure']) ? $_POST['tbMeasure'] : '';
	    $sql = '';
		if($act=='save_edit'){
		$sql = "UPDATE ". $GLOBALS['prefix']->table('instructs'). " SET " . "tbProductName='$tbProductName',tbClientID='$tbClientID',tbVersion='$tbVersion',tbIssueDate='$tbIssueDate',tbDescript='$tbDescript',tbEffect='$tbEffect',tbModel='$tbModel',tbPlan='$tbPlan',tbInspectLevel='$tbInspectLevel',tbCritical='$tbCritical',tbMajor='$tbMajor',tbMinor='$tbMinor',tbPacking='$tbPacking',tbStyle='$tbStyle',tbFactoryID='$tbFactoryID',tbProvide='$tbFactoryID' WHERE tbWN='$id'";}
        if($act=='save_upgrade' || $act=='save_create'){
        $sql = "INSERT INTO ". $GLOBALS['prefix']->table('instructs') . " (tbProductName,tbClientID,tbVersion,tbIssueDate,tbDescript,tbEffect,tbModel,tbPlan,tbInspectLevel,tbCritical,tbMajor,tbMinor,tbPacking,tbStyle,tbFactoryID,tbProvide) VALUES ('$tbProductName','$tbClientID','$tbVersion','$tbIssueDate','$tbDescript','$tbEffect','$tbModel','$tbPlan','$tbInspectLevel','$tbCritical','$tbMajor','$tbMinor','$tbPacking','$tbStyle','$tbFactoryID','$tbFactoryID')";}
        if($GLOBALS['db']->query($sql))
		{
			if($act=='save_edit'){
			ecs_header("Location:instructs.php?act=get_wi&id=$id&active=3&client=$pre");}
			}
			if($act=='save_upgrade' || $act=='save_create'){
			ecs_header("Location:instructs.php?act=ready&active=3&client=$pre");
			}
		else
		{
				$GLOBALS['err'] ->add($GLOBALS['_LANG']['rep_update_fail']);
				$GLOBALS['err'] ->show();
		}
}
elseif($act=='saveWiM')
{
	$st=$str=$sql=$wiM='';
	$tbTestedID=!empty($_POST['tbTestedID']) ? $_POST['tbTestedID'] : '';
	$wiM=get_row($pre,'tbMeasure','tbWN',$id,'instructs');
	$st=$wiM['tbMeasure'].','.$tbTestedID;
	$sql = "UPDATE ". $GLOBALS['prefix']->table('instructs'). " SET " ."tbMeasure='$st' WHERE tbWN='$id'";
	if($GLOBALS['db']->query($sql))
	   {
	   	require(str_replace('sicms','',dirname(__FILE__)) . '/includes/show_wititem.php');
		echo json_encode($str);
		//echo json_encode("OK!");
	   }else{
		echo json_encode("Database query error!");
	   }
}
elseif($act=='saveWiT')
{
	$str=$sql='';
	$tbTest=!empty($_POST['tbTest']) ? $_POST['tbTest'] : '';
	$sql = "UPDATE ". $GLOBALS['prefix']->table('instructs'). " SET " ."tbTest='$tbTest' WHERE tbWN='$id'";
	if($GLOBALS['db']->query($sql))
	   {
	   	require(str_replace('sicms','',dirname(__FILE__)) . '/includes/show_wititem.php');
		echo json_encode($str);
		//echo json_encode("OK!");
	   }else{
		echo json_encode("Database query error!");
	   }
}		
?>