<template>
	<view class="container">
		<navBar title="绘本生成" :back="true" color="#333333" background="#FFFFFF" />

		<view class="topBox">
			<view class="left">
				<image :src="picBase+'upCamera.png'" mode="heightFix" class="aiIcon" @click="upload"></image>
			</view>
			<view class="right">
				<view class="label1">
					纯AI制作
				</view>
				<view class="label2">
					创作独一无二的绘本
				</view>
			</view>
		</view>
		<view class="mainBox">
			<view class="titleBox">
				上传一张故事主角的照片
			</view>
			<view class="uploadBox">
				<view class="tipBox">
					<image :src="picBase+'upCamera.png'" mode="heightFix" class="tipIcon" @click="upload"></image>
					绘本内容会根据您所上传的照片进行创作
				</view>

				<view class="selBox">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="1">
						<view class="myUpArea">
							<image :src="picBase+'upCamera.png'" mode="heightFix" class="upIcon">
							</image>
							上传图片
						</view>
					</u-upload>
				</view>
			</view>
		</view>

		<view class="parmasBox">
			<view class="titleBox">
				故事主角信息
			</view>
			<view class="inputBox">
				<view class="selAndTileBox">
					<view class="nickname">
						<input type="text" class="nackI" v-model="formData.nickName" placeholder="请输入昵称" />
						<image :src="picBase+'arrR2.png'" mode="heightFix" class="arrIcon">
						</image>
					</view>
					<view class="selBtn" @click="show5=true">
						选择主角
					</view>
				</view>
				<view class="otherBox">
					<view class="item" @click="show=true">
						<input type="text" class="nackI" disabled v-model="formData.sex" placeholder="选择性别" />
						<image :src="picBase+'arrR2.png'" mode="heightFix" class="arrIcon">
						</image>
					</view>
					<view class="item" @click="show2=true">
						<input type="text" class="nackI" disabled v-model="formData.age" placeholder="选择年龄" />
						<image :src="picBase+'arrR2.png'" mode="heightFix" class="arrIcon">
						</image>
					</view>
					<view class="item" @click="show3=true">
						<input type="text" class="nackI" disabled v-model="formData.star" placeholder="选择星座" />
						<image :src="picBase+'arrR2.png'" mode="heightFix" class="arrIcon">
						</image>
					</view>
					<view class="item" @click="show4=true">
						<input type="text" class="nackI" disabled v-model="formData.class" placeholder="选择年级" />
						<image :src="picBase+'arrR2.png'" mode="heightFix" class="arrIcon">
						</image>
					</view>
				</view>
			</view>
		</view>
		<view class="pos">
		</view>
		<view class="btnBox">
			<view class="left">
				<image :src="picBase+'kefu.png'" mode="heightFix" class="kfIcon">
				</image>
				客服
			</view>
			<view class="right" @click="gen">
				<view class="top">
					立即生成
				</view>
				<view class="bot">
					绘本生成剩余次数：8次
				</view>
			</view>
		</view>

		<u-picker :show="show" title="性别选择" aColor="#007AFF" :columns="columns" confirmColor="#007AFF"
			@confirm="confirm1" @cancel="show=false"></u-picker>
		<u-picker :show="show2" title="年龄选择" keyName="label" aColor="#007AFF" :columns="columns2" confirmColor="#007AFF"
			@confirm="confirm2" @cancel="show2=false"></u-picker>
		<u-picker :show="show3" title="星座选择" aColor="#007AFF" :columns="columns3" confirmColor="#007AFF"
			@confirm="confirm3" @cancel="show3=false"></u-picker>
		<u-picker :show="show4" title="班级选择" aColor="#007AFF" :columns="columns4" confirmColor="#007AFF"
			@confirm="confirm4" @cancel="show4=false"></u-picker>

		<u-picker keyName="name" :show="show5" title="选择主角" aColor="#007AFF" :columns="columns5" confirmColor="#007AFF"
			@confirm="confirm5" @cancel="show5=false"></u-picker>


	</view>
