<template>
	<store-page-layout title="在线复诊配置" :show-back="true">
		<view class="page">
			<view class="field">
				<text class="label">互联网诊疗资质</text>
				<switch :checked="form.is_internet_medical === 1" color="#6ACDBB" @change="onMedicalChange" />
			</view>

			<view v-if="form.is_internet_medical !== 1 && storeOptions.length" class="field column">
				<text class="label">委托诊所</text>
				<picker :range="storeLabels" :value="delegateIndex" @change="onDelegateChange">
					<view class="picker-val">{{ storeLabels[delegateIndex] || '请选择' }}</view>
				</picker>
			</view>

			<view class="field column">
				<text class="label">复诊负责人</text>
				<picker :range="doctorLabels" :value="doctorIndex" @change="onDoctorChange">
					<view class="picker-val">{{ doctorLabels[doctorIndex] || '请选择' }}</view>
				</picker>
			</view>

			<view class="submit-btn" @click="submit">保存配置</view>
		</view>
	</store-page-layout>
</template>

<script>
import StorePageLayout from '@/subPackages/sub_platform_admin/components/store-page-layout.vue';
import {
	getPlatformStoreDetail,
	getDoctorOption,
	getInternetMedicalStoreOption,
	bindOnlineConsultation,
} from '@/api/platformStore.js';

export default {
	components: { StorePageLayout },
	data() {
		return {
			storeId: 0,
			form: {
				id: 0,
				is_internet_medical: 0,
				delegate_store_id: null,
				online_consultation_doctor_id: null,
			},
			storeOptions: [],
			doctorOptions: [],
		};
	},
	computed: {
		storeLabels() {
			return this.storeOptions.map((s) => s.name);
		},
		doctorLabels() {
			return this.doctorOptions.map((d) => d.name || d.nick_name || `医生#${d.id}`);
		},
		delegateIndex() {
			if (!this.form.delegate_store_id) return 0;
			const idx = this.storeOptions.findIndex((s) => Number(s.id) === Number(this.form.delegate_store_id));
			return idx >= 0 ? idx : 0;
		},
		doctorIndex() {
			if (!this.form.online_consultation_doctor_id) return 0;
			const idx = this.doctorOptions.findIndex((d) => Number(d.id) === Number(this.form.online_consultation_doctor_id));
			return idx >= 0 ? idx : 0;
		},
	},
	onLoad(options) {
		this.storeId = Number(options.id || 0);
		this.form.id = this.storeId;
		this.init();
	},
	methods: {
		async init() {
			const [detailWrap, storeWrap] = await Promise.all([
				getPlatformStoreDetail(this.storeId),
				getInternetMedicalStoreOption(),
			]);
			if (detailWrap && detailWrap.ok) {
				const d = detailWrap.data || {};
				this.form.is_internet_medical = Number(d.is_internet_medical) || 0;
				this.form.delegate_store_id = d.delegate_store_id || null;
				this.form.online_consultation_doctor_id = d.online_consultation_doctor_id || null;
			}
			this.storeOptions = (storeWrap && storeWrap.ok && storeWrap.data) ? storeWrap.data : [];
			await this.loadDoctors();
		},
		async loadDoctors() {
			const storeId = this.form.is_internet_medical === 1
				? this.storeId
				: (this.form.delegate_store_id || 0);
			if (!storeId) {
				this.doctorOptions = [];
				return;
			}
			const wrap = await getDoctorOption(storeId);
			this.doctorOptions = (wrap && wrap.ok && wrap.data) ? wrap.data : [];
		},
		onMedicalChange(e) {
			this.form.is_internet_medical = e.detail.value ? 1 : 0;
			if (this.form.is_internet_medical === 1) {
				this.form.delegate_store_id = null;
			}
			this.loadDoctors();
		},
		onDelegateChange(e) {
			const idx = Number(e.detail.value);
			const item = this.storeOptions[idx];
			this.form.delegate_store_id = item ? item.id : null;
			this.form.online_consultation_doctor_id = null;
			this.loadDoctors();
		},
		onDoctorChange(e) {
			const idx = Number(e.detail.value);
			const item = this.doctorOptions[idx];
			this.form.online_consultation_doctor_id = item ? item.id : null;
		},
		async submit() {
			uni.showLoading({ title: '保存中' });
			try {
				const wrap = await bindOnlineConsultation({
					id: this.storeId,
					is_internet_medical: this.form.is_internet_medical,
					delegate_store_id: this.form.delegate_store_id || null,
					online_consultation_doctor_id: this.form.online_consultation_doctor_id || null,
				});
				if (wrap && wrap.ok) {
					uni.showToast({ title: '保存成功', icon: 'success' });
					setTimeout(() => uni.navigateBack(), 500);
				}
			} finally {
				uni.hideLoading();
			}
		},
	},
};
</script>

<style lang="scss" scoped>
.page { padding: 24rpx; }
.field {
	display: flex; align-items: center; justify-content: space-between;
	background: #fff; border-radius: 16rpx; padding: 24rpx; margin-bottom: 16rpx; font-size: 28rpx;
}
.field.column { flex-direction: column; align-items: flex-start; gap: 16rpx; }
.label { font-weight: 500; }
.picker-val { color: #6ACDBB; font-size: 28rpx; }
.submit-btn {
	margin-top: 40rpx; background: linear-gradient(90deg, #00BFA6, #6ACDBB);
	color: #fff; text-align: center; padding: 24rpx; border-radius: 16rpx; font-size: 30rpx;
}
</style>
