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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'image', title: __('Image'), operate: false, events:{
                                'click .img-center': function (e, value, row, index) {
                                    if(row.showtype == 'image'){

                                        var data = [];
                                        value = value === null ? '' : value.toString();
                                        var arr = value != '' ? value.split(",") : [];
                                        var url;
                                        $.each(arr, function (index, value) {
                                            url = Fast.api.cdnurl(value);
                                            data.push({
                                                src: url,
                                                thumb: url.match(/^(\/|data:image\\)/) ? url : url + Config.upload.thumbstyle
                                            });
                                        });
                                        Layer.photos({
                                            photos: {
                                                "start": $(this).parent().index(),
                                                "data": data
                                            },
                                            anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                                        });

                                    }else{

                                    }


                                },
                            },  formatter:function (value, row, index) {
                            if(row.showtype == 'image'){
                                return  Table.api.formatter.images.call(this, value, row, index);
                            }else{
                                return Table.api.formatter.files.call(this, value, row, index);
                            }

                            }},
                        {field: 'type', title: __('Type'), searchList: {"in":__('Type in'),"out":__('Type out')}, formatter: Table.api.formatter.normal},
                        {field: 'showtype', title: __('Showtype'), searchList: showtypeListJson, formatter: Table.api.formatter.normal},
                        {field: 'url', title: __('Url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {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}
                    ]
                ]
            });

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'style/home_images/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            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: 'style/home_images/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'style/home_images/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;
});
