import { orderDetail, cancelOrder, getHospitalInfo } from "./service";
import history from "../../utils/history";
import { reportApi } from "../../utils/cloudMonitorHelper";
import {
	getSubscribeAuth,
	recordOpLog,
} from "../../../../core/utils/ywtService";

Component({
	data: {
		result: {},
		modalOpened: false,
		type: "",
		buttons: [
			{
				text: "不,先留着",
			},
			{
				text: "取消预约",
				extClass: "buttonBold",
			},
		],
		hospitalDistrictId: "",
		hospitalInfo: {},
	},

	didMount() {
		const { type, hospitalDistrictId } = this.$page.data.query;
		this.setData({
			type,
			hospitalDistrictId,
		});
		this.getBookingDetails();
		this.getHospital(hospitalDistrictId);
		/* 服务办结,核酸预约成功 */

		reportApi("核酸预约成功");
	},

	methods: {
		async getBookingDetails() {
			const {
				query = {
					orderId: "",
				},
			} = this.$page.data;
			my.showLoading();
			orderDetail({
				orderId: query.orderId,
			}).then((data) => {
				this.setData({
					result: data,
				});
				my.hideLoading();
			});
		},

		async getHospital(hospitalDistrictId) {
			const hospitalInfo = await getHospitalInfo({
				hospitalDistrictId,
			});
			this.setData({
				hospitalInfo: hospitalInfo.configInfo,
			});
		},

		handleOpenCheck(e) {
			const { url } = e.target.dataset;
			history.toH5(url);
		},

		handleBack() {
			const len = getCurrentPages().length;
			my.navigateBack({
				delta: len - 2,
			});
		},

		// 确认取消预约
		handleCancelBook() {
			my.confirm({
				title: "确认取消预约?",
				content: "已支付金额会退回原付款账户",
				confirmButtonText: "取消预约",
				cancelButtonText: "不,先留着",
				success: async ({ confirm }) => {
					if (confirm) {
						const {
							query = {
								orderId: "",
							},
						} = this.$page.data;
						await recordOpLog({ type: 191 });
						await getSubscribeAuth();

						cancelOrder({
							orderId: query.orderId,
							reason: "",
						}).then(() => {
							this.handleBack();
						});
					}
				},
			});
		},
	},
});