<template>
	<div class="p-5">
		<ObjectList :options="options" />
	</div>
</template>
<script>
import { h } from 'vue';
import { toast } from 'vue-sonner';
import { FeatherIcon, Tooltip, Badge, Button } from 'jingrow-ui';
import ObjectList from '../ObjectList.vue';
import Clock from '~icons/lucide/clock';
export default {
	name: 'PartnerApprovalRequests',
	components: {
		ObjectList,
	},
	computed: {
		options() {
			return {
				pagetype: 'Partner Approval Request',
				fields: ['approved_by_partner', 'status'],
				columns: [
					{
						label: '客户邮箱',
						fieldname: 'customer_email',
					},
					{
						label: '客户团队ID',
						fieldname: 'requested_by',
					},
					{
						label: '提交时间',
						fieldname: 'creation',
						width: 0.6,
						format(value) {
							return Intl.DateTimeFormat('en-US', {
								year: 'numeric',
								month: 'long',
								day: 'numeric',
							}).format(new Date(value));
						},
					},
					{
						label: 'Jingrow审批',
						fieldname: 'approved_by_jingrow',
						type: 'Component',
						align: 'center',
						width: 0.6,
						component({ row }) {
							if (row.approved_by_jingrow) {
								return h(
									Tooltip,
									{
										text: '已批准',
									},
									() =>
										h(FeatherIcon, {
											name: 'check-circle',
											class: 'h-4 w-4 text-green-600',
										}),
								);
							} else {
								return h(
									Tooltip,
									{
										text: '等待审批',
									},
									() =>
										h(Clock, {
											class: 'h-4 w-4 text-yellow-500',
										}),
								);
							}
						},
					},
					{
						label: '合作伙伴审批',
						fieldname: 'approved_by_partner',
						type: 'Component',
						align: 'center',
						width: 0.6,
						component({ row }) {
							if (row.approved_by_partner) {
								return h(
									Tooltip,
									{
										text: '已批准',
									},
									() =>
										h(FeatherIcon, {
											name: 'check-circle',
											class: 'h-4 w-4 text-green-600',
										}),
								);
							} else {
								return h(
									Tooltip,
									{
										text: '等待审批',
									},
									() =>
										h(Clock, {
											class: 'h-4 w-4 text-yellow-500',
										}),
								);
							}
						},
					},
					{
						label: '',
						type: 'Component',
						width: 0.8,
						align: 'center',
						component({ row, listResource }) {
							if (row.status === 'Pending' && row.approved_by_partner === 0) {
								return h(Button, {
									label: '批准',
									class: 'text-md',
									variant: 'subtle',
									onClick: () => {
										toast.promise(
											listResource.runDocMethod.submit({
												method: 'approve_partner_request',
												name: row.name,
											}),
											{
												loading: '正在批准...',
												success: '审批请求已发送至Jingrow',
												error: '批准失败',
											},
										);
									},
								});
							} else if (
								row.status === 'Pending' &&
								row.approved_by_jingrow === 0
							) {
								return h(Badge, {
									label: "等待Jingrow审批",
									theme: 'blue',
									variant: 'subtle',
									size: 'md',
								});
							}
						},
					},
				],
				filters: {
					partner: this.$team.pg.name,
				},
				orderBy: 'creation desc',
			};
		},
	},
};
</script>