policy-change-information.js 7.15 KB
import api from '@/api/api'
import {
	formdata,
	httpPost
} from '@/api/fetch-api.js'

import Auth from '@components/auth/auth.vue';
import PolicyHeadList from "./policy-head-list.vue";
import DatePicker from '@/components/date-picker/date-picker.vue'
import modalComp from '@/components/modal-comp/modal-comp.vue';
import modalUploadCardComp from '@/components/modal-upload-card-comp/modal-upload-card-comp.vue';

export default {
	data() {
		return {
			loading: false,
			showForm: false,
			key: 'value',
			isOwner: true,
			checked: false,
			modalUploadCardVisiable: false,
			selectedPolicies: [],
			data: null,
			// 保单信息
			policy: null,
			// ID证件照引用
			idFront: null,
			idBack: null,

			// 模态窗
			modalSimpleVisiable: false,
			modalVisiable: false,
			targetPath: "",
			modalIcon: "succ",
			modalContent: "",
		}
	},
	components: {
		modalUploadCardComp,
		PolicyHeadList,
		Auth,
		DatePicker,
		modalComp
	},
	computed: {
		locale() {
			return this.$i18n.locale || 'tc';
		},
		i18n() {
			return this.$i18n.messages && this.$i18n.locale ? this.$i18n.messages[this.$i18n.locale] : {};
		},
		sexCandidates() {
			let i18n = this.$i18n.messages[this.$i18n.locale];
			return i18n.infomationImprove.candidates.sex;
		},
		idTypeCandidates() {
			let i18n = this.$i18n.messages[this.$i18n.locale];
			return i18n.infomationImprove.candidates.idType;
		},
	},
	methods: {
		showModal(content, icon) {
			icon = !icon || typeof icon === "undefined" ? "succ" : icon;
			this.modalIcon = icon;
			this.modalContent = content;
			this.modalVisiable = true;
		},
		modalCallback() {
			this.modalVisiable = false;
		},
		loadData() {
			if (this.isOwner && this.data) {
				return;
			}

			if (this.loading) {
				return;
			}
			this.loading = true;
			this.data = null;
			let param = {
				policyId: this.selectedPolicies[0].id,
				policyCode: this.selectedPolicies[0].code
			};
			httpPost({
				url: api.policyDetail,
				data: param,
				sid: true
			}).then(res => {
				this.loading = false;
				if (res) {
					this.policy = res;
					let clientName = res.clientNameCn ? res.clientNameCn : res.clientNameEn;
					let insuredName = res.insuredNameCn ? res.insuredNameCn : res.insuredNameEn;
					this.policy.clientName = clientName;
					this.policy.insuredName = insuredName;
					// 证件类型、证件号要备份,用于比较
					this.policy.clientIdTypeBak = this.policy.clientIdType;
					this.policy.clientIdNumberBak = this.policy.clientIdNumber;

					this.policy.insuredIdTypeBak = this.policy.insuredIdType;
					this.policy.insuredIdNumberBak = this.policy.insuredIdNumber;
					this.initData();
				}
			}).catch(err => {
				this.loading = false;
				this.handleErrResponse(err);
			});
		},
		initData() {
			this.idFront = null;
			this.idBack = null;
			let res = this.policy;
			if (res) {
				let isOwner = this.isOwner;
				let data = {
					name: isOwner ? res.clientName : res.insuredName,
					idType: isOwner ? res.clientIdType : res.insuredIdType,
					idNumber: isOwner ? res.clientIdNumber : res.insuredIdNumber,
					idExpireAt: isOwner ? res.clientExpireAt : res.insuredExpireAt,
					nature: isOwner ? res.clientNationality : res.insuredNationality,
					company: isOwner ? res.clientCompany : res.insuredCompany,
					marriage: isOwner ? res.clientMarriage : res.insuredMarriage,
					sex: isOwner ? res.clientSex : res.insuredSex,
					birthday: isOwner ? res.clientBirthday : res.insuredBirthday
				};
				data.sex = data.sex ? data.sex : "M";
				this.$set(this, 'data', data);
			}
		},
		handlePolicySelect(data) {
			this.selectedPolicies = data;
			this.loadData();
		},
		handleUpdatePolicy() {
			let showUploadPicDialog = false;
			if (this.isOwner) {
				showUploadPicDialog = (this.policy.clientIdType != this.data.idType || this.policy.clientIdNumber != this.data.idNumber);
			} else {
				showUploadPicDialog = (this.policy.insuredIdType != this.data.idType || this.policy.insuredIdNumber != this.data.idNumber);
			}
			if (showUploadPicDialog) {
				this.modalUploadCardVisiable = true;
			} else {
				this.updatePolicyInfo();
			}
		},
		handleUpdatePolicyWithFile(data) {
			this.idFront = data.front;
			this.idBack = data.back;
			this.updatePolicyIdPic().then(() => {
				this.updatePolicyInfo();
			});
		},
		updatePolicyInfo() {
			if (this.loading) {
				return;
			}
			this.loading = true;
			let param = JSON.parse(JSON.stringify(this.data));
			delete param.birthday;
			delete param.sex;
			param.modifyObj = this.isOwner ? 1 : 2;
			let policies = [];

			this.selectedPolicies.forEach(element => {
				policies.push({ policyId: element.id, policyCode: element.code });
			});

			param.policies = policies;
			httpPost({ url: api.updatePolicyInfo, data: param, sid: true }).then(res => {
				this.loading = false;
				this.showModal(this.i18n.policyChangeInformation.success);
			}).catch(err => {
				this.loading = false;
				this.handleErrResponse(err);
			});
		},
		updatePolicyIdPic() {
			return new Promise((resolve, reject) => {
				if (this.loading) {
					return;
				}
				this.loading = true;
				let param = {
					policyId: this.selectedPolicies[0].id,
					policyCode: this.selectedPolicies[0].code,
					modifyObj: this.isOwner ? 1 : 2,
					front: this.idFront,
					back: this.idBack
				};
				this.modalUploadCardVisiable = false;
				formdata({ url: api.idPicUpload, data: param, sid: true }).then(res => {
					this.loading = false;
					resolve();
				}).catch(err => {
					this.loading = false;
					this.handleErrResponse(err);
				});
			});
		},
		switchPolicyRole(role) {
			if (this.isOwner && role == 1) {
				return;
			}
			if (!this.isOwner && role == 2) {
				return;
			}
			if (this.policy) {
				if (role == 1) {
					// 需要缓存结果,注意取反,此处存在受保人信息
					this.policy.insuredName = this.data.name;
					this.policy.insuredIdType = this.data.idType;
					this.policy.insuredIdNumber = this.data.idNumber;
					this.policy.insuredExpireAt = this.data.idExpireAt;
					this.policy.insuredNationality = this.data.nature;
					this.policy.insuredCompany = this.data.company;
					this.policy.insuredMarriage = this.data.marriage;
					this.policy.insuredSex = this.data.sex;
					this.policy.insuredBirthday = this.data.birthday;
				} else {
					// 需要缓存结果,注意取反,此处存在投保人信息
					this.policy.clientName = this.data.name;
					this.policy.clientIdType = this.data.idType;
					this.policy.clientIdNumber = this.data.idNumber;
					this.policy.clientExpireAt = this.data.idExpireAt;
					this.policy.clientNationality = this.data.nature;
					this.policy.clientCompany = this.data.company;
					this.policy.clientMarriage = this.data.marriage;
					this.policy.clientSex = this.data.sex;
					this.policy.clientBirthday = this.data.birthday;
				}
			}
			this.isOwner = role == 1 ? true : false;
			this.initData();
		},
		handleErrResponse(err) {
			if (err) {
				if (err.code == 404) {
					this.$refs.auth.noAuth();
				}
				if (err.errMsg) {
					this.showModal(errMsg, 'info');
				}
			}
		},
		userLogout() {
			this.showForm = false;
		},
		userLogin(data) {
			this.showForm = true;
		}
	},
	mounted() {
		this.isOwner = this.$route.query.u == 2 ? false : true;
	},
	created() { }
}