<template>
	<div class="wcc-loader-view" v-if="loading">
		<div class="wcc-page-content">
			<div class="wcc-page wcc-page-default" style="min-height:128px;padding:14px;">
				<card-loader/>
			</div>
		</div>
	</div>
	<div class="wcc-page-content" v-else>
		<div class="wcc-page wcc-page-default">
			<div class="wcc-cookie-icon"></div>
			<div class="wcc-cookie-policy-wrapper">
				<div class="wcc-cookie-policy-content">
					<div class="wcc-cookie-policy-title">
						<div class="wcc-default-title">{{ $i18n.__("Cookie policy", "webtoffee-cookie-consent") }}</div>
						<div class="wcc-policy-lastedited">{{ $i18n.__("Last edited:", "webtoffee-cookie-consent") }}{{ modifiedDate }}</div>
					</div>
					<div class="wcc-policy-buttons">
						<button class="wcc-button-edit" @click="redirectToEditPage" :data-test-id="`wcc-edit-cookie-policy-edit-button`">{{ $i18n.__("Edit", "webtoffee-cookie-consent") }}</button>
						<button class="wcc-button-policy" @click="redirectToCreatedPage" :data-test-id="`wcc-view-cookie-policy-button`">{{ $i18n.__("View cookie policy", "webtoffee-cookie-consent") }}</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import api from "@/modules/policies/helpers/api";
import CardLoader from "@/components/elements/CardLoader.vue";

export default {
	name: "EditPage",
	components: {
		CardLoader
	},
	data() {
		return {
			policyData: '', // Add policyData property
			dateCreated: '',
			modifiedDate: '',
			policyPage: null,
			loading: true, // Initialize loading to true
		};
	},
	methods: {
		async ModifiedDateFormat() {
			// Get the updated date
			this.modifiedDate = await api.updatedDate(this.policyPage);
		},
		async redirectToEditPage() {
			try {
				// Show renew consent warning
				let renewData = {};
				renewData.status = true;
				this.setOption("renew_consent_notice", renewData);
				this.saveOptions();
				this.updateConsentStatus(true);
				this.getPolicyPage();
				const url = `${window.location.origin}/wp-admin/post.php?&post=${this.policyPage}&action=edit`;
				window.open(url, '_blank');
			} catch (error) {
				console.error('API Error:', error);
			} finally {
				this.loading = false;
			}
		},
		async redirectToCreatedPage() {
			// Open a new tab/window synchronously
			const newWindow = window.open('', '_blank');
			try {
				this.getPolicyPage();
				const permalinkUrl = `${window.location.origin}?&page_id=${this.policyPage}`;
				// Set the location of the new window
				newWindow.location.href = permalinkUrl;
			} catch (error) {
				console.error('API Error:', error);
				// Close the new window if there was an error
				newWindow.close();
			} finally {
				this.loading = false;
			}
		},
		getPolicyPage() {
			this.policyPage = this.$store.state.settings.info.privacy_policy.page;
		},

	},
	async created() {
		try {
			this.getPolicyPage();
			// Fetch default data
			this.ModifiedDateFormat();
		} catch (error) {
			console.error('API Error:', error);
		} finally {
			this.loading = false;
		}
	},
	mounted() {
		this.getPolicyPage();
		// If you want to convert the empty string to null for better comparison, you can do this
		if (this.policyPage === '') {
			this.policyPage = null;
		}
	}
};
</script>

<style lang="scss" scoped>
.wcc-page-content {
	background: #FAFDFF;
}
.wcc-page-default {
	display: flex;
	justify-content: center;
	align-items: center;
}
.wcc-cookie-policy-wrapper {
	max-width: 926px;
	height: 98px;
	flex-shrink: 0;
	background: #FFF;
	border-radius: 6px;
	box-shadow: 0px 4px 12px 0px rgba(85, 101, 125, 0.10);
	position: relative;
	top: 60px;
	display: flex;
	align-items: center;
	width: 100%;
}

.wcc-cookie-icon {
	width: 446px;
	height: 446px;
	top: 23px;
	position: absolute;
	flex-shrink: 0;
	background-image: url("~@/assets/cookie.svg");
}

.wcc-cookie-policy-content {
	width: 100%;
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 0 40px;
}

.wcc-cookie-policy-title {
	display: flex;
	flex-direction: column;
}

.wcc-default-title {
	font-size: 16px;
	font-style: normal;
	font-weight: 600;
	color: #000000;
	padding: 4px 0px;
}

.wcc-policy-buttons {
	display: flex;
	gap: 10px;
}

.wcc-button-edit {
	display: inline-flex;
	padding: 12px 20px;
	justify-content: flex-end;
	align-items: flex-end;
	color: #3157A6;
	font-size: 14px;
	font-style: normal;
	font-weight: 600;
	line-height: 16px;
	border-radius: 4px;
	border: 1.8px solid var(--Primary-colour-primary-500, #3157A6);
	background: #FFF;
	box-shadow: 0px 3px 4px 0px rgba(85, 101, 125, 0.15);
	cursor: pointer;
}

.wcc-button-policy {
	display: inline-flex;
	padding: 12px 20px;
	justify-content: center;
	align-items: center;
	color: #FFF;
	border-radius: 4px;
	border: 1.5px solid var(--Primary-colour-primary-500, #3157A6);
	background: #3157A6;
	box-shadow: 0px 4px 12px 0px rgba(85, 101, 125, 0.10);
	cursor: pointer;
}

.wcc-policy-lastedited {
	font-size: 14px;
	font-style: normal;
	font-weight: 400;
	line-height: normal;
	padding: 4px 0px;
}
</style>
