<!DOCTYPE html>
<html>
    <head>

        {% set page_title = 'Bloom LIMS Object Details' %}
        <title>{{ page_title }}</title>
        
        {% set bloom_mod = 'lims' %}

        <link rel="stylesheet" type="text/css" href="{{ style.skin_css }}"  >
        <link rel="stylesheet" type="text/css" href="/static/legacy/style.css">
        <script src="/static/legacy/action_buttons.js"></script>
        
        <style>
            .floating-buttonf {
                position: fixed;
                top: 20px;
                right: 20px;
                /* Add more styling as needed */
            }

        </style>
    </head>
    <body>
        {% include 'legacy/bloom_header.html' %}

        <button id="toggleFilterButton" class="floating-buttonf">minimal recursion:{{ udat.get('wf_filter','no') }}</button>

        {% macro render_step(step, accordion_states) %}
            {% if step.polymorphic_discriminator == 'workflow_step_instance' or step.polymorphic_discriminator == 'workflow_instance' %}
                <hr>
                <button class="accordion"
                        data-state="{{ accordion_states[step.euid] }}"
                        id="{{ step.euid }}">
                    <small style="color:var(--accordion-prefix);"> {{ step.type }} </small>  <a href=euid_details?euid={{ step.euid }}>{{ step.euid }}</a> <a href="dindex2?globalStartNodeEUID={{ step.euid }}" target="bloom_dag">🕸️</a>  {{ step.json_addl['properties'].get('name', 'NONAME') }}  :: {{ step.bstatus }})  ({{ step.created_dt.strftime("%Y-%m-%d %H:%M:%S").split('.')[0] }})
                </button>
                <div class="workflow-step"
                     style="display: {{ 'block' if accordion_states[step.euid] == 'open' else 'none' }}">
                {% else %}
                    {% if step.type == 'plate' or step.type == 'rack' %}
                        <button style="width: inherit;
                                       background-color: var(--workflow-non-step-bg-color)"
                                class="accordion non-wf-step"
                                data-state="{{ accordion_states[step.euid] }}"
                                id="{{ step.euid }}">
                            <small><a href=plate_visualization?plate_euid={{ step.euid }}> {{ step.subtype }}</a> </small>  <a href=euid_details?euid={{ step.euid }}>{{ step.euid }}</a>  <a href="dindex2?globalStartNodeEUID={{ step.euid }}" target="bloom_dag">🕸️</a>  {{ step.json_addl['properties']['name'] }} ::  & {{ step.bstatus }})  ({{ step.created_dt.strftime("%Y-%m-%d %H:%M:%S").split('.')[0] }})
                        </button>
                        <div class="workflow-step"
                             style="font-size: 2px;
                                    display: {{ 'block' if accordion_states[step.euid] == 'open' else 'none' }}">
                        {% else %}
                            {% if step.type == 'package' %}
                                <button style="width: inherit;
                                               background-color: var( --workflow-non-step-bg-color)"
                                        class="accordion non-wf-step"
                                        data-state="{{ accordion_states[step.euid] }}"
                                        id="{{ step.euid }}">
                                    <small style="color: var(--accordion-prefix);">{{ step.type }}</small>
                                    <a href=euid_details?euid={{ step.euid }}>{{ step.euid }}</a>
                                    <a href="dindex2?globalStartNodeEUID={{ step.euid }}" target="bloom_dag">🕸️</a>
				    {{ step.json_addl['properties']['name'] }}
				    WW{{ step.subtype }} {{ step.version }}
                                    :: ({{ step.bstatus }})
                                    ({{ step.created_dt.strftime("%Y-%m-%d %H:%M:%S").split('.')[0] }})
                                    {% set fedex_data = step.json_addl['properties'].get('fedex_tracking_data', []) %}
                                    {% if fedex_data|length > 0 %}
                                        FEDEX: {{ fedex_data[0].get('Delivery_Status', '') }}
                                        {{ (step.json_addl['properties'].get('fedex_tracking_data', [{}])[0] .get('Transit_Time_sec', 0) / 60 / 60 / 24) | round(1) }} days
                                    {% else %}
                                        FEDEX: N/A
                                    {% endif %}
                                </button>
                                <div class="workflow-step"
                                     style="font-size: 2px;
                                            display: {{ 'block' if accordion_states[step.euid] == 'open' else 'none' }}">
                                {% else %}
                                    <button style="width: inherit;
                                                   background-color: var(--workflow-non-step-bg-color)"
                                            class="accordion non-wf-step"
                                            data-state="{{ accordion_states[step.euid] }}"
                                            id="{{ step.euid }}">
                                        <small style="color: var(--accordion-prefix);">{{ step.type }}</small>  <a href=euid_details?euid={{ step.euid }}>{{ step.euid }}</a>  <a href="dindex2?globalStartNodeEUID={{ step.euid }}" target="bloom_dag">🕸️</a> {{ step.type }}  {{ step.json_addl['properties']['name'] }} :: ({{ step.bstate }} & {{ step.bstatus }})  ({{ step.created_dt.strftime("%Y-%m-%d %H:%M:%S").split('.')[0] }})
                                    </button>
                                    <div class="workflow-step"
                                         style="font-size: 2px;
                                                display: {{ 'block' if accordion_states[step.euid] == 'open' else 'none' }}">
                                    {% endif %}
                                {% endif %}
                            {% endif %}
                            <div class="panel">
                                <ul>
                                    <div class="action-group-group">
                                        {% if step.json_addl and 'action_groups' in step.json_addl %}
                                            {% for group_key, group_value in step.json_addl['action_groups'].items() %}
                                                <div class="action-group">
                                                    {{ group_value.group_name }}
                                                    {% for action_key, action_value in group_value.actions.items() %}
                                                        <button class="action_button"
                                                                onclick="showCapturedDataForm(this, {{ action_value }}, '{{ step.euid }}', '{{ action_key }}', '{{ group_key }}')"
                                                                {% if action_value.action_enabled == "0" %}disabled style="color: gray;"{% endif %}>
                                                            {{ action_value.action_name }}
                                                        </button>
                                                    {% endfor %}
                                                </div>
                                            {% endfor %}
                                        {% endif %}
                                        <button id="property_button"
                                                class="collapsible"
                                                onclick="toggleCollapsible(this)">Edit Properties</button>
                                        <div class="content">
                                            <form id="prop-{{ step.euid }}"
                                                  style="padding: 0px"
                                                  method="post"
                                                  action="/update_obj_json_addl_properties"
                                                  class="form-container">
                                                <input type="hidden" name="obj_euid" value="{{ step.euid }}">
                                                {% for key, value in step.json_addl['properties'].items() %}
                                                    <div>
                                                        <label for="{{ step.euid }}">{{ key }}:</label>
                                                        {% if value is string %}
                                                            <input type="text" id="{{ step.euid }}" name="{{ key }}" value="{{ value }}">
                                                        {% elif value is iterable %}
                                                            <div id="list-{{ step.euid }}-{{ key }}">
                                                                {% for item in value %}
                                                                    <input id="{{ step.euid }}"
                                                                           type="text"
                                                                           name="{{ key }}[]"
                                                                           value="{{ item }}">
                                                                {% endfor %}
                                                            </div>
                                                            <button class="form-button"
                                                                    type="button"
                                                                    onclick="addListItem('{{ step.euid }}', '{{ key }}')">
                                                                Add
                                                            </button>
                                                            <button class="form-button"
                                                                    type="button"
                                                                    onclick="removeListItem('{{ step.euid }}', '{{ key }}')">
                                                                Remove
                                                            </button>
                                                        {% else %}
                                                            <input type="text" id="{{ step.euid }}" name="{{ key }}" value="{{ value }}">
                                                        {% endif %}
                                                    </div>
                                                {% endfor %}
                                                <div>
                                                    <input type="submit" value="Save Changes" class="form-button">
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </ul>
                                <div>
                                    <ul>
                                        <!-- Recursive call for child steps -->     
                                                                                      

                                        {# Query both container_instance AND content_instance lineages per Rule 6 #}
                                        {% if step.parent_of_lineages %}
                                            <div>
                                                {% for child_step in step.get_sorted_parent_of_lineages(['container_instance', 'content_instance']) %}
                                                {% if child_step.child_instance.category != 'workflow_step' and udat.get('wf_filter','off') == 'off' %}
                                                        <ul>
                                                        {% else %}
                                                        {% endif %}
                                                        {% if child_step.child_instance.category == 'workflow_step' %}

                                                        {{ render_step(child_step.child_instance, accordion_states) }}

                                                        {% elif  child_step.child_instance.category != 'workflow_step' and udat.get('wf_filter','off') == 'off' %}
                                                        {{ render_step(child_step.child_instance, accordion_states) }}
                                                        {% endif %}
                                                        {% if child_step.child_instance.category != 'workflow_step' and udat.get('wf_filter','off') == 'off' %}
                                                        </ul>
                                                    {% else %}
                                                    {% endif %}
                                                {% endfor %}
                                            </div>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        {% endmacro %}
                        <div style="display: -webkit-inline-box;">
                        <h1>
                        <a  href=euid_details?euid={{ workflow.euid }}>{{ workflow.euid }}</a> :
                         {{ workflow.json_addl.get('properties',{}).get('name','hey you, ADD A NAME') }} 
                        </h1>
                        
                    </div>
                        <ul>
                            {% if workflow.json_addl and 'action_groups' in workflow.json_addl %}
                                <div class="action-group-group">
                                    {% for group_key, group_value in workflow.json_addl['action_groups'].items() %}
                                        <div class="action-group">
                                            {{ group_value.group_name }}
                                            {% for action_key, action_value in group_value.actions.items() %}
                                                <button class="action_button"
                                                        onclick="showCapturedDataForm(this, {{ action_value }}, '{{ workflow.euid }}', '{{ action_key }}', '{{ group_key }}')"
                                                        {% if action_value.action_enabled == "0" %}disabled style="color: gray;"{% endif %}>
                                                    {{ action_value.action_name }}
                                                </button>
                                            {% endfor %}
                                        </div>
                                    {% endfor %}
                                </div>
                            {% endif %}
                            <ul>
                                <li>
                                    <strong>Status:</strong> {{ workflow.bstatus }}
                                    <li>
                                        <strong>Type:</strong> {{ workflow.type }} :: {{ workflow.subtype }} :: {{ workflow.version }}
                                        <li>
                                            <strong>Created at:</strong> {{ workflow.created_dt }}
                                        </ul>
                                    </ul>
                                    <div>
                                        {% for step in workflow.get_sorted_parent_of_lineages(['workflow_instance']) %}
                                        {% if loop.first %}
                                        <hr>

                                            {{ render_step(step.child_instance, accordion_states) }}
                                            <ul>
                                        {% else %}
                                            {{ render_step(step.child_instance, accordion_states) }}
                                        {% endif %}
                                    {% endfor %}
                                            </div>
                                        </ul>
                                        <a href="/workflow_summary">Back to WF Summary</a>
                                        <button id="expandAll" class="floating-button">+</button>
                                        <button id="collapseAll" class="floating-button">-</button>
                                        <script>
        function toggleVisibility(id) {
            var element = document.getElementById(id);
            if (element.style.display === "none") {
                element.style.display = "block";
            } else {
                element.style.display = "none";
            }
        }
     
        function expandAllAccordions() {
            var acc = document.getElementsByClassName("accordion");
            for (var i = 0; i < acc.length; i++) {
                acc[i].classList.add("active");
                acc[i].nextElementSibling.style.display = "block";
            }
        }
        
        function collapseAllAccordions() {
            var acc = document.getElementsByClassName("accordion");
            for (var i = 0; i < acc.length; i++) {
                acc[i].classList.remove("active");
                acc[i].nextElementSibling.style.display = "none";
            }
        }
        
        document.getElementById('expandAll').addEventListener('click', expandAllAccordions);
        document.getElementById('collapseAll').addEventListener('click', collapseAllAccordions);
                                        </script>
                                        <script>
                                            document.getElementById('toggleFilterButton').addEventListener('click', function() {
                                                var currentVal = '{{ udat.get("wf_filter","na") }}'; // or innerHTML, depending on how you've set up your button
                                        
                                                // Prepare the URL for the AJAX request
                                                var url = '/set_filter?curr_val=' + currentVal;
                                        
                                                // Perform the AJAX request
                                                fetch(url)
                                                    .then(response => response.json())
                                                    .then(data => {
                                                        // Update the button text with the new filter value
                                                        document.getElementById('toggleFilterButton').textContent = data.new_filter_value;
                                              
                                                    
                                              
                                                    })
                                                    .catch(error => console.error('Error:', error));
                                            
                                            
                                                // Reload the page
                                                window.location.reload();
                                                });
                                        </script>
                                    </body>
                                </html>
