<!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">

    <!-- 新 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">
    <script language="JavaScript">
        var rtid = '{{tid}}';
        $(document).ready(function(){
            $("#coffee_title_pc").text('R' + rtid + ' 中水桶操作介面');
            $("#coffee_title_phone").text('R' + rtid + ' 中水桶操作介面');
        });

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

    <script>
        // 清水入水電磁閥
        function tankSolenoidWaterIn(params) {
            if (params == '1') {
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_solenoid_water_in_status", "value": "on" };
            } else if (params == '0') {
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_solenoid_water_in_status", "value": "off" };
            } else {
                var status = "off";
                var check = $("input[name=tank_solenoid_water_in_status]:checked");
                console.log('check：', check);
                //大於0代表有被選中
                if (check.length > 0) {
                    status = "on";
                    $("#cmn-toggle-41").prop('checked', false);
                    if (!confirm("你確定要開啟清水入水電磁閥嗎?")) {
                        return false;
                    };

                } else {
                    $("#cmn-toggle-41").prop('checked', true);
                    if (!confirm("你確定要關閉清水入水電磁閥嗎?")) {
                        return false;
                    };
                };
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_solenoid_water_in_status", "value": status };
            }
            $.post('/mqtt/1', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-41").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-41").prop('checked', false);
                } else {
                    console.log(res)
                };
            }, 'text')
        }

        // 廢水出水電磁閥
        function tankSolenoidWaterOut(params) {
            if (params == '1') {
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_solenoid_water_out_status", "value": "on" };
            } else if (params == '0') {
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_solenoid_water_out_status", "value": "off" };
            } else {
                var status = "off";
                var check = $("input[name=tank_solenoid_water_out_status]:checked");
                console.log('check：', check);
                //大於0代表有被選中
                if (check.length > 0) {
                    status = "on";
                    $("#cmn-toggle-44").prop('checked', false);
                    if (!confirm("你確定要開啟廢水出水電磁閥嗎?")) {
                        return false;
                    };

                } else {
                    $("#cmn-toggle-44").prop('checked', true);
                    if (!confirm("你確定要關閉廢水出水電磁閥嗎?")) {
                        return false;
                    };
                };
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_solenoid_water_out_status", "value": status };
            }
            $.post('/mqtt/1', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-44").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-44").prop('checked', false);
                } else {
                    console.log(res)
                };
            }, 'text')
        }

        // 中水出水電磁閥
        function tankSolenoidReclaimedOut(params) {
            if (params == '1') {
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_solenoid_reclaimed_out_status", "value": "on" };
            } else if (params == '0') {
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_solenoid_reclaimed_out_status", "value": "off" };
            } else {
                var status = "off";
                var check = $("input[name=tank_solenoid_reclaimed_out_status]:checked");
                console.log('check：', check);
                //大於0代表有被選中
                if (check.length > 0) {
                    status = "on";
                    $("#cmn-toggle-47").prop('checked', false);
                    if (!confirm("你確定要開啟中水出水電磁閥嗎?")) {
                        return false;
                    };

                } else {
                    $("#cmn-toggle-47").prop('checked', true);
                    if (!confirm("你確定要關閉中水出水電磁閥嗎?")) {
                        return false;
                    };
                };
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_solenoid_reclaimed_out_status", "value": status };
            }
            $.post('/mqtt/1', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-47").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-47").prop('checked', false);
                } else {
                    console.log(res)
                };
            }, 'text')
        }

        // 幫浦(清洗槽入水)
        function tankPumpCleanwaterIn(params) {
            if (params == '1') {
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_pump_cleanwater_in_status", "value": "on" };
            } else if (params == '0') {
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_pump_cleanwater_in_status", "value": "off" };
            } else {
                var status = "off";
                var check = $("input[name=tank_pump_cleanwater_in_status]:checked");
                console.log('check：', check);
                //大於0代表有被選中
                if (check.length > 0) {
                    status = "on";
                    $("#cmn-toggle-50").prop('checked', false);
                    if (!confirm("你確定要開啟幫浦(清洗槽入水)嗎?")) {
                        return false;
                    };

                } else {
                    $("#cmn-toggle-50").prop('checked', true);
                    if (!confirm("你確定要關閉幫浦(清洗槽入水)嗎?")) {
                        return false;
                    };
                };
                var data = { "tank_num": "ReclaimedWaterTank", "command": "tank_pump_cleanwater_in_status", "value": status };
            }
            $.post('/mqtt/1', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-50").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-50").prop('checked', false);
                } else {
                    console.log(res)
                };
            }, 'text')
        }

        

    </script>

    <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(rtid)} , webupdate_time * 1000);
        }
    </script>
</head>

