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: '/organization/getList',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 10,
        id: "listTable",
        cols: [[
            {type: "checkbox", fixed: "left", width: 50},
            {field: 'orgName', title: '组织名称', minWidth: 100, align: "center"},
            {field: 'orgType', title: '组织类型', minWidth: 100, align: "center",templet:function(d){
                    if(d.orgType == "0"){
                        return "行业性";
                    }else if(d.orgType == "1"){
                        return "公益性";
                    }else if(d.orgType == "2"){
                        return "非盈利";
                    }else if(d.orgType == "3"){
                        return "国际性";
                    }else if(d.orgType == "9"){
                        return "国家组织";
                    }else{
                        return "保密";
                    }
                }},
            {field: 'orgGrade', title: '组织级别', minWidth: 100, align: "center",templet:function(d){
                    if(d.orgGrade == "0"){
                        return "系统级";
                    }else if(d.orgGrade == "1"){
                        return "一级组织";
                    }else if(d.orgGrade == "2"){
                        return "二级组织";
                    }else if(d.orgGrade == "3"){
                        return "三级组织";
                    }else if(d.orgGrade == "4"){
                        return "四级组织";
                    }else{
                        return "普通组织";
                    }
                }},
            {field: 'creator', title: '创建人', minWidth: 100, align: "center"},
            {field: 'createTime', title: '创建时间', minWidth: 100, align: "center",templet:function (d) {
                    return showTime(d.createTime);
                }},
            {field: 'updateTime', title: '更新时间', align: 'center', minWidth: 150,templet:function (d) {
                    return showTime(d.updateTime);
            }},
            {title: '操作', minWidth: 200, templet: '#listBar', fixed: "right", align: "center"}
        ]]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        if ($(".searchVal").val() != '') {
            table.reload("listTable", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
        } else {
            layer.msg("请输入搜索的内容");
        }
    });
    $(".add_btn").click(function () {
        add();
    })
    //添加
    function add() {
        var index = layui.layer.open({
            title: "添加组织",
            type: 2,
            content: "/organization/update",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                setTimeout(function () {
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        })
        layui.layer.full(index);
        window.sessionStorage.setItem("index", index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize", function () {
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }

    //批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('listTable'),
            data = checkStatus.data,
            ids = [];
        if (data.length > 0) {
            for (var i in data) {
                ids.push(data[i].id);
            }
            layer.confirm('确定删除选中的条目？', {icon: 3, title: '提示信息'}, function (index) {
                $.ajax({
                    url: "/organization/deleteByIds/" + ids,
                    type: "POST",
                    dataType: "json",
                    async: false,
                    contentType: 'application/json;charset=UTF-8',
                    success: function (res) {
                        tableIns.reload();
                        layer.close(index);
                    },
                    error: function (e) {
                        tableIns.reload();
                        layer.close(index);
                    }
                })
            })
        } else {
            layer.msg("请选择需要删除的条目");
        }
    })

    //列表操作
    table.on('tool(userList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;

        if (layEvent === 'edit') { //编辑
            addUser(data);
        } else if (layEvent === 'usable') { //启用禁用
            var _this = $(this),
                usableText = "是否确定禁用此用户？",
                btnText = "已禁用";
            if (_this.text() == "已禁用") {
                usableText = "是否确定启用此用户？",
                    btnText = "已启用";
            }
            layer.confirm(usableText, {
                icon: 3,
                title: '系统提示',
                cancel: function (index) {
                    layer.close(index);
                }
            }, function (index) {
                _this.text(btnText);
                layer.close(index);
            }, function (index) {
                layer.close(index);
            });
        } else if (layEvent === 'del') { //删除
            layer.confirm('确定删除？', {icon: 3, title: '提示信息'}, function (index) {
                // $.get("删除文章接口",{
                //     newsId : data.newsId  //将需要删除的newsId作为参数传入
                // },function(data){
                tableIns.reload();
                layer.close(index);
                // })
            });
        }
    });

    $(".edit_btn").click(function () {
        edit();
    })
    //edit
    function edit() {
        var checkStatus = table.checkStatus('listTable'),
            data = checkStatus.data,
            ids = [];
        if (data.length > 0) {
            if(data.length==1){
                for (var i in data) {
                    ids.push(data[i].id);
                }
                var index = layui.layer.open({
                    title: "编辑权限",
                    type: 2,
                    content: "/permission/edit/" + ids,
                    success: function (layero, index) {
                        var body = layui.layer.getChildFrame('body', index);
                        setTimeout(function () {
                            layui.layer.tips('点击此处返回信息列表', '.layui-layer-setwin .layui-layer-close', {
                                tips: 3
                            });
                        }, 500)
                    }
                })
                layui.layer.full(index);
                window.sessionStorage.setItem("index", index);
                //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
                $(window).on("resize", function () {
                    layui.layer.full(window.sessionStorage.getItem("index"));
                })
            }else{
                layer.msg("只能同时修改一条信息!");
            }
        } 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();
    })
})
