/*global $, google */
/*
**INFO**
KLISS: KS Legislature RESTian Interface
Source: http://kslegislature.org/klois/includes/kliss_restian_interface_guide_v12.pdf
*/

const klAjax = 'https://ksbar-site.uc.r.appspot.com/getkliss';
const getAjax = 'https://ksbar-site.uc.r.appspot.com/get';
const klHost = 'http://kslegislature.org/li';
const api = 'api';
const version = 'v12/rev-1';
const apiKey = 'AIzaSyBHnYpEqDQaRrMF_TNRvbTZhmPEKEsw_2E';
var people, geo, geoJSON, combined;

let items = [],
    ids = [],
    billIds = [],
    parent,
    membersParent = 'MembersListing',
    data,
    modalOrig,
    hsBase = `${getAjax}?q=${klHost}/s/images/pics/`,
    session, // = 'b2023_24',
    floor = 'house',
    floorFrml = 'House of Representatives',
    json,
    userTable = [],
    hiddenRows = [],
    matchedRows = [];

//console.log('KLISS API file loaded');




/* ********************************** */


    var fetchData = function (data) {
      return $.ajax({
        url: getAjax,
        data: data,
        dataType: 'json'
      });
    };
    var map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: 7,
        center: { lat: 38.40625379485267, lng: -98.2383671845704 }
      }),
      infowindow = new google.maps.InfoWindow();

    function makeMap(info) {
      map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: 7,
        center: { lat: 38.40625379485267, lng: -98.2383671845704 }
      });
      map.data.addGeoJson(info);

      /* Use map.data.loadGeoJson('https://...'); for External file/URL */
      /* map.data.loadGeoJson('https://ksbar-site.appspot.com/get?q=' + encodeURI(url)); */

      // Set the global styles.
      map.data.setStyle(function (feature) {
        var color = '#666666';
        // if (feature.getProperty('Party') === undefined) {
        //   console.log('PARTY', feature.getProperty('Party'));
        //   color = 'pink'; //feature.getProperty('color');
        // }
        if (feature.getProperty('Party') === 'Republican') {
          color = '#ff0000'; // 'red'
        }
        else if (feature.getProperty('Party') === 'Democrat') {
          color = '#0000ff'; // 'blue'
        }
        else if (feature.getProperty('Party') === 'Independent') {
          color = '#ff9900'; // 'yellow'
        }
        return ({
          fillColor: color,
          fillOpacity: '.25',
          strokeColor: '#666666',
          strokeWeight: 1,
          strokeOpacity: 0.5
        });
      });
      console.log('all done loading map');
      map.data.addListener('click', function (event) {

        console.log('event.feature', event.feature);
        let District = event.feature.getProperty('District');
        let KPID = event.feature.getProperty('KPID');

        var distMatch = $.map(combined.features, function (obj) {
          if (obj.District === event.feature.getProperty('District')) {
            return obj;
          }
        });
        //console.log(District, KPID, event.feature.h, distMatch[0]);
        console.log('distMatch[0]', distMatch);
        distMatch = distMatch[0];
        console.log('distMatch', distMatch);


        function infoWindowCreate(distMatch, e) {
          console.log('distMatch', distMatch, 'e.latLng', e.latLng);


          let html =
            `<div class="googft-info-window" style="font-family:Arial,sans-serif;width:425px;overflow-y:auto">` +
            `<img src="${getAjax}?q=${klHost}/s/images/pics/${e.feature.getProperty('KPID')}.jpg" style="height:160px;vertical-align:top;float:right;margin-right:10px;border:5px solid #e6e6e6">` +
            `<h2 style="margin-top:0px"><a href="${e.feature.getProperty('Webpage')}" target="_blank">${e.feature.getProperty('Full_Name')} <i class="fas fa-external-link-alt"></i></a></h2> ` +
            `<b>District:</b> ${e.feature.getProperty('District')}<br> ` +
            `<b>Party:</b> ${e.feature.getProperty('Party')}<br> ` +
            `<b>First Term:</b> ${e.feature.getProperty('First_Term')}<br> ` +
            `<b>Seat:</b> ${e.feature.getProperty('Seat')}<br> ` +
            `<h3 style="margin-bottom:0px">Contact Info:</h3> ` +
            `<b>Room: </b><i class="fa fa-door"></i>${e.feature.getProperty('Room')}<br> ` +
            `<b>Phone: </b><a href="tel:${e.feature.getProperty('Phone')}"><i class="fa fa-phone"></i> ${e.feature.getProperty('Phone')}</a><br> ` +
            `<b>Email: </b><a href="mailto:${e.feature.getProperty('Email')}"><i class="fa fa-envelope"></i> ${e.feature.getProperty('Email')}</a><br> ` +
            `<b>Webpage: </b><a href="${e.feature.getProperty('Webpage')}"target="_blank"><i class="fa-external-link-alt fas"></i> Link</a></div>`;

          infowindow.setContent(html); // show the html variable in the infowindow
          infowindow.setPosition(event.latLng);
          infowindow.setOptions({
            pixelOffset: new google.maps.Size(0, 0)
          }); // move the infowindow up slightly to the top of the marker icon
          infowindow.open(map);
          //getDetailsModal(KPID, distMatch);
        }

        if (event.feature.getProperty('clicked') === undefined) {
          var member = fetchData({
            q: `${klHost}/${api}/${version}/members/${KPID}/`
          });
          //$('#results_list').empty();
          member.done(function (response) {
            // $.each(response.content, function (k, v) {
            //   $('#results_list').append('<p>' + k + ': ' + v + '</p>');
            //   //console.log(k, v);
            // });
            console.log(distMatch.properties['First_Term']);
            distMatch.properties['Full_Name'] = response.content.FULLNAME;
            if (distMatch.properties['First_Term'] === '' || distMatch.properties['First_Term'] === undefined) { distMatch.properties['First_Term'] = response.content.FIRSTTERM; }
            else if (response.content.FIRSTTERM === '') { response.content.FIRSTTERM = distMatch.properties['First_Term'] }
            distMatch.properties['Seat'] = response.content.SEATNUM;
            console.log('distMatch:', distMatch);
            event.feature.setProperty('First_Name', response.content.FIRSTNAME);
            event.feature.setProperty('Full_Name', response.content.FULLNAME);
            event.feature.setProperty('Photo', `${getAjax}'q?=${klHost}/s/images/pics/${KPID}.jpg`);
            event.feature.setProperty('Webpage', `${klHost}/${session}/members/${KPID}/`);
            event.feature.setProperty('Party', response.content.PARTY);
            event.feature.setProperty('First_Term', response.content.FIRSTTERM);
            event.feature.setProperty('Seat', response.content.SEATNUM);
            event.feature.setProperty('Room', response.content.OFFICENUM);
            event.feature.setProperty('Phone', response.content.OFFPH);
            event.feature.setProperty('Email', response.content.EMAIL);
            event.feature.setProperty('Committees', response.content.COMMITTEES);
            event.feature.setProperty('Chaired_Committees', response.content.CHAIRED_COMMITTEES);
            event.feature.setProperty('Sbills', response.content.SBILLS);
            event.feature.setProperty('clicked', true);
            console.log(District, KPID);
            console.table(distMatch);
            infoWindowCreate(response.content, event);
            console.log('else infoWindowCreate response.content');
          });
          //});
        }
        else {
          infoWindowCreate(distMatch, event);
          console.log('else infoWindowCreate');
        }
      });
      // Set the fill color to red when the feature is moused over.
      // Stroke weight remains 1.
      map.data.addListener('mouseover', function (event) {
        map.data.overrideStyle(event.feature, { strokeWeight: 2, strokeOpacity: 1, fillOpacity: .45 });
      });

      map.data.addListener('mouseout', function (event) {
        map.data.overrideStyle(event.feature, { strokeWeight: 1, strokeOpacity: 0.5, fillOpacity: .25 });
      });
      $.each(info.features, function (k, v) {
        //console.log(v.properties);
        v.properties.Label = 'Rep.';

        function color(c) {
          if (c === 'Democrat') {
            return '#0000ff25';
          }
          else if (c === 'Republican') {
            return '#ff000025';
          }
          else if (c === 'Independent') {
            return '#ff990025'; // 'yellow'
          }
        }
        var cor = color(v.properties.Party);
        $('#' + v.properties.KPID)
          .attr({ 'data-party': v.properties.Party, 'data-dist': v.properties.District })
          .attr({ 'style': 'background-color: ' + cor })
          .children('td').children('a').children('span').text(v.properties.Label + ' ' + v.properties.First_Name + ' ' + v.properties.Last_Name);
        $('#' + v.properties.KPID + ' td')
          .next().attr({ 'class': 'details', 'data-id': v.properties.Party }).text(v.properties.Party)
          .next().attr({ 'class': 'details', 'data-id': v.properties.District }).text(v.properties.District)
          .next().attr({ 'class': 'details', 'data-id': v.properties.Gender }).text(v.properties.Gender)
          .next().attr({ 'class': 'details', 'data-id': v.properties.First_Term }).text(v.properties.First_Term);
      });
    }

    function initMap() {
      var url = 'https://storage.googleapis.com/ksbar-leg.appspot.com/2012-2022/' + floor + '_districts_simple.json?key=' + apiKey + '&v=20220809-1355';
      var jsonFetch = fetchData({
        q: url
      });
      var fetchLeg = fetchData({
        q: 'https://storage.googleapis.com/ksbar-leg.appspot.com/' + floor + '_latest.json?key=' + apiKey
      });




      $.when(jsonFetch, fetchLeg).done(function (geoData, details) {
        geoJSON = geoData[0];
        people = details[0];
        console.log('geoJSON:', geoJSON); // "Congressional districts"
        console.log('people:', people); // "People"

        geo = geoJSON.features;
        // var result = [
        //   [v1, v2].reduce((m, a) => (a.forEach(o => m.has(o.ObjId) && Object.assign(m.get(o.ObjId), o) || m.set(o.ObjId, o)), m), new Map).values()
        // ];


        var res = [];
        for (let i = 0; i < geo.length; i++) {
          let geoDist = geo[i].properties.District;
          res.push({
            ...geo[i],
            ...(people.features.find((itmInner) => itmInner.District === geoDist))
          });
          //console.log('res[i].properties:',res[i].properties);
          res[i].properties.District = geoDist;
          //$('#' + res[i].properties.KPID).attr('data-dist', geoDist); // sets district data to HTML element.
        }
        //console.log('res: ', res);
        combined = {
          "type": "FeatureCollection",
          "name": floorFrml,
          "features": res
        };
        console.log('combined: ', combined);
        makeMap(combined);

      });
      /*
      .fail(function(jqXHR, textStatus, errorThrown) {
        console.log("error " + textStatus);
        console.log("incoming Text " + jqXHR.responseText);
        console.log("Error Thrown " + errorThrown);
      });
    */
    }
    initMap();

