<template>
	<view class="content">
		<view class="card">
			<view class="item">
				<view class="lable">优惠券名称</view>
				<view class="value"> 
					<up-input v-model="formData.title" placeholder="填写名称" border="none" clearable ></up-input>	
				</view>
			</view>
			<view class="item">
				<view class="lable">使用门槛</view>
				<view class="value"> 
					<view>满</view><input v-model="formData.fullAmount" placeholder="填写金额" border="none"></input><view>元</view>，<view>减</view><input v-model="formData.discountAmount" placeholder="填写金额" border="none"></input><view>元</view>
				</view>
			</view>
		</view>
		<view class="card">
			<view class="item">
				<view class="lable">有效期类型</view>
				<view class="value" style="display: flex;flex-direction: column;align-items: flex-start;"> 
					<view>
						<up-radio-group v-model="formData.validityType" @change="typeChange('validityType')">
							<up-radio name="fixed" label="领券后有效期内可用" style="margin-right: 30rpx;"></up-radio>
							<up-radio name="custom" label="固定有效期范围内可用"></up-radio>
						</up-radio-group>
					</view>
					<view class="date"  v-if="formData.validityType=='custom'">
						<!-- {{formData.useStartTime +'—'+ formData.useEndTime}} -->
						<view @click="typeChange('validityType')">{{ formData.validStartTime || '开始时间'}}</view>--
						<view @click="typeChange('validityType')">{{ formData.validEndTime || '结束时间'}}</view>
					</view>
				
				</view>
			</view>
			<view class="item">
				<view class="lable">有效期（天）</view>
				<view class="value"> 
					<up-input v-model="formData.validDays" placeholder="填写天数" border="none"></up-input>	
				</view>
			</view>
			<view class="item">
				<view class="lable">隔天生效<up-icon @click="modalShow(1)" name="question-circle" color="#999" size="20" style="margin-left: 10rpx;"></up-icon></view>
				<view class="value"> 
					<view>隔</view><input v-model="formData.daysToTakeEffect" placeholder="填写天数" border="none" ></input><view>天生效</view>
				</view>
			</view>
			<view class="item">
				<view class="lable">可用日期</view>
				<view class="value"> 
					 <up-checkbox-group v-model="formData.userDays" placement="row" @change="checkboxChange" >
					    <up-checkbox
					        :customStyle="{marginBottom: '8px',marginRight: '15px'}"
					        v-for="(item, index) in pageData.value"
					        :key="index"
					        :label="item.name"
					        :name="item.name"
					    >
					    </up-checkbox>
					</up-checkbox-group>
				</view>
			</view>
			<view class="item">
				<view class="lable">指定时间段可用</view>
				<view class="value" style="display: flex;flex-direction: column;align-items: flex-start;">
					<view>
						<up-radio-group v-model="formData.useTimeType" >
							<up-radio name="all" label="全时段可用" style="margin-right: 30rpx;"></up-radio>
							<up-radio name="custom" label="指定时段可用"></up-radio>
						</up-radio-group>
					</view>
					<view class="time"  v-if="formData.useTimeType=='custom'">
						<!-- {{formData.useStartTime +'—'+ formData.useEndTime}} -->
						<view @click="useTimeChange('useStartTime')">{{ formData.useStartTime || '开始时间'}}</view>--
						<view @click="useTimeChange('useEndTime')">{{ formData.useEndTime || '结束时间'}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="card">
			<view class="item">
				<view class="lable">总发放数量
				<up-icon @click="modalShow(2)" name="question-circle" color="#999" size="20" style="margin-left: 10rpx;"></up-icon>
				</view>
				<view class="value"> 
					<up-input v-model="formData.number" placeholder="填写数量" border="none" clearable ></up-input>	
				</view>
			</view>
		</view>
		<view class="bottomPop">
			<button @click="save">保存</button>
		</view>
		<up-datetime-picker
		    :show="pageData.useTimeShow"
			v-model="pageData.useTime"
			mode="time" 
			@cancel="timeCancel"
			@confirm="timeConfirm"
			></up-datetime-picker>
		<my-date-pickerview @confirm="datePickerConfirm" :mode="pageData.mode" ref="datePicker"></my-date-pickerview>
		<up-modal :show="pageData.show" :title="pageData.title" @confirm="pageData.show = false" ></up-modal>
	</view>
</template>
<script setup>
	import go from '@/commons/utils/go.js'
	import {
		reactive,
		ref
	} from 'vue';
	import { onLoad, onShow  } from '@dcloudio/uni-app';
	import { getTbShopCouponInfo, addTbShopCoupon } from '@/http/yskApi/coupon.js'
	const pageData = reactive({
		value: [
			{ name: "周一", },
			{ name: "周二", },
			{ name: "周三", },
			{ name: "周四", },
			{ name: "周五", },
			{ name: "周六", },
			{ name: "周日", },
		],
		mode: "",
		title: "",
		show: false,
		useTimeShow: false,
		useTime: "",
		dateType: "",
		sueDateType: "",
	})
	const formData = reactive({
		type: '1',
		title: '',
		fullAmount: null,
		discountAmount: null,
		validityType: 'fixed',
		validStartTime: '',
		validEndTime: '',
		userDays: [],
		useTimeType: 'all',
		useStartTime: '',
		useEndTime: '',
		validDays: null,
		daysToTakeEffect: null,
		number: null,
	})
	onLoad((options) => {
		if ( options.type == 'info' ) {
			pageData.id = options.id;
			getConponInfo();
		}
	})
	let datePicker = ref(null)
	let refTime = ref(null)
	
	/**
	 * 查看生效时间/发放数量提示
	 */
	let modalShow = ( type ) => {
		if ( type == 1) {
			pageData.title = "领取后0天后的0点0分生效";
		} else {
			pageData.title = "限用户自行领取，（当库存为 0时，集草等活动仍会赠送）";
		}
		pageData.show = true;
	}
	
	/**
	 * 获取详情
	 */
	let getConponInfo = () => {
		getTbShopCouponInfo(pageData.id).then((res) => {
			// formData = res;
			for (let item in res) {
			    formData[item] = res[item]
			}
			formData.userDays = formData.userDays.split(",");
			console.log(formData)
		})
	}
	
	let typeChange = ( type ) =>{
		pageData.dateType = type;
		pageData.mode = 'date'
		
		if ( formData.validityType == "custom") {
			datePicker.value.open()
		}
		
	}
	/**
	 * 指定时间段选择
	 */
	let useTimeChange = ( type ) => {
		console.log(type)
		pageData.sueDateType = type;
		pageData.mode = 'time';
		pageData.useTimeShow = true;
		
	}
	
	/**
	 * 关闭时间段弹窗
	 */
	let timeCancel = () => {
		pageData.useTimeShow = false;
	}
	
	/**
	 * 确认时间端选择
	 */
	let timeConfirm = () => {
		pageData.useTimeShow = false;
		console.log(pageData.useTime)
		if ( pageData.sueDateType == 'useStartTime') { formData.useStartTime = pageData.useTime}
		if ( pageData.sueDateType == 'useEndTime') { formData.useEndTime = pageData.useTime}
	}
	
	/**
	 * 有效期选择
	 */
	let datePickerConfirm = (data) => {
		console.log(pageData.dateType)
		if ( pageData.dateType == 'validityType') {
			// .substring(0,10)
			formData.validStartTime = data.start;
			formData.validEndTime = data.end;
		}
	}
	
	/**
	 * 保存
	 */
	let save = () => {
		formData.userDays = formData.userDays.toString();
		let params = {
			...formData
		}
		console.log(params)
		addTbShopCoupon(params).then((res) => {
		// 	console.log(res)
			go.back(1)
		})
	}
	
</script>

<style lang="scss">
	
	.content {
		background: #F9F9F9;
		padding: 32rpx 28rpx 150rpx 28rpx;

		.card{
			padding: 32rpx 24rpx;
			background-color: #fff;
			margin-bottom: 32rpx;
			.item{
				padding-bottom: 24rpx;
				border-bottom: 2rpx solid #E5E5E5;
				margin-bottom: 24rpx;
				.lable{
					font-weight: bold;
					font-size: 28rpx;
					color: #333333;
					margin-bottom: 16rpx;
					display: flex;
					align-items: center;
				}
				.value{
					display: flex;
					justify-content: flex-start;
					align-items: center;
					input{
						width: 150rpx;
						text-align: center;
					}
					.date,.time{
						display: flex;
						align-items: center;
						font-weight: 400;
						font-size: 26rpx;
						color: #999999;
						margin-top: 10rpx;
						view{
							border: 1rpx solid #999;
							padding: 10rpx 20rpx;
							border-radius: 10rpx
						}
						view:nth-child(1){
							margin-right: 10rpx;
						}
						view:nth-child(2){
							margin-left: 10rpx;
						}
					}
					.time{
						view{
							padding: 10rpx 30rpx;
						}
					}
				}
			}
			.item:last-child{
				border-bottom: none;
				margin-bottom: 0;
			}
		}
		.bottomPop{
			position: fixed;
			bottom: 0;
			left: 0;
			height: 150rpx;
			width: 100%;
			background-color: #fff;
			>button {
				width: 530rpx;
				margin: 30rpx 0;
				margin-left: 50%;
				transform: translateX(-50%);
				height: 80rpx;
				line-height: 80rpx;
				color: #fff;
				background: #318AFE;
				border-radius: 56rpx 56rpx 56rpx 56rpx;
			}
		}
	}
</style>