</template>

<script>
	// import navBar from '@/components/navBar/index.vue'

	import uploadUrl from '@/common/config.js'

	import {
		getRoleparmas,
		postTask
	} from '@/api/index/index.js'
	export default {
		// components: {
		// 	navBar
		// },
		data() {
			return {
				// roleList:[],
				picBase: this.$picBase2,
				fileList1: [],

				formData: {
					nickName: '',
					sex: '',
					age: '',
					star: '',
					class: ''
				},
				show: false,
				columns: [
					['男孩', '女孩']
				],
				show2: false,
				columns2: [
					[
						'1岁',
						'2岁'
					]

				],
				show3: false,
				columns3: [
					['白羊座', '金牛座', '双子座 ', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座']
				],
				show4: false,
				columns4: [
					['蒙大一', '蒙大二', '蒙小一', '蒙小二', '蒙小三']
				],


				show5: false,
				columns5: [

				],
				roleId: -1


			}
		},
		onLoad() {
			this.getRoleparmas()
		},
		methods: {
			async gen() {

				// console.log('fileList1',this.fileList1[0].url);
				// return
				if (this.fileList1.length == 0) {
					return this.$toast('请上传图片')
				}
				if (!this.formData.nickName) {
					return this.$toast('请输入昵称')
				}
				if (!this.formData.sex) {
					return this.$toast('请选择性别')
				}
				if (!this.formData.age) {
					return this.$toast('请选择年龄')
				}
				if (!this.formData.star) {
					return this.$toast('请选择星座')
				}
				if (!this.formData.class) {
					return this.$toast('请选择班级')
				}
				let p = {}
				if (this.roleId != -1) {
					p = {
						roleId: this.roleId,
						image: this.fileList1[0].url
					}
				} else {
					p = {
						image: this.fileList1[0].url,
						name: this.formData.nickName,
						sex: this.formData.sex,
						age: this.formData.age.slice(0, 1),
						star: this.formData.star,
						level: this.formData.class,
					}
				}
				let res1 = await postTask(p)
				console.log('生成任务提交返回值--------2', res1);
				if (res1.code == 0) {
					this.$toast('生成任务提交成功')
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/index/genRes/index?taskId=' + res1.data.id
						})
					}, 1500)
				} else {
					this.$toast(res1.message)
				}
			},


			async getRoleparmas() {
				let res1 = await getRoleparmas()
				console.log('角色列表返回值--------2', res1);
				if (res1.code == 0) {
					this.columns5[0] = res1.data.data
					// this.chargeList=[]
				} else {
					this.$toast(res1.message)
				}
			},
			confirm1(e) {
				console.log('e', e);
				this.formData.sex = e.value[0]
				this.show = false
			},
			confirm2(e) {
				console.log('e', e);
				this.formData.age = e.value[0]
				this.show2 = false
			},
			confirm3(e) {
				console.log('e', e);
				this.formData.star = e.value[0]
				this.show3 = false
			},
			confirm4(e) {
				console.log('e', e);
				this.formData.class = e.value[0]
				this.show4 = false
			},
			confirm5(e) {
				console.log('e-----5', e.indexs[0], e.value[0]);
				this.formData.class = e.value[0].level
				this.formData.nickName = e.value[0].name
				this.formData.sex = e.value[0].sex
				this.formData.star = e.value[0].star
				this.formData.age = e.value[0].age + '岁'

				this.roleId = e.value[0].id
				this.show5 = false
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						// url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						url: uploadUrl.baseUrl + '/api/avatar/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							'Content-Type': 'multipart/form-data',
							'Authorization': uni.getStorageSync('token')
						},
						formData: {
							user: 'test',
							'tag': 'lamp',

						},
						success: (res) => {

							console.log('uploadFilePromise返回值', res);
							setTimeout(() => {
								resolve(JSON.parse(res.data).data.url)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "./index.scss";
</style>