define(['table', 'upload','form'], function (Table,Upload,Form) {
    var Card = {
        //Do setup work hereAction
        index: function () {
            Table.api.init({
                extend: {
                    index_url: 'card/select',
                    add_url: 'card/insert',
                    edit_url: 'card/update',
                    del_url: 'card/delete',
                    multi_url: 'card/multi',
                    dragsort_url: 'card/weigh',
                    table: 'card',
                }
            });
            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id',
                            title: 'ID',
                            filter: "number",
                            sortable: true    // 是否排序
                        },
                        {
                            title: "类型",
                            field: "type",
                            visible: false,
                        },
                        {
                            title: "标题",
                            field: "title",
                        },
                        {
                            title: "总数",
                            field: "total",
                        },
                        {
                            title: "已经使用",
                            field: "used",
                        },
                        {
                            title: "有效期",
                            field: "expires",
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            title: "创建时间",
                            field: "created_at",
                            visible: false,
                        },
                        {
                            title: "更新时间",
                            field: "updated_at",
                            visible: false,
                        },
                        {
                            title: "状态",
                            field: "status",
                            formatter: Table.api.formatter.switch
                        },
                        {
                            field: 'operate', 
                            title: '操作', 
                            formatter:Table.api.formatter.operate,
                            table: table, events: Table.api.events.operate, 
                            // formatter: function(v,row,index){
                            //     return '<a href="'+'card/export" class="btn btn-warning btn-xs">导出</a>&nbsp;'+Table.api.formatter.operate.call(this,v,row,index)
                            // },
                            buttons:[
                                {
                                    name: 'detail',
                                    text: '详情',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-dialog',
                                    url: 'card/detail'
                                },
                                {
                                    name: 'export',
                                    text: '导出',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-warning btn-xs',
                                    url: 'card/export',
                                    extend:' target="_blank"'
                                }
                            ]
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        update:function(){
            var form = $('form');
            Form.api.bindevent(form)
        },
        insert:function(){
            var form = $('form');
            Form.api.bindevent(form)
        },
        detail: function () {
            Table.api.init({
                extend: {
                    index_url: 'cdkey/select',
                    del_url: 'cdkey/delete',
                    multi_url: 'cdkey/multi',
                    table: 'cdkey',
                }
            });

            window.filterData['category_id'] = {
                'symbol'    :   '=',
                'value1'    :   Fast.api.query('ids'),
                'value2'    :   ''
            };
            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pagination: true,
                commonSearch: false,
                search: false,
                exportDataType:'all',
                exportTypes: ['json', 'csv'],
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id',
                            title: 'ID',
                            filter: "number",
                            sortable: true,    // 是否排序
                            visible: false
                        },
                        {
                            title: "类型",
                            field: "type",
                            visible: false,
                        },
                        {
                            title: "卡号",
                            field: "account",
                            filter: "string",
                        },
                        {
                            title: "密码",
                            field: "password",
                            visible: false,
                        },
                        {
                            title: "金额",
                            field: "days",
                            filter: "number",
                        },
                        {
                            title: "有效期",
                            field: "expires",
                            formatter: Table.api.formatter.datetime,
                            visible: false,
                        },
                        {
                            title: "已经使用",
                            field: "is_used",
                            formatter: Table.api.formatter.switch
                        },
                        {
                            title: "使用时间",
                            field: "use_time",
                            visible: false,
                        },
                        {
                            title: "创建时间",
                            field: "created_at",
                            visible: false,
                        },
                        {
                            title: "更新时间",
                            field: "updated_at",
                            visible: false,
                        },
                        {
                            title: "状态",
                            field: "status",
                            formatter: Table.api.formatter.switch
                        },
                        {
                            field: 'operate', 
                            title: '操作', 
                            table: table, events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
    };
    return Card
});