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

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

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

                        }},
                        {field: 'realname', title: __('Realname'), operate: 'LIKE',visible:false},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE',visible:false},
                        {field: 'identity_number', title: __('Identity_number'), operate: 'LIKE',visible:true},
                        {field: 'parent_user_id', title: __('Parent_user_id'), operate: false,visible:false},
                        // {field: 'area.MergerName', title: __('所属区域')},
                       /* {field: 'city_id', title: __('City_id')},
                        {field: 'area_id', title: __('Area_id')},*/
                        // {field: 'detail_address', title: __('Detail_address'), operate: 'LIKE'},
                        // {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.status},
                        {field: 'deviceCount', title: __('deviceCount'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {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: 'users/agent/detail'
                                }
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function(){
            var user_id = $('#user_id').val();

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'device/chargedevice/index?user_id=' + user_id,
                    add_url: 'device/chargedevice/add?user_id=' +user_id,
                    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')},
                        {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'},
                        {field: 'detail_address', title: __('Detail_address'), operate: 'LIKE'},
                        {field: 'charge_device_type', title: __('Charge_device_type'), 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', 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}
                    ]
                ]
            });

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

    };
    return Controller;
});
