<template>
	<Dialog
		v-model="show"
		:options="{
			title: $t('Steps to complete before publishing your app'),
			size: '2xl'
		}"
	>
		<template #body-content>
			<div v-if="appDoc.pg.review_stage === 'Ready for Review'">
				<p class="text-p-base text-gray-700">
					{{ $t('Your app has been sent to our team for review. Please wait for the review to complete.') }}
				</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="$t('Mark app as ready for review')"
				: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: this.$t('Step'),
						fieldname: 'step'
					},
					{
						label: this.$t('Completed'),
						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: this.$t('View'),
								variant: 'ghost',
								route
							};
						}
					}
				]
			};
		}
	}
};
</script>