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

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

            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: 'user_id', title: __('User_id'),
                            formatter: function (value,row,index) {
                                if(row.users){
                                    var html="id："+row.user_id + "<br/> 昵称：" + row.users.nickname;
                                    html  = row.realname?html+"<br/> 姓名：" + row.realname:html;
                                    html  = row.users.mobile?html+"<br/> 电话：" + row.users.mobile:html;
                                    return html;
                                } else {
                                    return "id：" + row.user_id;
                                }

                            }},
                        {field: 'charge_device_name', title: __('Charge_device_name'), operate: 'LIKE'},
                        {field: 'charge_device_sn', title: __('Charge_device_sn'), operate: 'LIKE'},
                        {field: 'area_name', title: __('Area_name'), operate: 'LIKE', sortable:true},
                        {field: 'detail_address', title: __('Detail_address'), operate: 'LIKE'},
                        {field: 'charge_device_type', title: __('Charge_device_type'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'authorizestatus', title: __('Authorizestatus'), searchList: {"1":__('Authorizestatus 1'),"2":__('Authorizestatus 2')}, formatter: Table.api.formatter.status},
                        {field: 'authorizetime', title: __('Authorizetime'), operate:'RANGE', sortable:true, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), 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,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('收益详情'),
                                    title: __('收益详情'),
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    icon: 'fa fa-eye',
                                    url: 'device/chargedevice/detail'
                                }
                            ],}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function(){
            var device_sn = $('#device_sn').val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/index?device_sn=' + device_sn,
                    add_url: 'order/order/add',
                    edit_url: 'order/order/edit',
                    del_url: 'order/order/del',
                    multi_url: 'order/order/multi',
                    import_url: 'order/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                $("#total_income").text(data.total_income);
                $("#today_income").text(data.today_income);
            });
            // 初始化表格
            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: 'order_sn', title: __('Order_sn'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        {field: 'need_pay', title: __('Need_pay'), operate:'BETWEEN'},
                        {field: 'device_sign', title: __('Device_sign'), operate: 'LIKE'},
                        {field: 'order_type', title: __('Order_type'), searchList: {"0":__('Order_type 0'),"2":__('Order_type 2'),"4":__('Order_type 4')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"":__('Status 3'),"4":__('Status 4')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), 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);
        },

            api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
