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

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
					del_url: 'order/del',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updatetime',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('订单号')},
                        {
                            field: 'goods_info', 
                            title: __('商品信息'),
                            formatter:function(value,row){
                                var html = '';
                                $.each(row.goods,function(i,v){
									var boder_bottom = row.goods.length != (i + 1) ? 'border-bottom:1px solid #ddd;' : '';
                                    html += '<div style="padding:5px; '+boder_bottom+'">'+
                                        '<img src="'+v.goods_image+'" class="img-sm">'+
                                        '<div>'+
                                            '<div>'+
                                                '<div>商品:'+v.goods_name+'</div>'+
                                            '</div>'+
                                            '<div>'+
                                                '<!--<span>规格:丝质柔滑,750ml</span>-->'+
                                                '<span>单价:'+v.goods_price+'元 </span>'+
                                                '<span class="col-xs-offset-1">数量:'+v.goods_num+'</span>'+
                                            '</div>'+
                                        '</div>'+
                                    '</div>';
                                })
                                return html;
                            }
                        },
                        {field: 'pay_status', title: __('支付状态'), searchList: {"0":__('未支付'),"1":__('已支付')}, formatter: Table.api.formatter.normal},
                        {field: 'delivery_status', title: __('发货状态'), searchList: {"0":__('未发货'),"1":__('已发货')}, formatter: Table.api.formatter.normal},
						{field: 'refund_status', title: __('售后状态'), searchList: {"0":__('未申请'),"1":__('申请中'),"2":__('已退款')}, formatter: Table.api.formatter.normal},
                        {field: 'user.nickname', title: __('下单用户')},
                        {field: 'order_price', title: __('实际支付(元)')},
                        {field: 'createtime', title: __('下单时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-book',
                                    url: 'order/info?order_id={id}'
                                },
                                {
                                    name: 'detail',
                                    title: __('发货'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-send',
                                    url: 'order/delivery?order_id={id}',
                                    visible: function(row){
                                        if(row.type.value == 'delivery'){
                                            return true;
                                        }
                                    }
                                },
								{
								    name: 'detail',
								    title: __('退款'),
								    classname: 'btn btn-xs btn-warning btn-dialog',
								    icon: 'fa fa-send',
								    url: 'order/refundY?order_id={id}',
								    visible: function(row){
								        if(row.type.value == 'refund_1'){
								            return true;
								        }
								    }
								},
								// {
								//     name: 'detail',
								//     title: __('拒绝退款'),
								//     classname: 'btn btn-xs btn-warning btn-dialog',
								//     icon: 'fa fa-send',
								//     url: 'order/refund_3?order_id={id}',
								//     visible: function(row){
								//         if(row.type.value == 'refund_1'){
								//             return true;
								//         }
								//     }
								// },
								{
								    name: 'detail',
								    title: __('退货'),
								    classname: 'btn btn-xs btn-warning btn-dialog',
								    icon: 'fa fa-send',
								    url: 'order/refundY?order_id={id}',
								    visible: function(row){
								        if(row.type.value == 'return_goods_1'){
								            return true;
								        }
								    }
								},
								// {
								//     name: 'detail',
								//     title: __('拒绝退货'),
								//     classname: 'btn btn-xs btn-warning btn-dialog',
								//     icon: 'fa fa-send',
								//     url: 'order/return_goods_3?order_id={id}',
								//     visible: function(row){
								//         if(row.type.value == 'return_goods_1'){
								//             return true;
								//         }
								//     }
								// },
                            ],
                            formatter: function (value, row, index) {
                            	var that = $.extend({}, this);
                            	var table = $(that.table).clone(true);
                            	if(row.type.value != 'payment'){
                            		$(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                            	}
                            	that.table = table; 
                            	return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        info: function () {
            Controller.api.bindevent();
        },
        delivery: function () {
            Controller.api.bindevent();
        },
		refundy: function () {
		    Controller.api.bindevent();
		},
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});