<!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 ptid = '{{tid}}';
        $(document).ready(function(){
            $("#coffee_title_pc").text('S' + ptid + ' 色選機操作介面');
            $("#coffee_title_phone").text('S' + ptid + ' 操作介面');
        });

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

    <script>
        // 清洗浮選 致動器開關
        var tank_num = '{{tid}}';

        // 桶內 真空吸料機
        function tankVacuum(params) {
            if (params == '1') {
                var data = { "tank_num": "S" + tank_num, "command": "tank_vacuum_status", "value": "on" };
            } else if (params == '0') {
                var data = { "tank_num": "S" + tank_num, "command": "tank_vacuum_status", "value": "off" };
            } else {
                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": "S" + tank_num, "command": "tank_vacuum_status", "value": status };
            }
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-35").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-35").prop('checked', false);
                } else {
                };
            }, 'text')
        }

        // 馬達
        function tankMotor_set() {
            var motor_data = $("#motor_rpm_data").val();
            console.log("motor_data: " + motor_data)
            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": "S" + 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')
        };

        function tankMotor(params) {
            var data = { "tank_num": "S" + tank_num, "command": "tank_motor_status", "value": params };
            $.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('/peel', '', function (res) {
                //console.log(res.peeling);
                $("#motor_rpm_status").text(res.peeling + ' rpm(每1分鐘更新一次)');
            }, 'json');

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

        // 馬達 反轉
        function tankMotor_Reverse() {
            $("#input").attr('placeholder');
            var tankMotor_now = ($('#motor_rpm_data').attr('placeholder'))*-1
            console.log('tankMotor_now: ' + tankMotor_now)
            tankMotor(String(tankMotor_now))
            setTimeout(function () { location.reload(); }, 3*1000);
        }

        // 噴嘴
        function tankNozzle(params) {
            if (params == '1') {
                var data = { "tank_num": "S" + tank_num, "command": "tank_nozzle_status", "value": "on" };
            } else if (params == '0') {
                var data = { "tank_num": "S" + tank_num, "command": "tank_nozzle_status", "value": "off" };
            } else {
                var status = "off";
                var check = $("input[name=tank_nozzle_status]:checked");
                //大於0代表有被選中
                if (check.length > 0) {
                    status = "on";
                    $("#cmn-toggle-38").prop('checked', false);
                    if (!confirm("你確定要開啟噴嘴嗎?")) {
                        return false;
                    };
                } else {
                    $("#cmn-toggle-38").prop('checked', true);
                    if (!confirm("你確定要關閉噴嘴嗎?")) {
                        return false;
                    };
                };
                var data = { "tank_num": "S" + tank_num, "command": "tank_nozzle_status", "value": status };
            }
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-38").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-38").prop('checked', false);
                } else {
                };
            }, '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(ptid)} , 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>真空吸料機</td>
                <td>馬達</td>
                <td>噴嘴</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-34">
                        </div>
                        <div class="switch text-center">
                            {% if tank_vacuum== 1 %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-35" checked
                                name="tank_vacuum_status" value="ON" onclick="tankVacuum()">
                            {% else %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-35"
                                name="tank_vacuum_status" value="OFF" onclick="tankVacuum()">
                            {% endif %}
                            <label class="label_ActuatorStatus" for="cmn-toggle-35"></label>
                        </div>
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-yes-no" id="cmn-toggle-36">
                        </div>
                    </div>
                </td>
                <td>
                    <input type="text" class="input-text" id="motor_rpm_data" placeholder="{{tank_motor}}" value="">RPM<br>
                    <input type="button" class="input-button" value="Confirm" name="tank_motor_status" onclick="tankMotor_set()">
                </td>
                <td>
                    <div class="switch_div">
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round" id="cmn-toggle-37">
                        </div>
                        <div class="switch text-center">
                            {% if tank_nozzle == 1 %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-38" checked
                                name="tank_nozzle_status" value="ON" onclick="tankNozzle()">
                            {% else %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-38"
                                name="tank_nozzle_status" value="OFF" onclick="tankNozzle()">
                            {% endif %}
                            <label class="label_ActuatorStatus" for="cmn-toggle-38"></label>
                        </div>
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-yes-no" id="cmn-toggle-39">
                        </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="tankVacuum('1')"></td>
                <td>
                    <input type="button" class="input-button-on" value="20" onclick="tankMotor('20')">
                    <input type="button" class="input-button-on" value="Rev." onclick="tankMotor_Reverse()">
                </td>
                <td><input type="button" class="input-button-on" value="ON" onclick="tankNozzle('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="tankVacuum('0')"></td>
                <td><input type="button" class="input-button-off" value="0" onclick="tankMotor('0')"></td>
                <td><input type="button" class="input-button-off" value="OFF" onclick="tankNozzle('0')"></td>
            </tr>
        </table>

        <br>

        <h4>感測器</h4>
        <table border="1" class="table-all" cellpadding="3">
            <tr>
                <td style="background-color:#f3f3f3">生豆高度</td>
                <td><span id="UltraSonic_t_status">{{tank_UltraSonic.UltraSonic}}</span> 公分</td>
                <td style="background-color:#f3f3f3">顏色感測器</td>
                <td><span id="Color_t_status">{{tank_color.color}}</span> (單位)</td>
            </tr>
        </table>

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

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

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

            $("#motor_rpm_data").attr("placeholder", res.tank_motor);
            $("#UltraSonic_t_status").text(res.UltraSonic);
            $("#Color_t_status").text(res.color);
            
        }, 'json');
    }
</script>

</html>