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


$testItems=getPullTestItem($_REQUEST['id']);
$ordersID=$testItems[0]['ordersID'];
$info=getOrderDetail($ordersID);
$equipments = getEquipments();
$ordersPageId=getIDFromOrderID($ordersID);
//$testItems=getOrderTestList($)
if($_REQUEST['updateID']){
    //取得設備參考值
    $equipmentId = getEquipmentIDFromName($_REQUEST['equipment']);
    $equipmentInfo = getEquipmentInfo($equipmentId);
    //
    $id=$_REQUEST['id'];
    $serialNum=(int)$_REQUEST['serialNum'];
    $testTypeName=$_REQUEST['testTypeName'];
    $number=$_REQUEST['number'];
    $equipment=$_REQUEST['equipment'];
    $type=$_REQUEST['type'];
    $info=$_REQUEST['info'];
    if($_REQUEST['forcePercentageCustom']==""){
        $forcePercentage=$_REQUEST['forcePercentage'];
    }else{
        $formOfForce=$_REQUEST['forcePercentageCustom'];
    }
    if($_REQUEST['formOfForceCustom']==""){
        $formOfForce=$_REQUEST['formOfForce'];
    }else{
        $formOfForce=$_REQUEST['formOfForceCustom'];
    }
    if($_REQUEST['directionCustom']==''){
        $direction = $_REQUEST['direction'];
    }else{
        $direction = $_REQUEST['directionCustom'];
    }
    
    $pressureValue=$_REQUEST['pressureValue'];
    $loadKgf=$_REQUEST['loadKgf'];
    if($pressureValue != 0 && $loadKgf==0 && count($equipmentInfo)>1){
        $loadKgf = linearInterpolation($pressureValue,$equipmentInfo);
    }
    $designLoadKgf=$_REQUEST['designLoadKgf'];
    //吃完飯改下面這行
   
    
    $destoryLocation=$_REQUEST['destoryLocation'];
    updatePullTest($id,$testTypeName, $ordersID, $number, $serialNum, $equipment, $type, $info, $forcePercentage, $formOfForce, $direction, $pressureValue, $designLoadKgf, $loadKgf, $destoryLocation);
    header("refresh:1;url=updatePullTestItem.php?id=".$id);
}


