// 详情弹框js start
var pointId = '';
var innerPage = 1;

// 详情弹框
$(document).on('click', 'a.view-detail', function() {
    let dataId = $(this).data('id'); // 单位ID
    pointId = dataId;
    queryPointDetailList = layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        shadeClose: true,
        skin: 'yourclass',
        area: [
            '700px', '500px'
        ],
        content: $(".queryPointDetailList"),
        success: function() {
            getPointDetailAjax({ "id": dataId })
                // 获取部门列表 ajax请求
            $('.clsBtn,.cancel').click(function() {
                innerPage = 1;
                layer.close(queryPointDetailList);
            })
        }
    })
})

// 获取列表 ajax请求
function getPointDetailAjax(queryParam = {}, innerPage = 1) {
    queryParam.page = innerPage;
    queryParam.limit = 1; // 每页显示条数
    queryParam.start = (queryParam.page - 1) * queryParam.limit;
    ajaxRequest(RECORD_AND_POINT, "POST", queryParam, function(result) {
        let data = result.RESULT;
        let items = '';
        if (result.totalCount > 0) {
            data.forEach(function(item, key) {
                items += "<tr>" + "<td class='status'></td>" + "<td>" + item.spot_name + "</td>" + "<td>" + item.spot_address + "</td>" + "<td>" + item.wcqk +
                    "</td>" + "<td>" + item.wcqk + "</td>" + "<td>" + item.wcqk + "</td>" +
                    "<td ><a class='pointDetail' data-id=" + item.dwid + "><img src='../../images/tupianbtn.png'></a></td>" + "<td ><a class='weibaoReport' data-id=" + item.jlid + "><img src='../../images/tupianbtn.png'></a></td>" + "</tr>"
            })
            $('#dataList2').html(items);
        } else {
            $('#dataList2').html('');
        }
        // console.log(result)
        totalPages = result.totalCount / result.limit;
        totalPages = Math.ceil(totalPages);
        $('#totalPage2').html(totalPages); // 总共多少页
        $('#dataTotal2').html(result.totalCount); // 总共多少条数据
        $('#currentPage2').val(innerPage); // 当前页面
        let pageFrom = (innerPage - 1) * result.limit + 1; // 开始
        let pageTo = result.page * result.limit; // 结束
        pageTo = pageTo > result.totalCount ? result.totalCount : pageTo;
        $('#pageFrom2').html(pageFrom);
        $('#pageTo2').html(pageTo);

        // 无数据时
        if (!result.totalCount) {
            $('.pager2.has-data').hide()
            $('.pager2.no-data').show()
        } else {
            $('.pager2.has-data').show()
            $('.pager2.no-data').hide()
        }
        if (innerPage < totalPages) {
            $('#nextPageButton2,#lastPageButton2').removeClass('disabled');
        } else {
            $('#nextPageButton2,#lastPageButton2').addClass('disabled');
        }
        if (innerPage === 1) {
            $('#firstPageButton2,#prevPageButton2').addClass('disabled');
        } else {
            $('#firstPageButton2,#prevPageButton2').removeClass('disabled');
        }
    }, function(errorMsg) {
        alert("请求数据失败!");
    }, 3)
};


// 分页操作
$('#firstPageButton2').on('click', function() {
    innerPage = 1;
    getPointDetailAjax(getSearchParamObj2(), 1);
});

$('#lastPageButton2').on('click', function() {
    innerPage = totalPages;
    getPointDetailAjax(getSearchParamObj2(), innerPage);
});

$('#prevPageButton2').on('click', function() {
    innerPage -= 1;
    getPointDetailAjax(getSearchParamObj2(), innerPage);
});

$('#nextPageButton2').on('click', function() {
    innerPage += 1;
    getPointDetailAjax(getSearchParamObj2(), innerPage);
})


// 按钮搜索
$('#buttonSearch2').on('click', function() {
    innerPage = 1;
    getPointDetailAjax(getSearchParamObj2());
})

// 拼接搜索条件
function getSearchParamObj2() {
    let queryParam = {};
    let weibao = $('#weibao').val();
    queryParam.data3 = weibao;
    queryParam.id = pointId;
    return queryParam;
}
// 分页刷新按钮
$('.pg-refresh2').click(resetForm);

// 重置表单
function resetForm() {
    innerPage = 1;
    $("#weibao").val("0");
    getPointDetailAjax(getSearchParamObj2());
}


//点位详情弹框
$(document).on('click', 'a.pointDetail', function() {
    let dwId = $(this).data('id');
    layerDepartmentIndex = layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        shadeClose: true,
        skin: 'yourclass',
        area: [
            '400px', '400px'
        ],
        content: $(".pointDetailOut"),
        success: function() {
            ajaxRequest(RECORD_POINT_DETAIL, "POST", { "id": dwId }, function(result) {
                console.log(result.RESULT[0])
                var data = result.RESULT[0];
                $('#pointDetailUrl').attr('src', BASEURL + data.picture_route);
            }, function(errorMsg) {
                alert("请求数据失败!");
            }, 3);

            $('.clsBtn2,.cancel2').click(function() {
                innerPage = 1;
                layer.close(layerDepartmentIndex);
            })
        }
    })
})

//维保报告点击
$(document).on('click', 'a.weibaoReport', function() {
    let jlId = $(this).data('id'); //单位ID
    layerReportIndex = layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        shadeClose: true,
        skin: 'yourclass',
        area: [
            '400px', '400px'
        ],
        content: $(".weibaoReportOut"),
        success: function() {
            ajaxRequest(RECORD_AND_POINT, "POST", { "jlid": jlId }, function(result) {
                console.log(result.RESULT[0])
                var data = result.RESULT[0];
                $('#weibaoReportUrl').attr('src', data.report_photos);
            }, function(errorMsg) {
                alert("请求数据失败!");
            }, 3);

            $('.clsBtn3,.cancel3').click(function() {
                innerPage = 1;
                layer.close(layerReportIndex);
            })
        }
    })
})