/*
	Descrição: Apresenta dados na grid da página
    Autor: Adriano Angélico
	Data: 20/03/2015
	Versão: 1.0
*/

var grid;

$(function () {
    // Função para alterar o formato do valor das células
    function formatter(row, cell, value, columnDef, dataContext) {
        return value;
    }

    // Configura as opções da grid
    var options = {
        enableCellNavigation: false,
        enableColumnReorder: true,
        forceFitColumns: true,
        syncColumnCellResize: true,
        multiColumnSort: true,
        headerRowHeight: 25,
        rowHeight: 25,
        //forceSyncScrolling: true,
        enableAsyncPostRender: true
    }

    // Array com os valores das células do cabeçalho
    var columns = [
        { id: "selecionar", name: "<input class='checkbox' type='checkbox' />", field: "selecionar", formatter: formatter, sortable: false, width: 20, toolTip: "Selecionar Todos" },
        { id: "idTarefa", name: "ID Tarefa", field: "idTarefa", sortable: true },
        { id: "abertura", name: "Abertura", field: "abertura", sortable: true },
        { id: "macroSetor", name: "Macro Setor", field: "macroSetor", sortable: true },
        { id: "regiao", name: "Região", field: "regiao", sortable: true },
        { id: "setor", name: "Setor", field: "setor", sortable: true },
        { id: "bairro", name: "Bairro", field: "bairro", sortable: true, width: 100 },
        { id: "equipe", name: "Equipe", field: "equipe", sortable: true },
        { id: "fiscal", name: "Fiscal", field: "fiscal", sortable: true },
        { id: "status", name: "Status", field: "status", sortable: true },
        { id: "foto", name: "", field: "foto", formatter: formatter, sortable: false, width: 30 },
        { id: "editar", name: "", field: "editar", formatter: formatter, sortable: false, width: 30 },
        { id: "excluir", name: "", field: "excluir", formatter: formatter, sortable: false, width: 30 }
    ];

    // Função que mostra os valores das células na grid
    $(function () {
        var data = [];

        for (var i = 0; i < 100; i++) {
            data[i] = {
                selecionar: "<input class='checkbox' type='checkbox' />",
                idTarefa: "1211802",
                abertura: "23/03/2015 - 09:00",
                macroSetor: "Macro 01",
                regiao: "Margem Direita",
                setor: "MD2",
                bairro: "Parque Tropical",
                equipe: "Coleta",
                fiscal: "Nestor Pereira",
                status: "Atrasada",
                foto: "<span class='tag-foto icon-cam'></span> 138",
                editar: "<span class='tag-editar icon-pencil'></span>",
                excluir: "<span class='tag-excluir'>X</span>"
            };
        }

        grid = new Slick.Grid("#myGridTarefas", data, columns, options);
        columnpicker = new Slick.Controls.ColumnPicker(columns, grid, options);

        grid.onSort.subscribe(function (e, args) {
            var cols = args.sortCols;

            data.sort(function (dataRow1, dataRow2) {
                for (var i = 0, l = cols.length; i < l; i++) {
                    var field = cols[i].sortCol.field;
                    var sign = cols[i].sortAsc ? 1 : -1;
                    var value1 = dataRow1[field], value2 = dataRow2[field];
                    var result = (value1 == value2 ? 0 : (value1 > value2 ? 1 : -1)) * sign;

                    if (result != 0) {
                        return result;
                    }
                }

                return 0;
            });

            grid.invalidate();
            grid.render();
        });
    });

    // Funções complementares - apenas para apresentar valores na grid
    /*function atividade() {
        var min = 1;
        var max = 4;
        var r = Math.floor(Math.random() * (max - min + 1) + min);

        switch (r) {
            case 1:
                return "INI COLETA";
                break;
            case 2:
                return "INI DESCARGA";
                break;
            default:
                return "EM TRANSPORTE";
        }
    }

    function porcentagem() {
        var p = Math.floor(Math.random() * 100) + 1;

        if (p <= 10) {
            return "<span class='progressbar'><span class='percentage-bar until-10-per' style='width: " + p + "%'></span><span class='percentage-value'>" + p + "%</span></span>";
        } else if (p <= 30) {
            return "<span class='progressbar'><span class='percentage-bar until-30-per' style='width: " + p + "%'></span><span class='percentage-value'>" + p + "%</span></span>";
        } else if (p <= 50) {
            return "<span class='progressbar'><span class='percentage-bar until-50-per' style='width: " + p + "%'></span><span class='percentage-value'>" + p + "%</span></span>";
        } else {
            return "<span class='progressbar'><span class='percentage-bar until-100-per' style='width: " + p + "%'></span><span class='percentage-value'>" + p + "%</span></span>";
        }
    }

    function situacao() {
        var min = 1;
        var max = 4;
        var r = Math.floor(Math.random() * (max - min + 1) + min);

        switch (r) {
            case 1:
                return "No Horário";
                break;
            case 2:
                return "<span class='red'>Adiantado</span>";
                break;
            case 3:
                return "<span class='red'>Atrasado</span>";
                break;
            default:
                return "Sem Status";
        }
    }

    function contestacao() {
        var min = 1;
        var max = 5;
        var r = Math.floor(Math.random() * (max - min + 1) + min);

        return "<span class='contestacao-" + r + "'></span>";
    }

    function showStatus(str1, str2) {
        var min = 1;
        var max = 4;
        var str1 = str1;
        var str2 = str2;
        var res = Math.floor(Math.random() * (max - min + 1) + min);

        if (res == 1 || res == 3) {
            return str1;
        } else {
            return str2;
        }
    }*/
});