/**新增 修改弹框中的列表渲染及分页操作**/

var innerPage = 1;
// 新增修改 组织人员 列表渲染 ajax请求
function getTeamInnerList(queryParam = {}, innerPage = 1) {
    queryParam.page = innerPage;
    queryParam.limit = 8; // 每页显示条数
    queryParam.start = (queryParam.page - 1) * queryParam.limit;
    ajaxRequest(RECORD_POINT_DETAIL, "POST", queryParam, function(result) {
        let data = result.RESULT;
        let items = '';
        if (result.totalCount > 0) {
            data.forEach(function(item, key) {
                if (item.pd) {
                    items += "<tr class='selected'>" + "<td class='status checked' data-id='" + item.id + "'></td>" + "<td>" + item.spot_name + "</td>" + "<td>" + item.spot_address + "</td>" + "<td ><a class='pointDetail' data-id=" + item.dwid + "><img src='../../images/tupianbtn.png'></a></td>" + "</tr>"
                } else {
                    items += "<tr>" + "<td class='status' data-id='" + item.id + "'></td>" + "<td>" + item.spot_name + "</td>" + "<td>" + item.spot_address + "</td>" + "<td ><a class='pointDetail' data-id=" + item.dwid + "><img src='../../images/tupianbtn.png'></a></td>" + "</tr>"
                }
            })
            $('#dataList2').html(items);
            $('#dataList3').html(items);
        } else {
            $('#dataList2').html('');
            $('#dataList3').html('');
        }
        console.log(result)
        totalPages = result.totalCount / result.limit;
        totalPages = Math.ceil(totalPages);
        $('#totalPage2,#totalPage3').html(totalPages); // 总共多少页
        $('#dataTotal2,#dataTotal3').html(result.totalCount); // 总共多少条数据
        $('#currentPage2,#currentPage3').val(innerPage); // 当前页面
        let pageFrom = (innerPage - 1) * result.limit + 1; // 开始
        let pageTo = result.page * result.limit; // 结束
        pageTo = pageTo > result.totalCount ? result.totalCount : pageTo;
        $('#pageFrom2,#pageFrom3').html(pageFrom);
        $('#pageTo2,#pageTo3').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');
            $('#nextPageButton3,#lastPageButton3').removeClass('disabled');
        } else {
            $('#nextPageButton2,#lastPageButton2').addClass('disabled');
            $('#nextPageButton3,#lastPageButton3').addClass('disabled');
        }
        if (innerPage === 1) {
            $('#firstPageButton2,#prevPageButton2').addClass('disabled');
            $('#firstPageButton3,#prevPageButton3').addClass('disabled');
        } else {
            $('#firstPageButton2,#prevPageButton2').removeClass('disabled');
            $('#firstPageButton3,#prevPageButton3').removeClass('disabled');
        }
    }, function(errorMsg) {
        alert("请求数据失败!");
    }, 3)
};




// 新增弹框分页操作
$('#firstPageButton2').on('click', function() {
    innerPage = 1;
    getTeamInnerList({}, 1);
});

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

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

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



// 修改弹框分页操作
$('#firstPageButton3').on('click', function() {
    innerPage = 1;
    getTeamInnerList(getSearchParamObj2(), 1);
});

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

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

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

// 新增分页刷新按钮
$('.pg-refresh2').click(resetForm);

function resetForm() {
    innerPage = 1;
    getTeamInnerList();
}


// 修改分页刷新
$('.pg-refresh3').click(resetForm);

function resetForm() {
    let dataId = $('.pure-table #dataList tr').find('.checked').data('id');
    innerPage = 1;
    getTeamInnerList({ 'dwid': dataId });
}


// 拼接搜索条件
function getSearchParamObj2() {
    let dataId = $('.pure-table #dataList tr').find('.checked').data('id');
    let queryParam = {};
    queryParam.dwid = dataId;
    return queryParam;
}


//点位详情弹框
$(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', data.picture_route);
            }, function(errorMsg) {
                alert("请求数据失败!");
            }, 3);

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


//新增修改弹框时间选择
layui.use(['form', 'layedit', 'laydate'], function() {

});
layui.use('laydate', function() {
    var laydate = layui.laydate;
    //开始日期
    var insStart = laydate.render({
        elem: '#startTime',
        trigger: 'click',
        done: function(value, date) {
            //更新结束日期的最小日期
            insEnd.config.min = lay.extend({}, date, {
                month: date.month - 1
            });
            //自动弹出结束日期的选择器
            insEnd.config.elem[0].focus();
        }
    });

    //结束日期
    var insEnd = laydate.render({
        elem: '#endTime',
        trigger: 'click',
        done: function(value, date) {
            //更新开始日期的最大日期
            insStart.config.max = lay.extend({}, date, {
                month: date.month - 1
            });
        }
    });
});

layui.use('laydate', function() {
    var laydate = layui.laydate;
    //开始日期
    var insStart = laydate.render({
        elem: '#startTime2',
        trigger: 'click',
        done: function(value, date) {
            //更新结束日期的最小日期
            insEnd.config.min = lay.extend({}, date, {
                month: date.month - 1
            });
            //自动弹出结束日期的选择器
            insEnd.config.elem[0].focus();
        }
    });

    //结束日期
    var insEnd = laydate.render({
        elem: '#endTime2',
        trigger: 'click',
        done: function(value, date) {
            //更新开始日期的最大日期
            insStart.config.max = lay.extend({}, date, {
                month: date.month - 1
            });
        }
    });
});


//维保时间
var timeArr = [];
var start = 0;
var a = '';
for (let i = 0; i < 48; i++) {
    a = timeMeter(start, 1)
    timeArr.push(a)
    start += 1800;
}
var itemsArr = ''
timeArr.forEach(function(item, key) {
    itemsArr += `<option value="${item}">${item}</option>`
})
$('#start_inspect_time,#start_inspect_time2').append(itemsArr);
$('#end_inspect_time,#end_inspect_time2').append(itemsArr);

// 维保时间转化
function timeMeter(t, type = 1) {
    let time = Number(t);
    let h = time / 3600 < 10 ? "0" + parseInt(time / 3600) : parseInt(time / 3600);
    if (h != '00') time = time - h * 3600;
    let m = time / 60 < 10 ? "0" + parseInt(time / 60) : parseInt(time / 60);
    // let s = time % 60 < 10 ? "0" + (time % 60) : time % 60;
    if (type == 1) {
        return h + ':' + m;
    } else if (type == 2) {
        return m + "分" + s + "秒";
    }
}