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,
                    add_url: 'order/add',
//                    edit_url: 'order/edit',
//                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            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')},

                        {field: 'product_name', title: __('Product_name'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'pay_fee', title: __('Pay_fee'), operate:'BETWEEN'},

//                        {field: 'usera.nickname', title: __('Usera.nickname'), operate: 'LIKE'},
//                        {field: 'usera.mobile', title: __('Usera.mobile'), operate: 'LIKE'},
                        {field: 'realname', title: __('Realname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'ticket_str', title: __('Ticket_str'), operate: 'LIKE'},

                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"10":__('Status 10'),"21":__('Status 21'),"22":__('Status 22')}, formatter: Table.api.formatter.status},

                        {field: 'roadname', title: __('Roadname'), operate: 'LIKE'},

                        {field: 'roadtype', title: __('Roadtype'), searchList: {"1":__('Roadtype 1'),"2":__('Roadtype 2'),"3":__('Roadtype 3')}, formatter: Table.api.formatter.normal},
                        {field: 'go_chufatime', title: __('Go_chufatime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'go_chufaname', title: __('Go_chufaname'), operate: 'LIKE'},

                        {field: 'back_chufatime', title: __('Back_chufatime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'back_chufaname', title: __('Back_chufaname'), operate: 'LIKE'},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'finishtime', title: __('Finishtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'user_id', title: __('User_id')},
                        {field: 'product_id', title: __('Product_id')},
                        {field: 'road_id', title: __('Road_id')},

                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'pay_out_trade_no', title: __('Pay_out_trade_no'), operate: 'LIKE'},

                        {field: 'cancle_time', title: __('Cancle_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
//                        {field: 'refund_reason', title: __('Refund_reason'), operate: 'LIKE'},
//                        {field: 'refund_time', title: __('Refund_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
//                        {field: 'refund_remark', title: __('Refund_remark'), operate: 'LIKE'},
//                        {field: 'refund_fee', title: __('Refund_fee'), operate:'BETWEEN'},

                        {field: 'jxtype', title: __('Jxtype'), searchList: {"0":__('Jxtype 0'),"1":__('Jxtype 1')}, formatter: Table.api.formatter.normal},
//                        {field: 'product.name', title: __('Product.name'), operate: 'LIKE'},
                        {field: 'share_uid', title: __('Share_uid')},
                        {field: 'userb.nickname', title: __('Userb.nickname'), operate: 'LIKE'},
                        {field: 'userb.mobile', title: __('Userb.mobile'), operate: 'LIKE'},

                        {field: 'operate', title: __('Operate'), table: table,
                            buttons:[
                                {
                                    name:'refund',
                                    text:'退款',
                                    title:'退款',
                                    icon:'fa fa-exclamation-circle',
                                    classname:'btn btn-xs btn-danger btn-dialog',
                                    url:'order/refund/id/{ids}',
                                    target:'_self',
                                    refresh:true,
                                    hidden:function($row){
                                        if($row.status == 21 || $row.status == 22){
                                            return false;
                                        }
                                        return true;
                                    }
                                },
                                {
                                    name:'xieyipdf',
                                    text:'协议导出',
                                    title:'协议导出',
                                    icon:'fa fa-exclamation-circle',
                                    classname:'btn btn-xs btn-success btn-addtabs',
                                    url:'order/xieyipdf/id/{ids}',
                                    target:'_blank',
                                    refresh:true,
                                    hidden:function($row){
                                        if($row.xieyi_id == 0){
                                            return true;
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name:'refundcancel',
                                    text:'取消售后',
                                    title:'取消售后',
                                    icon:'fa fa-exclamation-circle',
                                    classname:'btn btn-xs btn-danger btn-ajax',
                                    url:'order/refundcancel/id/{ids}',
                                    target:'_self',
                                    refresh:true,
                                    hidden:function($row){
                                        if($row.status == 21){
                                            return false;
                                        }
                                        return true;
                                    }
                                },
                            ],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        xieyipdf: function () {
            Controller.api.bindevent();
        },
        refund: function () {
            Controller.api.bindevent();
        },
        refundcancel: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
