<!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">
        // 指定 10秒 刷新網頁一次
        var ptid = '{{tid}}';
        console.log('ptid:' + ptid)

        WebUpdate_set = setInterval(function(){WebUpdate(ptid)} , 5 * 1000)
    </script>
    <style>
        body {
            margin: 0;
        }

        input[type="button"] {
            padding: 4px 15px;
            background: #f3f3f3;
            border: 0 none;
            cursor: pointer;
            -webkit-border-radius: 5px;
            border-radius: 5px;

        }

        .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: 70px;
            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: 70px;
            line-height: 70px;
            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: 70px;
            }
        }

        @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>
    <script>
        // 清洗浮選 制動器開關
        var tank_num = '{{tid}}';

        // 入料 真空吸料機
        function inputVacuum() {
            var status = "off";
            var check = $("input[name=input_vacuum_status]:checked");  // 這裡面是 jQuery 撈取資料的方法, jQuery 常使用到 $ 錢字號
            console.log('check：', check);
            //大於0代表有被選中, 如果有多個可以呈現勾取的項目數量
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-62").prop('checked', false); // 確認是否勾選
                if (!confirm("你確定要開啟入料儲豆槽真空吸料機嗎?")) {
                    return false;
                };
            } else {
                $("#cmn-toggle-62").prop('checked', true);
                if (!confirm("你確定要關閉入料儲豆槽真空吸料機嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "P" + tank_num, "command": "input_vacuum_status", "value": status };
            // jquery 請求 '/mqtt/{{tid}}' 頁面
            $.post('/mqtt/{{tid}}', data, function (res) {      //res：HTTP response argument to the middleware function
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-62").prop('checked', true);
                    setTimeout("alert('入料儲豆槽真空吸料機_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-62").prop('checked', false);
                    setTimeout("alert('入料儲豆槽真空吸料機_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')

        };

        function inputVacuum_ON() {
            var data = { "tank_num": "P" + tank_num, "command": "input_vacuum_status", "value": "on" };
            // jquery 請求 '/mqtt/{{tid}}' 頁面
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-62").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-62").prop('checked', false);
                } else {
                };
            }, 'text')

        };

        function inputVacuum_OFF() {
            var data = { "tank_num": "P" + tank_num, "command": "input_vacuum_status", "value": "off" };
            // jquery 請求 '/mqtt/{{tid}}' 頁面
            $.post('/mqtt/{{tid}}', data, function (res) {      //res：HTTP response argument to the middleware function
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-62").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-62").prop('checked', false);
                } else {
                };
            }, 'text')

        };

        // 桶內 真空吸料機
        function tankVacuum() {
            var status = "off";
            var check = $("input[name=tank_vacuum_status]:checked");
            console.log('check：', check);
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-65").prop('checked', false);
                if (!confirm("你確定要開啟真空吸料機嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-65").prop('checked', true);
                if (!confirm("你確定要關閉真空吸料機嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "P" + tank_num, "command": "tank_vacuum_status", "value": status };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-65").prop('checked', true);
                    setTimeout("alert('真空吸料機_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-65").prop('checked', false);
                    setTimeout("alert('真空吸料機_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')

           
        };

        function tankVacuum_ON() {
            var data = { "tank_num": "P" + tank_num, "command": "tank_vacuum_status", "value": "on" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-65").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-65").prop('checked', false);
                } else {
                };
            }, 'text')

           
        };

        function tankVacuum_OFF() {
            var data = { "tank_num": "P" + tank_num, "command": "tank_vacuum_status", "value": "off" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-65").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-65").prop('checked', false);
                } else {
                };
            }, 'text')

        };

        // 馬達
        function ChangeMotor() {
            var motor_data = $("input[id=motor_rpm_data]").val();
            if (motor_data == '') {
                //$("#cmn-toggle-20").prop('checked', false);
                alert("請先輸入要運轉的值!");
                return false;

            } else if (motor_data == 0) {
                alert("轉速 0 為關閉馬達");

            } else if (Number(motor_data) < -50 || Number(motor_data) > 50 || !Number(motor_data)) {
                // 
                //$("#cmn-toggle-20").prop('checked', false);
                alert("您輸入的值已超過範圍，請重新輸入!" + !Number(motor_data));
                return false;
            };
            var value = "off";
            //var check = $("input[name=peeling-machine-on]:checked");
            if (motor_data != 0) {
                value = motor_data;
                //$("#cmn-toggle-20").prop('checked', false);
                if (!confirm("你確定要開啟攪拌馬達，運轉速度為 " + motor_data + " RPM 嗎?")) {
                    return false;
                };

            } else {
                //$("#cmn-toggle-20").prop('checked', true);
                if (!confirm("你確定要關閉攪拌馬達嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "P" + tank_num, "command": "tank_motor_status", "value": motor_data };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    //$("#cmn-toggle-14").prop('checked', true); // prop 設置元素屬型與元素值, 設定 checked 屬性為 true
                    setTimeout("alert('攪拌馬達_開啟成功!')", 500); // 設定時間執行函式 delay(500), 只執行一次
                    var timer = setInterval(Rotate, 60000); // 啟動後會在 60000 毫秒(更新時間一分鐘)內不斷執行 (原因須配合更新時間？)

                } else if (res == 'off') {
                    //$("#cmn-toggle-14").prop('checked', false);
                    setTimeout("alert('攪拌馬達_關閉成功!')", 500);
                    clearInterval(timer);                   // 取消 timer 的不斷執行
                    $("#motor_rpm_status").text('');        // 設置 #motor_rpm_status 的文字為空, #井字號
                } else {
                    alert(res);
                };
            }, 'text')
            $.get('/loading/D{{tid}}', '', function (res) {
                $("#motor_rpm_data").attr("placeholder", res.tank_motor)
            }, 'json');
        };
        function ChangeMotor_ON() {
            var data = { "tank_num": "P" + tank_num, "command": "tank_motor_status", "value": "20" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    var timer = setInterval(Rotate, 60000);
                } else if (res == 'off') {
                    clearInterval(timer);
                    $("#motor_rpm_status").text('');
                } else {
                };
            }, 'text')
        }
        function ChangeMotor_OFF() {
            var data = { "tank_num": "P" + tank_num, "command": "tank_motor_status", "value": "0" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    var timer = setInterval(Rotate, 60000);
                } else if (res == 'off') {
                    clearInterval(timer);
                    $("#motor_rpm_status").text('');
                } else {
                };
            }, 'text')
        }

        function Rotate() {
            $.get('/peeling', '', function (res) {
                //console.log(res.peeling);
                $("#motor_rpm_status-status").text(res.peeling + ' rpm(每1分鐘更新一次)');
            }, 'json');

            setTimeout(function () { location.reload(); }, 500);
        };


        // 出料 真空吸料機
        function outputVacuum() {
            var status = "off";
            var check = $("input[name=output_vacuum_status]:checked");
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-71").prop('checked', false);
                if (!confirm("你確定要開啟出料儲豆槽真空吸料機嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-71").prop('checked', true);
                if (!confirm("你確定要關閉出料儲豆槽真空吸料機嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "P" + tank_num, "command": "output_vacuum_status", "value": status };
            console.log('data:', data)
            $.post('/mqtt/{{tid}}', data, function (res) {
                if (res == 'on') {
                    $("#cmn-toggle-71").prop('checked', true);
                    setTimeout("alert('出料儲豆槽真空吸料機_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-71").prop('checked', false);
                    setTimeout("alert('出料儲豆槽真空吸料機_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')

        };

        function outputVacuum_ON() {
            var data = { "tank_num": "P" + tank_num, "command": "output_vacuum_status", "value": "on" };
            console.log('data:', data)
            $.post('/mqtt/{{tid}}', data, function (res) {
                if (res == 'on') {
                    $("#cmn-toggle-71").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-71").prop('checked', false);
                } else {
                };
            }, 'text')

        };

        function outputVacuum_OFF() {
            var data = { "tank_num": "P" + tank_num, "command": "output_vacuum_status", "value": "off" };
            console.log('data:', data)
            $.post('/mqtt/{{tid}}', data, function (res) {
                if (res == 'on') {
                    $("#cmn-toggle-71").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-71").prop('checked', false);
                } else {
                };
            }, 'text')

        };


    </script>
</head>

<body>
    狀態更新時間(秒)：
    <input name="webupdate_time" type="text" value="5" style="width:100px;">
    <input type="button" value="設定更新時間" onclick="changeUpdate()">
    <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(ptid)} , webupdate_time * 1000);
        }

        
    </script>
    <h4>制動器</h4>
    <table border="1px solid black" style="font-size:18px;" width=100%>
        <tr>
            <td style="color:red;">Valve</td>
            <td>入料<br>真空吸料機</td>
            <td>脫皮機入料<br>真空吸料機</td>
            <td>馬達</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 id="cmn-toggle-01" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-01"></label>
                    </div>
                    <div class="switch text-center">
                        {% if input_vacuum== 1 %}
                        <input id="cmn-toggle-62" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="input_vacuum_status" value="ON" onclick="inputVacuum()">
                        {% else %}
                        <input id="cmn-toggle-62" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="input_vacuum_status" value="OFF" onclick="inputVacuum()">
                        {% endif %}
                        <label for="cmn-toggle-62"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-03" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-03" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-04" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-04"></label>
                    </div>
                    <div class="switch text-center">
                        {% if tank_vacuum== 1 %}
                        <input id="cmn-toggle-65" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="tank_vacuum_status" value="ON" onclick="tankVacuum()">
                        {% else %}
                        <input id="cmn-toggle-65" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="tank_vacuum_status" value="OFF" onclick="tankVacuum()">
                        {% endif %}
                        <label for="cmn-toggle-65"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-06" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-06" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <input id="motor_rpm_data" type="text" placeholder="{{tank_motor}}" style="width:100px;">RPM (±50)
                <span id="motor_rpm_status" style="color:red;"></span>
                <br>
                <input type="button" value="送出" name="tank_motor_status" onclick="ChangeMotor()">
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-25" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-25"></label>
                    </div>
                    <div class="switch text-center">
                        {% if output_vacuum== 1 %}
                        <input id="cmn-toggle-71" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="output_vacuum_status" value="ON" onclick="outputVacuum()">
                        {% else %}
                        <input id="cmn-toggle-71" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="output_vacuum_status" value="OFF" onclick="outputVacuum()">
                        {% endif %}
                        <label for="cmn-toggle-71"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-27" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-27" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td><span style="color:#008CBA;"><b>ON</b></span></td>
            <td><input type="button" value="ON" name="input_vacuum_ON" onclick="inputVacuum_ON()"></td>
            <td><input type="button" value="ON" name="tank_vacuum_ON" onclick="tankVacuum_ON()"></td>
            <td><input type="button" value="ON" onclick="ChangeMotor_ON()"></td>
            <td><input type="button" value="ON" name="output_vacuum_ON" onclick="outputVacuum_ON()"></td>
        </tr>
        <tr>
            <td><span style="color:#C0C0C0;"><b>OFF</b></span></td>
            <td><input type="button" value="OFF" name="input_vacuum_OFF" onclick="inputVacuum_OFF()"></td>
            <td><input type="button" value="OFF" name="tank_vacuum_OFF" onclick="tankVacuum_OFF()"></td>
            <td><input type="button" value="OFF" onclick="ChangeMotor_OFF()"></td>
            <td><input type="button" value="OFF" name="output_vacuum_OFF" onclick="outputVacuum_OFF()"></td>
        </tr>
    </table>

    <br>

    <h4>感測器</h4>
    <table style="font-size:18px; border:2px #cccccc solid;" cellpadding="10" border='1' width=100%>
        <tr>
            <td>
                暫無
            </td>
        </tr>
    </table>

    <br>

    <h4>排程測試</h4>
    <table style="font-size:18px; border:2px #cccccc solid;" cellpadding="10" border='1' width=100%>
        <tr>
            <td>暫無</td>
        </tr>
    </table>

</body>

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

                $("#motor_rpm_data").attr("placeholder", res.tank_motor);
                // console.log("res.tank_motor"+res.tank_motor)
                
            }, 'json');
        }
</script>

</html>