// $equipmentInfo = getEquipmentInfo(6);
// $newKgf = linearInterpolation(1675,$equipmentInfo);
?>
<!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">
                <div class="head">
                    
                    <form action="updatePullTestItem.php?updateID=1&id=<?php echo $_REQUEST['id'];?>" method="post">
                    <div class="item title">測試項目資訊</div>
                    <div class="item searchInput"><input type="submit" value="儲存"></div>
                    <a href="testItemList.php?id=<?php echo $ordersPageId;?>"><div class="item searchInput" style="padding-top:0.3em">回測試項目列表</div></a>
                    <div class="clear"></div>
                </div>
                <div class="testItemList">
                <div class="testItem gray">
                        <div class="item">項目編號：<?php echo $testItems[0]['serialNum'];?></div>
                        <?php if($testItems[0]['testTypeName']==""){ ?>
                        <div class="item">測試項目：<select name="testTypeName" id="testTypeName">
                                <option value="現場植筋拉拔試驗報告" <?php echo ($testItems[0]['testTypeName'] == '現場植筋拉拔試驗報告') ? 'selected' : ''; ?>>現場植筋拉拔試驗報告</option>
                                <option value="現場預埋鐵件拉拔試驗報告" <?php echo ($testItems[0]['testTypeName'] == '現場預埋鐵件拉拔試驗報告') ? 'selected' : ''; ?>>現場預埋鐵件拉拔試驗報告</option>
                                <option value="現場化學螺栓拉拔試驗報告" <?php echo ($testItems[0]['testTypeName'] == '現場化學螺栓拉拔試驗報告') ? 'selected' : ''; ?>>現場化學螺栓拉拔試驗報告</option>
                                <option value="現場內迫螺栓拉拔試驗報告" <?php echo ($testItems[0]['testTypeName'] == '現場內迫螺栓拉拔試驗報告') ? 'selected' : ''; ?>>現場內迫螺栓拉拔試驗報告</option>
                                <option value="現場拉脹螺栓拉拔試驗報告" <?php echo ($testItems[0]['testTypeName'] == '現場拉脹螺栓拉拔試驗報告') ? 'selected' : ''; ?>>現場拉脹螺栓拉拔試驗報告</option>
                            </select>
                        </div>
                        <?php }else{ ?>
                        <div class="item">測試項目：<input type="text" name="testTypeName" value="<?php echo $testItems[0]['testTypeName'];?>" id="testTypeName"></div>
                        <?php } ?>
                        <div class="item">使用設備：<select name="equipment" id="equipment">
                            <?php foreach($equipments as $equipment) {?>
                                <option value="<?php echo $equipment['name'];?>" <?php echo ($testItems[0]['equipment'] == $equipment['name']) ? 'selected' : ''; ?>><?php echo $equipment['name']; ?></option>
                                
                            <?php } ?>
                            </select>
                        </div>
                        <div class="item">TYPE：<input type="text" name="type" value="<?php echo $testItems[0]['type'];?>" id="type"></div>
                        <div class="item">說明：<input type="text" name="info" id="info" value="<?php echo $testItems[0]['info'];?>"></div>
                        <?php if($testItems[0]['direction']==""){ ?>
                            <div class="itemFlex">
                                <div class="item">方向：
                                    <select name="direction" id="direction">
                                        <option value="東" <?php echo ($testItems[0]['direction'] == '東') ? 'selected' : ''; ?>>東</option>
                                        <option value="南" <?php echo ($testItems[0]['direction'] == '南') ? 'selected' : ''; ?>>南</option>
                                        <option value="西" <?php echo ($testItems[0]['direction'] == '西') ? 'selected' : ''; ?>>西</option>
                                        <option value="北" <?php echo ($testItems[0]['direction'] == '北') ? 'selected' : ''; ?>>北</option>
                                        <option value="東南" <?php echo ($testItems[0]['direction'] == '東南') ? 'selected' : ''; ?>>東南</option>
                                        <option value="西南" <?php echo ($testItems[0]['direction'] == '西南') ? 'selected' : ''; ?>>西南</option>
                                        <option value="西北" <?php echo ($testItems[0]['direction'] == '西北') ? 'selected' : ''; ?>>西北</option>
                                        <option value="東北" <?php echo ($testItems[0]['direction'] == '東北') ? 'selected' : ''; ?>>東北</option>
                                    </select>
                                </div>
                                <div class="item"><input type="text" name="directionCustom" value="" id="directionCustom"></div>
                            </div>
                        <?php }else{ ?>
                        <div class="item">方向：<input type="text" name="direction" value="<?php echo $testItems[0]['direction'];?>" id="direction"></div>
                        <?php } ?>
                        <?php if($testItems[0]['formOfForce']==""){ ?>
                            <div class="itemFlex">
                                <div class="item">施力形式：
                                    <select name="formOfForce" id="formOfForce">
                                        <option value="拉力" <?php echo ($testItems[0]['formOfForce'] == '拉力') ? 'selected' : ''; ?>>拉力</option>
                                        <option value="剪力" <?php echo ($testItems[0]['formOfForce'] == '剪力') ? 'selected' : ''; ?>>剪力</option>
                                        <option value="水平" <?php echo ($testItems[0]['formOfForce'] == '水平') ? 'selected' : ''; ?>>水平</option>
                                        <option value="垂直" <?php echo ($testItems[0]['formOfForce'] == '垂直') ? 'selected' : ''; ?>>垂直</option>
                                    </select>
                                </div>
                                <div class="item"><input type="text" name="formOfForceCustom" value="" id="formOfForceCustom"></div>
                            </div>
                        
                        <?php }else{ ?>
                        <div class="item">施力形式：<input type="text" name="formOfForce" value="<?php echo $testItems[0]['formOfForce'];?>" id="formOfForce"></div>
                        <?php } ?>
                        <?php if($testItems[0]['forcePercentage']==""){ ?>
                            <div class="itemFlex">
                                <div class="item">施力％：
                                    <select name="forcePercentage" id="forcePercentage">
                                        <option value="50%" <?php echo ($testItems[0]['forcePercentage'] == '50%') ? 'selected' : ''; ?>>50%</option>
                                        <option value="100%" <?php echo ($testItems[0]['forcePercentage'] == '100%') ? 'selected' : ''; ?>>100%</option>
                                        <option value="150%" <?php echo ($testItems[0]['forcePercentage'] == '150%') ? 'selected' : ''; ?>>150%</option>
                                        <option value="200%" <?php echo ($testItems[0]['forcePercentage'] == '200%') ? 'selected' : ''; ?>>200%</option>
                                    </select>
                                </div>
                                <div class="item"><input type="text" name="forcePercentageCustom" value="" id="forcePercentageCustom"></div>
                            </div>
                        
                        <?php }else{ ?>
                        <div class="item">施力％：<input type="text" name="forcePercentage" value="<?php echo $testItems[0]['forcePercentage'];?>" id="forcePercentage"></div>
                        <?php } ?>
                        <div class="item">壓力表值(psi)：<input type="text" name="pressureValue" value="<?php echo $testItems[0]['pressureValue']; ?>" id="pressureValue"></div>
                        <div class="item">測試荷重(kgf)：<input type="text" name="loadKgf" id="loadKgf" value="<?php echo $testItems[0]['loadKgf']; ?>"><span id='alertLine'></span></div>
                        <div class="item">設計規範值(kgf)：<input type="text" name="designLoadKgf" value="<?php echo $testItems[0]['designLoadKgf']; ?>" id="designLoadKgf">
                        <?php 
                        if($testItems[0]['designLoadKgf'] != "" && $testItems[0]['designLoadKgf'] != '0'){
                            $equipmentId = getEquipmentIDFromName($testItems[0]['equipment']);
                            $equipmentInfo = getEquipmentInfo($equipmentId);    
                            $designLoadPsi = linearInterpolationKgf($testItems[0]['designLoadKgf'],$equipmentInfo);
                            echo "PSI:".$designLoadPsi;
                        }
                        ?>
                        </div>

                        <?php if($testItems[0]['destoryLocation']==""){ ?>
                        <div class="item">破壞位置：
                            <select name="destoryLocation" id="destoryLocation">
                                <option value="1.混凝土基材破壞" <?php echo ($testItems[0]['destoryLocation'] == '1.混凝土基材破壞') ? 'selected' : ''; ?>>1.混凝土基材破壞</option>
                                <option value="2.樣品斷裂" <?php echo ($testItems[0]['destoryLocation'] == '2.樣品斷裂') ? 'selected' : ''; ?>>2.樣品斷裂</option>
                                <option value="3.握裹喪失鬆脫" <?php echo ($testItems[0]['destoryLocation'] == '3.握裹喪失鬆脫') ? 'selected' : ''; ?>>3.握裹喪失鬆脫</option>
                                <option value="4.達規範值未破壞" <?php echo ($testItems[0]['destoryLocation'] == '4.達規範值未破壞') ? 'selected' : ''; ?>>4.達規範值未破壞</option>
                            </select>
                        </div>
                        <?php }else{ ?>
                        <div class="item">破壞位置：<input type="text" name="destoryLocation" value="<?php echo $testItems[0]['destoryLocation'];?>" id="destoryLocation"></div>
                        <script>
                        // 获取输入字段
                        var destoryLocationInput = document.getElementById('destoryLocation');

                        // 添加事件监听器
                        destoryLocationInput.addEventListener('change', function() {
                            // 获取输入字段的值
                            var inputValue = destoryLocationInput.value;

                            // 检查值是否为空白
                            if (inputValue.trim() === '') {
                            // 发出警告
                            alert('破壞位置不能為空白！');
                            }
                        });
                        </script>
                        <?php } ?>
                        <input type="hidden" name="serialNum" value="<?php echo $testItems[0]['serialNum'];?>">
                        <div class="input"><input type="submit" value="儲存"></div>
                    </form>
                </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<script src="cal.js"></script>
