<template>
	<view class="wrapper" v-if="isRecord">
		<view class="bg" @click="onTap">
			<view class="edit" @click.stop>
				<view class="title">{{$t("index.manualRecording")}}</view>
				<view class="editem">
					<view class="left">{{$t('common.titleDate')}}</view>
					<view class="right">
						<picker mode="date" :end="endDate" @change="changeLog" :fields="fields">
							<view class="uni-input">{{regTime?regTime:$t('tips.verifyPicker')}}</view>
							<icon class="iconfont icon-arrow-down-bold"></icon>
						</picker>
					</view>
				</view>
				<view v-if="rtype!=8&&rtype!=2">
					<!-- 项目 -->
					<view class="editem" v-if="info&&info.list.length">
						<view class="left">{{$t('recordType')}}</view>
						<view class="right">
							<picker :range="info.list" range-key="name" :value="lableTndex" @change="bindLableChange">
								<view class="uni-input">{{name?name:$t('tips.verifyPicker')}}</view>
								<icon class="iconfont icon-arrow-down-bold"></icon>
							</picker>
						</view>
					</view>
					<!-- 项目时长类型 -->
					<view class="editem" v-if="info&&info.list.length&&listType">
						<view class="name">{{describe}}</view>
						<view class="right" v-if="listType==4">
							<picker mode="multiSelector" :range="timeList" :value="timesTndex" @change="bindTimeChange">
								<view class="size16">{{time_m?time_m+':':$t('tips.verifyPicker')}}{{time_s?time_s:''}}
								</view>
							</picker>
							<uni-icons type="bottom" class="ml-15 c666"></uni-icons>
						</view>
						<view class="right" v-if="listType!=4">
							<input :type="listType==1?'number':'digit'" v-model="number"
								:placeholder="$t('tips.verifyRecord')">
							{{unit}}
						</view>
					</view>
					<view class="editem" v-if="info&&info.time">
						<view class="name">{{$t('recordTime')}}</view>
						<view class="right">
							<picker mode="multiSelector" :range="timeList" :value="timesTndex" @change="bindTimeChange">
								<view class="size16">{{time_m?time_m+':':$t('tips.verifyPicker')}}{{time_s?time_s:''}}
								</view>
							</picker>
							<uni-icons type="bottom" class="ml-15 c666"></uni-icons>
						</view>
					</view>
					<view class="editem" v-if="info&&info.number">
						<view class="name">{{$t('recordNumber')}}</view>
						<view class="right">
							<input type="number" v-model="number"
								:placeholder="$t('tips.verifyRecord')">{{$t('skip.Number')}}
						</view>
					</view>
				</view>
				<!-- 身体 -->
				<view class="" v-if="rtype==2">
					<view class="editem" v-if="info&&info.height">
						<view class="name">{{$t('common.infoHeight')}}</view>
						<view class="right">
							<input type="digit" v-model="height" :placeholder="$t('tips.verifyRecord')" />cm
						</view>
					</view>
					<view class="editem" v-if="info&&info.weight">
						<view class="name">{{$t('common.infoWeight')}}</view>
						<view class="right">
							<input type="digit" v-model="weight" :placeholder="$t('tips.verifyRecord')">kg
						</view>
					</view>
					<view class="editem" v-if="userInfo.stage=='婴儿'">
						<view class="left">{{$t('common.infoHead')}}</view>
						<view class="right">
							<input v-model="head" type="digit" :placeholder="$t('tips.verifyRecord')" />cm
						</view>
					</view>
				</view>
				<!-- 肺活量 -->
				<view class="" v-if="rtype==8">
					<view class="editem">
						<view class="name">{{$t('recordNumberFirst')}}</view>
						<view class="right">
							<input type="digit" v-model="number1" :placeholder="$t('tips.verifyRecord')">ml
						</view>
					</view>
					<view class="editem">
						<view class="name">{{$t('recordNumberSecond')}}</view>
						<view class="right">
							<input type="digit" v-model="number2" :placeholder="$t('tips.verifyRecord')">ml
						</view>
					</view>
					<view class="editem">
						<view class="name">{{$t('recordNumberThird')}}</view>
						<view class="right">
							<input type="digit" v-model="number3" :placeholder="$t('tips.verifyRecord')">ml
						</view>
					</view>
				</view>
				<view class="btn close" @click="onTap()">{{$t("tips.btnSancellation")}}</view>
				<view class="btn" @click="handleTarget">{{$t("tips.btnConfirm")}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	export default {
		props: {
			rtype: null,
		},
		data() {
			return {
				name: "",
				number: '',
				regTime: "",
				number1: '',
				number2: "",
				number3: "",
				time_m: "",
				time_s: "",
				weight: "",
				height: '',
				timeList: [],
				lableTndex: 0,
				timesTndex: [1, 0],
				listType: null,
				describe: "",
				unit: "",
				fields: "",
				head: ""
			}
		},
		computed: {
			...mapState(["user", "isRecord", "PublicRecord", "labelList"]),
			userInfo() {
				return this.user
			},
			info() {
				let that = this
				let info = {
					height: false,
					weight: false,
					number: false,
					time: false,
					list: []
				}
				if (that.PublicRecord.length) {
					that.PublicRecord.forEach(ite => {
						if (ite.id == that.rtype) {
							info = ite
						}
					})
				}
				return that.PublicRecord.length ? info : null
			},
			endDate() {
				return this.$tools.getDate("start")
			},
			startDate() {
				return this.$tools.GetDateStr(-90);
			},
		},
		mounted() {
			let that = this
			that.timeList = that.$tools.gethms()
			// #ifdef APP-PLUS
			that.fields = "time"
			// #endif 
			// #ifndef APP-PLUS 
			that.fields = "day"
			// #endif
		},
		methods: {
			// 手动记录
			handleTarget() {
				let that = this
				if (that.rtype == 2) {
					that.handleinsertmeasure()
				} else if (that.rtype == 6) {
					that.handleskipmeasure()
				} else if (that.rtype == 8) {
					that.handleLungmeasure()
				} else {
					that.handlepublicmeasure()
				}
			},
			// 通用
			handlepublicmeasure() {
				let that = this
				if (!that.name) {
					that.$tools.msg(that.$t("tips.verifyRecordType"))
					return
				}
				if (!that.regTime) {
					that.$tools.msg(that.$t("tips.verifyDate"))
					return
				}
				if (that.listType == 4 && (!that.time_m || !that.time_s)) {
					that.$tools.msg(that.$t("tips.verifyRecordTime"))
					return
				}
				if (that.listType != 4 && !that.number) {
					let msg = that.listType == 1 ? that.$t("tips.verifyRecordNumber") : that.$t("tips.verifyRecordTime")
					that.$tools.msg(msg)
					return
				}
				let time_m = that.time_m + ':' + that.time_s
				that.$model.getpublicmeasure({
					acd_id: that.rtype,
					name: that.name,
					record_time: that.regTime,
					data: that.listType == 4 ? time_m : that.number,
					aud_id: uni.getStorageSync('userid')
				}).then(res => {
					if (res.code != 0) return
					that.$tools.msg(res.msg)
					that.$store.dispatch("getUserInfo", {
						aud_id: uni.getStorageSync('userid'),
					})
					that.$store.dispatch("getPublicContent", {
						acd_id: that.rtype,
						aud_id: uni.getStorageSync('userid')
					})
					that.onTap()
				})
			},
			// 肺活量
			handleLungmeasure() {
				let that = this
				if (!that.regTime) {
					that.$tools.msg(that.$t("tips.verifyDate"))
					return
				}
				if (!that.number1) {
					that.$tools.msg(that.$t("lung.oneNumberTips"))
					return
				}
				if (!that.number2) {
					that.$tools.msg(that.$t("lung.twoNumberTips"))
					return
				}
				if (!that.number3) {
					that.$tools.msg(that.$t("lung.threeNumberTips"))
					return
				}
				that.$model.getLungmeasure({
					aud_id: uni.getStorageSync('userid'),
					time: that.regTime,
					one: that.number1,
					two: that.number2,
					three: that.number3
				}).then(res => {
					if (res.code != 0) return
					that.$tools.msg(res.msg)
					that.$store.dispatch("getLungResult", {
						aud_id: uni.getStorageSync('userid')
					})
					that.onTap()
				})
			},
			// 跳绳
			handleskipmeasure() {
				let that = this
				if (!that.regTime) {
					that.$tools.msg(that.$t("tips.verifyDate"))
					return
				}
				if (!that.number) {
					that.$tools.msg(that.$t("tips.verifyRecordNumber"))
					return
				}
				that.$model.getskipmeasure({
					aud_id: uni.getStorageSync('userid'),
					num: that.number,
					r_time: that.regTime,
					time_m: that.time_m,
					time_s: that.time_s,
					type: "free",
				}).then(res => {
					if (res.code != 0) return
					that.$tools.msg(res.msg)
					that.$store.dispatch("getSkipResult", {
						aud_id: uni.getStorageSync('userid'),
					})
					that.onTap()
				})
			},
			// 身体
			handleinsertmeasure() {
				let that = this
				if (!that.regTime) {
					that.$tools.msg(that.$t("tips.verifyDate"))
					return
				}
				if (!that.height) {
					that.$tools.msg(that.$t("tips.verifyHeight"))
					return
				}
				if (!that.weight) {
					that.$tools.msg(that.$t("tips.verifyWeight"))
					return
				}
				that.$model.getinsertmeasure({
					aud_id: uni.getStorageSync('userid'),
					time: that.regTime,
					weight: that.weight,
					height: that.height,
					head_data: that.head ? that.head : 0
				}).then(res => {
					if (res.code != 0) return
					that.$tools.msg(res.msg)
					that.$store.dispatch("getResult", {
						aud_id: uni.getStorageSync('userid')
					})
					that.$store.dispatch("getUserInfo", {
						aud_id: uni.getStorageSync('userid')
					})
					that.onTap()
				})
			},
			bindTimeChange(e) {
				let that = this
				let m = e.target.value[0]
				let s = e.target.value[1]
				that.timesTndex = e.target.value
				let time_m = Number(that.timeList[0][m].substring(0, 2)) * 60
				let time_s = Number(that.timeList[1][s].substring(0, 2))
				that.time_m = that.timeList[0][m].substring(0, 2)
				that.time_s = that.timeList[1][s].substring(0, 2)
			},
			bindLableChange(e) {
				console.log("e", e)
				this.name = this.info.list[e.detail.value].name
				this.listType = this.info.list[e.detail.value].type
				this.describe = this.info.list[e.detail.value].describe
				this.unit = this.info.list[e.detail.value].unit
			},
			changeLog(e) {
				this.regTime = e.detail.value
			},
			onTap() {
				let that = this
				that.name = ""
				that.weight = ""
				that.height = ""
				that.regTime = ""
				that.number1 = ""
				that.number2 = ""
				that.number3 = ""
				that.number = ''
				that.time_m = ""
				that.time_s = ""
				that.lableTndex = 0
				that.timesTndex = [1, 0]
				that.listType = null
				that.$store.commit("changeRecord", false);
			},
		}
	}
</script>

<style scoped lang="scss">
	.btn {
		width: 40%;
		float: right;
		margin-top: 15px;
		background: $maincolor !important;
	}

	.close {
		background: #dfdfdf !important;
		float: left;
	}
</style>