var currentDate = new Date();
// 获取当前月份的第一天
var firstDay = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
// 获取下一个月的第一天
var nextMonthFirstDay = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
// 获取当前月份的天数 //本月多少天
var daysInMonth = Math.floor((nextMonthFirstDay.getTime() - firstDay.getTime()) / (24 * 60 * 60 * 1000));
// console.log(daysInMonth);
var year = currentDate.getFullYear();
var month = currentDate.getMonth() + 1;//当前第几月
var pers = []
var table,form,laydate,element;
var tabIndex = 0;
layui.use(['form','layer','table','laydate','element'], function () {
    table = layui.table;
    form = layui.form;
    laydate = layui.laydate;
    element = layui.element;

    // 渲染时间选择器
    laydate.render({
        elem: '#month',
        type: 'month',
        format: 'yyyy-MM',
        fullPanel: true,
    });

    //监听Tab切换，以改变地址hash值
    element.on('tab(chosenTab)', function(data){
        // console.log(data)
        tabIndex = data.index;
        //切换重置
        // $('#search1').val('');
        // $('#search2').val('');
        // $('#search3').val('');
        // $('#month').val('');

        if(tabIndex==0){
            initTable1();
        }else if(tabIndex==1){
            initTable2();
        }else if(tabIndex==2){
            initTable3();
        }else{
            initTable1();
        }
    });

    // 触发submit提交 按钮点击或者表单被执行提交时触发
    form.on('submit(query)', function (data) {
        console.log($('#month').val())
        let arr = $('#month').val().split('-')
        month = Number(arr[1]);
        year = Number(arr[0]);
        daysInMonth = new Date(arr[0], arr[1], 0).getDate();

        console.log("搜索")
        if(tabIndex==0){
            initTable1();
        }else if(tabIndex==1){
            initTable2();
        }else if(tabIndex==2){
            initTable3();
        }else{
            initTable1();
        }
        return false; // 阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });

    table.on('tool(table)', function (obj) {
        switch (obj.event) {
            case 'detail': 
                console.log('详情',obj) 
                checkView()

            break;
        }
    });
 
    initTable1();
    
});

// 静态数据
var tableData = [
    {id: 1,isWork:['2.1','2.3','7.4','7.15'],name: '李云飞', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.21','7.31','7.14'],name: '肖洁', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'/'},
    {id: 1,isWork:['7.22','7.23','7.24'],name: '李小龙', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.12','7.13','7.14'],name: '李小龙', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '张爱国', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'/'},
    {id: 1,isWork:['7.4','7.5','7.7'],name: '孙建国', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'/'},
    {id: 1,isWork:['7.4','7.9','7.7'],name: '孙建国', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'/'},
    {id: 1,isWork:['7.4','7.5','7.8'],name: '孙建国', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'/'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '孙建国', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'/'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '孙建国', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'/'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '孙建国', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '李小龙', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '李小龙', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '李小龙', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '李云飞', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '肖洁', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '李云飞', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '肖洁', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '肖洁', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '李云飞', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
    {id: 1,isWork:['7.2','7.3','7.4'],name: '肖洁', idCard: '342224196202241356', phone: '15665366624',month:'2024-03',normal:'20',special:'3',line:'500kV大宝Ⅰ回线',station:'昆明运检一站',first:'#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12',second:'#1,#2,#3,#4,#5'},
];
/**
 * 重置操作
 */
function reset(){
    console.log("重置")
    console.log(tabIndex)
    $('#search1').val('');
    $('#search2').val('');
    $('#search3').val('');
    $('#month').val('');
    if(tabIndex==0){
        initTable1();
    }else if(tabIndex==1){
        initTable2();
    }else if(tabIndex==2){
        initTable3();
    }else{
        initTable1();
    }
}
    
/**
 * 初始化数据 全部
 */
function initTable1(){
    console.log('全部')
    let obj = {
        "tabIndex": tabIndex,
        "search1": $("#search1").val(),
        "search2": $("#search2").val(),
        "search3": $("#search3").val(),
        "month": $("#month").val(),
    }
    // 渲染表格
    table.render({
        elem: '#table',
        // url: "/sys/achievement",
        // where: obj,
        page: true,
        skin: 'line', // 表格样式
        cols: [[
            {
                title: '序号',
                field: 'zizeng',
                align: 'center',
                type: 'numbers'
            },
            {field:'name', title: '姓 名',align: 'center',},
            {field:'idCard', title: '身份证号',align: 'center',},
            {field:'phone', title: '联系电话',align: 'center',},
            {field:'month', title: '月份',align: 'center',},
            {field:'normal', title: '正常巡视(基*次)',align: 'center',},
            {field:'special', title: '正常特巡（天*人）',align: 'center',width:170},
            {field:'line', title: '管辖线路名称',align: 'center',width:170},
            {field:'station', title: '所属运检站',align: 'center', },
            {
                title: '操作',
                toolbar: '#opeator-bar',
                align: 'center',
                width: 200
            }
        ]],
        data: tableData
    });
     
}

/**
 * 初始化数据 正常巡视统计
 */
function initTable2(){
    console.log('正常巡视统计')
    let obj = {
        "tabIndex": tabIndex,
        "search1": $("#search1").val(),
        "search2": $("#search2").val(),
        "search3": $("#search3").val(),
        "month": $("#month").val(),
    }
    // 渲染表格
    table.render({
        elem: '#table',
        // url: "/sys/achievement",
        // where: obj,
        page: true,
        skin: 'line', // 表格样式
        cols: [[
            {
                title: '序号',
                field: 'zizeng',
                align: 'center',
                type: 'numbers'
            },
            {field:'name', title: '姓名',align: 'center',},
            {field:'line', title: '管辖线路名称',align: 'center',},
            {field:'station', title: '所属运检站',align: 'center',},
            {field:'month', title: '月份',align: 'center',},
            {field:'first', title: '巡1次区段（塔）',align: 'center',},
            {field:'normal', title: '巡1次张数',align: 'center',width:170},
            {field:'second', title: '巡2次区段（塔）',align: 'center',width:170},
            {field:'special', title: '巡2次张数',align: 'center', },
            {
                title: '操作',
                toolbar: '#opeator-bar',
                align: 'center',
                width: 200
            }
        ]],
        data: tableData
    });
}

/**
 * 初始化数据 特巡统计
 */
function initTable3(){
    console.log('特巡统计')
    let obj = {
        "tabIndex": tabIndex,
        "search1": $("#search1").val(),
        "search2": $("#search2").val(),
        "search3": $("#search3").val(),
        "month": $("#month").val(),
    }
    let cols = [
        {
            title: '序号',
            field: 'zizeng',
            align: 'center',
            type: 'numbers'
        },
        {field:'name', title: '姓 名',align: 'center',width:100},
        {field:'line', title: '管辖线路名称',align: 'center',width:200},
        {field:'station', title: '所属运检站',align: 'center',width:200},
        {field:'special', title: '总计（天）',align: 'center',width:100,templet: function(d){
            return `<apan>${d.isWork.length}</apan>`
        }},
        
    ]
    console.log(daysInMonth,'daysInMonth')
    for (let k = 1; k <= daysInMonth; k++) {
        let str = month+'.'+k;
        let obj = {field:'isWork', title: str,align: 'center',width:70,
            templet: function(d){
                if(d.isWork.includes(d.LAY_COL.title)){
                    return `<i class="layui-icon" style="color: red;">&#xe605;</i> `
                }else{
                    return `<apan></apan>`
                }
            }}
        cols.push(obj)
    }
    cols.push({
            title: '操作',
            toolbar: '#opeator-bar',
            align: 'center',
            width: 200
    })
    // 渲染表格
    table.render({
        elem: '#table',
        // url: "/sys/achievement",
        // where: obj,
        page: true,
        skin: 'line', // 表格样式
        cols: [cols],
        data: tableData
    });
}


/**
 * 详情
 * */
function checkView(id) {
    let height = '98%';
    let width = '95%';
    // $.ajax({
    //     type: 'post',
    //     contentType: "application/x-www-form-urlencoded",
    //     url: oiPlanUrl + '/monthlyPlanSubmission/getDataById',
    //     dataType: 'json',
    //     data: {
    //         id: id
    //     },
    //     success: function (data) {
            var index = layer.open({
                title: ['详情'],
                type: 2,
                content: 'attendanceDetail.html',
                area: [width, height],
                maxmin: false,
                success: function (layero, index) {
                    // console.log(data);
                    var myIframe = window[layero.find('iframe')[0]['name']]; 
                    myIframe.init(year,month);
                    // var fnc = myIframe.setData(data.data); //aaa()为子页面的方法

                },
            });
        // }
    // })
}










//导出
function exportData() {
    // let search1 = $('#search1').val();
    // let search2 = $('#search2').val();
    window.location.href = oiPlanUrl + "/monthlyPlanSubmission/export?token=" + token;
         

}