//实现表格信息的分页操作
//显示模态框
function display_form(homeworkname) {
    //模态框显示事件
    $('#homeworkName').text(homeworkname)
    $("#formWindow").modal('show')
}

function clock(){
    $('.countdown').text(minusOne($('.countdown').text()))
}

function minusOne(time){
    var date=Date.parse(time)
    date-=1000
    return Date(date)
}

function jumpto(url) {
    var token = window.sessionStorage.getItem('token')
    if (token != null) {
        $.ajax({
            async: true,
            type: "GET",
            url: url,
            dataType: "html",//返回整合HTML
            headers: {
                "token": token//此处放置请求到的用户token
            },
            mimeType: "multipart/form-data",
            contentType: false,
            cache: false,
            processData: false,
            success: function (data) {
                $("html").html(data);//刷新整个body页面的html
            },
            error: function () {
                var tk = 100;
                tk = tk + 100;
            }
        })
    }
    else {
        window.location.replace("admin/login")
    }
}

function studentList() {
    $('#homeworkName').text("hihi")
    $("#studentList").modal('show')

}

function cancelHomework(homeworkName){
    var token = window.sessionStorage.getItem('token');
    $.ajax({
        async: false,
        type: "POST",
        url: "/admin/cancel/"+homeworkName,
        headers: {
            "token": token//此处放置请求到的用户token
        },
        dataType: "JSON",
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        success: function (data) {
            swal("删除成功")
        },
        error: function () {
            swal("网络异常，请稍后重试")
        }
    });
    jumpto("/admin");
}

function submit_new_studentList() {
    var form = document.getElementById("upload");
    var formData = new FormData(form);
    var fileInput = $('#file').get(0).files[0];
    if (!fileInput) {
        return
    }
    var token = window.sessionStorage.getItem('token');
    //发送ajax请求
    $.ajax({
        async: false,
        type: "POST",
        url: "/admin/studentListFile",
        data: formData,
        headers: {
            "token": token//此处放置请求到的用户token
        },
        dataType: "JSON",
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        success: function (data) {
            swal("上传成功")
        },
        error: function () {
            swal("网络异常，请稍后重试")
        }
    })
    jumpto("/admin/studentList");
}

function login() {
    //模态框显示事件
    var form = document.getElementById("login_form");
    var formData = new FormData(form);
    $.ajax({
        async: false,
        type: "POST",
        url: "/admin/verify",
        data: formData,
        dataType: "JSON",
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        success: function (data) {
            if (data["result"]) {
                swal("登录成功");
                //window.location.replace("/admin");
                window.sessionStorage.setItem('token', data['token']);
                jumpto("/admin")
            }
            else {
                swal("密码有误")
            }
        },
        error: function () {
            swal("网络异常，请稍后重试")
        }
    })
}

//提交表格value
function submit_form() {
    var form = document.getElementById("upload");
    var formData = new FormData(form);
    var homeworkname = document.getElementById("homeworkName").textContent;
    formData.append("homeworkName", homeworkname)
    var name = formData.get("name")
    var num = formData.get("num")
    //用户名为2-5个汉字
    var name_reg = /^[\u4e00-\u9fa5]{2,5}$/
    //学号为8位数字
    var num_reg = /^\d{11}$/
    if (!name_reg.test(name)) {
        swal("用户名错误!")
        return
    }
    if (!num_reg.test(num)) {
        swal("学号错误!")
        return
    }
    var fileInput = $('#file').get(0).files[0];
    if (!fileInput) {
        swal("请选择上传文件！")
        return
    }
    //发送ajax请求
    $.ajax({
        async: false,
        type: "POST",
        url: "/upload",
        data: formData,
        dataType: "JSON",
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        success: function (data) {
            swal("上传成功")
        },
        error: function () {
            swal("网络异常，请稍后重试")
        }
    })
}

function publish(){
    var form = document.getElementById("upload");
    var formData = new FormData(form);
    var token = window.sessionStorage.getItem('token');
    $.ajax({
        async: false,
        type: "POST",
        headers: {
            "token": token//此处放置请求到的用户token
        },
        url: "/admin/newHomework",
        data: formData,
        dataType: "JSON",
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        success: function (data) {
            swal("发布成功")
        },
        error: function () {
            swal("网络异常，请稍后重试")
        }
    });
    jumpto("/admin");
}