/**
 *
 * @param data
 * @param per_page
 */
function setList(data, per_page = 15)
{
    let count = data.length;
    let total_page = Math.ceil(count / per_page);
    let ul = $("#ul-data");

    $('.total').html(count)

    let list = data.slice(0, per_page);
    let  html = createLi(list,1,count);
    ul.html(html)

    createPagination(total_page, count, per_page)

    // $('.pagination').pagination({
    //     pageCount: total_page,
    //     totalData:count,
    //     showData:per_page,
    //     coping:true,
    //     // jump: true,
    //     callback: function (api) {
    //         search(api.getCurrent());
    //     }
    // });
}

/**
 * 查询
 *
 * @param page
 * @param type
 * @param per_page
 * @param operate
 */
function search(page = 1, type = 'more', per_page = 15, operate)
{
    if (operate === 'open') {
        if ($('.total').text() > 0) {
            return;
        }
    }
    $("#ul-data").html('<li class="file-uploading" style="height: 50%;border: none"></li>');
    if (type !== 'more') {
        $(".pagination").html('');
    }
    let data = {
        page: page,
        per_page: per_page,
        village:$("#village").val(),
        group:$("#group").val(),
        keyword:$("#keyword").val(),
    };
    $.getJSON(
        '/admin/map/data-page',
        data,
        function (json) {
            setListMore(json, type)
        }
    );
}
/**
 * 查询
 *
 * @param res
 * @param type
 */
function setListMore(res, type)
{
    let code = res.code;
    let data = res.data.data;
    let per_page = res.data.per_page;
    let current_page = res.data.current_page;
    let total = res.data.total;

    $('.total').html(total)
    if (code === 0) {
        if (type !== 'more') {
            let last_page = res.data.last_page;
            if (total > 0) {
                if (current_page === 1) {
                    type = 'more';
                }
                createPagination(last_page, total, per_page, type)
            }
        }
        let ul = $("#ul-data");
        let  html = createLi(data, current_page, total, type);
        ul.html(html)
    }
}

/**
 * 创建 li
 * @param data
 * @param current_page
 * @param total
 * @param type
 * @param per_page
 * @returns {string}
 */
function createLi(data, current_page, total, type = 'more', per_page = 15)
{
    let  html = '';
    if (total <= 0) {
        html ='<li style="height: 50%;border: none;text-align: center;display: block">';
        html += '<svg t="1562312016538" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"p-id="2076" width="128" height="128" style="fill: #e9e9e9;">';
        html += '<path d="M512.8 198.5c12.2 0 22-9.8 22-22v-90c0-12.2-9.8-22-22-22s-22 9.8-22 22v90c0 12.2 9.9 22 22 22zM307 247.8c8.6 8.6 22.5 8.6 31.1 0 8.6-8.6 8.6-22.5 0-31.1L274.5 153c-8.6-8.6-22.5-8.6-31.1 0-8.6 8.6-8.6 22.5 0 31.1l63.6 63.7zM683.9 247.8c8.6 8.6 22.5 8.6 31.1 0l63.6-63.6c8.6-8.6 8.6-22.5 0-31.1-8.6-8.6-22.5-8.6-31.1 0l-63.6 63.6c-8.6 8.6-8.6 22.5 0 31.1zM927 679.9l-53.9-234.2c-2.8-9.9-4.9-20-6.9-30.1-3.7-18.2-19.9-31.9-39.2-31.9H197c-19.9 0-36.4 14.5-39.5 33.5-1 6.3-2.2 12.5-3.9 18.7L97 679.9v239.6c0 22.1 17.9 40 40 40h750c22.1 0 40-17.9 40-40V679.9z m-315-40c0 55.2-44.8 100-100 100s-100-44.8-100-100H149.6l42.5-193.3c2.4-8.5 3.8-16.7 4.8-22.9h630c2.2 11 4.5 21.8 7.6 32.7l39.8 183.5H612z"p-id="2077"></path> </svg>';
        html +='</li>';
    } else {
        $(data).each(function (i,e) {
            if (e.id ) {
                let no = (i + 1) ;
                if (type === 'more') {
                    no = (current_page - 1 ) * per_page;
                    no +=  (i + 1)
                }

                html += '<li class="li" data-id="'+e.id+'" onclick="panToPoint('+e.id+','+e.longitude+','+e.latitude+')"><div class="markL"><i class="iconfont dont"></i>';
                html += '<span>' + no +'</span></div>';
                html += '<div class="li-content"><div class="content-top"><h3>'+e.title+'</h3></div>';
                html += '<div class="content-bottom">'+e.address+'</div>'
                html += '<div class="content-bottom">经度:' + e.longitude + '</div>'
                html += '<div class="content-bottom">纬度:' + e.latitude + '</div>'
                html += '</div>';
                html += '</div></li>';
            }
        })

    }

    return html;
}

/**
 * 创建分页
 *
 * @param count
 * @param total
 * @param per_page
 * @param type
 */
function createPagination(count, total, per_page, type)
{
    $('.pagination').pagination({
        pageCount: count,
        totalData: total,
        showData: per_page,
        coping: true,
        // jump: true,
        callback: function (api) {
            search(api.getCurrent(), type);
        }
    });
}

/**
 *
 * @param thisObj
 */
function toggleLeftCtr(thisObj)
{
    if ($("#close-search-form").hasClass("expand")) {
        $("#close-search-form").removeClass("expand");
        $(thisObj).find("img").attr("src", "/assets/admin/img/map-close.png");
        $("#search-container").show();
        search(1, 'new', 15, 'open');
    } else {
        $("#close-search-form").addClass("expand");
        $(thisObj).find("img").attr("src", "/assets/admin/img/map-right.png");
        $("#search-container").hide();
    }
    // $(".ctr-box").toggle(100);
}

/**
 * @param thisObj
 */
function toggleRightCtr(thisObj)
{
    if ($("#close-ctr-box").hasClass("expand")) {
        $("#close-ctr-box").removeClass("expand");
        $(thisObj).find("img").attr("src", "/assets/admin/img/map-close.png");
        $(".ctr-box").show();
    } else {
        $("#close-ctr-box").addClass("expand");
        $(thisObj).find("img").attr("src", "/assets/admin/img/map-left.png");
        $(".ctr-box").hide();
    }
    // $(".ctr-box").toggle(100);
}

function panToPoint(id,longitude,latitude)
{
    if (longitude && latitude) {
        setMarket(id);
        const point = new BMap.Point(longitude,latitude);
        // const marker = new BMap.Marker(point); // 创建标注
        // map.addOverlay(marker); // 将标注添加到地图中
        map.panTo(point); //转到该点位置
    } else {
        toastr.error("该楼栋还未设置经纬度!");
        return false;
    }
}