{% extends "base.html" %} {% block main_content %}
	<div class="container-fluid">
		<h3 class="offset-lg-2">{{page_title}}</h3>
        <div class="alert alert-info">Duplicate files have been detected.  They have the same binary content,
        but either have a different name or are stored in two different
        directories.  Choose between the options below.  NOTE: after you click
        'Delete Duplicates', the files / directories in red will be deleted from the file
        system, those in green will remain</div>

        <script>
            let D=[]
            let F=[]
        </script>
		<div class="row">
			<form class="form form-inline col-lg-12" action="{{url_for('rm_dups')}}" method="POST">
                {# pass this through so that the back-end can delete this message when it rm_dups #}
                <input type="hidden" name="fe_msg_id" value={{fe_msg_id}}>
                {% for dups in per_file_dups %}
                    {% set outer_loop=loop.index %}
                    <div class="col-lg-12 py-4">
                        <h5>Choose between these files:</h5>
                        {% for dup in dups %}
                            <alert id="kf{{outer_loop}}-f{{loop.index}}" style="cursor: pointer;"  class="alert"
                                onClick="KeepFile({{outer_loop}},{{loop.index}})">{{dup.d}}/{{dup.f}}</alert>
                            {% if loop.index < dups|length %}
                                or 
                            {% else %}
                                <input type="hidden" name="kfhash-{{outer_loop}}" value="{{dup.h}}">
                                <input type="hidden" id="kfname-{{outer_loop}}" name="kfname-{{outer_loop}}" value="">
                            {% endif %}
                            <script>
                                F[{{outer_loop}}{{loop.index}}]="{{dup.id}}"
                            </script>
                        {% endfor %}
                    </div class="col-lg-12">
                {% endfor %}

                {% for dup in per_path_dups %}
                    <div class="col-lg-12 py-4">
                        <h5>{{dup.count}} duplicate files (same file names in 2 different directories). Choose path to KEEP:</h5>
                        <alert id="kd{{loop.index}}-d1" style="cursor: pointer;" onClick="KeepDir({{loop.index}}, 1)" class="alert">{{dup.d1}}</alert> or
                        <alert id="kd{{loop.index}}-d2" style="cursor: pointer;" onClick="KeepDir({{loop.index}}, 2)" class="alert">{{dup.d2}}</alert>
                        <input id="kdid-{{loop.index}}" type="hidden" name="kdid-1">
                        <input name="kdhash-{{loop.index}}" type="hidden" value="{{dup.hashes}}">
                        <script>
                            D[{{loop.index}}1]="{{dup.did1}}"
                            D[{{loop.index}}2]="{{dup.did2}}"
                        </script>
                    </div>
                {% endfor %}

                <input class="btn btn-danger" type="submit" value="Delete Duplicates">
            </form>
		</div class="row">
	</div class="container">
{% endblock main_content %}
{% block script_content %}
    <script>
        function KeepFile(row, which)
        {
            $('[id^=kf' + row + ']').attr('class', 'alert alert-danger sm-txt')
            $('#kf'+row+'-f'+which).attr('class', 'alert alert-success')
            $('#kfname-'+row).val( F[row.toString()+which.toString()] )
        }

        function KeepDir(row, which) 
        {
            $('[id^=kd]').attr('class', 'alert alert-danger sm-txt')
            $('#kd'+row+'-d'+which).attr('class', 'alert alert-success')
            $('#kdid-'+row).val( D[row.toString()+which.toString()] )
        }

        // force choose last of each keep file set
        $('[id$=f1]').each( function () { $(this).siblings( '.alert' ).last().click() } )
        // force choose last of each dir set
        $('[id$=d2]').click()
    </script>
{% endblock script_content %}
