模組:Jctint/TWN

--本模組特別為{{TWNint}}模板設計,以便於維護,並且改善一些原先難以改良與調整的部分。
--詳細使用方法請見{{TWNint}}之說明文件。

local getArgs = require('Module:Arguments').getArgs
local format = mw.ustring.format --導入文字格式處理

local p = {}

local function status_bgcolor(status) --公路交會點狀態背景色碼
	local tables = {closed = "#d3d3d3",
					concur = "#ddffdd",
					etc = "#dcdcfe",
					hov = "#ffff99",
					incomplete = "#ffdddd",
					toll = "#dcdcfe",
					trans = "#dff9f9",
					unbuilt = "#ffdead"}
	return tables[status]
end

function p.twnint(frame)
	local args = getArgs(frame,
						{trim = false,
						 removeBlanks = false,
						 wrappers = 'Template:TWNint'
						})
	return p._twnint(args)
end

function p._twnint(args)
	local county = args.county
	local tcd = args.tcd
	local tcd_link = args.tcd_link or tcd
	local location = args.location
	local km = args.km
	local status = args.status
	local road = args.road
	local notes = args.notes
	local cspan = args.cspan or 1
	local tspan = args.tspan or 1
	local lspan = args.lspan or 1
	local kspan = args.kspan or 1
	local rspan = args.rspan or 1
	local nspan = args.nspan or 1
		
	local ret = mw.html.create('tr')
				:css( {
						['background-color'] = "#f8f9fa",
						width = args.width,
						color = args.textcolor,
						border = 'darkgray solid 1px'
					} )
	if county then
		ret:tag('td')
			:attr('rowspan',cspan)
			:wikitext(format("[[%s]]",county))
			:done()
	end
	if tcd then
		ret:tag('td')
			:attr('rowspan',tspan)
			:wikitext(format("[[%s|%s]]",tcd_link,tcd))
			:done()
	end
	if location  then
		if not (tonumber(lspan) > 1 and location =="") then
			ret:tag('td')
				:attr('rowspan',lspan)
				:wikitext(location)
				:done()
		end
	end
	if km then
		if tonumber(km) then
			kms = format("%.3f",tonumber(km)) --補到小數點第三位
		elseif km == "" then
			kms = "<abbr title=缺少里程>-</abbr>"
		else
			kms = "<b><span class=error mw-ext-cite-error>模組錯誤:非數值</span></b>"
		end
		ret:tag('td')
			:attr('rowspan',kspan)
			:css({['text-align'] = 'right',
				  ['background-color'] = "#eaecf0"})
			:wikitext(kms)
			:done()
	end
	if road then
		ret:tag('td')
			:attr('rowspan',rspan)
			:css({['background-color'] = status_bgcolor(status) or "#f8f9fa"})
			:wikitext(road)
			:done()
	end
	if notes then
		ret:tag('td')
			:attr('rowspan',nspan)
			:css({['background-color'] = status_bgcolor(status) or "#f8f9fa"})
			:wikitext(notes)
			:done()
	end
	
	--合併儲存格參數非數值報錯
	for _,i in ipairs({cspan,tspan,lspan,kspan,rspan,nspan}) do
		if (not tonumber(i) or tonumber(i) < 1) then
			local msgbox = {}
			for _,j in pairs({county,tcd,location,km,road,notes}) do
				if j then
			    	table.insert(msgbox,j)
			    end
			end
			ret:tag('p'):wikitext(format("<b><span class=error mw-ext-cite-error>模組錯誤:合併儲存格參數非數值或小於1,於:%s</span></b>",tostring(table.concat(msgbox, "、")))):done()
		end
	end
	return ret
end

return p