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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'video', title: __('Video'), operate: false, formatter: function (val, data) {
                                if (!val) return '';
                                // 返回一个 video 标签，限制宽高以适应表格
                                return '<video style="width: 250px; height: 120px;" src="' + val + '" controls="controls"></video>';
                            }
                        },
                        {field: 'video_cover', title: __('Cover'), operate: false, formatter: function (val, data) {
                            return val;
                                if (!val) return '';
                                // 返回一个 图片 标签，限制高以适应表格
                                return '<img style="width: auto; height: 120px;" src="' + val + '" controls="controls"></img>';
                            }
                        },
                        {field: 'link', title: __('Link'), operate: false},
                        {field: 'skip_time', title: __('Skip_time'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {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: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, 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;
});
