<template>
	<view class="box">
		<view class="b-r-8 p-row-32 white">
			<u-form :model="info" ref="form" label-width="150rpx">
				<u-form-item label="选择患者" prop="patient" :border-bottom="false">
					<u-input input-align="right" v-model="getName" type="select" :select-open="select" @click="go"
						placeholder="请选择" />
				</u-form-item>
				<u-form-item label="消息内容" prop="msg" label-position="top">
					<view class="flex-col b-r-8 p-row-16 bg">
						<u-input v-model="info.msg" placeholder="请输入" type="textarea" :maxlength="500" auto-height />
						<d-text :text="`${info['msg']?info['msg'].length:0}/500`" color="#C4C7CC" width="614"
							align="right"></d-text>
					</view>
				</u-form-item>
				<!-- <u-form-item label="选择文章" prop="article" :border-bottom="false">
					<u-input input-align="right" v-model="info.article" type="select" :select-open="select1"
						@click="goArtcle" placeholder="请选择" />
				</u-form-item> -->
				<u-form-item label="定时发送" :border-bottom="false">
					<view class="flex-row flex-jus-end">
						<u-switch v-model="info.ifTime" active-color="#6ACDBB"></u-switch>
					</view>
				</u-form-item>
				<u-form-item label="发送时间" prop="time" :border-bottom="false" v-if="info.ifTime">
					<u-input input-align="right" v-model="info.time" type="select" :select-open="select2"
						@click="select2=true;show=true" placeholder="请选择" />
				</u-form-item>
			</u-form>
		</view>
		<view class="button">
			<u-button :throttle-time="0" shape="circle" @click="submit" :ripple="true">发送</u-button>
		</view>
		<u-picker :safe-area-inset-bottom="true" :mask-close-able="false"
			@cancel="select=false;select1 = false;select2 = false" v-model="show" mode="time" :params="params"
			@confirm="confirm"></u-picker>
	</view>
</template>

<script>
	// import { sendArticleApi } from "@/api/all.js";

import { sendDoctor } from '../../api/all';

	export default {
		data() {
			return {
				patient: false,
				select: false,
				select1: false,
				select2: false,
				info: {
					patient: '',
					ifTime: false,
					msg: '',
					article: '',
					time: '',
				},
				show: false,
				params: {
					year: false,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: false,
					timestamp: true
				},
				startTimestamp: 0,
				endTimestamp: 0,
				rules: {
					patient: [{
						required: true,
						validator: (rule, value, callback) => {
							if (!this.info['patient']) {
								callback(new Error('请选择患者'));
							} else {
								callback();
							}
						},
						trigger: ['change', 'blur']
					}],
					msg: [{
						required: true,
						validator: (rule, value, callback) => {
							if (!this.info['msg']) {
								callback(new Error('消息内容不能为空'));
							} else {
								callback();
							}
						},
						trigger: ['change', 'blur']
					}],
					// article: [{
					// 	required: true,
					// 	validator: (rule, value, callback) => {
					// 		if (!this.info['article']) {
					// 			callback(new Error('请选择文章'));
					// 		} else {
					// 			callback();
					// 		}
					// 	},
					// 	trigger: ['change', 'blur']
					// }],
					time: [{
						required: true,
						validator: (rule, value, callback) => {
							if (!this.info['time'] && this.info.ifTime) {
								callback(new Error('请选择发送时间'));
								return
							}
							callback();
						},
						trigger: ['change', 'blur']
					}],
					 // 发送内容
					send_at:"", // 发送时间
				}
			}
		},
		computed: {
			getName: {
				get(val) {
					let names = ''
					this.info.patient && (names = this.info.patient.reduce((co, item) => {
						co.push(item.patient)
						return co
					}, []))
					return names.length > 0 ? names.join() : ''
				},
				set(val) {
					return val
				}
			},
		},
		onShow() {
			let articleList = uni.getStorageSync('articleList')
			if (articleList) {
				uni.removeStorageSync('articleList')
				this.info.article = articleList[0].title
				this.info.articleId = articleList[0].id
			}
			let patients = uni.getStorageSync('patients')
			if (patients) {
				uni.removeStorageSync('patients')
				this.info.patient = patients;
				this.select = false
				uni.removeStorageSync('patients')
			}
		},
		created() {
			this.$nextTick(() => {
				this.$refs.form.setRules(this.rules);
			})
		},
		methods: {
			go() {
				this.select = true;
				this.$go(`patient_select?list=${this.info.patient&&JSON.stringify(this.info.patient)}`)
			},
			// goArtcle(){
			// 	this.select1 = true;
			// 	this.$go(`article_select`)
			// },
			confirm(e) {
				// console.log(e,'time');
				let time = `${e.month}/${e.day} ${e.hour}:${e.minute}`
				this.info.time = time;
				this.select1 = false
				this.select2 = false
			},
			submit() {
				this.$refs.form.validate(valid => {
					if (valid) {
						let info = uni.getStorageSync('userInfo') || {}
						sendDoctor({
							content: this.info.msg,
							up_ids: this.info.patient.map(item => item.patient_id),
							send_at:this.info.time || ' ',
							store_id: uni.getStorageSync('store_id') || 11001,
						}).then(res => {
							if (res.errcode == 0) {
								uni.showToast({
									title: '发送成功',
									icon: 'success',
									mask: true,
								})
								setTimeout(() => {
									uni.navigateBack({
										delta: 1
									})
								}, 1000)
							} else {
								this.$toast(res.msg)
							}
						})
					}
				})
			},
		}
	}
</script>
<style>
	page {
		background-color: #F9FAFB;
	}
</style>
<style scoped lang="scss">
	.box {
		padding: 20rpx;
	}

	.button {
		position: fixed;
		bottom: env(safe-area-inset-bottom);
		left: 0;
		width: 750rpx;
		height: 110rpx;
		background: #FFFFFF;
		border-top: 2rpx solid #F2F3F5;
		padding: 12rpx 32rpx;
		box-sizing: border-box;
		z-index: 999;

		::v-deep button {
			background: #6ACDBB;
			color: #FFFFFF;
		}
	}
</style>
