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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'theme_id', title: __('Theme_id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'usereditor', title: __('Usereditor')},
                        {field: 'username', title: __('Username')},
                        {field: 'title', title: __('Title')},
                        {field: 'images', title: __('Images'), formatter: Table.api.formatter.images},
                        {field: 'label_name', title: __('Label_name')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'selectnum', title: __('Selectnum')},
                        {field: 'longtime', title: __('Longtime')},
                        {field: 'smalldesc', title: __('Smalldesc')},
                        {
                            field: 'operate',
                            width: "120px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'up',
                                    title: __('发布'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'video/statusS/status/1',
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg + ",返回数据:" + JSON.stringify(data));
                                        location.href= location.href;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'down',
                                    title: __('取消发布'),
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'video/statusS/status/0',
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg + ",返回数据:" + JSON.stringify(data));
                                        //如果需要阻止成功提示,则必须使用return false;
                                        //return false;
                                        location.href= location.href;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'down',
                                    title: __('引用'),
                                    classname: 'btn btn-xs btn-danger btn-magic btn-addtabs',
                                    icon: 'fa fa-edit',
                                    url: 'quote/index?controller='+Config.controller,

                                }
                            ],

                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                search:false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-up", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                $.ajax({
                    "url":"video/statusS/status/1/ids/"+ids,
                    "type":"get",
                    success:function ($res) {
                        location.href= location.href;
                    },
                    error:function(){
                        location.href= location.href;
                    }
                })
            });
            //取消发布
            $(document).on("click", ".btn-down", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                $.ajax({
                    "url":"video/statusS/status/0/ids/"+ids,
                    "type":"get",
                    success:function ($res) {
                        location.href= location.href;
                    },
                    error:function(){
                        location.href= location.href;
                    }
                })
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});