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

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

            var table = $("#table");

            // 从 DOM 读取状态映射
            var statusList = table.data('status-list') || {};
            var orderStatusList = table.data('order-status-list') || {};

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'status', title: __('Status'), formatter: function (value) {
                            var text = statusList[value] || '-';
                            var cls = '';
                            switch (parseInt(value)) {
                                case 0:  cls = 'btn-warning'; break;  // 待结算
                                case 1:  cls = 'btn-success'; break;  // 已结算
                                case -1: cls = 'btn-default'; break;  // 已取消
                                case -2: cls = 'btn-danger';  break;  // 已退款
                                default: cls = 'btn-default'; break;
                            }
                            return '<button class="btn btn-xs ' + cls + '" style="pointer-events:none;">' + text + '</button>';
                        }},
                        {field: 'agent_id', title: __('Agent_id')},
                        {field: 'agent_user_id', title: __('Agent_user_id')},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'order_sn', title: __('Order_sn'), operate: 'LIKE'},
                        {field: 'buyer_user_id', title: __('Buyer_user_id')},
                        {field: 'commission_rate_snapshot', title: __('Commission_rate_snapshot'), operate:'BETWEEN'},
                        {field: 'commission_amount', title: __('Commission_amount'), operate:'BETWEEN',
                            formatter: function (value) {
                                var price = value ? '¥' + parseFloat(value).toFixed(2) : '-';
                                 return '<span style="color:#f39c12;">' + price + '</span>';
                            }
                        },
                        {field: 'receive_province_id', title: __('Receive_province_id')},
                        {field: 'receive_province_name', title: __('Receive_province_name'), operate: 'LIKE'},
                        // {field: 'goods_id', title: __('Goods_id')},
                        // {field: 'goods_title', title: __('Goods_title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'goods_pay_price', title: __('Goods_pay_price'), operate:'BETWEEN'},
                        {field: 'order_status', title: __('Order_status'), operate: 'LIKE', formatter: function (value) { return orderStatusList[value] || value || '-'; }},
                        {field: 'aftersale_order_sn', title: __('Aftersale_order_sn'), operate: 'LIKE', formatter: function (value) { return value || '-'; }},
                        {field: 'order_confirm_time', title: __('Order_confirm_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order_createtime', title: __('Order_createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {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;
});
