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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'agent_user_id', title: __('Agent_user_id'),
                            formatter: function (value, row, index) {
                                if (row.users) {
                                    var html = "id：" + row.agent_user_id + "<br/> 昵称：" + row.users.nickname;
                                    html = row.users.realname ? html + "<br/> 姓名：" + row.users.realname : html;
                                    html = row.users.mobile ? html + "<br/> 电话：" + row.users.mobile : html;
                                    return html;
                                } else {
                                    return "id：" + row.agent_user_id;
                                }

                            }
                        },
                        { field: 'area.MergerName', title: __('所属区域') },
                        // {field: 'province_id', title: __('Province_id')},
                        // {field: 'city_id', title: __('City_id')},
                        // {field: 'area_id', title: __('Area_id')},
                        { field: 'area_name', title: __('Area_name'), 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 }
                    ]
                ]
            });
            // 绑定用户
            $('#authorizedevice').click(function () {
                var url = $(this).attr('data-url');
                var title = $(this).attr('title');
                Fast.api.open("device/site/authorizedevice", '批量设备授权', {
                    callback: function (value) {
                        console.log(value);
                    }
                });
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        authorizedevice: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
