<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- <meta http-equiv="refresh" content="60" />  每 content 秒網頁自動更新-->

    <!-- 新 Bootstrap4 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.1.0/css/bootstrap.min.css">
    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <!-- popper.min.js 用于弹窗、提示、下拉菜单 -->
    <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
    <!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
    <!--可用來建立使用者小圖示-->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <!-- Rita 乾燥貨櫃 - 入料、桶槽、出料制動器 function-->
    <script type="text/javascript" src="../static/js/dry_function.js"></script>

    <script language="JavaScript">
        // 指定 秒 刷新網頁一次
        var tid = '{{tid}}';
        console.log('tid:' + tid)

        var tank_num = '{{tid}}';

        $(function(){
            $("#ctrl_D_Temp_title").text('乾燥槽 D' + tid + ' 設定溫度')
            $("#dry_tank_page").text('乾燥槽 D' + tid + ' 操作介面   ')
            $('#dry_tank_page').attr("href", "/dry_container/" + tid)
            $("#coffee_title").text('D' + tid + ' 乾燥槽_設定溫度');
        });

        //WebUpdate_set = setInterval(function(){WebUpdate(dtn)} , 10 * 1000)
    </script>

    <style>
        .footer{
            position: absolute;
            width: 100%;
            background-color: #eee;
            text-align: center;
        }

        body {
            margin: 0;
        }

        .navbar-dark .navbar-nav .nav-link {
            color: white;
            cursor: pointer;
            text-decoration: none;
            width: 110px;
            height: 46px;
        }

        .nav-top {
            line-height: 40px;
            background-color: #C4C4C4;
        }

        .website_title {
            font-family: Roboto;
            font-style: normal;
            font-weight: normal;
            font-size: 30px;
            color: #000000;
        }

        .navbar-nav>li {
            float: none;
            display: inline-block;
            width: 100px;
            margin: 0 auto;
            text-align: center;
        }

        .navbar-nav>li a {
            font-size: 20px;
        }

        .main-page {
            margin-top: 200px;
        }

        .page-title {
            font-family: Roboto;
            font-style: normal;
            font-weight: bold;
            font-size: 36px;
        }


        .flex {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: center;
        }

        .set-link {
            display: inline-block;
            width: 350px;
            height: 100px;
            line-height: 100px;
            background: #008CBA;
            border: 1px solid #CFCFCF;
            box-sizing: border-box;
            color: #FFFFFF;
            border-radius: 10px;
            font-size: 36px;
        }

        .cmn-toggle {
            position: absolute;
            margin-left: 0px;
            visibility: hidden;
        }

        .cmn-toggle+label {
            display: block;
            position: relative;
            cursor: pointer;
            outline: none;
            user-select: none;
        }

        input.cmn-toggle-round-flat+label {
            padding: 2px;
            width: 60px;
            height: 30px;
            background-color: #C0C0C0;
            border-radius: 60px;
            transition: background 0.4s;
        }

        input.cmn-toggle-round-flat+label:before,
        input.cmn-toggle-round-flat+label:after {
            display: block;
            position: absolute;
            content: "";
        }

        input.cmn-toggle-round-flat+label:before {
            top: 2px;
            left: 2px;
            bottom: 2px;
            right: 2px;
            background-color: #fff;
            border-radius: 60px;
            transition: background 0.4s;
        }

        input.cmn-toggle-round-flat+label:after {
            top: 4px;
            left: 4px;
            bottom: 4px;
            width: 24px;
            background-color: #dddddd;
            border-radius: 52px;
            transition: margin 0.4s, background 0.4s;
        }

        input.cmn-toggle-round-flat:checked+label {
            background-color: #C0C0C0;
        }

        input.cmn-toggle-round-flat:checked+label:after {
            margin-left: 27px;
            background-color: #008CBA;
        }

        @media(max-width:373px) {
            .card {
                margin-right: 0px;
            }

            .set-link {
                width: 250px;
            }
        }

        @media(max-width:577px) {}

        @media(min-width:576px) {}

        @media(min-width:768px) {
            .navbar-nav>li {
                margin-left: 0px;
            }

            .navbar-nav .li-block {
                display: none;
            }
        }

        @media(min-width:991px) {
            .navbar-nav>li {
                margin-left: 20px;
            }

            .navbar-nav .li-block {
                display: none;
            }
        }

        @media(min-width:1200px) {
            .navbar-nav>li {
                margin-left: 50px;
            }

            .navbar-nav .li-block {
                display: inline-block;
                width: 100px;
            }
        }

        @media(min-width:1400px) {
            .navbar-nav .li-block {
                display: inline-block;
                width: 200px;
            }
        }


        @media(min-width:1689px) {
            .navbar-nav>li {
                margin-left: 50px;
            }

            .navbar-nav .li-block {
                display: inline-block;
                width: 500px;
            }
        }
    </style>
    
