[% USE raw %]
[% USE Asset %]
[% SET footerjs = 1 %]
[% USE Koha %]
[% USE KohaDates %]
[% USE Price %]
[% PROCESS 'i18n.inc' %]
[% INCLUDE 'doc-head-open.inc' %]
<title
    >[% FILTER collapse %]
        [% t("Koha usage statistics") | html %]
        &rsaquo; [% t("Administration") | html %] &rsaquo; [% t("Koha") | html %]
    [% END %]</title
>
[% INCLUDE 'doc-head-close.inc' %]
[% Asset.css("lib/leaflet/leaflet.css") | $raw %]
</head>

<body id="admin_usage_statistics" class="admin">
[% WRAPPER 'header.inc' %]
    [% INCLUDE 'cat-search.inc' %]
[% END %]

[% WRAPPER 'sub-header.inc' %]
    [% WRAPPER breadcrumbs %]
        [% WRAPPER breadcrumb_item %]
            <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a>
        [% END %]
        [% WRAPPER breadcrumb_item bc_active= 1 %]
            <span>Usage statistics</span>
        [% END %]
    [% END #/ WRAPPER breadcrumbs %]
[% END #/ WRAPPER sub-header.inc %]

[% WRAPPER 'main-container.inc' aside='admin-menu' %]

    [% FOR m IN messages %]
        <div class="alert alert-[% m.type | html %]">
            [% SWITCH m.code %]
            [% CASE 'error_on_update' %]
                <span>An error occurred when updating this patron category. Perhaps it already exists.</span>
            [% CASE 'error_on_insert' %]
                <span>An error occurred when inserting this patron category. The patron category might already exist.</span>
            [% CASE 'error_on_delete' %]
                <span>An error occurred when deleting this patron category. Check the logs for details.</span>
            [% CASE 'success_on_update' %]
                <span>Patron category updated successfully.</span>
            [% CASE 'success_on_insert' %]
                <span>Patron category inserted successfully.</span>
            [% CASE 'success_on_delete' %]
                <span>Patron category deleted successfully.</span>
            [% CASE 'already_exists' %]
                <span>This patron category already exists.</span>
            [% CASE %]
                <span>[% m.code | html %]</span>
            [% END %]
        </div>
    [% END %]

    [% IF NOT Koha.Preference('UsageStats') %]
        <div class="alert alert-info">You are not sharing any data with the Koha community</div>
    [% END %]

    <form id="category_form" action="/cgi-bin/koha/admin/usage_statistics.pl" method="post">
        [% INCLUDE 'csrf-token.inc' %]
        <h1>Modify the statistics you share with the Koha community</h1>
        <div class="row">
            <div class="col col-md-6">
                <fieldset class="rows">
                    <ol>
                        <li>
                            <label for="UsageStats">Share my Koha usage statistics: </label>
                            <select name="UsageStats" id="UsageStats">
                                [% IF Koha.Preference('UsageStats') == 1 %]
                                    <option value="2">Undecided</option>
                                    <option value="1" selected="selected">Yes</option>
                                    <option value="0">No</option>
                                [% ELSIF Koha.Preference('UsageStats') == 0 %]
                                    <option value="2">Undecided</option>
                                    <option value="1">Yes</option>
                                    <option value="0" selected="selected">No</option>
                                [% ELSE %]
                                    <option value="2" selected="selected">Undecided</option>
                                    <option value="1">Yes</option>
                                    <option value="0">No</option>
                                [% END %]
                            </select>
                        </li>
                        <li>
                            <label for="UsageStatsCountry">Your country: </label>
                            [% SET UsageStatsCountry = Koha.Preference('UsageStatsCountry') %]
                            [% SET IsUsageStatsCountryValid = 0 %]
                            [% IF NOT UsageStatsCountry %]
                                [% SET IsUsageStatsCountryValid = 1 %]
                            [% END %]
                            [% PROCESS 'country-list.inc' %]
                            <select name="UsageStatsCountry" id="UsageStatsCountry">
                                <option value="">&nbsp;</option>
                                [% FOREACH country IN country_strings.split(',') %]
                                    [% IF country == UsageStatsCountry %]
                                        <option value="[% country | html %]" selected="selected">[% country | html %]</option>
                                        [% SET IsUsageStatsCountryValid = 1 %]
                                    [% ELSE %]
                                        <option value="[% country | html %]">[% country | html %]</option>
                                    [% END %]
                                [% END %]
                            </select>
                            [% UNLESS IsUsageStatsCountryValid %]
                                Note: The value of your system preference 'UsageStatsCountry' is not valid ([% UsageStatsCountry | html %]). Please select a valid one.
                            [% END %]
                        </li>
                        <li>
                            <label for="UsageStatsLibraryName">Library name: </label>
                            <input type="text" value="[% Koha.Preference('UsageStatsLibraryName') | html %]" name="UsageStatsLibraryName" id="UsageStatsLibraryName" />
                        </li>
                        <li>
                            <label for="UsageStatsLibraryType">Library type: </label>
                            [% SET UsageStatsLibraryType = Koha.Preference('UsageStatsLibraryType') %]
                            <select name="UsageStatsLibraryType" id="UsageStatsLibraryType">
                                <option value="">&nbsp;</option>
                                [% IF UsageStatsLibraryType == 'public' %]
                                    <option value="public" selected="selected">Public</option>
                                [% ELSE %]
                                    <option value="public">Public</option>
                                [% END %]

                                [% IF UsageStatsLibraryType == 'school' %]
                                    <option value="school" selected="selected">School</option>
                                [% ELSE %]
                                    <option value="school">School</option>
                                [% END %]
                                [% IF UsageStatsLibraryType == 'academic' %]
                                    <option value="academic" selected="selected">Academic</option>
                                [% ELSE %]
                                    <option value="academic">Academic</option>
                                [% END %]
                                [% IF UsageStatsLibraryType == 'research' %]
                                    <option value="research" selected="selected">Research</option>
                                [% ELSE %]
                                    <option value="research">Research</option>
                                [% END %]
                                [% IF UsageStatsLibraryType == 'private' %]
                                    <option value="private" selected="selected">Private</option>
                                [% ELSE %]
                                    <option value="private">Private</option>
                                [% END %]
                                [% IF UsageStatsLibraryType == 'societyAssociation' %]
                                    <option value="societyAssociation" selected="selected">Society or association</option>
                                [% ELSE %]
                                    <option value="societyAssociation">Society or association</option>
                                [% END %]
                                [% IF UsageStatsLibraryType == 'corporate' %]
                                    <option value="corporate" selected="selected">Corporate</option>
                                [% ELSE %]
                                    <option value="corporate">Corporate</option>
                                [% END %]
                                [% IF UsageStatsLibraryType == 'government' %]
                                    <option value="government" selected="selected">Government</option>
                                [% ELSE %]
                                    <option value="government">Government</option>
                                [% END %]
                                [% IF UsageStatsLibraryType == 'religiousOrg' %]
                                    <option value="religiousOrg" selected="selected">Religious organization</option>
                                [% ELSE %]
                                    <option value="religiousOrg">Religious organization</option>
                                [% END %]
                                [% IF UsageStatsLibraryType == 'subscription' %]
                                    <option value="subscription" selected="selected">Subscription</option>
                                [% ELSE %]
                                    <option value="subscription">Subscription</option>
                                [% END %]
                            </select>
                        </li>
                        <li>
                            <label for="UsageStatsLibraryUrl">Library URL: </label>
                            <input type="text" name="UsageStatsLibraryUrl" value="[% Koha.Preference('UsageStatsLibraryUrl') | html %]" id="UsageStatsLibraryUrl" />
                        </li>
                        <li>
                            <span class="label">Last update: </span>
                            [% IF UsageStatsLastUpdateTime %]
                                [% UsageStatsLastUpdateTime | html %]
                            [% ELSE %]
                                <span>Your data have never been shared</span>
                            [% END %]
                        </li>
                        <li>
                            <label for="UsageStatsGeolocation">Geolocation: </label>
                            <input
                                id="UsageStatsGeolocation"
                                name="UsageStatsGeolocation"
                                value="[% Koha.Preference('UsageStatsGeolocation') | html %]"
                                type="text"
                                value="[% Koha.Preference('UsageStatsLibrariesInfo') | html %]"
                                size="35"
                                readonly="readonly"
                            />
                            <a href="#" class="change_geolocation" data-branchname="" data-id="UsageStatsGeolocation"
                                ><img src="[% interface | html %]/lib/leaflet/images/marker-icon.png" title="Set geolocation" style="max-height: 2em;"
                            /></a>
                        </li>
                        <li>
                            <label for="UsageStatsLibrariesInfo">Libraries information: </label>
                            <select name="UsageStatsLibrariesInfo" id="UsageStatsLibrariesInfo">
                                [% IF Koha.Preference('UsageStatsLibrariesInfo') %]
                                    <option value="1" selected="selected">Yes</option>
                                    <option value="0">No</option>
                                [% ELSE %]
                                    <option value="1">Yes</option>
                                    <option value="0" selected="selected">No</option>
                                [% END %]
                            </select>
                        </li>

                        <li id="libraries_info">
                            <fieldset class="rows">
                                <legend>Libraries</legend>
                                <ol>
                                    [% FOR l IN libraries %]
                                        <li>
                                            <label for="[% l.branchcode | html %]">[% l.branchname | html %]: </label>
                                            <div style="float:left;">
                                                <div>Country: [% l.branchcountry | html %]</div>
                                                <div>Url: [% l.branchurl | html %]</div>
                                                <div>
                                                    Geolocation: <input type="text" id="geolocation_[% l.branchcode | html %]" name="geolocation_[% l.branchcode | html %]" value="[% l.geolocation | html %]" size="35" readonly="readonly" />
                                                    <a href="#" class="change_geolocation" data-branchname="[% l.branchname | html %]" data-id="geolocation_[% l.branchcode | html %]"
                                                        ><img src="[% interface | html %]/lib/leaflet/images/marker-icon.png" title="Set geolocation for [% l.branchname | html %]" style="max-height: 2em;"
                                                    /></a>
                                                </div>
                                            </div>
                                        </li>
                                    [% END %]
                                </ol>
                            </fieldset>
                        </li>
                        <li>
                            <label for="MyPublicLink">See your public page: </label>
                            [% IF Koha.Preference('UsageStatsPublicID') %]
                                [% SET my_url = 'https://hea.koha-community.org/libraries/' _  Koha.Preference('UsageStatsPublicID') %]
                                <a href="[% my_url | url %]">[% my_url | html %]</a>
                            [% ELSE %]
                                You do not have anything public yet.
                            [% END %]
                        </li>
                    </ol>
                </fieldset>
            </div>
            <div class="col-md-6">
                <div>
                    <div id="mapid" style="width:100%;height: 400px;"></div>
                </div>
            </div>
        </div>

        <fieldset class="action">
            <input type="hidden" name="op" value="cud-update" />
            <input type="submit" class="btn btn-primary" value="Update your statistics usage" />
            <a class="cancel" href="/cgi-bin/koha/admin/usage_statistics.pl">Cancel</a>
        </fieldset>
    </form>
[% END %]

[% MACRO jsinclude BLOCK %]
    [% INCLUDE 'calendar.inc' %]
    [% Asset.js("js/admin-menu.js") | $raw %]
    [% Asset.js("/lib/leaflet/leaflet.js") | $raw %]
    <script>
        function positionMap() {
            var tbh = $("#mapid");
            var mapContainer = tbh.parent();
            mapContainer.css('position', 'relative');
            mapContainer.css('min-height', tbh.outerHeight(true));
            tbh.css('position', 'absolute');
            tbh.css('right', 0);

            var offsetTop = mapContainer.offset().top;
            var s = parseInt($(window).scrollTop(), 10);
            tbh.css('top', '').css('bottom', '');
            if (s > offsetTop) {
                var mapContainerOffsetBottom = offsetTop + mapContainer.innerHeight();
                if (s + tbh.outerHeight(true) > mapContainerOffsetBottom) {
                    tbh.css('bottom', 0);
                } else {
                    tbh.css('top', s - offsetTop);
                }
            } else {
                tbh.css('top', 0);
            }
        }

        $(document).ready(function() {
            $(window).scroll(positionMap);
            positionMap();
        });

        var map = L.map('mapid').setView([0,0], 1);

        L.tileLayer('https://a.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            maxZoom: 18,
            attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
                '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'
        }).addTo(map);

        var default_elt = { 'text': _("Main library"), 'id': 'UsageStatsGeolocation' };
        var current_elt = default_elt;

        // Add the title
        var title = L.control({position: 'topright'});
        title.onAdd = function (map) {
            this._div = L.DomUtil.create('div', 'title');
            this.update();
            return this._div;
        };
        title.update = function (props) {
            this._div.innerHTML = '<h4>' + _("Click on the map to set the geolocation for %s").format(current_elt.text) + '</h4>';
        };
        title.addTo(map);

        var markers = {};
        function add_to_map(elt, latlng) {
            var marker = markers[elt.id];
            if (marker) map.removeLayer(marker);
            marker = L.marker(latlng).addTo(map);
            marker.bindPopup(elt.text);
            marker.on('mouseover', function (e) {this.openPopup(); });
            marker.on('mouseout',  function (e) {this.closePopup();});
            markers[elt.id] = marker;
        }

        // Init the map
        [% IF Koha.Preference('UsageStatsGeolocation') %]
            var latlng = [ [% Koha.Preference('UsageStatsGeolocation') | html %] ];
            add_to_map( current_elt, latlng );
        [% END %]
        [% FOR l IN libraries %]
            [% NEXT UNLESS l.geolocation %]
            add_to_map( { 'text': "[% l.branchname | html %]", 'id': "geolocation_[% l.branchcode | html %]" }, [ [% l.geolocation | html %] ] );
        [% END %]

        // On click, update the geolocation and the marker
        map.on('click', function(e){
            add_to_map( current_elt, e.latlng );
            $("#" + current_elt.id).val(e.latlng.lat + ',' + e.latlng.lng);
        });

        // Auto zoom
        var group = new L.featureGroup(Object.keys(markers).map(function(key){return markers[key]}));
        if ( group.getBounds().isValid() ) map.fitBounds(group.getBounds());

        // On click on the marker icons, update the title of the map
        $(document).ready(function(){
            $(".change_geolocation").on('click', function(e){
                e.preventDefault();
                if ( $(this).data('branchname') ) {
                    current_elt = { 'text': $(this).data('branchname'), 'id': $(this).data('id') };
                } else {
                    current_elt = default_elt;
                }
                title.update();
            });
            $("#UsageStatsLibrariesInfo").change();
        });

        $("#UsageStatsLibrariesInfo").on('change', function(){
            if ( $(this).val() == 1 ) $("#libraries_info").show()
            else $("#libraries_info").hide();
            positionMap();
        });
    </script>
[% END %]
[% INCLUDE 'intranet-bottom.inc' %]
