{% extends 'bom/bom-base.html' %}

{% load staticfiles %}
{% load materializecss %}

{% block head-title %}{{ title }}{% endblock %}

{% block main %}
    <link rel="stylesheet" type="text/css" href="{% static 'bom/css/style.css' %}"/>
    {{ add_subpart_form.media }}
{% endblock %}

{% block bom-menu %}
{% endblock %}

{% block content %}
    <div class="part-info container-app">
        <a href="{% url 'bom:part-info' part_id=part.id %}">Return to {{ part.full_part_number }}</a>
        <!-- <h5>{{ part.description }}</h5> -->
        <div id="bom" class="col s12">
            <br><h5>Modify Subparts</h5>

            {% include 'bom/bom-indented.html' with order_by='indented' manage=1 bom_items=indented_bom.parts part=part part_revision=part_revision profile=profile %}

            <br><h5>Add Subpart</h5>
            <div class="row">
                <form action="{% url 'bom:part-add-subpart' part_id=part.id part_revision_id=part_revision.id %}"
                      method="post" class="col s12">
                    {% csrf_token %}
                    {{ add_subpart_form.subpart_part_number|materializecss:'m4 s12' }}
                    {{ add_subpart_form.count|materializecss:'m1 s12' }}
                    {{ add_subpart_form.do_not_load|materializecss:'m2 s12 input-field materialize-checkbox-padding' }}
                    {{ add_subpart_form.reference|materializecss:'m4 s12' }}
                    <div class="fieldWrapper">
                        <button class="waves-effect waves-light btn green lighten-1" type="submit" name="action">add
                        </button>
                    </div>
                </form>
            </div>
            <br><h5>Batch Add Subparts</h5>
            <p>To add batch subparts, upload a csv that contains at least two columns with the headers:
                <b>part_number</b> <i>or</i> <b>manufacturer_part_number</b> and <b>quantity</b>; the 'part_number' or
                'manufacturer_part_number' column must contain part numbers that have already been created in this
                system.</p>
            <p>Additional fields include 'reference' or 'designator' to add a reference field to the subpart;
               'do_not_load' if you don't want the part loaded into the assembly that contains it; 'dnp' if
                you don't want to upload the part to IndaBOM.</p>
            <form action="{% url 'bom:part-upload-bom' part_id=part.id %}" method="post" enctype="multipart/form-data">
                {% csrf_token %}
                <div class="row">
                    <div class="col s9 file-field input-field">
                        <div class="green lighten-1 btn">
                            <span>File</span>
                            {{ upload_subparts_csv_form.file }}
                        </div>
                        <div class="file-path-wrapper">
                            <input class="file-path validate" type="text" placeholder="Upload a file.">
                            {{ upload_subparts_csv_form.file.errors }}
                        </div>
                    </div>
                    <div class="col s1 input-field">
                        <input class="green lighten-1 btn" type="submit" value="Upload"/>
                    </div>
                </div>
            </form>
            <div class="row">
                <div class="col s6">
                    <a href="{% url 'bom:part-info' part_id=part.id %}" class="waves-effect waves-light btn-flat grey-text lighten-1" style="margin-left: -16px;">Cancel</a>
                </div>
            </div>
            <br>
        </div>
    </div>
{% endblock %}

{% block bom-script %}
    <!-- Tree Table -->
    <script src="{% static 'bom/js/jquery.treetable.js' %}"></script>
    <script>
        $("#indented-bom").treetable({
            expandable: true,
            indent: 2,
            initialState: 'collapsed',
            onInitialized: function () {
                $("#indented-bom").treetable("reveal", {{ part_revision.id }});
            }
        });
    </script>

    <!-- Floating Horizontal Scrollbar -->
    <script type="text/javascript" src="{% static 'bom/js/jquery.ba-floatingscrollbar.min.js' %}"></script>
    <script>
        $(function () {
            $('.responsive-table-wrapper').floatingScrollbar();
        });
    </script>

    <!-- Handle if there's an anchor, select tab -->
    <script type='text/javascript'>
        // For dropdown menu
        $(document).ready(function () {
            $('.dropdown-trigger').dropdown({
                    inDuration: 300,
                    outDuration: 225,
                    constrainWidth: false, // Does not change width of dropdown to that of the activator
                    hover: false, // Activate on hover
                    gutter: 0, // Spacing from edge
                    alignment: 'left', // Displays dropdown with edge aligned to the left of button
                    stopPropagation: false, // Stops event propagation
                    coverTrigger: false
                }
            );

        });
    </script>
{% endblock bom-script %}