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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'pre_order_no', title: __('Pre_order_no'), operate: 'LIKE'},
                        {field: 'pre_order_status', title: __('Pre_order_status'), searchList: {"0":__('Pre_order_status 0'),"1":__('Pre_order_status 1'),"2":__('Pre_order_status 2')}, formatter: Table.api.formatter.status},

                        {field: 'company_id', title: __('Company_id')},
                        {field: 'company.name', title: __('Company.name'), operate: 'LIKE'},

                        {field: 'user_id', title: __('User_id')},
//                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
//                        {field: 'user.mobile', title: __('User.mobile'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
//                        {field: 'car_id', title: __('Car_id')},
                        {field: 'car_number', title: __('Car_number'), operate: 'LIKE'},
//                        {field: 'servicetype_id', title: __('Servicetype_id')},
                        {field: 'servicetype.title', title: __('Servicetype.title'), operate: 'LIKE'},

                        {field: 'pre_time', title: __('Pre_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
//                        {field: 'order_id', title: __('Order_id')},
                        {field: 'order_time', title: __('Order_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order.orderno', title: __('Order.orderno'), operate: 'LIKE'},

                        {field: 'cancel_time', title: __('Cancel_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'cancel_reason', title: __('Cancel_reason'), operate: 'LIKE'},
                        {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,
                            buttons:[
                                {
                                    name:'cancel',
                                    text:'取消预约',
                                    title:'取消预约',
                                    icon:'fa fa-exclamation-circle',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    extend: 'data-area=["80%","80%"]',
                                    url:'preorder/cancel/id/{ids}?dialog=1',
                                    target:'_self',
                                    hidden:function($row){
                                        if($row.pre_order_status == 1){
                                            return false;
                                        }
                                        return true;
                                    }
                                },
                                {
                                    name:'submitorder',
                                    text:'立即开单',
                                    title:'立即开单',
                                    icon:'fa fa-exclamation-circle',
                                    classname:'btn btn-xs btn-info btn-dialog',
                                    extend: 'data-area=["80%","80%"]',
                                    url:'preorder/submitorder/id/{ids}?dialog=1',
                                    target:'_self',
                                    hidden:function($row){
                                        if($row.pre_order_status == 1){
                                            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();
        },
        cancel: function () {
            Controller.api.bindevent();
        },
        submitorder: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
