Documentation icon دستاویز [تخلیق]
-- This module displays a navbox of all available Wikipedia ads. It is used in
-- [[Template:Wikipedia ads/دستاویز]].

local DATA_MODULE = 'Module:Wikipedia ads/data'

local p = {}

function p.main()
	local data = mw.loadData(DATA_MODULE)

	-- Table root.
	local root = mw.html.create('table')
	root
		:addClass('navbox collapsible collapsed')
		:css('width', '100%')
		:css('background', '#f7f8ff')
		:css('text-align', 'right')
		:css('border', 'solid 1px #aaaaaa')
		:css('margin', 0)
		:tag('tr')
			:tag('th')
				:css('background', 'transparent')
				:css('font-size', '105%;')
				:css('text-align', 'center;')
				:css('font-weight', 'bold')
				:wikitext('[[سانچہ:ویکی اشتہارات|ویکی اشتہارات]]: ایک نظر میں')
	
	-- Ad table.
	local adTable = root
		:tag('tr')
			:tag('td')
				:tag('table')
					:addClass('plainlinks sortable wikitable')
					:css('width', '100%')
					:css('text-align', 'right')
					:css('margin', '0')
					:css('font-size', '90%')

	-- Ad table headers.
	adTable
		:tag('tr')
			:tag('th')
				:css('width', '3em')
				:wikitext('#')
				:done()
			:tag('th')
				:addClass('unsortable')
				:wikitext('تخلیق کار')
				:done()
			:tag('th')
				:wikitext('صفحہ')
				:done()
			:tag('th')
				:wikitext('تصویر')
				:done()

	-- Ads.
	for i, imageData in ipairs(data.list) do
		-- Sanity checks. The ID is already checked by the data module.
		for j, field in ipairs{'author', 'link', 'image'} do
			assert(imageData[field], string.format(
				"no '%s' field found in image data with ID '%s'",
				field, tostring(imageData.id)
			))
		end
		adTable
			:tag('tr')
				:tag('th')
					:wikitext(imageData.id)
					:done()
				:tag('td')
					:wikitext(string.format(
						'[[فائل:%s|140پک]]',
						imageData.image
					))
					:done()
				:tag('td')
					:wikitext(string.format('[[%s]]', imageData.link))
					:done()
				:tag('td')
					:wikitext(imageData.author)
	end

	return tostring(root)
end

return p