<template>
	<NavBar />
	<view class="container">
		<view class="title">
			<PageTitle :title="$t('form.personalData.personalInformation.residentialVerification')" />
		</view>
		<view class="content">
			<uni-forms ref="identityForm" :modelValue="addressFormData">
				<view class="voucherTypeWrapper">
					<view :class="['voucherType', selectedVoucherStandBy === '1' ? 'active' : '']" @click="selectVoucherType('1')">
						<image src="/static/user/drivingLicense.png" mode="aspectFit" class="voucherIcon"></image>
						<text class="label">{{ $t('form.driverLicense.label') }}</text>
					</view>
					<view :class="['voucherType', selectedVoucherStandBy === '2' ? 'active' : '']" @click="selectVoucherType('2')">
						<image src="/static/user/utilityBill.png" mode="aspectFit" class="voucherIcon"></image>
						<text class="label">{{ $t('form.utilityBill.label') }}</text>
					</view>
					<view :class="['voucherType', selectedVoucherStandBy === '3' ? 'active' : '']" @click="selectVoucherType('3')">
						<image src="/static/user/bankStatement.png" mode="aspectFit" class="voucherIcon"></image>
						<text class="label">{{ $t('form.bankStatement.label') }}</text>
					</view>
				</view>
				<view class="voucherImgTitle">{{ $t('form.voucher.label_voucher') }}</view>
				<view class="voucherImgDesc">{{ $t('form.voucher.tips') }}</view>
				<view class="voucherImgWrapper">
					<uni-forms-item name="voucherImg0">
						<uni-file-picker ref="voucher_img_0" v-model="voucherImg0" file-mediatype="image" mode="grid" :limit="1" @select="(e) => select(e, 'voucher_img_0')" />
						<view style="max-width: 140px">
							<Progress :progress="voucher_img_0_progress" :error="voucher_img_0_error" />
						</view>
					</uni-forms-item>
				</view>
				<view class="normWrapper">
					<view class="normTitle">{{ $t('form.voucher.uploadNorm') }}</view>
					<view class="normContent">
						<view class="normColumn">
							<view class="normItem">
								<view class="point"></view>
								{{ $t('form.voucher.norm1') }}
							</view>
							<view class="normItem">
								<view class="point"></view>
								{{ $t('form.voucher.norm3') }}
							</view>
						</view>
						<view class="normColumn">
							<view class="normItem">
								<view class="point"></view>
								{{ $t('form.voucher.norm2') }}
							</view>
							<view class="normItem">
								<view class="point"></view>
								{{ $t('form.voucher.norm4') }}
							</view>
						</view>
					</view>
				</view>
			</uni-forms>
			<view v-if="proven === 'false'">
				<button :disabled="voucher_img_0_progress !== 100 || btnLoading" class="primaryButton" @click="handleSaveVoucher">
					<image v-show="btnLoading" src="/static/loadingCircle.svg" mode="aspectFit" style="width: 16px; height: 16px"></image>
					{{ $t('form.uploadDocument') }}
				</button>
			</view>
		</view>
	</view>
</template>

<script>
import { useUserStore } from '@/stores/user';

import { getAuthAddress, saveVoucher } from '@/services/user/completeInfo.ts';
import { imgUpload, getImgPath } from '@/services/common.ts';
import { UserLanguage } from '../../../../utils/const';
export default {
	data() {
		return {
			imgBaseUrl: '',
			btnLoading: false,
			proven: 'false',
			voucherImg0: {
				// url: ''
			},
			voucher_img_0_progress: 100,
			voucher_img_0_error: false,
			selectedVoucherStandBy: undefined,
			rawAddressFormData: {},
			addressFormData: { voucher_type: 'DZZM' },
			fieldsNeedCheck: ['voucher_type', 'voucher_standby_1', 'voucher_img_0']
		};
	},
	methods: {
		async getAuthInfo() {
			const res = await getAuthAddress();
			if (res && res.code === 0) {
				if (res.data) {
					this.rawAddressFormData = { ...res.data } ?? {};
					this.addressFormData = { ...this.addressFormData, ...res.data } ?? {};
					this.selectedVoucherStandBy = res.data?.voucher_standby_1;
					if (res.data?.voucher_img_0) {
						this.voucherImg0 = { url: this.imgBaseUrl + res.data.voucher_img_0 };
					}
				}
			}
		},
		async getImgPathData() {
			const res = await getImgPath();
			if (res && res.code === 0) {
				this.imgBaseUrl = res.data;
			}
		},
		selectVoucherType(standby) {
			this.selectedVoucherStandBy = standby;
			this.addressFormData.voucher_standby_1 = standby;
		},
		async select(e, field) {
			this[`${field}_progress`] = 0;
			this[`${field}_error`] = false;
			let step = 1;
			const timer = setInterval(() => {
				if (this[`${field}_progress`] > 85) {
					step = 0.1;
				} else if (this[`${field}_progress`] > 35) {
					step = 0.8;
				} else if (this[`${field}_progress`] > 15) {
					step = 0.4;
				}
				if (this[`${field}_progress`] + step > 98) {
					clearInterval(timer);
					return;
				}
				this[`${field}_progress`] += step;
			}, 34);
			const res = await imgUpload(e);
			this.$refs[field].files[0].progress = 100;
			this[`${field}_progress`] = 100;
			if (res && res.code === 0) {
				this.addressFormData[field] = res.data?.imgNameList?.[0];
				uni.showToast({
					icon: 'none',
					title: this.$t('common.success.uploadSuccess')
				});
			} else {
				this[`${field}_error`] = true;
				this.$cusModal.showModal({
					type: 'message',
					status: 'warning',
					contentText: res.msg ?? this.$t('common.error.uploadError')
				});
			}
		},
		async handleSaveVoucher() {
			let params = {
				qcc_language: UserLanguage,
				voucher: [
					{
						id: this.rawAddressFormData.id ?? ''
					}
				]
			};
			if (!this.selectedVoucherStandBy) {
				this.$cusModal.showModal({
					type: 'message',
					status: 'warning',
					contentText: this.$t('form.voucherType.required')
				});
				return;
			}
			this.fieldsNeedCheck.forEach((field) => {
				if (this.addressFormData[field] !== this.rawAddressFormData[field]) {
					params.voucher[0][field] = {
						edit_power: 1,
						nv: this.addressFormData[field],
						ov: this.rawAddressFormData[field],
						voucher_type: 'DZZM'
					};
				}
			});
			this.btnLoading = true;
			const res = await saveVoucher(params);
			this.btnLoading = false;
			if (res && res.code === 0) {
        uni.redirectTo({
          url: '/pages/user/personalInformation/address/success/index'
        });
			} else {
				this.$cusModal.showModal({
					type: 'message',
					status: 'warning',
					contentText: res.msg ?? this.$t('common.error.sysError')
				});
			}
		}
	},
	async created() {
		const userStore = useUserStore();
		this.userEmail = userStore.userInfo.email;
		this.selectVoucherType('1');
		await this.getImgPathData();
		await this.getAuthInfo();
	},
	onLoad(params) {
		this.proven = params.proven;
	}
};
</script>
<style lang="scss" scoped>
@import './index.scss';
</style>