<body>
    <div id="wrapper">
        <div id="coffee_header">
            <!-- 匯入共同使用的 header.html 內容 -->
            {% include 'header.html' %}
        </div>
        
        <h4>致動器</h4>
        <table border="1" class="table-all" cellpadding="3" style="text-align: center;">
            <tr>
                <td style="color:red;">Valve</td>
                <td>清水入水<br>電磁閥</td>
                <td>中水出水<br>電磁閥</td>
                <td>廢水出水<br>電磁閥</td>
                <td>入水清洗槽<br>幫浦</td>
            </tr>
            <tr>
                <td>
                    <span style="color:#C0C0C0;">off</span>
                    <span style="color:#008CBA;">on</span>
                </td>
                <td>
                    <div class="switch_div">
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round" id="cmn-toggle-40">
                        </div>
                        <div class="switch text-center">
                            {% if tank_solenoid_water_in == 1 %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-41" checked
                                name="tank_solenoid_water_in_status" value="ON" onclick="tankSolenoidWaterIn()">
                            {% else %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-41"
                                name="tank_solenoid_water_in_status" value="OFF" onclick="tankSolenoidWaterIn()">
                            {% endif %}
                            <label class="label_ActuatorStatus" for="cmn-toggle-41"></label>
                        </div>
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-yes-no" id="cmn-toggle-42">
                        </div>
                    </div>
                </td>
                <td>
                    <div class="switch_div">
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round" id="cmn-toggle-46">
                        </div>
                        <div class="switch text-center">
                            {% if tank_solenoid_reclaimed_out == 1 %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-47" checked
                                name="tank_solenoid_reclaimed_out_status" value="ON" onclick="tankSolenoidReclaimedOut()">
                            {% else %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-47"
                                name="tank_solenoid_reclaimed_out_status" value="OFF" onclick="tankSolenoidReclaimedOut()">
                            {% endif %}
                            <label class="label_ActuatorStatus" for="cmn-toggle-47"></label>
                        </div>
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-yes-no" id="cmn-toggle-48">
                        </div>
                    </div>
                </td>
                <td>
                    <div class="switch_div">
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round" id="cmn-toggle-43">
                        </div>
                        <div class="switch text-center">
                            {% if tank_solenoid_water_out == 1 %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-44" checked
                                name="tank_solenoid_water_out_status" value="ON" onclick="tankSolenoidWaterOut()">
                            {% else %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-44"
                                name="tank_solenoid_water_out_status" value="OFF" onclick="tankSolenoidWaterOut()">
                            {% endif %}
                            <label class="label_ActuatorStatus" for="cmn-toggle-44"></label>
                        </div>
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-yes-no" id="cmn-toggle-45">
                        </div>
                    </div>
                </td>
                <td>
                    <div class="switch_div">
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round" id="cmn-toggle-49">
                        </div>
                        <div class="switch text-center">
                            {% if tank_pump_reclaimed_out == 1 %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-50" checked
                                name="tank_pump_reclaimed_out_status" value="ON" onclick="tankPumpCleanwaterIn()">
                            {% else %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-50"
                                name="tank_pump_reclaimed_out_status" value="OFF" onclick="tankPumpCleanwaterIn()">
                            {% endif %}
                            <label class="label_ActuatorStatus" for="cmn-toggle-50"></label>
                        </div>
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-yes-no" id="cmn-toggle-51">
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td><span style="color:#008CBA;"><b>ON</b></span></td>
                <td><input type="button" class="input-button-on" value="ON" onclick="tankSolenoidWaterIn('1')"></td>
                <td><input type="button" class="input-button-on" value="ON" onclick="tankSolenoidReclaimedOut('1')"></td>
                <td><input type="button" class="input-button-on" value="ON" onclick="tankSolenoidWaterOut('1')"></td>
                <td><input type="button" class="input-button-on" value="ON" onclick="tankPumpCleanwaterIn('1')"></td>
            </tr>
            <tr>
                <td><span style="color:#C0C0C0;"><b>OFF</b></span></td>
                <td><input type="button" class="input-button-off" value="OFF" onclick="tankSolenoidWaterIn('0')"></td>
                <td><input type="button" class="input-button-off" value="OFF" onclick="tankSolenoidReclaimedOut('0')"></td>
                <td><input type="button" class="input-button-off" value="OFF" onclick="tankSolenoidWaterOut('0')"></td>
                <td><input type="button" class="input-button-off" value="OFF" onclick="tankPumpCleanwaterIn('0')"></td>
            </tr>
        </table>

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

<script>
    // jQuery 更新感測器致動器狀態
    function WebUpdate(rtid) {
        $.get('/loading/R' + rtid, '', function (res) {
            if (res.tank_solenoid_water_in == 0) {
                $("#cmn-toggle-41").prop('checked', false);
            } else if (res.tank_solenoid_water_in == 1) {
                $("#cmn-toggle-41").prop('checked', true);
            }

            if (res.tank_solenoid_water_out == 0) {
                $("#cmn-toggle-44").prop('checked', false);
            } else if (res.tank_solenoid_water_out == 1) {
                $("#cmn-toggle-44").prop('checked', true);
            }

            if (res.tank_solenoid_reclaimed_out == 0) {
                $("#cmn-toggle-47").prop('checked', false);
            } else if (res.tank_solenoid_reclaimed_out == 1) {
                $("#cmn-toggle-47").prop('checked', true);
            }

            if (res.tank_pump_reclaimed_out == 0) {
                $("#cmn-toggle-50").prop('checked', false);
            } else if (res.tank_pump_reclaimed_out == 1) {
                $("#cmn-toggle-50").prop('checked', true);
            }
            
        }, 'json');
    }
</script>

</html>