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

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'device/device/index' + location.search,
                    add_url: 'device/device/add',
                    edit_url: 'device/device/edit',
                    del_url: 'device/device/del',
                    multi_url: 'device/device/multi',
                    import_url: 'device/device/import',
                    task_url: 'device/device/task',
                    table: '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: 'group.name', title: __('设备分组')},
                        {field: 'number', title: __('Number')},
                        {field: 'task.name', title: __('任务')},
                        {
                            field: 'status', title: __('状态'), formatter: function (value, row, index) {
                                if (row.status < 1) {
                                    return '已停止';
                                } else {
                                    return '运行中';
                                }
                            }
                        },
                        {field: 'successions', title: __('Successions')},
                        {field: 'maxsuccessions', title: __('Maxsuccessions')},
                        {
                            field: 'prevtime',
                            title: __('Prevtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'logintime',
                            title: __('Logintime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'loginip', title: __('Loginip'), operate: 'LIKE'},
                        {field: 'joinip', title: __('Joinip'), operate: 'LIKE'},
                        {
                            field: 'jointime',
                            title: __('Jointime'),
                            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: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"30": __('Status 30')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 点击任务
            $(document).on("click", ".btn-task", function () {
                var selectedData = $('#table').bootstrapTable('getSelections');
                var selectedIds = selectedData.map(function (item) {
                    return item.id;
                });
                var url = $.fn.bootstrapTable.defaults.extend.task_url + '?ids=' + selectedIds.join(',');
                Fast.api.open(url, '任务', {
                    area: ['500px', '380px'],
                })
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        task: function () {
            Controller.api.bindevent();
            var urlParams = new URLSearchParams(window.location.search);
            var ids = urlParams.get('ids');
            $('input[name="row[ids]"]').val(ids);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});