/* ********************************** */

function toTitleCase(str) {
    return str.replace(/[a-zA-Z]+/g, function (txt) { // /\w\S*/g
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
}

// ref: http://stackoverflow.com/a/1293163/2343
// This will parse a delimited string into an array of
// arrays. The default delimiter is the comma, but this
// can be overriden in the second argument.
function CSVToArray(strData, strDelimiter) {
    // Check to see if the delimiter is defined. If not,
    // then default to comma.
    console.log('CSVToArray Called', strData);
    strDelimiter = (strDelimiter || ",");

    // Create a regular expression to parse the CSV values.
    var objPattern = new RegExp(
        (
            // Delimiters.
            "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

            // Quoted fields.
            "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

            // Standard fields.
            "([^\"\\" + strDelimiter + "\\r\\n]*))"
        ),
        "gi"
    );
console.log('objPattern', objPattern);

    // Create an array to hold our data. Give the array
    // a default empty first row.
    var arrData = [
        []
    ];

    // Create an array to hold our individual pattern
    // matching groups.
    var arrMatches = null;

console.log(arrMatches == objPattern.exec(strData));
    // Keep looping over the regular expression matches
    // until we can no longer find a match.
    while (arrMatches == objPattern.exec(strData)) {
console.log('ALERT',strMatchedDelimiter, arrMatches);
        // Get the delimiter that was found.
        var strMatchedDelimiter = arrMatches[1];
        console.log(strMatchedDelimiter);

        // Check to see if the given delimiter has a length
        // (is not the start of string) and if it matches
        // field delimiter. If id does not, then we know
        // that this delimiter is a row delimiter.
        if (
            strMatchedDelimiter.length &&
            strMatchedDelimiter !== strDelimiter
        ) {

            // Since we have reached a new row of data,
            // add an empty row to our data array.
            arrData.push([]);
        }

        var strMatchedValue;
        // Now that we have our delimiter out of the way,
        // let's check to see which kind of value we
        // captured (quoted or unquoted).
        if (arrMatches[2]) {

            // We found a quoted value. When we capture
            // this value, unescape any double quotes.
            strMatchedValue = arrMatches[2].replace(
                new RegExp("\"\"", "g"),
                "\""
            );
        }
        else {

            // We found a non-quoted value.
            strMatchedValue = arrMatches[3];

        }


        // Now that we have our value string, let's add
        // it to the data array.
        arrData[arrData.length - 1].push(strMatchedValue);
    }

    // Return the parsed data.
    return (arrData);
}

function doQuery(listing, id, status, floor) {
    //console.log('doQuery: ', listing);
    data = {
        listing: listing,
        detail: id,
        status: status,
        floor: floor
    };
    data.version = version; // current API version
    $.ajax({
        url: klAjax,
        method: 'GET',
        crossDomain: true,
        dataType: 'json',
        data: data,
        success: function (response) {
            console.log('response:', response, listing);
            parse(response, listing);
        },
        failure: function (response) {
            console.log('doQuery Failed:',response.message);
            throw response.message;

        }
    });
}

function parse(resp, listing) {
    console.log('query listing: ', listing);
    //let JSONdata = JSON.parse(data);
    //console.log(JSONdata);
    let itemRow;
    items.length = 0;
    let branches = [{ 'chamber': 'house' }, { 'chamber': 'senate' }],
        category, delay;
    if (listing === 'members') {
        $.each(branches, function (k, v) {
            items.length = 0;
            category = v['chamber'];
            //console.log(category);
            //let bran = ;
            itemRow = resp.content[category + '_members'];
            itemRow.forEach(function (e) {
                e['CAT'] = category;
            });
            //console.log(itemRow);
            //itemRow = itemRow.bran;

            itemRow.sort(compare);
            $.each(itemRow, members);

            write(items);
        });
        //console.log('ids', ids);
        delay = 3000;
        // $.each(ids, function (k, v) {
        //     setTimeout(function () {
        //         console.log(k, v.KPID);
        //         getDetails('members', v.KPID);
        //     }, k * delay);
        // });
        // console.log('resource/resmgr/legislative/2021-22/maps/' + floor + '_districts.txt', ',');
        // console.log(CSVToArray('resource/resmgr/api/kliss/kansas_senate_legislator_info.csv', ','));
    }
    if (listing === 'bill_status') {
        itemRow = resp.content;
        //console.log(itemRow);
        itemRow.sort(compareBills);
        $.each(itemRow, bills);

        //console.log(items);

        //writeTable(items);
    }
    if (listing === 'calendar') {
        itemRow = resp.content[0].document.split('/');
        session = itemRow[4];
        //console.log(session, itemRow);
        //itemRow[0].document;
        //$.each(itemRow, bills);

        //console.log(items);
        getSession();
        //writeTable(items);
    }

    function members() {
        let m = this;
        //console.log('this', this);
        /* Headshot Base URL */
        //console.log(m);
        let kpid = m.KPID;
        let memName = m.NAME;
        let parObj = membersParent;

        let idSplit = kpid,
            nameSplit = memName;
        //kpid.substr(kpid.length - 1);
        idSplit = toTitleCase(kpid);
        idSplit = idSplit.split('_');
        nameSplit = nameSplit.split(' ')[1];
        idSplit.pop();
        let title = idSplit.shift() + '.';


        //console.log(idSplit, idSplit.length);
        if (idSplit.length <= 2) {
            //console.log('normal');
            idSplit.push(idSplit.shift());
        }
        else {

            //idSplit.push(idSplit.shift());
            //idSplit.push(idSplit.shift());
            idSplit.shift();
            idSplit.shift();
            idSplit.push(nameSplit);
            //console.log('not normal', idSplit);
        }
        idSplit.unshift(title);
        //console.log(idSplit, idSplit.length);
        var nameJoin = idSplit.join(' ');
        var fullName = toTitleCase(nameJoin);
        //console.log(title, idSplit[0], idSplit[1], idSplit[2]);
        //console.log(nameJoin);
        let nameLast = toTitleCase(fullName),
            nameFirst = toTitleCase(fullName),
            nameNext = toTitleCase(fullName);


        //console.log('First Name: ', nameFirst + '-' + nameNext, 'Last Name:', nameLast, 'Full Name:', fullName);
        ids.push({ 'KPID': kpid, 'NAME': memName, 'FULLNAME': fullName });
        //console.log(ids);
        // ids['KPID'] = kpid;
        // ids['NAME'] = memName;
        //getDetails('members', kpid);
        if (m.CAT === floor) {
        items.push('<tr class="reps" id="' + kpid + '"><td data-id="' + kpid + '"><a href="#' + kpid + '" data-kpid="' + kpid + '" class="moreDetails"><img src="' + hsBase + kpid + '.jpg" class="photo-tn" /> <span>' + fullName + '</span></a></td><td></td><td></td><td></td><td></td></tr>');
        }
        parent = '#' + parObj;
    }

    function bills() {
        //console.log('bills loaded');

        /* *** Remove 'return;' below to load bills *** */
        return;

        let b = this,
            billURL = `${klHost}/${session}/measures/`,
            bill = b.BILLNO,
            sTitle = b.SHORTTITLE,
            lTitle = b.LONGTITLE,
            chamber = b['HISTORY'];
        //console.log(bill, chamber);
        var regexNew = /(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)/;
        b.ALPHA = bill.match(regexNew).groups.Alpha;
        b.NUMERIC = bill.match(regexNew).groups.Numeric;
        //console.log("Hey!");
        billIds.push({ 'bill': bill, 'alpha': b.ALPHA, 'numeric': b.NUMERIC });
        //console.log(billIds);
        // create('div', {
        //     'class': 'col-4 mb-3',
        //     'id': bill
        // });
        //console.log(userTable);
        var returnedData = $.grep(userTable, function (element, index) {
            //console.log('element.Bill', element.Bill);
            if (element.Bill == bill) {
                //console.log('Match! inside', element.Bill == bill, element.Bill);
            }
            return element.Bill == bill;
        });

        if (returnedData.length > 0) {
            //console.log('Match!', returnedData, +' = ' + bill);
        }
        var returnedData2 = userTable.filter(function () {
            //var thisRow = $(this).text().toLowerCase();
            return $(this).is(':contains("' + bill + '")');
        });
        //$(matchedRows).addClass('match');
        //console.log('This contains bill: ', bill);

        var year = 2021;
        var pdf = `${klHost}/${session}/measures/documents/` + bill.toLowerCase() + '_00_0000.pdf';
        var pdfLink = '<span class="pipe-spacer">|</span><a href="' + pdf + '" id="' + bill + '-pdf_LS_bill-tracking' + year + '" target="_blank" class="pdf">PDF</a> ';
        items.push('<tr class="bg-warning"><td id="' + bill + '"><a href="' + billURL + bill + '/" target="_blank">' + b.ALPHA + ' ' + b.NUMERIC + ' <i class="fas fa-external-link-alt"></i></a>' + pdfLink + '</td><td>' + sTitle + '</td><td></td><td>Monitor</td><td></td></tr>');
        // items.push("<li>" + title + "</li>");
        // items.push("<li>" + shortTitle + "</li>");
        parent = '#' + year;
    }

    function write(items) {
        //console.log('write items');
        let joinedItems = items.join('');
        $("<tbody/>", {
            html: joinedItems
        }).appendTo(parent);
    }

    // function write(items) {
    //     let joinedItems = items.join('');
    //     let th1 = '<thead><tr><th>Name</th>' +
    //         '<th>Party</th>' +
    //         '<th>District</th>' +
    //         '<th>Gender</th>' +
    //         '<th>First Term</th>' +
    //         '</tr></thead>';
    //     $(th1).appendTo(membersParent);

    //     $("<tbody/>", {
    //         html: joinedItems
    //     }).appendTo(parent);
    // }

    function writeTable(items) {
        let joinedItems = items.join('');
        let th = '<thead><tr><th class="bill">Bill</th>' +
            '<th class="description">Description</th>' +
            '<th class="section">Section</th>' +
            '<th class="action">Action</th>' +
            '<th class="testimony">Testimony</th>' +
            '</tr></thead';
        $(th).appendTo(parent);
        $("<tbody/>", {
            //'class': 'row',
            html: joinedItems
        }).appendTo(parent);
    }

    function getSession() {
    /* Get Google Table Info */
var fetchTable = function (data) {
    return $.ajax({
        url: `${getAjax}?q=${klHost}/${session}/members/csv/${floor}/`,
        //url: 'https://sheets.googleapis.com/v4/spreadsheets/1rtRnaFJbDqkTyQWwEhbhbPi_2OD8X5beDw9xtKRbvpg/values/A1:AU250',
        dataType: 'text',
        //'majorDimension': 'ROWS',
        data: {
            //key: 'AIzaSyCRsGgYKVVpF1qqYrTcBxeb-Nwj48-m5JQ',
            //majorDimension: 'ROWS'
            //type: 'text/csv'
        }
    });
};

/* Fetch Google Table - Works Perfect */

var table = fetchTable();
table.done(function (resp) {
    console.table('resp.values:',resp);


    //console.log(json);
    //var cvsArray = resp.split("\r\n");
    //console.log(lines[0]);
    //json = resp;
    var result = [];
var cvsArray = CSVToArray(resp);

console.log(cvsArray);
//csvJSON(resp);

    function arrayToJson(json) {
        var headers = json[0];
        for (var i = 1; i < json.length; i++) {
            var obj = {};
            var currentline = json[i];
            console.log(currentline);
            for (var j = 0; j < headers.length; j++) {
                obj[headers[j]] = currentline[j];
            }
            result.push(obj);
        }
        //return result; //JavaScript object
        return result; //JSON
    }

    console.log(result);
    //console.log('userTable', userTable);


}).fail(function (resp) {
    console.error(resp.responseJSON);
});
}
}




function filterResults(object, label) {
    var thisRow;
    // $.each($('[data-party="' + label + '"]'), function () {
    //     $(this).closest('tr').addClass('hidden');
    // });
    console.log('label: ', label);
    var match = $.map(combined.features, function (obj) {
        //if ($(object).is(':checked') !== true) {
        console.log('obj', obj);
        if (label.indexOf(obj.properties.Party) > -1) {
            console.log('combined.features MATCH:', obj);
            //obj.properties.clicked = 'true';
            console.log('obj.properties:', obj.properties);
            return obj;
        }

        //}
    });
    var party = {
        "type": "FeatureCollection",
        "name": floorFrml,
        "features": match
    };
    makeMap(party);
    console.log("match:",match);
    if ($(object).is(':checked')) {
        console.log($(object).is(':checked'));
        $('[data-party="' + label + '"]').removeClass('hidden');
    }
    // else if ($("#cbParty2").is(':checked')) {
    //     $('[data-party="' + label + '"]').removeClass('hidden');
    // }
    else {
        // hiddenRows = $('#memberstable .reps').filter(function () {
        //     thisRow = $(this).data('party');
        //     console.log(thisRow);
        //     return thisRow !== label;
        //     //return !$(this).is(':contains("' + srch + '")');
        // }).closest('tr');
        // $(hiddenRows).addClass('hidden');
        $('[data-party="' + label + '"]').addClass('hidden');
    }
}



function getDetailsModal(id, idMatch) {
    //console.log('id:', id, 'idMatch:', idMatch === undefined || Object.keys(idMatch).length);

    //let KPID = event.feature.getProperty('KPID');
    let KPID = id;
    if (idMatch === undefined || Object.keys(idMatch).length === 0) {
        idMatch = $.map(combined.features, function (obj) {
            //console.log('obj', obj);
            if (obj.properties.KPID === KPID) {
                console.log('combined.features MATCH:', obj);
                obj.properties.clicked = 'true';
                console.log('obj.properties:', obj.properties);
                return obj;
            }
        });
    }
    //console.log('idMatch', idMatch, KPID);


    //if (idMatch.properties.clicked === undefined) {
    var memberDetails = fetchData({
        q: `${klHost}/${api}/${version}/members/${KPID}/`
    });
    //console.log(member);
    //$('#results').empty();
    memberDetails.done(function (response) {
        console.table(response);
        $.each(response.content, function (k, v) {
            $('#results').append('<p>' + k + ': ' + v + '</p>');
            console.log(k, v);
        });
        var first_term_match;
        if (idMatch.length > 0) {first_term_match = idMatch[0].properties.First_Term} else {first_term_match = "Current session"};
        //console.log("FIRSTTERM :", idMatch[0], '' !== idMatch[0].length ? idMatch[0].properties.First_Term :'Current');
        let District = response.content.DISTRICT;
        let First_Name = response.content.FIRSTNAME;
        let Full_Name = response.content.FULLNAME;
        let Full_Long = response.content.JEMEMBFULLLONG;
        //let Webpage = event.feature.getProperty('Webpage');
        let Party = '' !== response.content.PARTY ? response.content.PARTY : idMatch[0].properties.Party;
        let First_Term = '' !== response.content.FIRSTTERM ? response.content.FIRSTTERM : first_term_match; //'2019 <a href="#" title="*Not provided by our resources—best guess"><i class="far fa-question-circle"></i></a>';
        let Seat = response.content.SEATNUM;
        let Room = response.content.OFFICENUM;
        let Phone = response.content.OFFPH;
        let Email = response.content.EMAIL;
        let Home_Phys_City = response.content.HMPHYSCITY;
        let SBills = response.content.SBILLS;
        //let KPID = event.feature.getProperty('KPID');
        //console.log('District:', District, 'KPID:', KPID);
        //console.table(response.content);

        //if (typeof District == "undefined") District = event.feature.getProperty('KPID');
        //if (typeof name == "undefined") name = event.feature.getProperty('district');
        let html =
            //`<div class="" style="font-family:Arial,sans-serif;width:100%;overflow-y:auto">` +
            `<img src="${getAjax}?q=${klHost}/s/images/pics/${KPID}.jpg" style="height:auto;vertical-align:top;float:right;margin-right:10px;border:5px solid #e6e6e6">` +
            `<h2 style="margin-top:0px" data-kliss="${KPID}"><a href="${klHost}/${session}/members/${KPID}/" target="_blank">${Full_Name}  <i class="fas fa-external-link-alt"></i></a></h2> ` +
            `<b>District:</b> ${District}<br> ` +
            `<b>Home City:</b> ${Home_Phys_City}<br> ` +
            `<b>Party:</b> ${Party}<br> ` +
            `<b>First Term:</b> ${First_Term}<br> ` +
            `<b>Seat:</b> ${Seat}<br> ` +
            `<b>Occupation:</b> ${response.content.OCCUPATION}<br> ` +
            `<h3 style="margin-bottom:0px">Contact Info:</h3> ` +
            `<b>Room: </b><i class="fa fa-door"></i>${Room}<br> ` +
            `<b>Phone: </b><a href="tel:${Phone}"><i class="fa fa-phone"></i> ${Phone}</a><br> ` +
            `<b>Email: </b><a href="mailto:${Email}"><i class="fa fa-envelope"></i> ${Email}</a><br> ` +
            `<b>Webpage: </b><a href="${klHost}/${session}/members/${KPID}/"target="_blank"><i class="fa-external-link-alt fas"></i> Link</a>`;
        //`</div>`;

        console.log(SBills);
        var SponsoredBills = `<h3>Sponsored Bills:</h3>` +
            `<table class="table table-responsive table-striped">` +
            `<thead><th style="min-width: 20%;">Bill No.</th><th>Short Title</th></thead><tbody>`;
        html += SponsoredBills;
        for (var i = 0; i < SBills.length; i++) {
            //console.log(SBills[i]);
            var bill = SBills[i]["BILLNO"],
                //name = SBills[i]["NAME"],
                stitle = SBills[i]["SHORTTITLE"];
            html += `<tr><td><a href="${klHost}/${session}/measures/${bill}/" target="_blank" id="${bill}_ls_members-${session}">${bill}</a></td><td>${stitle}</td></tr>`;
        }
        html += `</tbody></table>`;

        $('#modal-body').html(html); // show the html variable in the infowindow
        $('#myModalLabel').html(Full_Long);

        //console.log(thisMember);
        //infowindow.setPosition(event.latLng);
        //infowindow.setOptions({
        //  pixelOffset: new google.maps.Size(0, 0)
        //}); // move the infowindow up slightly to the top of the marker icon
        //infowindow.open(map);
        $('#leg-modal').modal('show');
    });
    // }
    // else {
    //     //infoWindowCreate();
    //     $('#leg-modal').modal('show');
    // }
}



/* Click listener */
$(document).on('click', '.moreDetails', function (e) {
    e.preventDefault();
    var dataKPID = $(this).data('kpid');
    var json = {
        KPID: dataKPID //e.target.dataset
    };
    //console.log('json:', json, 'kpid:', dataKPID);
    //getDetails('members', dataKPID);
    //console.log('trigger: ', combined);
    //console.log('trigger: ', combined, geo.properties);

    getDetailsModal(dataKPID);
});

$('#leg-modal').on('hidden.bs.modal', function () {
    $(this).html(modalOrig);
});

/* sorts alphabetical */
function compare(a, b) {
    if (a.KPID < b.KPID)
        return -1;
    if (a.KPID > b.KPID)
        return 1;
    return 0;
}
/* sorts district */
function compareDist(a, b) {
    console.log(a, b);
    if (a.properties.District < b.properties.District)
        return -1;
    if (a.properties.District > b.properties.District)
        return 1;
    return 0;
}

/* sorts alphabetical */
function compareBills(a, b) {
    if (a.BILLNO < b.BILLNO)
        return -1;
    if (a.BILLNO > b.BILLNO)
        return 1;
    return 0;
}

/* Start Email Sort Code */

/* sorts the table by it's table head (th) when clicked */
$('#' + membersParent).on('click', 'th', function () {
    // if this index is the checkbox column (aka col 0), return/exit function
    // if ($(this).index() == 0) {
    //     return;
    // }
    // else {
    //console.log(this);
    var table = $(this).parents('table').eq(0);
    var rows = table.find('tr').slice(1).toArray().sort(comparer($(this).index()));
    this.asc = !this.asc;
    if (!this.asc) { rows = rows.reverse(); }
    for (var i = 0; i < rows.length; i++) { table.append(rows[i]); }
    // }
});

/* Sorts the table by the table head */
/* https://stackoverflow.com/questions/3160277/jquery-table-sort
   https://stackoverflow.com/questions/34906543/issue-sorting-table-by-date-in-jquery
*/
function comparer(index) {
    var valA,
        valB;
    return function (a, b) {
        //if (getCellValue(a, index).indexOf("/") > -1) //comparing dates by checking if a slash is in cell
        // if (index === 3 || index === 4) {
        //     valA = new Date(getCellValue(a, index)).setHours(0, 0, 0, 0);
        //     valB = new Date(getCellValue(b, index)).setHours(0, 0, 0, 0);
        // }
        // else {
        valA = getCellValue(a, index);
        valB = getCellValue(b, index);
        // }
        console.log(valA, valB);
        return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.toString().localeCompare(valB);
    };
}

function getCellValue(row, index) {
    console.log(row, index, $(row).children('td').eq(index).attr('data-id'));
    return $(row).children('td').eq(index).attr('data-id');
}
/* End comparer*/

/* function for dynamic sorting */
/* https://www.sitepoint.com/sort-an-array-of-objects-in-javascript/  */
function compareValues(key, order = 'asc') {
    return function (a, b) {
        if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
            // property doesn't exist on either object
            return 0;
        }

        const varA = (typeof a[key] === 'string') ?
            a[key].toUpperCase() : a[key];
        const varB = (typeof b[key] === 'string') ?
            b[key].toUpperCase() : b[key];

        let comparison = 0;
        if (varA > varB) {
            comparison = 1;
        }
        else if (varA < varB) {
            comparison = -1;
        }
        return (
            (order == 'desc') ? (comparison * -1) : comparison
        );
    };
}
/* End Email Sort Code */




// let option = {};

// option.q = klHost + '/' + api + '/' + version + '/members/';
// option.floor = floor;


var members = fetchData({
    q: klHost + '/' + api + '/' + version + '/members/'
}, floor);

members.done(function (response) {
     //console.log(option.floor, 'TEST');
    //console.log(option.floor, response);
    let memberIds = [];

    var floorMemb = floor + '_members';
    $.each(response.content[floorMemb], function (k, v) {
        //console.log(k, v.KPID);
        memberIds.push(v.KPID);
    });

    function sequence(arr, callback) {
        var i = 0;
        var request = function (listIds) {
            return callback(listIds).then(function () {
                if (i < arr.length - 1)
                    return request(arr[++i]);
            });
        };
        return request(arr[i]);
    }
});

doQuery('calendar');
doQuery('members');
doQuery('bill_status');