<template>
	<Dialog
		v-model="show"
		:options="{
			title: '应用发布前需要完成的步骤',
			size: '2xl'
		}"
	>
		<template #body-content>
			<div v-if="appDoc.pg.review_stage === 'Ready for Review'">
				<p class="text-p-base text-gray-700">
					您的应用已发送给我们的团队进行审核。请等待审核完成。
				</p>
			</div>
			<ObjectList v-else :options="listOptions" />
		</template>
		<template #actions v-if="appDoc.pg.review_stage !== 'Ready for Review'">
			<Button
				class="w-full"
				variant="solid"
				label="标记应用为待审核"
				:loading="appDoc.markAppReadyForReview.loading"
				:disabled="$resources.reviewSteps.data.some(step => !step.completed)"
				@click="appDoc.markAppReadyForReview.submit"
			/>
		</template>
	</Dialog>
</template>

<script>
import { getCachedDocumentResource } from 'jingrow-ui';
import ObjectList from '../ObjectList.vue';

export default {
	props: ['app'],
	components: {
		ObjectList
	},
	data() {
		return {
			show: true
		};
	},
	resources: {
		reviewSteps() {
			return {
				url: 'jcloud.api.marketplace.review_steps',
				params: {
					name: this.app
				},
				cache: ['Marketplace App Review Steps', this.app],
				auto: true,
				initialData: []
			};
		}
	},
	computed: {
		appDoc() {
			return getCachedDocumentResource('Marketplace App', this.app);
		},
		listOptions() {
			return {
				data: () => this.$resources.reviewSteps.data,
				hideControls: true,
				columns: [
					{
						label: '步骤',
						fieldname: 'step'
					},
					{
						label: '已完成',
						fieldname: 'completed',
						type: 'Icon',
						width: 0.3,
						align: 'center',
						Icon(value) {
							return value ? 'check' : '';
						}
					},
					{
						label: '',
						type: 'Button',
						width: 0.2,
						align: 'right',
						Button: ({ row }) => {
							let route = `/apps/${this.app}/`;
							route += row.step.includes('Publish') ? 'versions' : 'listing';

							return {
								label: '查看',
								variant: 'ghost',
								route
							};
						}
					}
				]
			};
		}
	}
};
</script>