<?php
//ini_set('display_errors','1');
//error_reporting(E_ALL);
$page="main";
include("../function/func.php");
session_start();
checkSession();


$testItems=getTestItem($_REQUEST['id']);
$ordersID=$testItems[0]['ordersID'];
$info=getOrderDetail($ordersID);
//$testItems=getOrderTestList($)
if($_REQUEST['updateID']){
    //$strenthKGF=round($testItems[0]['kgf']/($testItems[0]['width']*$testItems[0]['length']),2)

    if($testItems[0]['serialNum']==$_REQUEST['serialNum']){
        //如果用戶修改了width height 會影響面積
        if($testItems[0]['length']!=$_REQUEST['length']||$testItems[0]['width']!=$_REQUEST['width']){
            $area_cal=round(($_REQUEST['width']*$_REQUEST['length']),2);
        }
        //如果用戶修改了kgf width height會影響strenthkgf/mpa
        if($testItems[0]['kN']!=$_REQUEST['kN']||$testItems[0]['kgf']!=$_REQUEST['kgf']||$testItems[0]['length']!=$_REQUEST['length']||$testItems[0]['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'];
    $ordersID=$_REQUEST['ordersID'];
    $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,$ordersID,$serialNum,$testLocat,$productionDate,$kN,$kgf,$length,$width,$area,$strengthKGF,$strengthMPa,$destructionLocat,$destructionMode);
    header("refresh:0;url=updateTestItem.php?id=".$_REQUEST['updateID']);
}

?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>桂田技術顧問訂單管理系統</title>
    <link href="./css/common.css?id=<?php echo rand(0,10000);?>" rel="stylesheet">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
    <script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
    
    <script>
        $( function() {
            $( "#productionDate" ).datepicker({
                showButtonPanel: true,
                dateFormat: 'yy-mm-dd'
            });
            
            
        } );
    </script>
  </head>
<body>
    <div class="bgBody">
        <?php include("./module/header.php");?>
        <div class="container">
            <?php include("./module/menu.php");?>
            <div class="mainContent">
            <form action="updateTestItem.php?updateID=<?php echo $testItems[0]['id'];?>&id=<?php echo $testItems[0]['id'];?>" method="post">
                <div class="head">
                    <div class="item title">測試項目資訊</div>
                    
                    <div class="item searchInput"><input type="submit" value="儲存"></div>
                    <a href="testItemList.php?id=<?php echo $testItems[0]['ordersID'];?>"><div class="item searchInput" style="padding-top:0.3em">回測試項目列表</div></a>

                    <div class="clear"></div>
                </div>
                <div class="testItem">
                    
                        <div class="item">項目編號：<?php echo $testItems[0]['serialNum'];?></div>
                        <div class="item">測試位置/樣品名稱：<input type="text" name="testLocat" value="<?php echo $testItems[0]['testLocat'];?>"></div>
                        <div class="item">製作日期：<input type="text" id="productionDate" name="productionDate" value="<?php echo $testItems[0]['productionDate'];?>"></div>
                        <?php if(substr($info[0]['equipUsed'],0,3)=="Pro"){?>
                            <div class="item">抗拉荷重kN：<input type="text" name="kN" id="kN" value="<?php echo $testItems[0]['kN'];?>"></div>
                            <div class="item">抗拉荷重kgf：<input type="text" name="kgf" id="kgf" value="<?php echo $testItems[0]['kgf'];?>"  style="background-color:#ddd;border:0;" readonly="readonly"></div>
                        <?php  }else{?>
                            <div class="item">抗拉荷重kN：<input type="text" name="kN" id="kN" value="<?php echo $testItems[0]['kN'];?>" style="background-color:#ddd;border:0;" readonly="readonly"></div>
                            <div class="item">抗拉荷重kgf：<input type="text" name="kgf" id="kgf" value="<?php echo $testItems[0]['kgf'];?>"></div>
                        <?php }?> 
                        <div class="item">長度：<input type="text" name="length" id="length" value="<?php  if($testItems[0]['length']!=0){echo $testItems[0]['length'];}?>"></div>
                        <div class="item">寬度：<input type="text" name="width" id="width" value="<?php if($testItems[0]['width']!=0){echo $testItems[0]['width'];}?>"></div>
                        <div class="item">面積：<input type="text" name="area" id="area" value="<?php if($testItems[0]['area']!=0){echo $testItems[0]['area'];}?>"></div>
                        <div class="item">強度kgf：<input type="text" name="strengthKGF" id="strengthKGF" value="<?php echo $testItems[0]['strengthKGF'];?>" readonly="readonly" style="background-color:#ddd;border:0"></div>
                        <div class="item">強度MPa：<input type="text" name="strengthMPa" id="strengthMPa" value="<?php echo $testItems[0]['strengthMPa'];?>" readonly="readonly" style="background-color:#ddd;border:0"></div>
                        <div class="item">破壞位置：
                        <select name="destructionLocat" id="destructionLocat">
                                <option value="A" <?php if($testItems[0]['destructionLocat']=="A"){echo "selected";};?>>A</option>
                                <option value="B" <?php if($testItems[0]['destructionLocat']=="B"){echo "selected";};?>>B</option>
                                <option value="C" <?php if($testItems[0]['destructionLocat']=="C"){echo "selected";};?>>C</option>
                                <option value="D" <?php if($testItems[0]['destructionLocat']=="D"){echo "selected";};?>>D</option>
                                <option value="E" <?php if($testItems[0]['destructionLocat']=="E"){echo "selected";};?>>E</option>
                            </select>
                        </div>
                        <div class="item">破壞模式：<input type="text" name="destructionMode" id="destructionMode" value="抗拉強度"  style="background-color:#ddd;border:0" readonly="readonly"></div>
                        
                        <input type="hidden" name="serialNum" value="<?php echo $testItems[0]['serialNum'];?>">
                        <input type="hidden" name="ordersID" value="<?php echo $testItems[0]['ordersID'];?>">
                        <div class="input"><input type="submit" value="儲存"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

<script src="cal.js"></script>
