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',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'user.nickname', title: __('User.nickname'), operate:false},
                        {field: 'teacher.name', title: __('Teacher.name'), operate:false},
                        {field: 'teacher.thumbnail', title: __('Teacher.thumbnail'), operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'territory_id', title: __('Territory_id'), operate:false},
                        {field: 'is_use', title: __('Is_use'), operate:false,formatter: Table.api.formatter.label,searchList:{'0': '不使用','1':'使用'}},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'),formatter: Table.api.formatter.status,searchList:{'1': '待支付','2':'已支付','3':'已退款'}},
                        {field: 'num', title: __('Num'), operate:false},
                        {field: 'business', title: __('Business'), operate:false},
                        {field: 'address', title: __('Address'), operate:false},
                        {field: 'product', title: __('Product'), operate:false},
                        {field: 'scale', title: __('Scale'), operate:false},
                        {field: 'content', title: __('Content'), operate:false},
                        {field: 'commission', title: __('佣金比例(单位%)'), operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'finish_status', title: __('Finish_status'),formatter: Table.api.formatter.label,searchList:{'1': '进行中','2':'待总结','3':'已总结','4':'售后','5':'已完成'}},
                        {field: 'is_complaint', title: __('Is_complaint'),formatter: Table.api.formatter.label,searchList:{'1': '是','2':'否','3':'已处理'}},
                        {field: 'is_chargeback', title: __('是否退单'),formatter: Table.api.formatter.label,searchList:{'1': '是','2':'否'}},
                        {field: 'id', title: __('按钮组'), table: table, events: Table.api.events.operate, buttons:
                            [
                                {
                                    text: '投诉详情',
                                    title: '投诉详情',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'order/complaint',
                                    // extend: 'data-area=\'["80%","80%"]\'',
                                    hidden:function(row){
                                        if(row.is_complaint != 1){
                                            return true;
                                        }
                                    },
                                },
                                {
                                    text: '聊天记录',
                                    title: '聊天记录',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'order_chat/index',
                                    extend: 'data-area=\'["80%","80%"]\'',
                                    hidden:function(row){
                                        if(row.status != 2){
                                            return true;
                                        }
                                    },
                                },
                                {
                                    text: '学生评价',
                                    classname: 'btn btn-xs btn-primary',
                                    url: 'order/detail',
                                },
                                {
                                    text: '老师总结',
                                    classname: 'btn btn-xs btn-primary',
                                    url: 'order/summary',
                                },
                                {
                                    text: '续约订单列表',
                                    title: '续约订单列表',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'sonorder/index1',
                                    extend: 'data-area=\'["80%","80%"]\'',
                                }
                            ], operate:false, formatter: Table.api.formatter.buttons
                        },
                        {field: 'order_status', title: __('Order_status'),formatter: Table.api.formatter.label,searchList:{'0':'待审核','1': '通过','2':'未通过'}},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'expirationtime', title: __('Expirationtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: function (value,row,index) {
                            var that = $.extend({},this);
                            var table = $(that.table).clone(true);
                                if(row.status == 2){
                                    $(table).data('operate-del',null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that,value,row,index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            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;
});