<template>
	<view class="form">
		<form @submit="formSubmit">
			<view class="value other">
				<view class="title">类型</view>
				<view class="label">
					<picker v-if="type=='0'" name="type" @change="typeChange" :value="index" :range="typeList"
						range-key="dictLabel">
						<view class="picker">{{form.type||'普通训练'}}</view>
					</picker>
					<picker v-else name="type" @change="typeChange" :value="index" :range="activityList"
						range-key="dictLabel">
						<view class="picker">{{form.type||'请选择'}}</view>
					</picker>
				</view>
			</view>
			<view class="value other">
				<view class="title">日期</view>
				<view class="label">
					<picker name="date" mode="date" @change="dateChange" :value="index">
						<view class="picker">{{form.date||'请选择'}}</view>
					</picker>
				</view>
			</view>
			<view class="value other">
				<view class="title">时间</view>
				<view class="label">
					<picker name="time" mode="time" @change="timeChange" :value="index">
						<view class="picker">{{form.time||'请选择'}}</view>
					</picker>
				</view>
			</view>
			<view class="value other">
				<view class="title">时长</view>
				<view class="label">
					<picker name="duration" @change="durationChange" :value="index" :range="durationList"
						range-key="dictLabel">
						<view class="picker">{{form.duration||'请选择时长'}}</view>
					</picker>
				</view>
			</view>
			<view class="value other margin">
				<view class="title">地点</view>
				<view class="label">
					<input name="name" class="input" :value="form.address" placeholder="请选择" />
				</view>
			</view>
			<view class="value other">
				<view class="title">费用</view>
				<view class="label">
					<input name="money" class="input" :value="form.money" placeholder="请输入费用" />
				</view>
			</view>
			<view class="value other">
				<view class="title">人数上限</view>
				<view class="label">
					<input name="name" class="input" :value="form.max_person" placeholder="请输入人数上限" />
				</view>
			</view>
			<view class="value other">
				<view class="title">公开报名</view>
				<view class="label">
					<picker name="is_open" @change="openChange" :value="index" :range="openList" range-key="dictLabel">
						<view class="picker">{{form.is_open||'请选择'}}</view>
					</picker>
				</view>
			</view>
			<view class="value other" v-if="form.date&&form.time">
				<view class="title">报名截止时间</view>
				<view class="label">
					<uni-datetime-picker :start="form.date" :border="false" v-model="form.end_time" />
				</view>
			</view>
			<view class="value other">
				<view class="title">活动标题</view>
				<view class="label">
					<input name="title" class="input" :value="form.title" placeholder="请输入活动标题" />
				</view>
			</view>
			<view class="remark margin">
				<view class="label">
					<textarea name="brief" :value="form.brief" placeholder="请输入简介" />
				</view>
			</view>
			<view class="remark">
				<view class="title">上传图片</view>
				<view class="label">
					<uni-file-picker v-model="form.file" fileMediatype="image" :list-styles="imageStyles" limit="6"
						title="最多选择6张图片" @select="toUpload" @delete="toDelete"></uni-file-picker>
				</view>
			</view>
		</form>
	</view>
</template>

<script setup lang="ts">
	import { ref, toRefs, getCurrentInstance } from 'vue';
	// 请求接口
	const $api = getCurrentInstance()?.appContext.config.globalProperties.$api;
	const $apifile = getCurrentInstance()?.appContext.config.globalProperties.$apifile;
	// 信息
	const form = ref({ file: [] });
	const listStyles = ref({
		width: 64,
		height: 64,
		border: {
			color: "#ff5a5f",
			width: 2,
			style: 'dashed',
			radius: '2px'
		}
	});
	const props = defineProps({
		type: { type: String, default: () => '0' },
		typeList: { type: Array, default: () => [] },
		activityList: { type: Array, default: () => [] },
		openList: { type: Array, default: () => [] },
		durationList: { type: Array, default: () => [] },
	});
	const { type, typeList, activityList, durationList, openList } = toRefs(props);
	// 类型选择
	const typeChange = (e) => {
		let data
		if (type.value == 0) {
			data = typeList.value[e.detail.value]
			if (data) form.value.type = data.dictLabel
		} else {
			data = activityList.value[e.detail.value]
			if (data) form.value.type = data.dictLabel
		}
	};
	// 时长选择选择
	const durationChange = (e) => {
		const data = durationList.value[e.detail.value]
		if (data) form.value.duration = data.dictLabel
	};
	// 日期选择器
	const dateChange = (e) => {
		form.value.date = e.detail.value
	};
	// 时间选择器
	const timeChange = (e) => {
		form.value.time = e.detail.value
	};
	// 是否公开选择器
	const openChange = (e) => {
		form.value.is_open = e.detail.value
	};
	// 上传图片
	const toUpload = async (e) => {
		const arr = await $apifile(`/common/upload`, 'file', e.tempFilePaths[0],
			'file');
		if (arr.code == 200) {
			form.value.file.push({
				newFileName: arr.newFileName,
				originalFilename: arr.originalFilename,
				url: arr.url
			})
			console.log(form.value.file);
		} else {
			uni.showToast({
				title: arr.msg,
				icon: 'none'
			});
		}
	};
	// 删除图片
	const toDelete = async (e) => {
		form.value.file = form.value.file.filter(i => i.originalFilename != e.tempFile.name)
		console.log(form.value.file);
	};
</script>
<style lang="scss" scoped>
	.form {
		display: flex;
		flex-direction: column;
		background-color: var(--f9Color);

		.other {
			padding: 4vw 2vw;
			border-bottom: 1px solid var(--footColor);
		}

		.margin {
			margin: 0 0 3vw 0;
		}

		.remark {
			background-color: var(--mainColor);
			padding: 3vw 2vw 0 2vw;

			.title {
				padding: 0 0 3vw 0;
				border-bottom: 1px solid var(--footColor);
			}
		}

		.value {
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: var(--mainColor);

			.label {
				.input {
					text-align: right;
				}
			}
		}
	}
</style>