'use strict';

var consts = require(__config_path + '/consts');

exports.getPriorityName = function (code) {
    if (consts.PRIORITY.length > 0) {
        return consts.PRIORITY[code];
    } else {
        return '';
    }
}

exports.getStatusName = function (code) {
    if (consts.STATUS.length > 0) {
        return consts.STATUS[code];
    } else {
        return '';
    }
}

/**
 * Hàm tạo link phân trang ở danh sách
 *
 * @param $totalRecord
 * @param $pageSize
 * @param $pageIndex
 * @param $paginationName
 * @returns {string}
 */
exports.genPaginationLink = function($totalRecord, $pageSize = 10, $pageIndex = 1, $paginationName = 'changePagination')
{
    var $pagination = "";

    if ($pageIndex < 1) $pageIndex = 1;

    if ($pageSize >= 1 && $pageIndex >= 1) {
        var $adjacents = "2";
        var $prev = $pageIndex - 1;
        var $next = $pageIndex + 1;
        var $lastpage = Math.ceil($totalRecord / $pageSize);
        var $lpm1 = $lastpage - 1;
    } else {
        return $pagination;
    }

    var $counter=1;

    if ($lastpage > 1) {
        $pagination += "<div class='pagination'>";

        if ($pageIndex > 1) {
            $pagination += "<a href=\"#page=" + ($prev) + "\" onClick='" + $paginationName + "(" + ($prev) + ");'>&laquo;&nbsp;</a>";
        } else {
            $pagination += "<span class='disabled'>&laquo;&nbsp;</span>";
        }

        if ($lastpage < 7 + ($adjacents * 2)) {
            for ($counter = 1; $counter <= $lastpage; $counter++) {
                if ($counter == $pageIndex) {
                    $pagination += "<span class='current'>"+$counter+"</span>";
                } else {
                    $pagination += "<a href=\"#page=" + ($counter) + "\" onClick='" + $paginationName + "(" + ($counter) + ");'>"+$counter+"</a>";
                }
            }
        } else if ($lastpage > 5 + ($adjacents * 2)) {
            if ($pageIndex < 1 + ($adjacents * 2)) {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) {
                    if ($counter == $pageIndex) {
                        $pagination += "<span class='current'>"+$counter+"</span>";
                    } else {
                        $pagination += "<a href=\"#page=" + ($counter) + "\" onClick='" + $paginationName + "(" + ($counter) + ");'>"+$counter+"</a>";
                    }
                }
                $pagination += "...";
                $pagination += "<a href=\"#page=" + ($lpm1) + "\" onClick='" + $paginationName + "(" + ($lpm1) + ");'>$lpm1</a>";
                $pagination += "<a href=\"#page=" + ($lastpage) + "\" onClick='" + $paginationName + "(" + ($lastpage) + ");'>"+$lastpage+"</a>";

            } else if ($lastpage - ($adjacents * 2) > $pageIndex && $pageIndex > ($adjacents * 2)) {
                $pagination += "<a href=\"#page=\"1\"\" onClick='" + $paginationName + "(1);'>1</a>";
                $pagination += "<a href=\"#page=\"2\"\" onClick='" + $paginationName + "(2);'>2</a>";
                $pagination += "...";
                for ($counter = $pageIndex - $adjacents; $counter <= $pageIndex + $adjacents; $counter++) {
                    if ($counter == $pageIndex) {
                        $pagination += "<span class='current'>"+$counter+"</span>";
                    } else {
                        $pagination += "<a href=\"#page=" + ($counter) + "\" onClick='" + $paginationName + "(" + ($counter) + ");'>"+$counter+"</a>";
                    }
                }
                $pagination += "..";
                $pagination += "<a href=\"#page=" + ($lpm1) + "\" onClick='" + $paginationName + "(" + ($lpm1) + ");'>"+$lpm1+"</a>";
                $pagination += "<a href=\"#page=" + ($lastpage) + "\" onClick='" + $paginationName + "(" + ($lastpage) + ");'>"+$lastpage+"</a>";
            } else {
                $pagination += "<a href=\"#page=\"1\"\" onClick='" + $paginationName + "(1);'>1</a>";
                $pagination += "<a href=\"#page=\"2\"\" onClick='" + $paginationName + "(2);'>2</a>";
                $pagination += "..";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
                    if ($counter == $pageIndex) {
                        $pagination += "<span class='current'>"+$counter+"</span>";
                    } else {
                        $pagination += "<a href=\"#page=" + ($counter) + "\" onClick='" + $paginationName + "(" + ($counter) + ");'>"+$counter+"</a>";
                    }
                }
            }
        }

        if ($pageIndex < $counter - 1) {
            $pagination += "<a href=\"#page=" + ($next) + "\" onClick='" + $paginationName + "(" + ($next) + ");'> &raquo;</a>";
        } else {
            $pagination += "<span class='disabled'> &raquo;</span>";
        }
        $pagination += "</div><p>(Tổng số kết quả: " + $totalRecord + ")</p>";
    }

    return $pagination;
}