package back

import (
	"html/template"
	"strconv"
)

func getNewsByPage(page int) template.HTML {

	tNews := template.HTML("")

	news := material{}
	news.Category = "новость"
	e := news.Select(page*8, 8)
	if e != nil {
		logger.Println(e.Error())
		return tNews
	}

	for i, m := range news.Rows {
		images := `<div class="image no"><img src="/asset/img/no-image.png" /></div>`
		if m.Image.Valid {
			images = `<div class="image"><img src="/` + m.Image.String + `" /><img class="blur" src="/` + m.Image.String + `" /></div>`
		}

		if i == 0 || i == 3 || i == 4 || i == 7 || i == 8 {
			tNews += template.HTML(`<div class="news"><div class="caption">` + m.Caption + `</div><div class="text">` + m.Preview.String + `</div><a href="/данные/новость/` + m.Link + `" class="gradient-button">Читать далее</a>` + images + `</div>`)
		} else {
			tNews += template.HTML(`<div class="news"><div class="caption">` + m.Caption + `</div><div class="text">` + m.Preview.String + `</div><a href="/данные/новость/` + m.Link + `" class="gradient-button">Читать далее</a></div>`)
		}
	}

	return tNews
}

func getNewsPaginationByPage(page int) template.HTML {

	tNewsPagination := template.HTML("")

	news := material{}
	news.Category = "новость"
	count, e := news.SelectCount()
	if e != nil {
		logger.Println(e.Error())
		return tNewsPagination
	}

	count = count / 8

	if count%8 != 0 {
		count++
	}

	for i := 0; i < count; i++ {
		if i == page {
			tNewsPagination += template.HTML(`<a class="active" href="javascript:void(0)">` + strconv.Itoa(i+1) + `</a>`)
		} else {
			tNewsPagination += template.HTML(`<a href="/страница/` + strconv.Itoa(i) + `">` + strconv.Itoa(i+1) + `</a>`)
		}
	}

	return tNewsPagination
}