<script>
  // 获取输入字段
  var loadKgfInput = document.getElementById('loadKgf');
  var designLoadKgfInput = document.getElementById('designLoadKgf');
  var alertLine = document.getElementById('alertLine');
  // 添加事件监听器
  loadKgfInput.addEventListener('input', function() {
    // 获取输入字段的值
    var loadKgf = parseFloat(loadKgfInput.value);
    var designLoadKgf = parseFloat(designLoadKgfInput.value);

    // 检查测试荷载是否高于设计规范的 150% 或低于设计规范值
    if (loadKgf > designLoadKgf * 1.5 || loadKgf < designLoadKgf) {
      loadKgfInput.style.color = 'red'; // 设置红色字体颜色
      alertLine.textContent = '警告：測試荷重數值異常'; // 设置警告文本
      alertLine.style.color = 'red'; // 设置红色字体颜色
    } else {
      loadKgfInput.style.color = ''; // 恢复默认字体颜色
      alertLine.textContent = ''; // 清空警告文本
      alertLine.style.color = ''; // 恢复默认字体颜色
    }
  });

  designLoadKgfInput.addEventListener('input', function() {
    // 触发测试荷载输入字段的事件监听器，以实时更新警告状态
    loadKgfInput.dispatchEvent(new Event('input'));
  });
</script>