define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'jdgw/article/index' + location.search,
                    add_url: 'jdgw/article/add',
                    edit_url: 'jdgw/article/edit',
                    del_url: 'jdgw/article/del',
                    multi_url: 'jdgw/article/multi',
                    import_url: 'jdgw/article/import',
                    table: 'jdgw_article',
                }
            });

            var table = $("#table");

            $(".btn-selected").click(function(){
                var arr = table.bootstrapTable('getAllSelections');
                if(arr.length == 0){
                    Toastr.error('请选择要推送的记录！');
                    return;
                }
                var ids = [];
                for (var i=0;i<arr.length;i++){ 
                    ids.push( {id:arr[i].id, diyname:arr[i].nav.diyname})
                } 
                $.ajax({
                    type: 'POST',
                    url: 'jdgw/article/bdpush',
                    data: {
                        ids: ids, 
                        type: $("#bd_type").val(),
                        action: $("#bd_action").val(), 
                    },
                    success: function(res) {               
                        console.log(res)
                        if(res.code == 1){
                            table.bootstrapTable('refresh', {});
                            Toastr.success(res.msg);
                        }else{
                            Toastr.error(res.msg);
                        }
                }});
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'topswitch desc, weigh desc, id desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'nav_id',
                            title: __('Nav_id'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="jdgw/article/selectSearch" data-field="title"',
                            operate: 'in',
                            formatter: Table.api.formatter.search
                        },
                        {field: 'nav.title', title: '导航', operate: false},
                        {field: 'typedata', title: __('Typedata'), searchList: {"article":__('Typedata article'),"images":__('Typedata images')}, formatter: Table.api.formatter.normal},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        // {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'seotitle', title: __('Seotitle'), operate: 'LIKE'},
                        // {field: 'keywords', title: __('Keywords'), operate: 'LIKE'},
                        // {field: 'description', title: __('Description'), operate: 'LIKE'},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'topswitch', title: __('Topswitch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'bddata', title: __('Bddata'), searchList: {"0":__('Bddata 0'),"1":__('Bddata 1')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'jdgw/article/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: '130px',
                            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: 'jdgw/article/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'jdgw/article/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});