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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            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: 'status',
                            title: __('Status'),
                            searchList: {"30": __('Status 30')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [{
                                name: 'detail',
                                text: '详情',
                                title: '详情',
                                classname: 'btn btn-xs btn-primary btn-info',
                                icon: 'fa fa-list',
                                url: function (data) {
                                    return 'steps?task_id=' + data.id;
                                },
                                callback: function (data) {
                                    Layer.alert(data.title);
                                }
                            }, {
                                name: 'edit',
                                text: '编辑',
                                title: '编辑',
                                classname: 'btn btn-xs btn-success btn-editone',
                                icon: 'fa fa-pencil',
                                url: 'task/task/edit',
                                callback: function (data) {
                                    Layer.alert(data.title);
                                }
                            }, {
                                name: 'del',
                                text: '删除',
                                title: '删除',
                                classname: 'btn btn-xs btn-danger btn-delone',
                                icon: 'fa fa-trash',
                                url: 'task/task/del',
                                callback: function (data) {
                                    Layer.alert(data.title);
                                }
                            }]
                        }
                    ]
                ]
            });

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