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

    /**
     * 列表 Ajax 仅保留 addtabs 参数。
     * 切勿拼接 location.search 中的 sort/filter/op 等，否则从其它模块跳入 /domain/index 时会带错字段导致列表为空。
     */
    var buildListQuery = function () {
        var rawSearch = location.search || '';
        if (!rawSearch) {
            return '';
        }
        var addtabsMatch = rawSearch.match(/(?:[?&])addtabs=[^&]*/);
        return addtabsMatch ? ('?' + addtabsMatch[0].replace(/^[?&]/, '')) : '';
    };

    var Controller = {
        index: function () {
            var listQuery = buildListQuery();
            var indexUrl = 'domain/index' + listQuery;
            Table.api.init({
                extend: {
                    index_url: indexUrl,
                    add_url: 'domain/add',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: 'domain',
                }
            });

            var table = $("#table");

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'domain', title: __('Domain'), operate: 'LIKE', renderDefault: false},
                        {field: 'full_url', title: __('Full_url'), operate: false, formatter: Table.api.formatter.url},
                        {field: 'zone_status', title: __('Zone_status'), searchList: Config.zoneStatusList, formatter: Table.api.formatter.status},
                        {field: 'ns_status', title: __('Ns_status'), searchList: Config.nsStatusList, formatter: Table.api.formatter.status},
                        {field: 'dns_status', title: __('Dns_status'), searchList: Config.dnsStatusList, formatter: Table.api.formatter.status},
                        {field: 'check_time', title: __('Check_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'check_result', title: __('Check_result'), operate: 'LIKE', class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime, sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('Detail'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'domain/detail'
                                },
                                {
                                    name: 'detect',
                                    text: __('Detect'),
                                    icon: 'fa fa-refresh',
                                    classname: 'btn btn-success btn-xs btn-detect',
                                    url: 'domain/detect',
                                    refresh: true
                                },
                                {
                                    name: 'delconfirm',
                                    text: __('Del'),
                                    icon: 'fa fa-trash',
                                    classname: 'btn btn-danger btn-xs btn-delconfirm'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            table.on('click', '.btn-detect', function (e) {
                e.preventDefault();
                e.stopPropagation();
                var that = this;
                var rowIndex = $(that).data('row-index');
                var row = Table.api.getrowbyindex(table, rowIndex);
                if (!row) {
                    return false;
                }
                $(that).addClass('disabled');
                Fast.api.ajax({
                    url: 'domain/detect/ids/' + row.id,
                    type: 'post'
                }, function () {
                    table.bootstrapTable('refresh');
                    return false;
                }, function () {
                    $(that).removeClass('disabled');
                });
            });

            table.on('click', '.btn-delconfirm', function (e) {
                e.preventDefault();
                e.stopPropagation();
                e.stopImmediatePropagation();
                var that = this;
                var rowIndex = $(that).data('row-index');
                var row = Table.api.getrowbyindex(table, rowIndex);
                if (!row) {
                    return false;
                }
                Layer.prompt({
                    title: __('Delete domain confirm prompt'),
                    formType: 0,
                    value: ''
                }, function (value, index) {
                    if ($.trim(value) !== row.domain) {
                        Toastr.error(__('Delete domain confirm mismatch'));
                        return false;
                    }
                    Layer.close(index);
                    Fast.api.ajax({
                        url: 'domain/del',
                        data: {ids: row.id, confirm_domain: $.trim(value)}
                    }, function () {
                        table.bootstrapTable('refresh');
                    });
                });
            });

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