<?php
// ini_set('display_errors','1');
// error_reporting(E_ALL);
$page="main";
include("../function/func.php");
session_start();
checkSession();
$testType = "normal";
$testItems=getOrderTestList($_REQUEST['id']);
if(count($testItems)==0){
    $testType = "pullTest";
    $ordersId=getOrderIDFromID($_REQUEST['id']);
    $testItems = getOrderPullTestList($ordersId);
}
//print_r($testItems);
//如果是一般測試
if($testType == "normal"){
    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=testItemList.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?id=".$_REQUEST['id']);
    }
    if($_REQUEST['deleteID']){
        deleteTestItem($_REQUEST['deleteID']);
        header("refresh:0;url=testItemList.php?id=".$_REQUEST['orderID']);
    }
//如果是拉拔測試
}else{
     //echo $testType;
    // echo $_REQUEST['id'];
    // var_dump($testItems);
    if($_REQUEST['deleteID']){
        //echo "delete";
        deletePullTestItem($_REQUEST['deleteID']);
        header("refresh:0;url=testItemList.php?id=".$_REQUEST['id']);
    }else{
        //echo "no";
    }
}
?>
<!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>
                    <?php if($testType == "normal"){?>
                        <div class="item button blue" style="padding:5px;margin-top:0px;" onclick="location.href = 'insertTestItem.php?id=<?php echo $_REQUEST['id'];?>';">新增測試項目</div>
                    <?php }else{?>
                        <div class="item button blue" style="padding:5px;margin-top:0px;" onclick="location.href = 'insertPullTestItem.php?id=<?php echo $_REQUEST['id'];?>';">新增拉拔測試項目</div>
                    <?php }?>
                    <?php if($testType != "normal"){?>
                    <div class="item button blue" style="padding:5px;margin-top:0px;" onclick="location.href = 'equipmentList.php';">設備列表</div>
                    <?php }?>
                    <a href="main.php"><div class="item" style="padding-top:0.3em">回前頁</div></a>
                    <div class="clear"></div>
                </div>
                <div class="orderList">
                    <div class="orderItem mainOrderItem">
                        <div class="item projectId">項目編號</div>
                        <?php if($testType == "normal"){?>
                            <div class="item placeId">測試位置/樣品名稱</div>
                        <?php }else{?>
                            <div class="item placeId">拉拔測試項目</div>
                        <?php }?>
                        
                    </div>
                    <?php for($i=0;$i<count($testItems);$i++){?>
                    <div class="orderItem <?php if($i%2==0){echo "gray";}?>" >
                        <div class="item projectId"><?php echo $testItems[$i]['serialNum'];?></div>
                        <?php if($testType == "normal"){?>
                            <div class="item placeId"><?php echo $testItems[$i]['testLocat'];?></div>
                        <?php }else{?>
                            <div class="item placeId"><?php echo $testItems[$i]['testTypeName'];?></div>
                        <?php }?>
                        <?php if($testType == "normal"){?>
                            <div class="item button blue" onclick="location.href = 'updateTestItem.php?id=<?php echo $testItems[$i]['id'];?>';">修改</div>
                        <?php }else{?>
                            <div class="item button blue" onclick="location.href = 'updatePullTestItem.php?id=<?php echo $testItems[$i]['id'];?>';">修改</div>
                        <?php }?>
                        <div class="item button deleteButton" onclick="deleteItem(<?php echo $testItems[$i]['id'];?>);">刪除</div>
                    </div>
                    <?php } ?>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<script>
function deleteItem(id){
    var yes = confirm('確定要刪除此項目？?');
    if (yes) {
        location.href='testItemList.php?deleteID='+id+'&id=<?php echo $_REQUEST['id'];?>'
    }
}
</script>