<template>
	<view>
		<z-navbar title="处理预定" back>
			<view slot="right" @click="submit">保存</view>
		</z-navbar>
		<w-picker mode="date" startYear="2000" endYear="2100" v-model="info.alertDate" :current="true" fields="day" @confirm="onConfirm($event)"
		 @cancel="onCancel" :disabled-after="false" ref="date"></w-picker>
		<z-card class="mt-30 ptb-10">
			<z-form :model="info" :rules="rules" ref="info">
				<z-form-item height="100rpx" label="客户姓名" showline :labelStyle="{'color':'#ccc'}">
					<z-text v-model="customName" placeholder="" disabled textColor="#ccc"></z-text>
				</z-form-item>
				<z-form-item height="100rpx" label="关闭提醒" showline>
					<switch :checked="switchAlert" @change="changeSwitchAlert" />
				</z-form-item>
				<z-form-item height="100rpx" label="下次提醒日期" v-show="!switchAlert" prop="alertDate" showline arrow :required="!switchAlert"
				 dynamic>
					<z-text v-model="info.alertDate" placeholder="请选择下次提醒时间" @click="chooseDate"></z-text>
				</z-form-item>
				<z-form-item label="备注信息" prop="remark" labelPos="top" :required="!switchAlert" dynamic>
					<textarea class="textarea" v-model="info.remark" placeholder="请输入备注信息" maxlength="512" auto-height></textarea>
				</z-form-item>
			</z-form>
		</z-card>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	const app = getApp({
		allowDefault: true
	});
	export default {
		data() {
			return {
				id: '',
				customName: '',

				info: {
					alertDate: '',
					remark: ''
				},
				rules: {},
				switchAlert: true,
				
				orderAlertDaysBefore:0,
				orderData:''
			};
		},
		onLoad(option) {
			console.log(option)
			this.id = option.id;
			this.orderDate=option.orderDate;
			this.customName = option.customName;
			this.getorderalertTime();
		},
		methods: {
			getorderalertTime() {
				//app/config/baseByStoreId/
				this.$axios.get('app/config/baseByStoreId/' + uni.getStorageSync('storeId')).then(res => {
					console.log('预定提醒前', res.data.orderAlertDaysBefore, '天')
					console.log(res.data, res.data.orderAlertDaysBefore)
					this.orderAlertDaysBefore = Number(res.data.orderAlertDaysBefore) || 0;
				}).catch(err => {
					this.$refs.uToast.error(err.msg);
				})
			},
			chooseDate() {
				this.$refs.date.show();
			},
			onConfirm(e) {
				console.log(e)
				this.$set(this.info, 'alertDate', e.value)
			},
			onCancel() {
				console.log(this.$refs.date.value, this.$refs.date.pickVal)
			},
			changeSwitchAlert(e) {
				this.switchAlert = e.detail.value;
				if (this.switchAlert) {
					this.$set(this.info, 'alertDate', null)
				} else {
					var orderDate = this.orderDate;
					var time = new Date(new Date(orderDate).getTime() - this.orderAlertDaysBefore * 24 * 60 * 60 * 1000)
					console.log(new Date(orderDate).getTime() ,time)
					var month = time.getMonth() + 1,
						day = time.getDate();
					if (time.getTime() < new Date(this.$Date.getDayDate().date).getTime()) {
						this.$set(this.info, 'alertDate', null);
					} else {
						this.$set(this.info, 'alertDate', time.getFullYear() + '-' + (month < 10 ? ('0' + month) : month) + '-' + (
							day < 10 ? ('0' + day) : day))
					}
				}
			},
			submit() {
				this.$refs.info.validate(val => {
					console.log(val)
					if (val) {
						//app
						this.$axios.post('app/order/orderFollowAdd', {
							"orderId": this.id,
							"alertDate": this.info.alertDate,
							"remark": this.info.remark
						}).then(res => {
							this.$refs.uToast.success('处理咨询成功！');
							uni.navigateBack({
								delta: 1
							});
						}).catch(err => {
							this.$refs.uToast.error(err.msg);
						})
					} else {
						this.$refs.uToast.error('表单填写有误');
					}
				})
				
			}
		}
	}
</script>

<style scoped lang="scss">
	.textarea {
		font-size: 28rpx;
		min-height: 180rpx;
		padding-bottom: 20rpx;
		width: 100%;
	}
</style>
