<?php
//ini_set('display_errors','1');
//error_reporting(E_ALL);
$page="main";
include("../function/func.php");
session_start();
checkSession();
$testItems=getOrderTestList($_REQUEST['orderID']);
//print_r($testItems);
if($_REQUEST['updateID']){
    //$strenthKGF=round($testItems[$i]['kgf']/($testItems[$i]['width']*$testItems[$i]['length']),2)
    
    for($i=0;$i<count($testItems);$i++){
        if($testItems[$i]['serialNum']==$_REQUEST['serialNum']){
            //如果用戶修改了width height 會影響面積
            if($testItems[$i]['length']!=$_REQUEST['length']||$testItems[$i]['width']!=$_REQUEST['width']){
                $area_cal=round(($_REQUEST['width']*$_REQUEST['length']),2);
            }
            //如果用戶修改了kgf width height會影響strenthkgf/mpa
            if($testItems[$i]['kN']!=$_REQUEST['kN']||$testItems[$i]['kgf']!=$_REQUEST['kgf']||$testItems[$i]['length']!=$_REQUEST['length']||$testItems[$i]['width']!=$_REQUEST['width']){
                if($_REQUEST['kgf']==""){
                    $_REQUEST['kgf']=round($_REQUEST['kN']*101.97);
                }
                $strengthKGF_cal=round($_REQUEST['kgf']/($_REQUEST['width']*$_REQUEST['length']),2);
                $mpa_cal=round(($strengthKGF_cal*0.0980665),2);
                $area_cal=round(($_REQUEST['width']*$_REQUEST['length']),2);
            }
        }
    }
    $id=$_REQUEST['updateID'];
    $orderID=$_REQUEST['orderID'];
    $serialNum=(int)$_REQUEST['serialNum'];
    $testLocat=$_REQUEST['testLocat'];
    $productionDate=$_REQUEST['productionDate'];
    $kN=$_REQUEST['kN'];
    $kgf=$_REQUEST['kgf'];
    $length=$_REQUEST['length'];
    $width=$_REQUEST['width'];
    $area=$_REQUEST['area'];
    if($area_cal!=""){
        $area=$area_cal;
    }
    $strengthKGF=$_REQUEST['strengthKGF'];
    $strengthMPa=$_REQUEST['strengthMPa'];
    if($strengthKGF_cal!=""){
        $strengthKGF=$strengthKGF_cal;
        $strengthMPa=$mpa_cal;
    }
    $destructionLocat=$_REQUEST['destructionLocat'];
    $destructionMode=$_REQUEST['destructionMode'];
    updateTest($id,$orderID,$serialNum,$testLocat,$productionDate,$kN,$kgf,$length,$width,$area,$strengthKGF,$strengthMPa,$destructionLocat,$destructionMode);
    header("refresh:0;url=uploadTestItem.php?orderID=".$_REQUEST['orderID']);
}
if($_REQUEST['insertOneTest']==1){
    $orderID=$_REQUEST['orderID'];
    $serialNum=(int)$_REQUEST['serialNum'];
    $testLocat=$_REQUEST['testLocat'];
    $productionDate=$_REQUEST['productionDate'];
    $kN=$_REQUEST['kN'];
    $kgf=$_REQUEST['kgf'];
    $length=$_REQUEST['length'];
    $width=$_REQUEST['width'];
    $area=$_REQUEST['area'];
    $strengthKGF=$_REQUEST['strengthKGF'];
    $strengthMPa=$_REQUEST['strengthMPa'];
    $destructionLocat=$_REQUEST['destructionLocat'];
    $destructionMode=$_REQUEST['destructionMode'];
    createTest($orderID,$serialNum,$testLocat,$productionDate,$kN,$kgf,$length,$width,$area,$strengthKGF,$strengthMPa,$destructionLocat,$destructionMode);
    header("refresh:0;url=uploadTestItem.php?orderID=".$_REQUEST['orderID']);
}
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>桂田技術顧問訂單管理系統</title>
    <link href="./css/common.css?id=<?php echo rand(0,10000);?>" rel="stylesheet">
  </head>
