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

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pre_order/index' + location.search,
                    add_url: 'pre_order/add',
                    edit_url: 'pre_order/edit',
                    del_url: 'pre_order/del',
                    multi_url: 'pre_order/multi',
                    import_url: 'pre_order/import',
                    table: 'pre_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: '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: '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'), operate: false, visible:false},
                        {field: 'car_number', title: __('Car_number'), operate: 'LIKE'},
                        {field: 'servicetype_id', title: __('Servicetype_id'), operate: false, visible:false},
                        {field: 'servicetype.title', title: __('Servicetype_title')},
                        {field: 'pre_time', title: __('Pre_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order_time', title: __('Order_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {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: '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: '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 () {
                $("#c-car_id").on('change', function () {
                    var carId = $('#c-car_id').val();
                    $.ajax({
                        url:'user_car/index?filter={"id":"'+carId+'"}&op={"id":"="}',
                        method:'get',
                        success:function(res){
                            var rows = res.rows[0];
                            var carNumber = rows["car_number"];
                            $("#c-car_number").val(carNumber);
                        }
                    });
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
