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

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'manystore/shop_apply/index' + location.search,
                    add_url: 'manystore/shop_apply/add',
                    edit_url: 'manystore/shop_apply/edit',
                    del_url: 'manystore/shop_apply/del',
                    multi_url: 'manystore/shop_apply/multi',
                    generate_url: 'manystore/shop_apply/generate',//生成机构
                    import_url: 'manystore/shop_apply/import',
                    table: 'manystore_shop_apply',
                }
            });

            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: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'realname', title: __('Realname'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'shop_id', title: __('Shop_id')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'user.mobile', title: __('User.mobile'), operate: 'LIKE'},
                        {field: 'user.avatar', title: __('User.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'shop.name', title: __('Shop.name'), operate: 'LIKE'},
                        {field: 'shop.logo', title: __('Shop.logo'), operate: 'LIKE'},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                        {field: 'operate', title: __('Operate'), table: table , buttons: [
                                {
                                    name: 'edit',
                                    text: __('生成机构'),
                                    title: __('生成机构'),
                                    auth:"edit",
                                    classname: 'btn btn-xs btn-danger btn-magic  btn-dialog',
                                    icon: 'fa fa-cube',
                                    // dropdown : '更多',
                                    url: generate_url,
                                    callback: function (data) {

                                    },
                                    visible: function (row) {
                                        return !row.shop_id;
                                    }
                                },



                                {
                                    name: 'generate',
                                    text: __('查看机构'),
                                    title: __('查看机构'),
                                    auth:"generate",
                                    classname: 'btn btn-xs btn-danger btn-magic  btn-dialog',
                                    icon: 'fa fa-cubes',
                                    // dropdown : '更多',
                                    url: list_url,
                                    callback: function (data) {

                                    },
                                    visible: function (row) {
                                        return row.shop_id;
                                    }
                                },

                            ], 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 () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };



    var generate_url = function (row,dom) {

        return 'manystore/index/add?user_id='+row.user_id
            + '&type='+row.type
            + '&name=' + (row.name || row.realname)
            + '&tel='+row.mobile
            + '&shop_apply_id='+row.id
            + '&address_detail='+row.address + '&legal_entity='+row.realname;

    };

    var list_url = function (row,dom) {
        return 'manystore/index/index?id='+row.store_id;
    };

    return Controller;
});
