<template>
	<FormControl class="mt-4" v-model="billing_name" label="账单名称" />
	<AddressForm
		ref="address-form"
		class="mt-4"
		v-model:address="billingInformation"
	/>
	<ErrorMessage
		class="mt-2"
		:message="$resources.updateBillingInformation.error"
	/>
	<div class="flex w-full justify-end">
		<Button
			class="mt-2 w-full sm:w-fit"
			variant="solid"
			:loading="$resources.updateBillingInformation.loading"
			@click="$resources.updateBillingInformation.submit"
		>
			更新账单信息
		</Button>
	</div>
</template>

<script>
import { DashboardError } from '../utils/error';
import AddressForm from './AddressForm.vue';
import { notify } from '@/utils/toast.js';

export default {
	name: 'UpdateBillingDetailsForm',
	emits: ['updated'],
	components: {
		AddressForm
	},
	data() {
		return {
			billing_name: '',
			billingInformation: {
				address: '',
				city: '',
				state: '',
				postal_code: '',
				country: '',
				gstin: ''
			}
		};
	},
	resources: {
		currentBillingInformation() {
			return {
				url: 'jcloud.api.account.get_billing_information',
				auto: true,
				onSuccess(billingInformation) {
					if ('country' in (billingInformation || {})) {
						Object.assign(this.billingInformation, {
							address: billingInformation.address_line1,
							city: billingInformation.city,
							state: billingInformation.state,
							postal_code: billingInformation.pincode,
							country: billingInformation.country,
							gstin:
								billingInformation.gstin == 'Not Applicable'
									? ''
									: billingInformation.gstin
						});
						this.billing_name = billingInformation.billing_name;
					}
				}
			};
		},
		updateBillingInformation() {
			return {
				url: 'jcloud.api.account.update_billing_information',
				makeParams() {
					return {
						billing_details: {
							...this.billingInformation,
							billing_name: this.billingInformation.billing_name
						}
					};
				},
				onSuccess() {
					notify({
						title: '地址更新成功！'
					});
					this.$emit('updated');
				},
				validate() {
					var billing_name = this.billing_name.trim();
					var billingNameRegex = /^[a-zA-Z0-9\-\'\,\.\s]+$/;
					var billingNameValid = billingNameRegex.test(billing_name);
					if (!billingNameValid) {
						throw new DashboardError(
							'账单名称包含无效字符'
						);
					}
					this.billing_name = billing_name;
					return this.$refs['address-form'].validateValues();
				}
			};
		}
	}
};
</script>