//版本记录
//由张明2020 / 09 / 01修正修改、详情获取ID和NAME的方法
/// <reference path="utils.js" />
//根据用户屏幕高度设置表格加载行数
var divsearch = 1;//对筛选区域进行显示隐藏
var jspage = 10;
var ispage = "yes";
var ispageok = true;
if (ispage == "no") {
    ispageok = false;
}
////根据屏幕高度自动判断加载的页数
//if ($(window).height() >= 690) {
//    jspage = 15;
//    //document.write("<style>.layui-table td{padding: 2px 0!important;}</style>");
//}
//if ($(window).height() >= 900) {
//    jspage = 20;
//}
//屏幕高度设置结束

layui.config({
    base: "/webconfig/layuiJs/"
}).define(['jquery', 'table', 'basic', 'utils'], function (exports) {
    var $ = layui.jquery;
    var table = layui.table;
    var basic = layui.basic;
    //列表页
    var mainTable = function () {
        var page = 0;
        var rows = 0;
        //初始化表格
        var init = function () {
            page = 1;
            rows = 15;
            table.init('list',
                {
                    elem: '#list',
                    id: 'list',
                    toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                    ,
                    defaultToolbar: [
                        'filter', 'exports', 'print',
                        { title: '筛选搜索开关', layEvent: 'LAYTABLE_TIPS', icon: 'layui-icon-search' }
                    ],
                    title: pagename
                    //,page: ispageok
                    //,limit: jspage
                    ,
                    even: true //开启隔行背景
                    ,
                    text: {
                        none: nodata //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                    }
                });
        };

        //主列表加载，可反复调用进行刷新
        var load = function (pageA, rowsA) {
            if (pageA) page= pageA;
            if (rowsA) rows = rowsA;
            AjaxGet(listurl,
                {
                    page: page,
                    limit: rows
                },
                function (result) {
                    if (!result.iserror) {
                        table.reload("list",
                            {
                                limit: rows,
                                data: result.data.list
                            });
                        layui.laypage.render({
                            elem: 'pager',
                            count: result.data.count,
                            curr: page,
                            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                            limit: rows,
                            limits: [10, 15, 20, 30, 50, 100, 500],
                            jump: function (pager, first) {
                                if (!first) {
                                    mainTable.reload(pager.curr, pager.limit);
                                }
                            }
                        });
                    }
                });
        }
        //load(1, 10);
        return {
            init: init,
            reload: load
        };
    }();

    //配置搜索筛选二次加载列表
    function loadtable() {
        mainTable.reload();
    }

    //监听头部工具事件
    table.on('toolbar(list)',
        function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;

            var dataok = JSON.stringify(checkStatus.data);
            var tsstr = "";
            
            switch (obj.event) {
                case 'btn_refresh':
                    tsstr = "刷新";
                    break;
                case 'btn_add':
                    tsstr = "添加";
                    //editDlg.add();
                    //alert("添加");
                    break;
                case 'btn_edit':
                    tsstr = "编辑";
                    break;
                case 'btn_del':
                    tsstr = "删除";
                    break;
                case 'btn_view':
                    tsstr = "查看";
                    break;
                case 'btn_examine':
                    tsstr = "审核";
                    break;
                case 'btn_chehui':
                    tsstr = "撤回";
                    break;
                case 'btn_export':
                    tsstr = "导出";
                    break;
                case 'btn_Download':
                    tsstr = "下载";
                    break;
            }
            if ((data.length == 0 && obj.event == "btn_examine") ||
                (data.length == 0 && obj.event == "btn_del") ||
                (data.length == 0 && obj.event == "btn_view") ||
                (data.length == 0 && obj.event == "btn_edit") ||
                (data.length == 0 && obj.event == "btn_chehui")) {
                layer.msg(tsstr + '请先选择一条数据！');
                //alert("请选择一条数据");
            } else {
                switch (obj.event) {
                    case 'btn_refresh':
                        location.reload();
                        break;
                    case 'LAYTABLE_TIPS':
                        //layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        if (divsearch == 1) {
                            $("#div_search").css("display", "none");
                            $("#btn_search").css("display", "none");
                            divsearch = 0;
                        } else {
                            $("#div_search").css("display", "");
                            $("#btn_search").css("display", "");
                            divsearch = 1;
                        }
                        break;
                    case 'btn_add':
                        parent.layer.open({
                            type: 2,
                            title: '' + tsstr + pagename + '',
                            shadeClose: true,
                            maxmin: true,
                            shade: 0.8,
                            area: ['95%', '98%'],
                            content: '' + addurl + '',
                            btn: '关闭返回',
                            yes: function (index, layero) {
                                parent.layer.close(index);
                                layer.msg('添加结束！');

                                //    var iframeWin = thiswin[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                                //    iframeWin.submit();
                                //},
                                //btn_2: function (index, layero) {
                                //    var iframeWin = thiswin[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：重置;
                                //    iframeWin.reset();
                                //},
                                //btn_3: function (index) {
                                //    parent.layer.close(index);
                                //    layer.msg('添加结束！');
                                //    loadtable();
                                //    //mainList();
                            },
                            cancel: function (index) {
                                parent.layer.close(index);
                                mainTable.reload();
                            }
                        });
                        break;
                    case 'btn_del':
                        //获取需删除数据的id:
                        var id = "";
                        for (i = 0; i < data.length; i++) {
                            if (i == 0) {
                                id = data[i].Guid;
                            } else {
                                id += "," + data[i].Guid;
                            }
                        }


                        //调用删除api
                        layer.confirm('真的删除这【' + data.length + '】条数据么?',
                            function (index) {
                                AjaxPost(
                                    delurl,
                                    { Guid: id },
                                    function (res) {
                                        if (!res.iserror) {
                                            layer.msg(res.message);
                                            loadtable();
                                        }
                                    }
                                );
                            });
                        break;
                    case 'btn_edit':
                        var Guid = data[0].Guid;
                        console.log("000-" + Guid);
                        var id = "";
                        //获取ID值
                        for (i = 0; i < data.length; i++) {
                            if (i == 0) {

                                var d = 0;
                                var iid = data[i];
                                for (e in iid) {
                                    if (d == 0) id = iid[e];//JSON数据，请将编号放在第一个位置
                                    d++;
                                }

                            }
                            else {
                                var d = 0;
                                var iid = data[i];
                                for (e in iid) {
                                    if (d == 0) id += "," + iid[e];
                                    d++;
                                }
                            }

                        }
                        //获取name值
                        var name = "";
                        var iname = data[0];//JSON数据，请将名字放在第二个位置
                        var c = 0;
                        for (i in iname) {
                            if (c == 1) name = iname[i];
                            c++;
                        }
                        if (data.length > 1) {
                            layer.msg('最多只能选择1条数据编辑！');
                        }
                        else {

                            parent.layer.open({//+ ' ' + $.trim(name)
                                type: 2, title: '编辑' + pagename , shadeClose: true, maxmin: true, shade: 0.8, area: ['95%', '98%'], content: editurl + editparameter + Guid,
                                btn: '关闭返回',
                                yes: function (index, layero) {
                                    parent.layer.close(index);
                                    layer.msg('关闭返回，结束编辑！');
                                    loadtable();
                                    //    var iframeWin = thiswin[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                                    //    iframeWin.submit();
                                    //},
                                    //btn_2: function (index, layero) {
                                    //    var iframeWin = thiswin[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：重置;
                                    //    iframeWin.reset();
                                    //},
                                    //btn_3: function (index) {
                                    //    parent.layer.close(index);
                                    //    layer.msg('关闭返回，结束编辑！');
                                    //    //loadtable();
                                    //    //mainList();
                                },
                                cancel: function (index) {
                                    parent.layer.close(index);
                                    layer.msg('关闭返回，结束编辑！');
                                    loadtable();
                                    //mainList();
                                }
                            });
                        }
                        //layer.msg('编辑完成');
                        break;

                    case 'btn_view':
                        var id  = data[0].Guid;
                     
     

                            parent.layer.open({
                                type: 2, title: '查看详情', shadeClose: true, maxmin: true, shade: 0.8, area: ['95%', '98%'], content: viewurl + id, btn: '关闭返回', yes: function (index, layero) { parent.layer.close(index); }, cancel: function (index, layero) { }
                            });
                        
                        //layer.msg('编辑完成');
                        break;
                    case "btn_importStudent":
                        $("#btn_importUpload").click();
                        break;
                    case 'btn_examine':
                        if (data.length > 1) {
                            //获取需批量审核数据的Guid:

                            var id = "";
                            //获取ID值
                            for (i = 0; i < data.length; i++) {
                                if (i == 0) {

                                    var d = 0;
                                    var iid = data[i];
                                    for (e in iid) {
                                        if (d == 0) id = iid[e];//JSON数据，请将编号放在第一个位置
                                        d++;
                                    }

                                }
                                else {
                                    var d = 0;
                                    var iid = data[i];
                                    for (e in iid) {
                                        if (d == 0) id += "," + iid[e];
                                        d++;
                                    }
                                }

                            }
                            //获取name值
                            var name = "";
                            var iname = data[0];//JSON数据，请将名字放在第二个位置
                            var c = 0;
                            for (i in iname) {
                                if (c == 1) name = iname[i];
                                c++;
                            }
                            layer.confirm('您共选择了【' + data.length + '】条数据，请问是批量【通过】还是【不通过】？',
                                {
                                    btn: ['审核通过', '审核不通过'] //按钮
                                    ,
                                    cancel: function () {
                                        layer.closeAll('loading');
                                    }
                                },
                                function () {
                                    AjaxPost(
                                        examineyesurl,
                                        { "id": id },
                                        function (res) {
                                            if (res.code == "0") {
                                                layer.msg('成功审核【通过】' + res.count + '条记录!');
                                                loadtable();
                                            } else {
                                                layer.msg('<font color=yellow>审核失败!</font>');
                                            }
                                            layer.closeAll('loading');
                                        });
                                    //执行批量通过
                                },
                                function () {
                                    AjaxPost(
                                        examinenourl,
                                        { "id": id },
                                        function (res) {
                                            if (res.code == "0") {
                                                layer.msg('成功审核【不通过】' + res.count + '条记录!');
                                                loadtable();
                                            } else {
                                                layer.msg('<font color=yellow>审核失败!</font>');
                                            }
                                            layer.closeAll('loading');
                                        }
                                    );
                                });
                        } else {
                            //单条审核
                            var id = data[0].id;
                            if (id == "undefined" || id == "") {
                                id = data[0].ROWNUM;

                            }
                            var name = data[0].name;
                            if (name == "" || name == "undefined") name = data[0][1];
                            name = "";
                            if (name == "" || name == "undefined") name = "";
                            parent.layer.open({
                                type: 2,
                                title: '审核' + pagename + '【' + id + '】' + name,
                                shadeClose: true,
                                maxmin: true,
                                shade: 0.8,
                                area: ['95%', '98%'],
                                content: examineurl + id,
                                btn: '关闭返回',
                                yes: function (index, layero) { parent.layer.close(index); },
                                cancel: function (index, layero) { }
                            });
                        }
                        break;
                    case 'btn_export':
                        //获取导出数据的id:
                        var id = "";
                        if (data.length > 0) { 
                            for (i = 0; i < data.length; i++) {
                                if (i == 0) {
                                    id = data[i].Guid;
                                } else {
                                    id += "," + data[i].Guid;
                                }
                            }
                        } else {
                            layer.msg('请您选择需要导出的数据！');
                            return false;
                        }
                        AjaxPost(
                            basic.$Basicapi_url + exporturl,
                            { "Guid": id},
                            function (res) {
                                if (!res.iserror) {
                                    console.log(res);
                                    $("#Stu").prop("href", res.message);
                                    document.getElementById("Stu").click();
                                }
                                //form.render();
                            }
                        );
                        break;
                    case 'btn_nationalDataExport':
                        //获取导出数据的id:
                        var id = "";
                        if (data.length > 0) {
                            for (i = 0; i < data.length; i++) {
                                if (i == 0) {
                                    id = data[i].Guid;
                                } else {
                                    id += "," + data[i].Guid;
                                }
                            }
                        } else {
                            layer.msg('请您选择需要导出的数据！');
                            return false;
                        }

                        AjaxPost(
                            basic.$Basicapi_url + nationalDataurl,
                            { "Guid": id },
                            function (res) {
                                if (!res.iserror) {
                                    console.log(res);
                                    $("#Stu").prop("href", res.message);
                                    document.getElementById("Stu").click();
                                }                                
                            }
                        );
                        break;
                    case 'btn_Download':
                        AjaxPost(
                            basic.$Basicapi_url + Downloadurl,
                            data.field,
                            function (res) {
                                if (!res.iserror) {
                                    console.log(res);
                                    $("#Stu1").prop("href", res.message);
                                    document.getElementById("Stu1").click();
                                }
                                //form.render();
                            }
                        );
                        break;

                };
            }

        });



    //监听行单击事件
    table.on('row(list)', function (obj) {
        //console.log(obj.tr) //得到当前行元素对象

        //console.log(obj.data) //得到当前行数据
        //obj.del(); //删除当前行
        //obj.update(fields) //修改当前行数据
    });

    //监听行双击事件
    table.on('rowDouble(list)', function (obj) {
        //obj 同上
    });

    //监听行工具事件
    table.on('tool(list)', function (obj) {
        var data = obj.data;
        var guid = data.Guid;
        var StuName = data.StuName;
        var id = "";
        //console.log(data);
        //获取ID值
        var d = 0;
        var iid = data;
        for (e in iid) {
            if (d == 0) id = iid[e];//JSON数据，请将编号放在第一个位置
            d++;
        }


        //获取name值
        var name = "";
        var iname = data;//JSON数据，请将名字放在第二个位置
        var c = 0;
        for (i in iname) {
            if (c == 1) name = iname[i];
            c++;
        }
        if (obj.event === 'btn_del') {
            layer.confirm('确定删除么?',
                function (index) {
                    AjaxPost(
                        delurl,
                        { guid: guid },
                        function (res) {
                            if (!res.iserror) {
                                layer.msg('删除成功!');
                                obj.del();
                            }
                        }
                    );
                });
        } else if (obj.event === 'btn_edit') {

            parent.layer.open({
                type: 2,
                title: '编辑' + pagename + '【' + id + '' + '】' + name,
                shadeClose: true,
                shade: 0.8,
                area: ['95%', '98%'],
                content: editurl + editparameter + guid,
                btn: '关闭返回',
                yes: function (index, layero) {
                    parent.layer.close(index);
                    loadtable();
                },
                cancel: function (index, layero) { loadtable(); }
            });
        } else if (obj.event === 'btn_view') {
            parent.layer.open({
                type: 2,
                title: '查看详情',
                maxmin: true,
                shadeClose: true,
                shade: 0.8,
                area: ['95%', '98%'],
                content: viewurl + guid,
                btn: '关闭返回',
                yes: function (index, layero) { parent.layer.close(index); },
                cancel: function (index, layero) { }
            });

        } else if (obj.event === 'btn_examine') {
            parent.layer.open({
                type: 2,
                title: '审核' + pagename + '【' + id + '】' + name,
                maxmin: true,
                shadeClose: true,
                shade: 0.8,
                area: ['95%', '98%'],
                content: examineurl + guid,
                btn: '关闭返回',
                yes: function (index, layero) { parent.layer.close(index); },
                cancel: function (index, layero) { }
            });

        } else if (obj.event === 'btn_tuixue') {

            sessionStorage.setItem("Y_StuName", StuName);
            sessionStorage.setItem("Y_IdCarld", data.IdCarld);
            sessionStorage.setItem("Y_ClassName", data.ClassName);
            parent.layer.open({
                type: 2,
                title: '退学页面',
                maxmin: true,
                shadeClose: true,
                shade: 0.8,
                area: ['60%', '60%'],
                content: '/WebApp/School Roll/Tuixue.html?Guid=' + guid,
                btn: '关闭返回',
                yes: function (index, layero) { parent.layer.close(index); },
                cancel: function (index, layero) { }
            });
           
        } else if (obj.event === 'btn_chexiaotuixue') {
            layer.confirm("确认要撤销退学么", { title: "撤销确认" }, function (index) {
                AjaxGet(basic.$api_url + "/api/admin/Student/UpdateStudentStatus?StuGuid=" + data.Guid + "&Status=1", null, function (result) {
                    layer.msg(result.message);
                    obj.del();
                });
            });

        }

    });
    //导航按钮操作演示
    layui.$('#lay_refresh').on('click', function () {
        //刷新
        location.reload();
    });


    ////监听页面主按钮操作 end
    //$(document).tooltip({
    //    items: "img, [titop], [title]", content: function () {
    //        var element = $(this);
    //        if (element.is("[titop]")) {
    //            var text = element.attr("alt");
    //            return "<img class='map'  src='" + text + "'>";
    //        }
    //        if (element.is("[txttop]")) {
    //            return element.attr("title");
    //        }
    //    }
    //});

    exports('layui_list', mainTable);
});