import { doctorInfo, doctorOrderSourceList, getDepDoctorList } from "./service";
import { doctorSchedule } from "../../../../core/utils/ywtService";
import * as utils from "../../utils";
const datePeriodMap = {
	全天: 1,
	上午: 2,
	下午: 3,
	夜间: 4,
	下半夜: 5,
	午间: 6,
};
Component({
	props: {},
	data: {
		doctor: {
			doctorId: "",
			name: "",
			picUrl: "",
			level: "",
			description: "",
			specialty: "",
			departments: [],
		},
		days: [],
		startTime: "",
		endTime: "",
		showIntro: false,
		showRange: false,
		rangeItem: {},
		routeState: {},
	},

	didMount() {
		const d = new Date();
		const startTime = d.toLocaleDateString().replace(/\//g, "-");
		d.setDate(d.getDate() + 7);
		const endTime = d.toLocaleDateString().replace(/\//g, "-");
		this.setData({
			startTime,
			endTime,
		});
		const {
			query = {
				depId: "",
				doctorId: "",
			},
		} = this.$page.data;
		this.init(query);
	},

	methods: {
		init(query) {
			my.showLoading();
			Promise.all([
				doctorInfo({
					doctorId: query.doctorId,
				}).then((res) => {
					this.setData({
						doctor: res,
					});
				}),
				getDepDoctorList({
					depId: query.depId,
					depCode: query.depCode,
					doctorCode: query.doctorCode,
				}).then((result) => {
					const list = result.list[0].orderSources || [];
					const days = [];
					list.forEach((item) => {
						const date = item.serviceDate;
						const temp = days.find((m) => m.date === date);
						if (temp) {
							temp.orderSources.push(item);
						} else {
							days.push({
								date,
								today: date === utils.cuttingDate().date,
								orderSources: [item],
							});
						}
					});
					this.setData({
						days,
					});
				}),
			]).then(() => {
				my.hideLoading();
			});
		},

		// 查看更多
		showIntro() {
			this.setData({
				showIntro: true,
			});
		},

		onClose() {
			this.setData({
				showIntro: false,
			});
		},

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

		// 跳转预约
		async onAppointment({ target }) {
			const cost = target.dataset.row.list[0].cost || "0";
			let routeState = {};
			const {
				query = {
					depId: "",
				},
			} = this.$page.data;
			const { doctor } = this.data;
			const depName = (
				doctor.departments.find((m) => m.departmentId === query.depId) || {
					name: "",
				}
			).name;

			const { hospitalId, depCode, doctorCode, depId, yuanqu } =
				this.getQuery();
			let doctorInfo = {};
			let newList = [];
			let finalList = [];
			const [result] = await doctorSchedule({
				hospitalId: Number(hospitalId),
				deptCode: depCode,
				doctorCode,
				serviceDate: target.dataset.row.date,
				period: datePeriodMap[target.dataset.row.name],
				depId,
			});
			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,
					});
				});
				routeState = {
					query: {
						level: doctor.level,
						depName,
						doctorId: doctor.doctorId,
						doctorName: doctor.name,
						queueNum: "",
						payType: "就诊时支付",
						districtName: doctor.districtName,
						datePeriod,
						deptCode,
						doctorCode,
						fee,
						feeStr,
						treatFee,
						treatFeeStr,
						deptName,
						registeredDate: serviceDate,
						registeredFee: fee,
						medicalFee: treatFee,
						period,
						doctorName,
						deptId: depId,
						yuanqu,
					},
					title: "挂号确认",
					pageType: "order-pay",
				};
			}
			target.dataset.row.list = finalList;

			this.setData({
				showRange: true,
				rangeItem: target.dataset.row,
				routeState,
			});
		},

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