</head>

<body>
    <div id="wrapper">
        <div id="coffee_header">
            <!-- 匯入共同使用的 header.html 內容 -->
            {% include 'header.html' %}
        </div>
        <!--
        <div style="text-align: left;">
            <a href="/dry" style="float: left;">&nbsp;&nbsp;&nbsp;返回乾燥貨櫃首頁</a>
        </div>
        <div style="text-align: right;">
            <a id="dry_tank_page" href="/index_new" style="float: right;">___________________</a>
        </div>
        <div id="ctrl_D_Temp_title" style="text-align: center; margin-top: 30px; font-size: 24px; margin-bottom: 15px;">
            乾燥槽 D_ 設定溫度
        </div>
        -->

        <table border="1" style="margin: auto; font-size:18px; border:2px #cccccc solid;" width=700px cellpadding="8">
            <tr>
                <!--
                <td rowspan="3">
                    手動控制
                </td>
                -->
                <td>
                    目前溫度
                </td>
                <td>
                    {% if tank_temp1 >= 0 %}
                    <span style="color:#008CBA; font-size:20px;"><strong>{{tank_temp1}}</strong></span>&nbsp;℃
                    {% elif tank_temp1 == 0 %}
                    <span style="color:#acacac; font-size:20px;"><strong>未設定溫度</strong></span>
                    {% else %}
                    <span style="color: #fc7373;; font-size:18px;">ERROR</span>
                    {% endif %}
                </td>
            </tr>
            <tr>
                <td>
                    溫控開關 狀態
                </td>
                <td>
                    {% if tank_temp1_enable == 1 %}
                    <span style="color:#008CBA; font-size:18px;"><strong>ON</strong></span>
                    {% elif tank_temp1_enable == 0 %}
                    <span style="color:#acacac; font-size:18px;"><strong>OFF</strong></span>
                    {% else %}
                    <span style="color: #fc7373;; font-size:18px;">ERROR</span>
                    {% endif %}
                </td>
            </tr>
            <tr>
                <td>開啟 / 關閉 溫控開關</td>
                <td>
                    <input type="button" value="ON" onclick="tankTemp1Enable_ON()"> / 
                    <input type="button" value="OFF" onclick="tankTemp1Enable_OFF()">
                </td>
            </tr>
            <tr>
                <td>設定溫度 30℃</td>
                <td><input type="button" value="ON" onclick="ChangeTemp1_ON()"></td>
            </tr>
            <tr>
                <td>設定溫度 25℃</td>
                <td><input type="button" value="OFF" onclick="ChangeTemp1_OFF()"></td>
            </tr>
            <tr>
                <td colspan="2">
                    <strong>
                        溫度權重設定:
                        桶內溫度:&nbsp;<input id="Temp_Weights_SHT11" type="text" value="0.5" style="width: 40px;">&nbsp;+&nbsp;
                        咖啡生豆溫度:&nbsp;<input id="Temp_Weights_Soil" type="text" value="0.5" style="width: 40px;" >
                    </strong>
                </td>
            </tr>
            <tr>
                <td>
                    設定溫度
                </td>   
                
                <td>
                    <span id="tank_temp1_status" style="color:red;"></span>
                    <input id="tank_temp1_data" type="text" placeholder="{{tank_temp1}}" style="width: 40px;">&nbsp;℃, 持續&nbsp;
                    <input id="tank_temp_duration" type="text" placeholder="" style="width: 40px;">&nbsp;分鐘&nbsp;
                    <input type="button" value="送出" name="tank_temp1_status" onclick="setTemp1_func()"><br>
                    <span id="testing_Temp_Weight" style="color: #f00;"></span>

                    <script>
                        var setTemp_process = 0;
                        
                        function setTemp1_func(){
                            var temp_data = $("input[id=tank_temp1_data]").val();
                            console.log('設定溫度: ', temp_data)
                            var temp_duration = $("input[id=tank_temp_duration]").val();
                            console.log('持溫時間: ', temp_duration)
                            var Temp_Weights_SHT11 = parseFloat($('#Temp_Weights_SHT11').val());
                            var Temp_Weights_Soil = parseFloat($('#Temp_Weights_Soil').val());
                            setTemp1(temp_data, temp_duration)
                            // var temp_interval = setInterval(checkTemp, 5000);

                            function checkTemp(){
                                if (setTemp_process == 1) {
                                    return;
                                }

                                setTemp_process = 1;
                                $.ajax({
                                    async:false,
                                    type:"GET",
                                    url:"/dry_Temp/"+tank_num+"/"+Temp_Weights_SHT11+"/"+Temp_Weights_Soil+"/"+temp_data,
                                    dataType:"JSON",
                                    success:function(response){
                                        // 0915 var present_SHT11_Temp = parseFloat(response.SHT11_Temp)
                                        // 0915 var present_SOIL_Temp = parseFloat(response.Soil_Temp)
                                        // Math.round 會將數值四捨五入至整數位
                                        // 所以 Math.round(x * 100)/100 就可以取小數點第二位
                                        // 0915 present_temp = Math.round((present_SHT11_Temp*Temp_Weights_SHT11 + present_SOIL_Temp*Temp_Weights_Soil)/(Temp_Weights_SHT11+Temp_Weights_Soil)*100) / 100
                                        
                                        console.log('目前溫度(加權):', response.present_temp)
                                        if (response.TempUpTime == '指定溫度過高, 持續升溫中') {
                                            $('#testing_Temp_Weight').text('目前溫度(加權):' + response.present_temp + '℃, 指定溫度過高, 持續升溫中')
                                        } else {
                                            $('#testing_Temp_Weight').text('目前溫度(加權):' + response.present_temp + response.TempUpTime)
                                        }
                                        $('#SHT11_Temp_status').text(response.SHT11_Temp);
                                        $('#SHT11_Humidity_status').text(response.SHT11_Humidity);
                                        $('#soil_Temp_status').text(response.Soil_Temp);
                                        $('#soil_Humidity_status').text(response.Soil_Humidity);
                                        

                                        if (response.present_temp >= temp_data) {
                                            clearInterval(temp_interval);
                                            $('#testing_Temp_Weight').text('已達指定溫度, 關閉加熱裝置')
                                            console.log('已達指定溫度, 關閉溫控開關')
                                            var time = new Date();
                                            while ((new Date() - time) < 3 * 1000) { };

                                            tankTemp1Enable_OFF()
                                            tankBlower_OFF()
                                            tankHeater1_OFF()
                                            tankHeater2_OFF()

                                            $('#testing_Temp_Weight').text('')
                                        }
                                        setTemp_process = 0;
                                    },
                                    error:function(thrownError){
                                        console.log('dry_Temp_Error: ', thrownError)
                                        setTemp_process = 0;
                                    }
                                });
                            }
                        };
                    </script>
                </td>
            </tr>
            
        </table>

        <br>

        <table border="1" style="margin: auto; font-size:18px; border:2px #cccccc solid;" width=700px cellpadding="8">
            <tr>
                <td colspan="3">
                    桶內溫度:<span id="SHT11_Temp_status">{{tank_SHT11.SHT11_Temp}}</span>&nbsp;℃
                </td>
                <td colspan="3">
                    桶內濕度:<span id="SHT11_Humidity_status">{{tank_SHT11.SHT11_Humidity}}</span>&nbsp;%
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    咖啡生豆溫度:<span id="soil_Temp_status">{{tank_Soil.soil_Temp}}</span>&nbsp;℃
                </td>
                <td colspan="3">
                    咖啡生豆濕度:<span id="soil_Humidity_status">{{tank_Soil.soil_Humidity}}</span>&nbsp;%
                </td>
            </tr>
            <tr>
                
            </tr>
        </table>

        <br>

        <div style="text-align: center;">
            狀態更新時間(秒):
            <input name="webupdate_time" type="text" value="5" style="width: 40px;">
            <input type="button" value="設定更新時間" onclick="changeUpdate()">
        </div>

        <script language="JavaScript">
            function changeUpdate() {
                // clearInterval(WebUpdate_set);

                var webupdate_time = $("input[name=webupdate_time]").val()
                console.log('webupdate_time' + webupdate_time)
                WebUpdate_set = setInterval(function(){WebUpdate(dtn)} , webupdate_time * 1000);
            }

            // jQuery 更新感測器制動器狀態
            function WebUpdate(dtn) {
                $.get('/loading/D' + dtn, '', function (res) {
                    if (res.tank_vacuum == 0) {
                        $("#cmn-toggle-05").prop('checked', false);
                    } else if (res.tank_vacuum == 1) {
                        $("#cmn-toggle-05").prop('checked', true);
                    }
                    $("#UltraSonic_t_status").text(res.UltraSonic);
                }, 'json');
            }
        </script>

        <br>

        <footer class="footer">
            <div style="text-align: center; margin-top: 10px; font-size: 13px; margin-bottom: 10px;">
                Copyright © 2021 Gold-in Tech. All Rights Reserved. 金子進科技股份有限公司 版權所有
                <a href="mailto:service.gitc@gmail.com" target="_blank">service.gitc@gmail.com</a>
            </div>
        </footer>

        <div id="coffee_footer">
            <!-- 匯入共同使用的 footer.html 內容 -->
            {% include 'footer.html' %}
        </div>
    </div>

</body>

</html>