import { payHistory } from "./service";
import history from "../../utils/history";
import { tradePay } from "../../utils/tradePay";
import { reportCmPV_YL } from "../../utils/cloudMonitorHelper";
import { getSubscribeAuth } from "../../../../core/utils/ywtService";
Component({
	props: {},
	data: {
		empty: true,
		clinicCard: {},
		filterParams: {
			type: "9",
			cardNum: "",
			endTime: "",
			startTime: "",
			medicCards: [],
			timeShow: false,
		},

		/* 赛选项目 */
		typeOptions: [],
		statusOptions: [
			{
				value: "0",
				label: "待支付",
			},
			{
				value: "1",
				label: "已支付",
			},
		],
		showAll: false,
		showTimeSelector: true,
		defaultLabel: "待支付",
		defaultName: "待支付",
		defaultStatus: "待支付",
	},

	didMount() {
		/* 服务预警，缴费记录查询 */
		reportCmPV_YL({
			title: "缴费记录查询",
		});
	},
	onInit() {},

	methods: {
		onFilterHandel(filters) {
			if (!this.hisPatientId) return;
			this.filters = filters;
			this.scrollRef.refresh();
		},

		saveRef(ref) {
			this.scrollRef = ref;
		},

		saveSubscribe(ref) {
			this.subscribe = ref;
		},

		getTimeChange(startTime, endTime) {
			if (!startTime || !endTime) {
				return true;
			}
			const time1 = Date.parse(startTime);
			const time2 = Date.parse(endTime);
			const timeDiff = time2 - time1;
			const duration = 15 * 24 * 60 * 60 * 1000;
			return timeDiff < 0 || timeDiff > duration;
		},

		onService(page) {
			const { status, startTime, endTime } = this.filters || {};
			if (!this.hisPatientId) return;
			if (status && status === "1") {
				if (!startTime || this.getTimeChange(startTime, endTime)) {
					my.showToast({
						type: "fail",
						content: "请选择15天以内的时间",
						duration: 1500,
					});
				}
			}

			return payHistory({
				...this.filters,
				status: status || "0",
				...page,
				hisPatientId: this.hisPatientId,
			});
		},

		onAutoFilter(patient) {
			if (patient) {
				this.hisPatientId = patient.hisPatientId;
				this.scrollRef.refresh();
			}
		},

		// 进入详情
		toDetail(e) {
			const {
				item: {
					type,
					outTradeNo,
					status,
					tradeNo,
					invoiceNo,
					hisPatientId,
					date,
					depName,
					amount,
					name,
					age,
					sex,
					prescribeDate,
					hisOrderNo,
					doctorName,
					doctorCode,
					total,
					hisClinicCode,
					deptName,
				},
			} = e.target.dataset;
			history.push({
				title: "缴费详情",
				query: {
					type: type || 0,
					outTradeNo,
					status,
					tradeNo,
					invoiceNo,
					hisPatientId,
					date,
					depName,
					amount,
					name,
					age,
					sex,
					prescribeDate,
					hisOrderNo,
					doctorName,
					doctorCode,
					total,
					hisClinicCode,
					deptName,
				},
				pageType: "hospital-payment-detail",
			});
		},

		// 付款
		async onPayment(e) {
			const { medicareBinded = false, item } = e.target.dataset; // 同意订阅消息后发起支付
			try {
				my.showLoading();
				// 获取智能消息推送授权
				await getSubscribeAuth();
				// 挂号
				if (item.type === 1) {
					await tradePay(
						{
							type: item.type,
							idNum: item.outTradeNo,
							depName: item.depName,
						},
						{
							tradeType: "Appointment",
						}
					);
				} else if (item.type === 2) {
					// 充值
					await tradePay(
						{
							type: item.type,
							idNum: item.outTradeNo,
							amount: item.amount,
						},
						{
							tradeType: "Appointment",
						}
					);
				} else {
					// 门诊订单
					await tradePay(
						{
							useBalance: !medicareBinded,
							useMedicare: medicareBinded,
							outTradeNo: item.outTradeNo,
							...item,
						},
						{
							tradeType: "Outpatient",
						}
					);
				}
			} catch (error) {
			} finally {
				my.hideLoading();
			}
			// 支付成功去订单详情页
			this.toDetail({
				target: {
					dataset: {
						outTradeNo: item.outTradeNo,
						type: item.type ? item.type : 0,
						...item,
					},
				},
			}); // 发起消息订阅

			// this.subscribe.subscribeMessage();
		},
	},
});
