import { connect } from "herculex";
import history from "../../../utils/history";
import { doctorSchedule } from "../../../../../core/utils/ywtService";
/* 默认头像 */
const datePeriodMap = {
	全天: 1,
	上午: 2,
	下午: 3,
	夜间: 4,
	下半夜: 5,
	午间: 6,
};
const defaultUrl =
	"https://gw.alipayobjects.com/mdn/rms_373ab8/afts/img/A*TuMaTaHOpyUAAAAAAAAAAAAAARQnAQ";
Component(
	connect({
		mapStateToProps: {
			isLogin: ({ $global }) => $global.userInfo.isLogin,
		},
	})({
		props: {
			item: {},
			isFlex: true,
			isRange: false,
			componentData: {},
		},
		data: {
			defaultUrl,
			rangeItem: {},
			routeState: {},
			showRange: false,
			showDoctor: false,
		},

		didMount() {},

		methods: {
			getQuery() {
				const { $routeConfig } = this.$page;
				return $routeConfig.query;
			},

			onPopupClose() {
				this.props.onClose();
			},

			/* 检查剩余 */
			checkRemain(item) {
				const { remain = 0 } = item;
				const disabled = remain < 1;

				if (disabled) {
					my.showToast({
						content: "该医生当前已约满，无法预约!",
					});
				}

				return !disabled;
			},

			async toDoctorPage({ target }) {
				const { item } = target.dataset;
				const { doctorId, doctorCode } = item;
				const {
					depCode,
					hospitalId,
					departmentId: queryDepartmentId,
					subHospitalId,
					subHospitalTitle,
				} = this.getQuery();
				const { componentData } = this.props;
				if (!this.checkRemain(item)) return;
				const { department } = componentData || {};
				const { departmentId } = department || {};
				history.push({
					query: {
						doctorId,
						// 医生唯一识别号
						depId: departmentId || queryDepartmentId, // 科室id
						depCode,
						doctorCode,
						hospitalId,
						subHospitalId,
						subHospitalTitle,
					},
					title: "医生主页",
					pageType: "doctor-page",
				});
			},

			// 点击预约进行弹窗时间段选择
			async onItemFollow({ target }) {
				const { item } = target.dataset;
				const cost = item.list[0].cost || "0";
				const {
					hospitalId,
					departmentId: queryDepartmentI,
					subHospitalId,
					subHospitalTitle,
				} = this.getQuery();
				if (!this.checkRemain(item)) return;
				const { rangeItem } = this.data;
				const { componentData } = this.props;
				const params = { ...rangeItem, ...componentData };
				const {
					level,
					doctorId,
					onlinePay = false,
					name: doctorName,
					doctorCode,
					deptCode,
					districtName,
				} = this.props.item;
				const { department, area } = params || {};
				const { name: depName, departmentId } = department || {};
				let doctorInfo;
				let routeState = {};
				let newList = [];
				let finalList = [];
				const [result] = await doctorSchedule({
					hospitalId: Number(hospitalId),
					deptCode,
					doctorCode,
					serviceDate: item.date,
					period: datePeriodMap[item.name],
				});
				if (result && result.data) {
					doctorInfo = result.data.length > 0 ? result.data[0] : {};
					const {
						datePeriod,
						deptCode,
						doctorCode,
						fee,
						feeStr,
						treatFee,
						treatFeeStr,
						serviceDate = "",
						deptName,
						period,
						doctorName,
					} = doctorInfo;
					newList = doctorInfo.schedule || [];
					finalList = newList.map((items) => {
						const { startTime, endTime, availableLeftNum } = items;
						return Object.assign(items, {
							sTime: startTime,
							eTime: endTime,
							remain: availableLeftNum,
							cost,
							serviceDate: item.date,
						});
					});
					routeState = {
						query: {
							level,
							depName,
							doctorId,
							doctorName,
							onlinePay,
							districtName,
							payType: "就诊时支付",
							// 新增参数
							datePeriod,
							deptCode,
							doctorCode,
							fee,
							feeStr,
							treatFee,
							treatFeeStr,
							deptName,
							registeredDate: serviceDate,
							registeredFee: fee,
							medicalFee: treatFee,
							period,
							doctorName,
							deptId: departmentId || queryDepartmentI,
							subHospitalId,
							subHospitalTitle,
						},
						title: "挂号确认",
						pageType: "order-pay",
					};
				}
				item.list = finalList;
				this.setData({
					showRange: true,
					rangeItem: item,
					routeState,
				});
			},

			onTimeRangeClose() {
				this.setData({
					showRange: false,
				});
			},

			onClosedDoctor() {
				this.setData({
					showDoctor: false,
				});
			},

			onShowDoctor() {
				this.setData({
					showDoctor: true,
				});
			},
		},
	})
);
