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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: false,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'company_id', title: __('Company_id')},
                        {field: 'company.name', title: __('Company_name'), operate: 'LIKE'},
                        {field: 'coupons_id', title: __('Coupons_id')},
                        {field: 'coupon_name', title: __('Coupon_name'), operate: 'LIKE'},
                        {field: 'coupon_info', title: __('Coupon_info'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'number', title: __('Number')},
                        {field: 'remain', title: __('Remain')},
                        {field: 'payorder_id', title: __('Payorder_id'), operate: false, visible:false},
                        {field: 'getfrom', title: __('Getfrom'), operate: 'LIKE'},
                        //{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 () {

                $("#c-coupons_id").on('change', function () {
                    var couponId = $('#c-coupons_id').val();
                    $("#c-coupons_id").data("params",function(){
                        return {custom:{company_id:companyId}};
                    });
                    $.ajax({
                        url:'coupons/index?filter={"id":"'+couponId+'"}&op={"id":"="}',
                        method:'get',
                        success:function(res){
                            var rows = res.rows[0];
                            var couponName = rows["name"];
                            $("#c-coupon_name").val(couponName);
                        }
                    });
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
