layui.use(['form', 'layer', 'table', 'laytpl'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //列表
    var tableIns = table.render({
        elem: '#list',
        url: '/generator/list',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 15,
        id: "listTable",
        cols: [[
            {type: "checkbox", fixed: "left", width: 50},
            {field: 'tableName', title: '数据表名', minWidth: 100, align: "center"},
            {field: 'engine', title: '数据库引擎', minWidth: 100, align: "center"},
            {field: 'tableComment', title: '数据库备注', minWidth: 100, align: "center"},
            {field: 'createTime', title: '创建时间', minWidth: 100, align: "center",templet:function (d) {
                    return showTime(d.createTime);
                }}
        ]]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        table.reload("listTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                tableName: $("#tableName").val(),  //搜索的关键字
            }
        })
    });

    //批量删除
    $(".generate_btn").click(function () {
        generate();
    })
    function generate(){
        var checkStatus = table.checkStatus('listTable'),
            data = checkStatus.data,
            ids = [],
            tableName ="";
        if (data.length > 0) {
            for (var i in data) {
                ids.push(data[i].id);
                tableName += data[i].tableName + ",";
            }
            layer.confirm('确定生成代码？', {icon: 3, title: '提示信息'}, function (index) {
                location.href = "/generator/generator?tableName=" + tableName;
                tableIns.reload();
                layer.close(index);
                /*$.ajax({
                    url: "/generator",
                    data:{ "tableName":tableName },
                    type: "POST",
                    async: true,
                    success: function (res) {
                        tableIns.reload();
                        layer.close(index);
                    },
                    error: function (e) {
                        tableIns.reload();
                        layer.close(index);
                    }
                })*/
            })
        } else {
            layer.msg("请选择需要生成的数据表!");
        }
    }



    //时间转换函数
    function showTime(tempDate)
    {
        var d = new Date(tempDate);
        var year = d.getFullYear();
        var month = d.getMonth();
        month++;
        var day = d.getDate();
        var hours = d.getHours();

        var minutes = d.getMinutes();
        var seconds = d.getSeconds();
        month = month<10 ? "0"+month:month;
        day = day<10 ? "0"+day:day;
        hours = hours<10 ? "0"+hours:hours;
        minutes = minutes<10 ? "0"+minutes:minutes;
        seconds = seconds<10 ? "0"+seconds:seconds;


        var time = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
        return time;
    }
    $(".refresh").click(function () {
        location.reload();
    })
})
