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

    <style>
        .newcondition_css {
            padding: 10px;
            margin-top: 10px;
            background-color:honeydew;
        }
        hr {
            margin-top: 5px;
            margin-bottom: 5px;
        }

    </style>

</head>

<body>
    <!-- <h6>感測器數值</h6>
    入料儲豆槽液位計高度(cm) : !測試時請用生豆高度!  {{input_UltraSonic.UltraSonic}}
    <div id="input_UltraSonic" d={{input_UltraSonic.UltraSonic}} style="display:none"></div>
    <br>
    設定空桶高度應低於  10
    <br>
    乾燥槽液位計高度(cm) : !測試時請用生豆高度!  {{tank_UltraSonic.UltraSonic}}
    <div id="tank_UltraSonic" d={{tank_UltraSonic.UltraSonic}} style="display:none"></div>
    <br> -->

    <!-- <h6>入料排程</h6>
    設定入料儲豆槽生豆高度(cm) : 
    <input name="schedule_input_height" id="schedule_input_height" type="text" style="width:100px;" value="70">
    <br>
    設定吸料時間(s) : 
    <input name="schedule_tank_vacuumin" id="schedule_tank_vacuumin" type="text" style="width:100px;" value="5">
    <br>
    設定放料時間(s) : 
    <input name="schedule_tank_vacuumout" id="schedule_tank_vacuumout" type="text" style="width:100px;" value="10">
    <br>
    設定乾燥槽生豆高度(cm) : 
    <input name="schedule_tank_height" id="schedule_tank_height" type="text" style="width:100px;" value="70">
    <br>
    入料時馬達轉速(rpm) : 
    <input name="schedule_tank_motorrpm" id="schedule_tank_motorrpm" type="text" style="width:100px;" value="10">
    <br>
    <button type="submit" class="btn btn-primary" onclick="schedule_Start()">開始入料</button> -->
    <script>
        function schedule_Start() {
            //word = document.getElementById("schedule_SOP").innerHTML;
            //document.getElementById("schedule_SOP").innerHTML = word+'<br>*';
            var schedule_input_height = $("input[name=schedule_input_height]").val();
            var schedule_tank_vacuumin = $("input[name=schedule_tank_vacuumin]").val();
            var schedule_tank_vacuumout = $("input[name=schedule_tank_vacuumout]").val();
            var schedule_tank_height = $("input[name=schedule_tank_height]").val();
            var schedule_tank_motorrpm = $("input[name=schedule_tank_motorrpm]").val();
            var tank_UltraSonic = parseInt(document.getElementById('tank_UltraSonic').getAttribute('d'));

            // 入料判斷
            if (schedule_input_height <= input_UltraSonic) {
                console.log('儲豆槽達指定高度')
                console.log('tank_UltraSonic:' + tank_UltraSonic)

                if (tank_UltraSonic <= 10) {
                    var DRY_STATE = 'Ready'
                    console.log('乾燥桶為空, 狀態為 Ready, 準備入料')

                    // 開始入料
                    while (schedule_tank_height >= tank_UltraSonic) {

                        // 真空吸料機 ON
                        var data = { "tank-number": "1", "command": "tank_vacuum_status", "value": "on" };
                        console.log('data:', data)
                        /*
                        $.post('/mqtt/{{tid}}', data, function (res) {
                            console.log('真空吸料機 '+res)
                        }, 'text')
                        */

                        // 吸料時間
                        var time = new Date();
                        while ((new Date() - time) < schedule_tank_vacuumin * 1000) { }

                        // 真空吸料機 OFF
                        var data = { "tank-number": "1", "command": "tank_vacuum_status", "value": "off" };
                        console.log('data:', data)
                        /*
                        $.post('/mqtt/{{tid}}', data, function (res) {
                            console.log('真空吸料機 '+ res)
                        }, 'text')
                        */

                        // 放料時間
                        var time = new Date();
                        while ((new Date() - time) < schedule_tank_vacuumout * 1000) { }
                        location.reload();
                    }
                    console.log('乾燥桶生豆以達指定高度, 入料結束')


                } else { console.log('乾燥桶不為空, 無法入料') }
            } else { console.log('儲豆槽未達指定高度, 無法入料') }


        }
    </script>

    <!-- [物件]文字輸入框 選擇後連結 [動作]文字輸入框 -->
    <script>
        dry_object = new Array();
        dry_object[0] = ["-"];  // 制動器 / 感測器
        dry_object[1] = ["on", "off"];  // 入料真空吸料機
        dry_object[2] = ["on", "off"];  // 真空吸料機
        dry_object[3] = ["ON 吸料", "OFF 排氣"];  // 入料三通閥
        dry_object[4] = ["0", "10", "20", "30", "40", "50"];  // 馬達
        dry_object[5] = ["on", "off"];  // 鼓風機
        dry_object[6] = ["on", "off"];  // 加熱器 1
        dry_object[7] = ["on", "off"];  // 加熱器 2
        dry_object[8] = ["on", "off"];  // 蝴蝶閥
        dry_object[9] = ["on", "off"];  // 消毒電磁閥
        dry_object[10] = ["on", "off"];  // 排水電磁閥
        dry_object[11] = ["on", "off"];  // 夾層進水電磁閥
        dry_object[12] = ["on", "off"];  // 溫控開關
        dry_object[13] = ["0.0", "28.0", "30.0", "32.0", "34.0", "36.0", "38.0", "40.0", "42.0", "44.0", "46.0", "48.0", "50.0"];  // 設定溫度
        dry_object[14] = ["on", "off"];  // 出料真空吸料機

        dry_object[15] = ["1", "5", "10", "30", "60"];  // 暫停秒數

        // dry_object[14] = ["on", "off"];  // 入料儲豆槽_液位計
        // dry_object[15] = ["on", "off"];  // 液位計
        // dry_object[16] = ["on", "off"];  // SHT11_溫度
        // dry_object[17] = ["on", "off"];  // SHT11_濕度
        // dry_object[18] = ["on", "off"];  // 土壤三合一_溫度
        // dry_object[19] = ["on", "off"];  // 土壤三合一_濕度
        // dry_object[20] = ["on", "off"];  // 土壤三合一_EC
        // dry_object[21] = ["on", "off"];  // 氣壓
        // dry_object[22] = ["on", "off"];  // 出料儲豆槽_液位計

        function renew(obj_num, index) {
            var Value="";
            for(var i=0; i<dry_object[index].length; i++){
                Value = Value + '<option value=' + dry_object[index][i] + '> ' + dry_object[index][i] + '</option>';
            }
            var sectorSelect = document.getElementById("do_act" + obj_num);
            // console.log("sectorSelect: " + sectorSelect)
            sectorSelect.innerHTML = Value;
        }

    </script>
    <br>
    
    <div id="box">
        <div id="newcondition_1" class="newcondition_css">
            <!-- 總 條件 -->
            <div id="condition1">
                <!-- 子條件 1_1-->
                <div id="condition1_1">
                    條件
                    <select id="cond_z1_1">
                        <option value="if">if</option>
                        <option value="while">while</option>
                    </select>
                    <select id="cond_tank1_1">
                        <option value="D1">乾燥桶 D1</option>
                        <option value="D2">乾燥桶 D2</option>
                        <option value="D3">乾燥桶 D3</option>
                        <option value="D4">乾燥桶 D4</option>
                        <option value="D5">乾燥桶 D5</option>
                        <option value="D6">乾燥桶 D6</option>
                        <option value="D7">乾燥桶 D7</option>
                        <option value="D8">乾燥桶 D8</option>
                        <option value="D9">乾燥桶 D9</option>
                        <option value="D10">乾燥桶 D10</option>
                        <option value="D11">乾燥桶 D11</option>
                        <option value="D12">乾燥桶 D12</option>
                    </select>
                    <select id="cond_a1_1">
                        <option value="">制動器 / 感測器</option>
                        <optgroup label="制動器">
                            <option value="input_vacuum_status">入料真空吸料機</option>
                            <option value="tank_vacuum_status">真空吸料機</option>
                            <option value="tank_threewayvalve_input_status">入料三通閥</option>
                            <option value="tank_motor_status">馬達</option>
                            <option value="tank_blower_status">鼓風機</option>
                            <option value="tank_heater1_status">加熱器 1</option>
                            <option value="tank_heater2_status">加熱器 2</option>
                            <option value="tank_diskvalve_status">蝴蝶閥</option>
                            <option value="tank_solenoid_disinfect_status">消毒電磁閥</option>
                            <option value="tank_solenoid_water_out_status">排水電磁閥</option>
                            <option value="outer_solenoid_water_status">夾層進水電磁閥</option>
                            <option value="tank_temp_enable">溫控開關</option>
                            <option value="tank_temp">設定溫度</option>
                            <option value="output_vacuum">出料真空吸料機</option>
                        </optgroup>
                        <optgroup label="感測器">
                            <option value="input_UltraSonic">入料儲豆槽_液位計(cm)</option>
                            <option value="tank_UltraSonic">液位計(cm)</option>
                            <option value="tank_SHT11_Temp">SHT11_溫度(℃)</option>
                            <option value="tank_SHT11_Humidity">SHT11_濕度(%)</option>
                            <option value="tank_soil_Temp">土壤三合一_溫度(℃)</option>
                            <option value="tank_soil_Humidity">土壤三合一_濕度(%)</option>
                            <option value="tank_soil_EC">土壤三合一_EC(%)</option>
                            <option value="tank_PA">氣壓(PA)</option>
                            <option value="output_UltraSonic">出料儲豆槽_液位計(cm)</option>
                        </optgroup>
                    </select>
                    <select id="cond_b1_1">
                        <option value="equal">==</option>
                        <option value="morethan">&gt;=</option>
                        <option value="lessthan">&lt;=</option>
                    </select>
                    <input list="cond_list_c1_1" id="cond_c1_1">
                    <datalist id="cond_list_c1_1">
                        <option value="on">
                        <option value="off">
                        <option value="(或請自行輸入數值)">
                    </datalist>
                    <input type="button" id="cond_add1_1" value="條件++" onclick="cond_add(cond_add1_1);"><br>
                    <script>
                        var cond_add_child_i = 1                                                // 這是子條件

                        function cond_add(evt) {                                                // cond_add1_1 下一個是 cond_add1(條件)_2(子條件)
                            evt.style.display = "none";                                         // 不顯示原按鈕
                            var condadd_parent_num = String(evt.id).split('_')[1].slice(3, )    // 把第一個條件的編號記下
                            // console.log("condadd_parent_num: " + condadd_parent_num)

                            cond_add_child_i++

                            var html = '';
                            html +=
                            '<div id="condition' + condadd_parent_num + '_' + cond_add_child_i + '">' +
                            '<select id="cond_d' + condadd_parent_num + '_' + cond_add_child_i + '">' + 
                            '<option value="and">and</option>' + 
                            '<option value="or">or</option>' + 
                            '</select>' + 
                            '<select id="cond_a' + condadd_parent_num + '_' + cond_add_child_i + '">' + 
                            '<optgroup label="制動器">' + 
                            '<option value="input_vacuum_status">入料真空吸料機</option>' + 
                            '<option value="tank_vacuum_status">真空吸料機</option>' + 
                            '<option value="tank_threewayvalve_input_status">入料三通閥</option>' + 
                            '<option value="tank_motor_status">馬達</option>' + 
                            '<option value="tank_blower_status">鼓風機</option>' + 
                            '<option value="tank_heater1_status">加熱器 1</option>' + 
                            '<option value="tank_heater2_status">加熱器 2</option>' + 
                            '<option value="tank_diskvalve_status">蝴蝶閥</option>' + 
                            '<option value="tank_solenoid_disinfect_status">消毒電磁閥</option>' + 
                            '<option value="tank_solenoid_water_out_status">排水電磁閥</option>' + 
                            '<option value="outer_solenoid_water_status">夾層進水電磁閥</option>' + 
                            '<option value="tank_temp_enable">溫控開關</option>' + 
                            '<option value="tank_temp">設定溫度</option>' + 
                            '<option value="output_vacuum">出料真空吸料機</option>' + 
                            '</optgroup>' + 
                            '<optgroup label="感測器">' + 
                            '<option value="input_UltraSonic">入料儲豆槽_液位計(cm)</option>' + 
                            '<option value="tank_UltraSonic">液位計(cm)</option>' + 
                            '<option value="tank_SHT11_Temp">SHT11_溫度(℃)</option>' + 
                            '<option value="tank_SHT11_Humidity">SHT11_濕度(%)</option>' + 
                            '<option value="tank_soil_Temp">土壤三合一_溫度(℃)</option>' + 
                            '<option value="tank_soil_Humidity">土壤三合一_濕度(%)</option>' + 
                            '<option value="tank_soil_EC">土壤三合一_EC(%)</option>' + 
                            '<option value="tank_PA">氣壓(PA)</option>' + 
                            '<option value="output_UltraSonic">出料儲豆槽_液位計(cm)</option>' + 
                            '</optgroup>' + 
                            '</select>' + 
                            '<select id="cond_b' + condadd_parent_num + '_' + cond_add_child_i + '">' + 
                            '<option value="equal">==</option>' +
                            '<option value="morethan">&gt;=</option>' +
                            '<option value="lessthan">&lt;=</option>' +
                            '</select>' +
                            '<input list="cond_list_c' + condadd_parent_num + '_' + cond_add_child_i + '" id="cond_c' + condadd_parent_num + '_' + cond_add_child_i + '">' + 
                            '<datalist id="cond_list_c' + condadd_parent_num + '_' + cond_add_child_i + '">' +
                            '<option value="on">' +
                            '<option value="off">' +
                            '<option value="(或請自行輸入數值)">' +
                            '</datalist>' +
                            '<input type="button" id="cond_del' + condadd_parent_num + '_' + cond_add_child_i + '" value="條件--" onclick="cond_del(\'' + condadd_parent_num + '_' + cond_add_child_i + '\');">' +
                            '<input type="button" id="cond_add' + condadd_parent_num + '_' + cond_add_child_i + '" value="條件++" onclick="cond_add(cond_add' + condadd_parent_num + '_' + cond_add_child_i + ');"><br>' +
                            '</div>';

                            var divbox = document.getElementById('condition' + condadd_parent_num);
                            divbox.insertAdjacentHTML('beforeend', html);
                        }

                        function cond_del(evt) {
                            // console.log('evt: ' + evt)
                            var del_cond_child = document.getElementById('condition' + evt);
                            // console.log("del_cond_child: " + del_cond_child)
                            del_cond_child.innerHTML = ""

                            var parentObj = del_cond_child.parentNode;       // 取得欲刪除 div 的父類別
                            parentObj.removeChild(del_cond_child)            // 刪除 div

                            // 上一個按鈕要顯示出來
                            var del_cond_parent_num = String(evt).split('_')[0]
                            var del_cond_child_num = String(evt).split('_')[1]
                            // console.log('del_cond_parent_num: ' + del_cond_parent_num)
                            // console.log('del_cond_child_num: ' + del_cond_child_num)
                            var button_return_id = document.getElementById('cond_add' + del_cond_parent_num + '_' + String(parseInt(del_cond_child_num)-1));
                            // console.log("test: " + 'cond_add' + del_cond_parent_num + '_' + String(parseInt(del_cond_child_num)-1))
                            // console.log("button_return_id: " + button_return_id)    // null
                            if (button_return_id == null) {
                                var button_return_id = document.getElementById('cond_add' + del_cond_parent_num + '_1');
                            }
                            button_return_id.style.display = "inline";
                        }
                    </script>

                </div>
            </div>

            <div id="action1">
                <div id="action1_1">
                    物件 : 
                    <select id="do_obj1_1" onChange="renew('1_1', this.selectedIndex);">
                        <option value="">制動器</option>
                        <option value="input_vacuum_status">入料真空吸料機</option>
                        <option value="tank_vacuum_status">真空吸料機</option>
                        <option value="tank_threewayvalve_input_status">入料三通閥</option>
                        <option value="tank_motor_status">馬達</option>
                        <option value="tank_blower_status">鼓風機</option>
                        <option value="tank_heater1_status">加熱器 1</option>
                        <option value="tank_heater2_status">加熱器 2</option>
                        <option value="tank_diskvalve_status">蝴蝶閥</option>
                        <option value="tank_solenoid_disinfect_status">消毒電磁閥</option>
                        <option value="tank_solenoid_water_out_status">排水電磁閥</option>
                        <option value="outer_solenoid_water_status">夾層進水電磁閥</option>
                        <option value="tank_temp_enable">溫控開關</option>
                        <option value="tank_temp">設定溫度</option>
                        <option value="output_vacuum_status">出料真空吸料機</option>
                        <option value="time.sleep">暫停秒數</option>
                    </select>
                    動作 : 
                    <select id="do_act1_1">
                        <option value="">請由左方選取物件
                    </select>
                    <input type="button" id="act_add1_1" value="物件動作++" onclick="act_add(act_add1_1);"><br>
                
                </div>

                <script>
                    var act_add_child_i = 1
                    function act_add(evt) {
                        evt.style.display = "none";
                        var actadd_parent_num = String(evt.id).split('_')[1].slice(3, )
                        // console.log("actadd_parent_num: " + actadd_parent_num)

                        act_add_child_i++;

                        var html = '';
                        html +=
                        '<div id="action' + actadd_parent_num + '_' + act_add_child_i + '">' +
                        '物件 : ' +
                        '<select id="do_obj' + actadd_parent_num + '_' + act_add_child_i + '" onChange="renew(\'' + actadd_parent_num + '_' + act_add_child_i + '\', this.selectedIndex);">' +
                        '<option value="">制動器</option>' +
                        '<option value="input_vacuum_status">入料真空吸料機</option>' +
                        '<option value="tank_vacuum_status">真空吸料機</option>' +
                        '<option value="tank_threewayvalve_input_status">入料三通閥</option>' +
                        '<option value="tank_motor_status">馬達</option>' +
                        '<option value="tank_blower_status">鼓風機</option>' +
                        '<option value="tank_heater1_status">加熱器 1</option>' +
                        '<option value="tank_heater2_status">加熱器 2</option>' +
                        '<option value="tank_diskvalve_status">蝴蝶閥</option>' +
                        '<option value="tank_solenoid_disinfect_status">消毒電磁閥</option>' +
                        '<option value="tank_solenoid_water_out_status">排水電磁閥</option>' +
                        '<option value="outer_solenoid_water_status">夾層進水電磁閥</option>' +
                        '<option value="tank_temp_enable">溫控開關</option>' +
                        '<option value="tank_temp">設定溫度</option>' +
                        '<option value="output_vacuum_status">出料真空吸料機</option>' +
                        '<option value="time.sleep">暫停秒數</option>' +
                        '</select>' +
                        '動作 : ' +
                        '<select id="do_act' + actadd_parent_num + '_' + act_add_child_i + '">' +
                        '<option value="">請由左方選取物件</option>' +
                        '</select>' +
                        '<input type="button" id="act_del' + actadd_parent_num + '_' + act_add_child_i + '" value="物件動作--" onclick="act_del(\'' + actadd_parent_num + '_' + act_add_child_i + '\');">' +
                        '<input type="button" id="act_add' + actadd_parent_num + '_' + act_add_child_i + '" value="物件動作++" onclick="act_add(act_add' + actadd_parent_num + '_' + act_add_child_i + ');"><br>' +
                        '</div>';

                        var divbox = document.getElementById('action' + actadd_parent_num);
                        divbox.insertAdjacentHTML('beforeend', html);
                    }

                    function act_del(evt) {
                        var del_action = document.getElementById('action' + evt);
                        del_action.innerHTML = ""

                        var parentObj = del_action.parentNode;       // 取得欲刪除 div 的父類別
                        parentObj.removeChild(del_action)            // 刪除 div
                        
                        var del_act_parent_num = String(evt).split('_')[0]
                        var del_act_child_num = String(evt).split('_')[1]
                        // console.log('del_act_parent_num: ' + del_act_parent_num)
                        // console.log('del_act_child_num: ' + del_act_child_num)
                        var button_return_id = document.getElementById('act_add' + del_act_parent_num + '_' + String(parseInt(del_act_child_num)-1));
                        // console.log("test: " + 'cond_add' + del_act_parent_num + '_' + String(parseInt(del_act_child_num)-1))
                        // console.log("button_return_id: " + button_return_id)    // null
                        if (button_return_id == null) {
                            var button_return_id = document.getElementById('act_add' + del_act_parent_num + '_1');
                        }
                        button_return_id.style.display = "inline";

                    }
                </script>

                
            </div>

            <input type="button" id="add1" value="條件物件動作++" onclick="Add(add1);">
        </div>

        <script>
            var new_condition_i = 1

            function Add(evt) {                                 // 網頁初始按鈕 onclick="Add(add1);
                evt.style.display = "none";                     // 元素不顯示。將 add1 的 input-button 不顯示
                new_condition_i++;                                            // 設定編號用
                // var div = document.createElement('div');        // 輸出 <div></div>。此時並不會顯示, 
                                                                // 需透過 appendChild()、insertBefore() 或 replaceChild() 等方法將新元素加入至指定的位置之後才會顯示。
                // var cond_number = String(evt).slice(3, )        // 目前的 condition 條件物件動作 數 (最外層)
                // var cond_a = 'cond_a' + cond_number + '_' + new_condition_i;  // [條件] 左方 : 制動器感測器
                // var cond_b = 'cond_b' + cond_number + '_' + new_condition_i;  // [條件] 中間 : > / = / < 
                // var cond_c = 'cond_c' + cond_number + '_' + new_condition_i;  // [條件] 右方 : ON / OFF / 數值
                // var cond_d = 'cond_d' + cond_number + '_' + new_condition_i;  // 條件] 最左方條件連接 : and / or
                // div.id = 'newcondition_' + new_condition_i;                   // 設定 div 的 id
                // div.style.marginTop = "10px";                   // 新增的 div 設定 marginTop 屬性值
                var html = ''

                

                // 新增的條件
                html += 
                '<div id="newcondition_' + new_condition_i + '" class="newcondition_css">' +
                
                // 新增的分隔線
                // '<hr size="50" width="100%">' +


                '<div id="condition' + new_condition_i + '">' +
                '<div id="condition' + new_condition_i + '_1">' +
                '條件 : ' +
                '<select id="cond_z' + new_condition_i + '_1">' +
                '<option value="if">if</option>' +
                '<option value="while">while</option>' +
                '</select>' +
                '<select id="cond_tank' + new_condition_i + '_1">' +
                '<option value="D1">乾燥桶 D1</option>' +
                '<option value="D2">乾燥桶 D2</option>' +
                '<option value="D3">乾燥桶 D3</option>' +
                '<option value="D4">乾燥桶 D4</option>' +
                '<option value="D5">乾燥桶 D5</option>' +
                '<option value="D6">乾燥桶 D6</option>' +
                '<option value="D7">乾燥桶 D7</option>' +
                '<option value="D8">乾燥桶 D8</option>' +
                '<option value="D9">乾燥桶 D9</option>' +
                '<option value="D10">乾燥桶 D10</option>' +
                '<option value="D11">乾燥桶 D11</option>' +
                '<option value="D12">乾燥桶 D12</option>' +
                '</select>' +
                '<select id="cond_a' + new_condition_i + '_1">' +
                '<option value="">制動器 / 感測器</option>' +
                '<optgroup label="制動器">' +
                '<option value="input_vacuum_status">入料真空吸料機</option>' +
                '<option value="tank_vacuum_status">真空吸料機</option>' +
                '<option value="tank_threewayvalve_input_status">入料三通閥</option>' +
                '<option value="tank_motor_status">馬達</option>' +
                '<option value="tank_blower_status">鼓風機</option>' +
                '<option value="tank_heater1_status">加熱器 1</option>' +
                '<option value="tank_heater2_status">加熱器 2</option>' +
                '<option value="tank_diskvalve_status">蝴蝶閥</option>' +
                '<option value="tank_solenoid_disinfect_status">消毒電磁閥</option>' +
                '<option value="tank_solenoid_water_out_status">排水電磁閥</option>' +
                '<option value="outer_solenoid_water_status">夾層進水電磁閥</option>' +
                '<option value="tank_temp_enable">溫控開關</option>' +
                '<option value="tank_temp">設定溫度</option>' +
                '<option value="output_vacuum_status">出料真空吸料機</option>' +
                '<option value="time.sleep">暫停秒數</option>' +
                '</select>' +
                '</optgroup>' +
                '<optgroup label="感測器">' +
                '<option value="input_UltraSonic">入料儲豆槽_液位計(cm)</option>' +
                '<option value="tank_UltraSonic">液位計(cm)</option>' +
                '<option value="tank_SHT11_Temp">SHT11_溫度(℃)</option>' +
                '<option value="tank_SHT11_Humidity">SHT11_濕度(%)</option>' +
                '<option value="tank_soil_Temp">土壤三合一_溫度(℃)</option>' +
                '<option value="tank_soil_Humidity">土壤三合一_濕度(%)</option>' +
                '<option value="tank_soil_EC">土壤三合一_EC(%)</option>' +
                '<option value="tank_PA">氣壓(PA)</option>' +
                '<option value="output_UltraSonic">出料儲豆槽_液位計(cm)</option>' +
                '</optgroup>' +
                '</select>' +
                '<select id="cond_b' + new_condition_i + '_1">' +
                '<option value="equal">==</option>' +
                '<option value="morethan">&gt;=</option>' +
                '<option value="lessthan">&lt;=</option>' +
                '</select>' +
                '<input list="cond_list_c' + new_condition_i + '_1" id="cond_c' + new_condition_i + '_1">' +
                '<datalist id="cond_list_c' + new_condition_i + '_1">' +
                '<option value="on">' +
                '<option value="off">' +
                '<option value="(或請自行輸入數值)">' +
                '</datalist>' +
                '<input type="button" id="cond_add' + new_condition_i + '_1" value="條件++" onclick="cond_add(cond_add' + new_condition_i + '_1)"><br>' +
                '</div></div>';

                // 新增的物件動作
                html +=
                '<div id="action' + new_condition_i + '">' +
                '<div id="action' + new_condition_i + '_1">' +
                '物件 : ' +
                '<select id="do_obj' + new_condition_i + '_1" onChange="renew(\''+ new_condition_i + '_1\', this.selectedIndex);">' +
                '<option value="">制動器</option>' +
                '<option value="input_vacuum_status">入料真空吸料機</option>' +
                '<option value="tank_vacuum_status">真空吸料機</option>' +
                '<option value="tank_threewayvalve_input_status">入料三通閥</option>' +
                '<option value="tank_motor_status">馬達</option>' +
                '<option value="tank_blower_status">鼓風機</option>' +
                '<option value="tank_heater1_status">加熱器 1</option>' +
                '<option value="tank_heater2_status">加熱器 2</option>' +
                '<option value="tank_diskvalve_status">蝴蝶閥</option>' +
                '<option value="tank_solenoid_disinfect_status">消毒電磁閥</option>' +
                '<option value="tank_solenoid_water_out_status">排水電磁閥</option>' +
                '<option value="outer_solenoid_water_status">夾層進水電磁閥</option>' +
                '<option value="tank_temp_enable">溫控開關</option>' +
                '<option value="tank_temp">設定溫度</option>' +
                '<option value="output_vacuum_status">出料真空吸料機</option>' +
                '<option value="time.sleep">暫停秒數</option>' +
                '</select>' +
                '動作 : ' +
                '<select id="do_act' + new_condition_i + '_1">' +
                '<option value="">請由左方選取物件' +
                '</select>' +
                '<input type="button" id="act_add' + new_condition_i + '_1" value="物件動作++" onclick="act_add(act_add' + new_condition_i + '_1);"><br>' +
                '</div>' +
                '</div>' +

                // 新增的 條件物件動作++ 條件物件動作-- 按鈕
                '<input type="button" id="del' + new_condition_i + '" value="條件物件動作--" onclick="Delete(' + new_condition_i + ');"><br>' +
                '<input type="button" id="add' + new_condition_i + '" value="條件物件動作++" onclick="Add(add' + new_condition_i + ');">' +
                '</div>' ;

                var divbox = document.getElementById('box');
                divbox.insertAdjacentHTML('beforeend', html);

            };
            function Delete(evt) {
                var del_condition = document.getElementById('newcondition_' + evt);
                del_condition.innerHTML = ""

                var parentObj = del_condition.parentNode;       // 取得欲刪除 div 的父類別
                parentObj.removeChild(del_condition)            // 刪除 div

                // 上一個按鈕要顯示出來
                var button_return_id = document.getElementById('add' + String(parseInt(evt)-1));
                if (button_return_id == null) {
                    var button_return_id = document.getElementById('add1');
                }
                // console.log('button_return_id: ' + button_return_id)
                button_return_id.style.display = "block";        // 元素顯示。顯示前一個 input-button
            };

        </script>        

        <!-- 新增加的 newcondition_(編號) 會加在這裡 -->

    </div>
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    <br>

    <input type="button" id="test" value="取值測試" onclick="test();"><br>
    <span id="value_test"> ( 取值測試 ) </span><br>
    <span id="command_test"> ( command 測試 ) </span><br>
    <input type="button" id="test" value="ajax 測試" onclick="AJAXtest();">

    <script>
        var set_Schedule = '';

        function test(){
            // console.log("new_condition_i: " + new_condition_i)      // 總數 : 條件物件動作
            // console.log("cond_add_child_i: " + cond_add_child_i)    // 總數 : 子 條件
            // console.log("act_add_child_i: " + act_add_child_i)      // 總數 : 子 物件動作

            // set_Schedule += 'set_Schedule = { ';
            set_Schedule = '{ ';

            var command_test = '';
            // 條件物件動作 取值
            for (let z = 1; z <= new_condition_i; z++) {
                // if-while 取值
                try {
                    var cond_z_value = document.getElementById("cond_z" + z + "_1").value;
                    console.log("cond_z" + z + "_1: " + cond_z_value)
                    set_Schedule += '"cond_z' + z + '_1": "' + cond_z_value + '", ' ;
                } catch (error) {
                    if (error.name == 'TypeError') {
                        console.log("cond_z" + z + "_1: null")
                    } else {
                        alert("cond_z" + z + "_1: " + error.name)
                        break
                    }
                }

                // 桶槽號碼 取值
                try {
                    var cond_tank_value = document.getElementById("cond_tank" + z + "_1").value;
                    console.log("cond_tank" + z + "_1: " + cond_tank_value)
                    set_Schedule += '"cond_tank' + z + '_1": "' + cond_tank_value + '", ' ;
                } catch (error) {
                    if (error.name == 'TypeError') {
                        console.log("cond_tank" + z + "_1: null")
                    } else {
                        alert("cond_tank" + z + "_1: " + error.name)
                        break
                    }
                }

                // 子 條件 取值
                for (let x = 1; x <= cond_add_child_i; x++) {
                    // 子條件取值 : 條件連接 and or
                    if (x > 1) {
                        try {
                            var cond_d_value = document.getElementById("cond_d" + z + "_" + x).value;
                            // console.log("cond_d" + z + "_" + x + ": " + cond_d_value)
                            set_Schedule += '"cond_d' + z + '_' + x + '": "' + cond_d_value + '", ' ;
                        } catch (error) {
                            if (error.name == 'TypeError') {
                                // console.log("cond_d" + z + "_" + x + ": null")
                            } else {
                                alert("cond_d" + z + "_" + x + ": " + error.name)
                                break
                            }
                        }
                    }
                    try {
                        var cond_a_value = document.getElementById("cond_a" + z + "_" + x).value;
                        // console.log("cond_a" + z + "_" + x + ": " + cond_a_value)
                        set_Schedule += '"cond_a' + z + '_' + x + '": "' + cond_a_value + '", ' ;
                        if (cond_a_value == '') {
                            alert('條件欄位輸入有誤, 請再次確認')
                            break
                        }
                    } catch (error) {
                        if (error.name == 'TypeError') {
                            // console.log("cond_a" + z + "_" + x + ": null")
                        } else {
                            alert("cond_a" + z + "_" + x + ": " + error.name)
                            break
                        }
                    }
                    // 子條件取值 : 條件中 > = <
                    try {
                        var cond_b_value = document.getElementById("cond_b" + z + "_" + x).value;
                        // console.log("cond_b" + z + "_" + x + ": " + cond_b_value)
                        set_Schedule += '"cond_b' + z + '_' + x + '": "' + cond_b_value + '", ' ;
                        if (cond_b_value == '') {
                            alert('條件欄位輸入有誤, 請再次確認')
                            break
                        }
                    } catch (error) {
                        if (error.name == 'TypeError') {
                            // console.log("cond_b" + z + "_" + x + ": null")
                        } else {
                            alert("cond_b" + z + "_" + x + ": " + error.name)
                            break
                        }
                    }
                    // 子條件取值 : 條件右 ON / OFF / 輸入數值
                    try {
                        var cond_c_value = document.getElementById("cond_c" + z + "_" + x).value;
                        // console.log("cond_c" + z + "_" + x + ": " + cond_c_value)
                        set_Schedule += '"cond_c' + z + '_' + x + '": "' + cond_c_value + '", ' ;
                        if (cond_c_value == '') {
                            alert('條件欄位輸入有誤, 請再次確認')
                            break
                        }
                    } catch (error) {
                        if (error.name == 'TypeError') {
                            // console.log("cond_c" + z + "_" + x + ": null")
                        } else {
                            alert("cond_c" + z + "_" + x + ": " + error.name)
                            break
                        }
                    }   
                }

                // 子 物件動作 取值
                for (let y = 1; y <= act_add_child_i; y++) {
                    // 子物件動作取值 : 物件 do_obj1_1
                    try {
                        var do_obj_value = document.getElementById("do_obj" + z + "_" + y).value;
                        if (do_obj_value == '') {
                            alert('物件欄位輸入有誤, 請再次確認')
                            break
                        }
                        // console.log("do_obj" + z + "_" + y + ": " + do_obj_value)
                        set_Schedule += '"do_obj' + z + '_' + y + '": "' + do_obj_value + '", ' ;
                        command_test += '{"tank_num": "' + cond_tank_value + '", "command": "' + do_obj_value + '", "value":"';
                    } catch (error) {
                        if (error.name == 'TypeError') {
                            // console.log("do_obj" + z + "_" + y + ": null")
                        } else {
                            alert("do_obj" + z + "_" + y + ": " + error.name)
                            break
                        }
                    }
                    // 子物件動作取值 : 動作 do_act1_1
                    try {
                        var do_act_value = document.getElementById("do_act" + z + "_" + y).value;
                        if (do_act_value == '') {
                            alert('動作欄位輸入有誤, 請再次確認')
                            break
                        }
                        // console.log("do_act" + z + "_" + y + ": " + do_act_value)
                        set_Schedule += '"do_act' + z + '_' + y + '": "' + do_act_value + '", ' ;
                        command_test += do_act_value + ' }';
                    } catch (error) {
                        if (error.name == 'TypeError') {
                            // console.log("do_obj" + z + "_" + y + ": null")
                        } else {
                            alert("do_act" + z + "_" + y + ": " + error.name)
                            break
                        }
                    }
                }
            }


            console.log('-- test FINISH --')
            // 整理資料用
            set_Schedule += '"": ""}' ;
            console.log('set_Schedule: ' + set_Schedule)
            $('#value_test').text('set_Schedule: ' + set_Schedule)
            $('#command_test').text('command_test: ' + command_test)
        }
    
        function AJAXtest(params) {
            test();

            var schedule_data = {
                "condition":new_condition_i,
                "cond_add_child":cond_add_child_i,
                "act_add_child":act_add_child_i,
                "set_Schedule":set_Schedule
            };

            $.get('/dry_block_set', schedule_data, function (res) {
                // console.log("res.condition: " + res.condition)
                // console.log("res.cond_add_child: " + res.cond_add_child)
                // console.log("res.act_add_child: " + res.act_add_child)
                console.log("res.set_Schedule: " + res.set_Schedule)
            }, 'json');
            
        }
    </script>
    <br>

    

</body>

</html>