<body>
    <div class="bgBody">
        <?php include("./module/header.php");?>
        <div class="container">
            <?php include("./module/menu.php");?>
            <div class="mainContent">
                <div class="head">
                    <div class="item title">測試項目資訊</div>
                </div>
                <div class="testItemList">
                <div class="testItem gray">
                    <form action="uploadTestItem.php?insertOneTest=1&orderID=<?php echo $_REQUEST['orderID'];?>" method="post">
                        <div class="item"><span style="color:#1b61e4">新增一筆測試項目</span></div>
                        <div class="item">項目編號：<input type="text" name="serialNum" value=""></div>
                        <div class="item">測試位置/樣品名稱：<input type="text" name="testLocat" value=""></div>
                        <div class="item">製作日期：<input type="text" name="productionDate" value=""></div>
                        <div class="item">抗拉荷重kN：<input type="text" name="kN" value=""></div>
                        <div class="item">抗拉荷重kgf：<input type="text" name="kgf" value=""></div>
                        <div class="item">長度：<input type="text" name="length" value=""></div>
                        <div class="item">寬度：<input type="text" name="width" value=""></div>
                        <div class="item">面積：<input type="text" name="area" value=""></div>
                        <div class="item">強度kgf：<input type="text" name="strengthKGF" value=""></div>
                        <div class="item">強度MPa：<input type="text" name="strengthMPa" value=""></div>
                        <div class="item">破壞位置：<input type="text" name="destructionLocat" value=""></div>
                        <div class="item">破壞模式：<input type="text" name="destructionMode" value=""></div>
                        <div class="input"><input type="submit" value="新增測試"></div>
                    </form>
                </div>
                <?php for($i=0;$i<count($testItems);$i++){?>
                    <div class="testItem">
                    <form action="uploadTestItem.php?updateID=<?php echo $testItems[$i]['id'];?>&orderID=<?php echo $_REQUEST['orderID'];?>" method="post">
                        <div class="item">項目編號：<?php echo $testItems[$i]['serialNum'];?></div>
                        <div class="item">測試位置/樣品名稱：<input type="text" name="testLocat" value="<?php echo $testItems[$i]['testLocat'];?>"></div>
                        <div class="item">製作日期：<input type="text" name="productionDate" value="<?php echo $testItems[$i]['productionDate'];?>"></div>
                        <div class="item">抗拉荷重kN：<input type="text" name="kN" value="<?php echo $testItems[$i]['kN'];?>"></div>
                        <div class="item">抗拉荷重kgf：<?php echo $testItems[$i]['kgf'];?></div>
                        <div class="item">長度：<input type="text" name="length" value="<?php echo $testItems[$i]['length'];?>"></div>
                        <div class="item">寬度：<input type="text" name="width" value="<?php echo $testItems[$i]['width'];?>"></div>
                        <div class="item">面積：<?php echo $testItems[$i]['area'];?></div>
                        <div class="item">強度kgf/cm2：<?php echo $testItems[$i]['strengthKGF'];?></div>
                        <div class="item">強度MPa：<?php echo $testItems[$i]['strengthMPa'];?></div>
                        <div class="item">破壞位置：<input type="text" name="destructionLocat" value="<?php echo $testItems[$i]['destructionLocat'];?>"></div>
                        <div class="item">破壞模式：<input type="text" name="destructionMode" value="<?php echo $testItems[$i]['destructionMode'];?>"></div>
                        <input type="hidden" name="strengthKGF" value="<?php echo $testItems[$i]['strengthKGF'];?>">
                        <input type="hidden" name="strengthMPa" value="<?php echo $testItems[$i]['strengthMPa'];?>">
                        <input type="hidden" name="serialNum" value="<?php echo $testItems[$i]['serialNum'];?>">
                        <div class="input"><input type="submit" value="更新"></div>
                    </form>
                    </div>
                <?php }?>
                </div>
            </div>
        </div>
    </div>
</body>
</html>