define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form) {
    //读取选中的条目
    $.jstree.core.prototype.get_all_checked = function (full) {
        var obj = this.get_selected(), i, j;
        for (i = 0, j = obj.length; i < j; i++) {
            obj = obj.concat(this.get_node(obj[i]).parents);
        }
        obj = $.grep(obj, function (v, i, a) {
            return v != '#';
        });
        obj = obj.filter(function (itm, i, a) {
            return i == a.indexOf(itm);
        });
        return full ? $.map(obj, $.proxy(function (i) {
            return this.get_node(i);
        }, this)) : obj;
    };
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trainactive/index' + location.search,
                    add_url: 'trainactive/add',
                    edit_url: 'trainactive/edit',
                    del_url: 'trainactive/del',
                    multi_url: 'trainactive/multi',
                    import_url: 'trainactive/import',
                    table: 'train_active',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},

                        {field: 'score_sum', title: __('Score_sum'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'score_count', title: __('Score_count'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'score_times', title: __('Score_times'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'score_avg', title: __('Score_avg'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},

                        {field: 'logo_image', title: __('Logo_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'number', title: __('Number'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'version', title: __('Version'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'bianzhiren', title: __('Bianzhiren'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'pizhunren', title: __('Pizhunren'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'shenheren', title: __('Shenheren'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'kongzhiren', title: __('Kongzhiren'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'department', title: __('Department'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'wenjianstatus', title: __('Wenjianstatus'), operate: 'LIKE', formatter: Table.api.formatter.status},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
//                        {field: 'cate_id', title: __('Cate_id')},
                        {field: 'cate.name', title: __('Cate.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},

//                        {field: 'level_id', title: __('Level_id')},
                        {field: 'level.name', title: __('Level.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},

                        {field: 'fangshi', title: __('Fangshi'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'starttime', title: __('Starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'zhuchiren', title: __('Zhuchiren'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'zhujiangren', title: __('Zhujiangren'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
//                        {field: 'type_id', title: __('Type_id')},
                        {field: 'type.name', title: __('Type.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},

                        {field: 'info', title: __('Info'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'files', title: __('Files'), operate: false, formatter: Table.api.formatter.files},
                        {field: 'pingjia_image', title: __('Pingjia_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'pingjia_time', title: __('Pingjia_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'pingjia_uid', title: __('Pingjia_uid')},
                        {field: 'sign_endtime', title: __('Sign_endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'usercount', title: __('Usercount')},
                        {field: 'userauth_status', title: __('Userauth_status'), searchList: {"0":__('Userauth_status 0'),"1":__('Userauth_status 1')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons:[
                                {
                                    name:'selectuser',
                                    text:'选择用户',
                                    title:'选择用户',
                                    icon:'fa fa-exclamation-circle',
                                    classname:'btn btn-xs btn-info btn-dialog btn-selectuser',
                                    url:'trainactive/selectuser/id/{ids}?dialog=1',
                                    target:'_self',
                                },
                                {
                                    name:'qrcode',
                                    text:'二维码',
                                    title:'二维码',
                                    icon:'fa fa-exclamation-circle',
                                    classname:'btn btn-xs btn-info btn-dialog bt-qrcode',
                                    url:'trainactive/qrcode/id/{ids}?dialog=1',
                                    target:'_self',
                                },
                                {
                                    name:'showinfo',
                                    text:'打印',
                                    title:'打印',
                                    icon:'fa fa-exclamation-circle',
                                    classname:'btn btn-xs btn-info btn-addtabs',
                                    url:'trainactive/showinfo/id/{ids}',
                                    target:'_blank',
                                },
                                {
                                    name:'nograde',
                                    text:'未到',
                                    title:'未到',
                                    icon:'fa fa-exclamation-circle',
                                    classname:'btn btn-xs btn-info btn-dialog btn-nograde',
                                    url:'trainactive/nograde/id/{ids}',
                                    target:'_self',
                                }
                            ],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-selectuser").data("area", ['80%','80%']);
                    $(".bt-qrcode").data("area", ['50%','90%']);
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'trainactive/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'trainactive/restore',
                                    refresh: true
                                },
                               /* {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'trainactive/destroy',
                                    refresh: true
                                }*/
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        selectuser: function () {
            Controller.apisu.bindevent();
        },
        qrcode: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        showinfo: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        apisu: {

            bindevent: function () {
                Form.api.bindevent($("form[role=form]"), null, null, function () {
                    if ($("#treeview").length > 0) {
                        var r = $("#treeview").jstree("get_all_checked");
                        $("input[name='row[user_rule]']").val(r.join(','));
                    }
                    return true;
                });
                //渲染权限节点树
                //销毁已有的节点树
                $("#treeview").jstree("destroy");
                Controller.apisu.rendertree(nodeData);
                //全选和展开
                $(document).on("click", "#checkall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
                });
                $(document).on("click", "#expandall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
                });
                $("select[name='row[pid]']").trigger("change");
            },
            rendertree: function (content) {
                $("#treeview")
                    .on('redraw.jstree', function (e) {
                        $(".layer-footer").attr("domrefresh", Math.random());
                    })
                    .jstree({
                        "themes": {"stripes": true},
                        "checkbox": {
                            "keep_selected_style": false,
                        },
                        "types": {
                            "root": {
                                "icon": "fa fa-folder-open",
                            },
                            "menu": {
                                "icon": "fa fa-folder-open",
                            },
                            "file": {
                                "icon": "fa fa-file-o",
                            }
                        },
                        "plugins": ["checkbox", "types"],
                        "core": {
                            'check_callback': true,
                            "data": content
                        }
                    });
            }
        }
    };
    return Controller;
});
//打印切换
$('#left').click(function(){
    $('#left_table').show();
    $('#right_table').hide();
})
$('#right').click(function(){
    $('#left_table').hide();
    $('#right_table').show();
})
//打印下载
function jump(url){
    console.log(url);
    window.location.href = url;
}
//打印ctrl+P
function printPage() {
    window.print();
}