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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},

                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'is_deleted', title: __('Is_deleted'), searchList: {"0":__('Is_deleted 0'),"1":__('Is_deleted 1')}, formatter: Table.api.formatter.normal},
                        {field: 'sort', title: __('Sort')},
                        {field: 'status', title: __('状态'), searchList: {"0":__('启用'),"1":__('禁用')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [{
                                name: 'click',
                                text:'启用',
                                title: __('启用'),
                                classname: 'btn btn-xs btn-info btn-click',
                                // icon: 'fa fa-leaf',
                                click: function (data,row) {
                                    var temp=table.bootstrapTable('getSelections');
                                    var id = row.id;
                                    console.log(row);
                                    return $.ajax({
                                        // url:'http://up.kuman.cn/settle/cancel',
                                        url:'books.books_series/update_status?ids='+id,
                                        type:'post',
                                        // dataType:'json',
                                        data:{status:0},
                                        success:function(data){
                                            if(data.code === 1){
                                                layer.msg('操作成功');
                                                window.location.reload();
                                            }else{
                                                layer.msg(data.msg);
                                            }
                                        }
                                    });
                                    // Layer.alert("点击按钮执行的事件");
                                },
                                visible: function (row) {
                                    console.log(row)
                                    if (row.status === "1"){
                                        return true;
                                    }
                                    return false;
                                },
                                refresh:true,
                            },
                                {
                                    name: 'click',
                                    text:'禁用',
                                    title: __('禁用'),
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    // icon: 'fa fa-leaf',
                                    click: function (data,row) {
                                        var temp=table.bootstrapTable('getSelections');
                                        var id = row.id;
                                        console.log(row);
                                        return $.ajax({
                                            // url:'http://up.kuman.cn/settle/cancel',
                                            url:'books.books_series/update_status?ids='+id,
                                            type:'post',
                                            // dataType:'json',
                                            data:{status:1},
                                            success:function(data){
                                                if(data.code === 1){
                                                    layer.msg('操作成功');
                                                    window.location.reload();
                                                }else{
                                                    layer.alert(data.msg);
                                                }
                                            }
                                        });
                                        // Layer.alert("点击按钮执行的事件");
                                    },
                                    visible: function (row) {
                                        // console.log(row)
                                        if (row.status === "0"){
                                            return true;
                                        }
                                        return false;
                                    },
                                    refresh:true,
                                },
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            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;
});
