var App = function () {

    
    this.selectedRow = [];

    this.Startup = function () {
        $('#manage').addClass('active');

        this.ReLayout();
        this.InitDate();
        this.InitDataGrid();
        
        $('#query-btn').on('click', this.OnQueryButtonClick.bind(this));
        $('#query-btn').trigger('click');

        window.onresize = this.ReLayout.bind(this);
    };

    this.ReLayout = function () {
        var width = $(window).width();
        var height = $(window).height();

        $('.manage-table, .manage-table .datagrid').width(width - 247);
        $('.manage-table, .manage-table .datagrid').height(height - 109);
    };

    this.OnQueryButtonClick = function () {
        this.ReLoadTableData();
    };

    this.ReLoadTableData = function () {
        $.ajax({
            type: "POST",
            dataType: 'text',
            url: '/StatisticAnalysis/Query',
            data: {
                fromTime: $("#from-date").datetimebox('getValue'),
                toTime: $("#to-date").datetimebox('getValue'),
                pageIndex: 1,
                pageSize: 10
            },
            success: function (result) {
                $('#task-grid').datagrid('loadData', JSON.parse(result));
            }.bind(this)
        });
    };

    this.InitDate = function () {
        $('#from-date').datetimebox({
            panelWidth: 190,
            panelAlign: 'right',
            panelHeight: 230,
            showSeconds: true,
            currentText: '现在',
            onSelect: function (date) {
                var startTime = date.getTime();
                var endDate = $('#to-date').val();

                if (endDate) {
                    var endTime = new Date(endDate).getTime();
                    if (startTime > endTime)
                        alert('开始日期不能大于结束日期,请重新选择。');
                }
            }
        });

        $('#to-date').datetimebox({
            panelWidth: 190,
            panelHeight: 230,
            panelAlign: 'right',
            showSeconds: true,
            currentText: '现在',
            onSelect: function (date) {
                var endTime = date.getTime();
                var startDate = $('#from-date').val();

                if (startDate) {
                    var startTime = new Date(startDate).getTime();
                    if (startTime > endTime)
                        alert('结束日期不能小于开始日期,请重新选择。');
                }
            }
        });

        $("#from-date").datetimebox('setValue', moment().add(-30, 'days').format('YYYY/MM/DD HH:mm'));
    };

    this.InitDataGrid = function () {
        $('#task-grid').datagrid({
            columns: [[
                { field: 'User', title: '姓名', align: 'center', width: 10 },
                { field: 'ComputeCount', title: '计算次数', align: 'center', width: 10 },
                { field: 'LastComputeTime', title: '最后计算时间', align: 'left', width: 70 }
            ]],
            striped: true,
            singleSelect: false,
            fitColumns: true,
            fit: true,
            scrollbarSize: 0,
            pagination: true,
            pageNumber: 1,
            pageSize: 20,
            pageList: [10, 20, 30],
            onSelect: this.OnTaskSelected.bind(this),
            onUnselect: this.OnTaskUnselected.bind(this),
            onBeforeLoad: this.OnTableGridBeforeLoad.bind(this)
        });
    };

    this.formatText = function (value) {
        return value === 1 ? '<span style="color: #00c541">已启用</span>' : '<span style="color: #f40000">已禁用</span>';
    };

    this.OnTaskSelected = function (index, row) {
        this.selectedRow = row;
        $('.datagrid-btable tr').removeClass('datagrid-row-selected');
        $('.datagrid-btable tr').eq(index).addClass('datagrid-row-selected');

        //set buttons disabled state
        $('#edit-btn').prop('disabled', row === null);
        $('#delete-btn').prop('disabled', row === null);
        $('#enable-btn').prop('disabled', row.isEnable !== 1 ? null : true);
        $('#disable-btn').prop('disabled', row.isEnable === 1 ? null : true);
    };

    this.OnTaskUnselected = function (index, row) {
        $('#edit-btn').prop('disabled', true);
        $('#delete-btn').prop('disabled', true);
        $('#enable-btn').prop('disabled', true);
        $('#disable-btn').prop('disabled', true);
    };

    this.OnAddButtonClick = function () {
        this.AddDialog.Show();
    };

    this.onEditButtonClick = function () {
        this.EditDialog.Show(this.selectedRow);
    };

    this.onDeleteButtonClick = function () {
        $('.dialog-delete').show();
        $('.dialog-clear h2').text('确定删除名为「{0}」的账户吗?'.format(this.selectedRow.Name));
    };

    this.CloseDeleteDialog = function () {
        $('.dialog-delete').hide();
    };

    this.OnTableGridBeforeLoad = function () {
        $('#task-grid').datagrid('getPager').pagination({
            beforePageText: '第',
            afterPageText: '页&nbsp;&nbsp;&nbsp;共{pages}页',
            displayMsg: '当前显示{from}-{to}条记录&nbsp;&nbsp;&nbsp;共{total}条记录',
            layout: ['list', 'sep', 'first', 'prev', 'sep', 'manual', 'sep', 'next', 'last', 'sep', 'refresh', 'info']
        });
    };
};

$(document).ready(function () {
    var app = new App();
    app.Startup();
});