<template>
	<div>
		<div v-if="pg" class="overflow-x-auto">
			<table
				class="text w-full border-separate border-spacing-y-2 text-base font-normal text-gray-900"
			>
				<thead class="bg-gray-100">
					<tr class="text-gray-600">
						<th class="rounded-l p-2 text-left font-normal">描述</th>
						<th class="whitespace-nowrap p-2 text-right font-normal">费率</th>
						<th class="whitespace-nowrap p-2 text-right font-normal">
							数量
						</th>
						<th class="rounded-r p-2 text-right font-normal">金额</th>
					</tr>
				</thead>
				<tbody>
					<template v-for="(items, type) in groupedLineItems" :key="type">
						<tr class="mt-1 bg-gray-50">
							<td colspan="100" class="rounded p-2 text-base font-medium">
								{{ type }}
							</td>
						</tr>
						<tr v-for="(row, i) in items" :key="row.idx">
							<td class="py-1 pl-2 pr-2">
								{{ row.document_name }}
								<span v-if="row.plan" class="text-gray-700">
									({{ formatPlan(row.plan) }}/月)
								</span>
							</td>
							<td class="py-1 pl-2 pr-2 text-right">
								{{ formatCurrency(row.rate) }}
							</td>
							<td class="py-1 pl-2 pr-2 text-right">
								{{ row.quantity }}
								{{
									[
										'Site',
										'Release Group',
										'Server',
										'Database Server'
									].includes(row.document_type)
										? $format.plural(row.quantity, '天', '天')
										: ''
								}}
							</td>
							<td class="py-1 pl-2 pr-2 text-right font-medium">
								{{ formatCurrency(row.amount) }}
							</td>
						</tr>
					</template>
				</tbody>
				<tfoot>
					<tr v-if="pg.total_discount_amount > 0">
						<td></td>
						<td></td>
						<td class="pb-2 pr-2 pt-4 text-right font-medium">
							总金额（不含折扣）
						</td>
						<td class="whitespace-nowrap pb-2 pr-2 pt-4 text-right font-medium">
							{{ formatCurrency(pg.total_before_discount) }}
						</td>
					</tr>
					<tr v-if="pg.total_discount_amount > 0">
						<td></td>
						<td></td>
						<td class="pb-2 pr-2 pt-4 text-right font-medium">
							总折扣金额
						</td>
						<td class="whitespace-nowrap pb-2 pr-2 pt-4 text-right font-medium">
							{{
								$team.pg.jerp_partner
									? formatCurrency(pg.total_discount_amount)
									: formatCurrency(0)
							}}
						</td>
					</tr>
					<tr v-if="pg.gst > 0">
						<td></td>
						<td></td>
						<td class="pb-2 pr-2 pt-4 text-right font-medium">
							总金额（不含税）
						</td>
						<td class="whitespace-nowrap pb-2 pr-2 pt-4 text-right font-medium">
							{{ formatCurrency(pg.total) }}
						</td>
					</tr>
					<tr v-if="pg.gst > 0">
						<td></td>
						<td></td>
						<td class="pb-2 pr-2 pt-4 text-right font-medium">
							IGST @ {{ Number(gstPercentage * 100) }}%
						</td>
						<td class="whitespace-nowrap pb-2 pr-2 pt-4 text-right font-medium">
							{{ pg.gst }}
						</td>
					</tr>
					<tr>
						<td></td>
						<td></td>
						<td class="pb-2 pr-2 pt-4 text-right font-medium">总计</td>
						<td class="whitespace-nowrap pb-2 pr-2 pt-4 text-right font-medium">
							{{
								formatCurrency(pg.total + pg.gst)
							}}
						</td>
					</tr>
					<template
						v-if="
							pg.total !== pg.amount_due &&
['Paid', 'Unpaid'].includes(pg.status)
						"
					>
						<tr>
							<td></td>
							<td></td>
							<td class="pr-2 text-right font-medium">已应用余额</td>
							<td class="whitespace-nowrap py-3 pr-2 text-right font-medium">
								- {{ formatCurrency(pg.applied_credits) }}
							</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td class="pr-2 text-right font-medium">应付金额</td>
							<td class="whitespace-nowrap py-3 pr-2 text-right font-medium">
								{{ formatCurrency(pg.amount_due) }}
							</td>
						</tr>
					</template>
				</tfoot>
			</table>
		</div>
		<div class="py-20 text-center" v-if="$resources.invoice.loading">
			<Button :loading="true">加载中</Button>
		</div>
	</div>
</template>
<script>
import { getPlans } from '../data/plans';

export default {
	name: 'InvoiceTable',
	props: ['invoiceId'],
	resources: {
		invoice() {
			return {
				type: 'document',
				pagetype: 'Invoice',
				name: this.invoiceId
			};
		}
	},
	computed: {
		groupedLineItems() {
			if (!this.pg) return {};
			const groupedLineItems = {};
			for (let item of this.pg.items) {
				groupedLineItems[item.document_type] =
					groupedLineItems[item.document_type] || [];
				groupedLineItems[item.document_type].push(item);
			}
			return groupedLineItems;
		},
		pg() {
			return this.$resources.invoice.pg;
		},
		gstPercentage() {
			return this.$team.pg.billing_info.gst_percentage;
		}
	},
	methods: {
		formatPlan(plan) {
			let planDoc = getPlans().find(p => p.name === plan);
			if (planDoc) {
				let china = this.$team.pg.currency === 'CNY';
				return this.$format.userCurrency(
					china ? planDoc.price_cny : planDoc.price_usd
				);
			}
			return plan;
		},
		formatCurrency(value) {
			if (!this.pg) return;
			let currency = this.pg.currency;
			return this.$format.currency(value, currency);
		}
	}
};
</script>