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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'version_name', title: '版本名称', operate: 'LIKE'},
                        {field: 'version_code', title: '版本号', operate: false},
                        {field: 'apk_file', title: 'APK文件', operate: false, formatter: Table.api.formatter.file},
                        {field: 'file_size', title: '文件大小', operate: false, formatter: function (value) {
                            if (!value || value === 0) return '-';
                            var mb = (value / 1024 / 1024).toFixed(1);
                            return mb + ' MB';
                        }},
                        {field: 'sha256', title: 'SHA-256', operate: false, formatter: function (value) {
                            if (!value) return '-';
                            return '<code title="' + value + '">' + value.substring(0, 16) + '...</code>';
                        }},
                        {field: 'is_force_update', title: '强制更新', searchList: {"0":"否","1":"是"}, formatter: Table.api.formatter.toggle},
                        {field: 'status', title: '状态', searchList: {"draft": __('Draft'), "published": __('Published'), "archived": __('Archived')}, formatter: Table.api.formatter.status},
                        {field: 'target_devices', title: '目标设备', operate: false, visible: false},
                        {field: 'min_version', title: '最低版本', operate: 'LIKE', visible: false},
                        {field: 'creator_id', title: __('Creator_id'), visible: false},
                        {field: 'createtime', title: '创建时间', operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: '更新时间', operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime, visible: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'publish',
                                    text: '发布',
                                    title: '发布此版本',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    url: 'ota/version/publish',
                                    icon: 'fa fa-cloud-upload',
                                    confirm: '确认发布此版本？发布后其他已发布版本将自动归档。',
                                    success: function () {
                                        table.bootstrapTable('refresh');
                                    }
                                }
                            ],
                            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;
});
