<template>
	<view class="p-2">
		<view class="white b-r-8 p-row-32">
			<u-form :model="form" ref="uForm" label-width="250rpx">
				<u-form-item prop="openServe" label="开通服务" :label-style="{fontSize: '36rpx',color: '#31353D'}"
					:border-bottom="form['openServe']">
					<view class="flex-row flex-jus-end">
						<u-switch active-color="#6ACDBB" v-model="form['openServe']"></u-switch>
					</view>
				</u-form-item>
				<u-form-item prop="price" v-if="form['openServe']" label="挂号价格">
					<view class="flex-row flex-jus-end flex-ali-center">
						<u-input v-model="form.price" placeholder="请输入" type="digit" input-align="right"
							@input="inputChange($event,'price')" />
						<d-text text="元" className="fs-3 content-c"></d-text>
					</view>
				</u-form-item>
			</u-form>
		</view>
		<view class="button">
			<u-button :throttle-time="0" :custom-style="{ backgroundColor:'#6ACDBB',color:'#fff' }" shape="circle" @click="submit">
				提交
			</u-button>
		</view>
	</view>
</template>

<script>
	import {
		doctorInfoSet,
		info
	} from '@/api/all.js'
	export default {
		data() {
			return {
				form: {
					openServe: false,
					price: 0,
					reply: false,
					replys: 0,
					openVideo: false,
					videoPrice: 0,
					openDuration: false,
					duration: 0,
				},
				identity: 1,
				rules: {
					price: [{
						required: true,
						validator: (rule, value, callback) => {
							console.log(this.form['price'], 'sss')
							if (this.form['price'] && this.form['price'] >= 0) {
								callback();
							} else {
								callback(new Error('挂号价格不能为空'));
							}
						},
						trigger: ['change', 'blur']
					}]
				}
			};
		},
		created() {
			this.getInfo()
		},
		async onReady() {
			this.$refs.uForm.setRules(this.rules);
			// this.$refs.uForm2.setRules(this.rules);
		},
		methods: {
			inputChange(val, key) {
				var that = this;
				var idx = -1;
				var value = val.replace(/[^0-9.]/g, '').trim();
				if (value == "") {
					return;
				}

				idx = value.indexOf('.');
				var raw = value;
				if (idx > 0) {
					var ext = value.slice(idx + 1, idx + 3)
					if (ext.indexOf('.') > -1) {
						ext = ext.slice(0, ext.indexOf('.'));
					}
					if (ext.length > 2) {
						ext = ext.slice(0, 2);
					}

					value = value.slice(0, idx) + '.' + ext;

				} else if (idx == 0) {
					value = '0.'
				}

				setTimeout(function() {
					that.form[key] = value
				}, 0);
			},
			async submit() {
				let valid = await this.$refs.uForm.validate();
				if (valid) {
					const data = {
						register_status: this.form['openServe'] === true? 1: 0,
						register_price: Number(this.form['price'] || 0),
						store_id: uni.getStorageSync('store_id') || 11001
					}
					doctorInfoSet(data).then(res => {
						if (res.errcode == 0) {
							uni.showToast({
								title: '设置成功',
								icon: 'success',
								mask: true
							})
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							}, 1500)
						} else {
							this.$toast(res.msg);
							this.show = false
						}
					})
				}
			},
			async getInfo() {
				let res = {}
				this.identity == 1 && (res = await info({
					store_id: uni.getStorageSync('store_id') || 11001
				}));
				if (res.errcode == 0) {
					this.form.openServe = (res.data.doctor_service.register_status) % 2 == 0 ? false : true
					this.form.price = res.data.doctor_service.register_price
					console.log(this.form.openServe, 'cs');
				} else {
					this.$toast(res.msg);
				}
			}
		}
	};
</script>
<style>
	page {
		background-color: #F9FAFB;
	}
</style>
<style lang="scss" scoped>
	.button {
		width: 750rpx;
		height: 110rpx;
		background: #FFF;
		border-top: 2rpx solid #F2F3F5;
		position: fixed;
		bottom: env(safe-area-inset-bottom);
		left: 0;
		padding: 12rpx 32rpx;
	}
</style>