<template>
	<view class="u-p-l-40 u-p-r-40">
		<view>
			<image src="@/me/static/ymf/1b3d78fa5f92af069751ea64d493c0d85050b1e03cc5b-tPC378.png" mode=""></image>
			<image src="@/me/static/ymf/390e926273eea85c894e613d42c9931ee2cd1f0a200fd-ApyNVV.png" mode=""></image>
			<image src="@/me/static/ymf/79cbb0e34c24915f1883feaa3cf3179b0a1ceb1362a18-unU6xQ.png" mode=""></image>
			<image src="@/me/static/ymf/cb319b604e49b816971b708b1231321318e0023610d8c-I24hXU.jpg" mode=""></image>
			<image src="@/me/static/ymf/cf2d172a6da494b2e577f699254a780907b7c222246d7-ehahn1.png" mode=""></image>
			<image src="@/me/static/ymf/eaae3095c9e433fb8a015aaebbe52047009dd863ff2b9-igSItK.png" mode=""></image>
			<image src="@/me/static/ymf/f3c60c6f31c74bdf38ba2d8993bcb443ab9bc335cc875-xfx4CJ.png" mode=""></image>
			<image src="@/me/static/ymf/c93b4cc9363d52cba140f7239516344ece9388141079a4-do8mFc.png" mode=""></image>
			<image src="@/me/static/ymf/031da1e7f043de5d5e1ee355994a93e5ec983aca44dc-sZf0Vl_fw1200.jpg" mode=""></image>
			<image src="@/me/static/ymf/c4e6c5023b94de32df2f51d9ebd633790cc6125a88e4-TIyAcR_fw1200.jpg" mode=""></image>
			<image src="@/me/static/ymf/f7da7a8ca57f9f0d56bbb3d7be1d1c86f96304ce4b3a-iVbD8U_fw1200.jpg" mode=""></image>
			<image src="@/me/static/ymf/2e521eee5a8c80ea6879028ee866c5d0531bc4f1d5e48-Bh95lN_fw1200.jpg" mode=""></image>
			<image src="@/me/static/ymf/38cf4e97e52b50be9dcdcdc1352c497e1acc9266cd52-aUOrxn_fw1200.webp" mode=""></image>
			<image src="@/me/static/ymf/73170e6a596b72538dbe743b06ef8b96e4885aff5b4c3-MsouS7_fw1200.jpg" mode=""></image>
			<image src="@/me/static/ymf/e9f9cb6b95c21c714015f15c372f595367aa553f443a-WpOCNo_fw1200.webp" mode=""></image>
			<image src="@/me/static/ymf/f5d96faaa94cb0151a5c95db44e27b80ed6eeac5cb32-rApi2S_fw1200.webp" mode=""></image>
			<image src="@/me/static/ymf/0e114b9ac6afc2b4c0f29520178be64915a111e218a8b5-mbMpg2_fw1200.jpg" mode=""></image>
			<image src="@/me/static/ymf/4783f7272bf31eeafbe1362605477de14b7cd3da24b0-ggkAnW_fw658webp.webp" mode=""></image>
			<image src="@/me/static/ymf/8dce1d3c3a8287c35bc1bf394048d57a64acfa047d4e-6wXnWE_fw658webp.webp" mode=""></image>
			<image src="@/me/static/ymf/ff919b6254e376cc902c89afdcc194ac2ab39c93793b-8TdL1Z_fw658webp.webp" mode=""></image>
			<image src="@/me/static/ymf/3c9a5848127a67f98e25176a042511b89afd31daccc2f-5EHbxD_fw658webp.webp" mode=""></image>
			<image src="@/me/static/ymf/6c9e57bcbcb047039a8edb30ecdd3b912bac99555a8fc-9EK0oO_fw658webp.webp" mode=""></image>
			<image src="@/me/static/ymf/78a020586cfbb791918b767520a8ff3c2ab9660f1ee1b-GX4zcc_fw658webp.webp" mode=""></image>
			<image src="@/me/static/ymf/babf535567728d84346fbb13348e94aae3ec8f82106e2-QKPJoe_fw658webp.webp" mode=""></image>
			<image src="@/me/static/ymf/d1e4d760358ba0733faee6bcad1e23cb4a1d6d76ba290-O3hUrf_fw240webp.webp" mode=""></image>
			<image src="@/me/static/ymf/dcb5a4b0b1060b569f9fc618e6e2708b584f7def1cadf-R7jpIr_fw658webp.webp" mode=""></image>
			<image src="@/me/static/ymf/e4330f184149f502f4948ce3b60059edaffc9c3d94ca4-B11p5c_fw658webp.webp" mode=""></image>
			<image src="@/me/static/ymf/fa22e2a27871e5f8841e8abb12cc4347a6b9440816068-3XsAnJ_fw658webp.webp" mode=""></image>
		</view>
		<view class="list u-flex u-flex-wrap u-row-between" style="padding-bottom: 200rpx;">
			<view @click.stop="toDetail(item)" class="list-item u-m-b-40" v-for="(item,index) in arr" :key="index">
				<view class="item">
					<view class="u-absolute u-flex u-row-center u-col-center">
						<view style="position: absolute;top:10rpx;left: 0;right: 0;" class="color-fff font-bold  u-text-center">ID:
							{{item.uuid}}</view>
						<template v-if="item.state==3">
							<view class="pro" v-if="item.isPro">
								<view>Pro</view>
							</view>
							<view class="gou" v-if="userInfo.modelId==item.id">
								<u-icon name="checkmark" size="40" color="#fff"></u-icon>
							</view>
						</template>
						<template v-else>
							<view class="color-fff font-bold u-font-32 u-flex u-row-center">
								<view v-if="item.state==0">分身未完成</view>
								<view v-if="item.state==1">分身训练中</view>
								<view v-if="item.state==2">分身制作失败</view>
							</view>
						</template>

					</view>

					<image :src="item.mainPhoto" :class="{blur:item.state!=3}" mode="aspectFill"></image>
				</view>
				<view class="u-flex u-m-t-20 u-row-between u-p-r-10">
					<view class="u-font-26 color-666" v-if="item.state">生成于{{item.createTime|timer}}</view>
					<view class="u-font-26 color-666" v-else>-</view>
					<view class="u-flex" @click.stop="del(item,index)">
						<u-icon name="trash" color="#999"></u-icon>
					</view>
				</view>
				<template v-if="item.state==0">
					<view class="u-flex  u-m-t-20">
						<view v-if="item.isPro" class="u-flex" @click.stop="$u.route('/pages/control-fenshen/ceate-fenshen?id='+item.id)">
							<view class="btn-chongzhi font-bold">继续制作</view>
						</view>
						<view v-else class="u-flex" @click.stop="$u.route('/pages/control-fenshen/free-create-fenshen?id='+item.id)">
							<view class="btn-chongzhi font-bold">继续制作</view>
						</view>
					</view>

				</template>
				<template v-if="item.state==1">
					<view class="u-flex  u-m-t-20">
						<view class="u-flex " @click.stop="$u.route('/pages/control-fenshen/wating-result?id='+item.id)">
							<view class="btn-chongzhi font-bold">训练中</view>
						</view>
						<!-- <view class="status u-m-l-40" @click.stop="chongzhi(item)">免费重置</view> -->
					</view>

				</template>
				<template v-if="item.state==2 ">
					<view class="u-flex  u-m-t-20">
						<view class="u-flex " v-if="!item.resetCount" @click.stop="chongzhi(item)">
							<view class="btn-chongzhi font-bold">免费重置</view>
						</view>
						<view class="status" @click.stop="" v-else>免费重置</view>
					</view>
				</template>
				<template v-if="item.state==3&&item.isPro">
					<view class="u-flex u-row-center u-m-t-20">
						<view class="btn-chongzhi font-bold" v-if="!item.resetCount" @click.stop="chongzhi(item)">免费重置</view>
						<view class="status" @click.stop="" v-else>免费重置</view>
						<view class="status u-m-l-40" v-if="userInfo.modelId==item.id" @click.stop="">使用中</view>
						<view class="btn-chongzhi font-bold u-m-l-40" v-else @click.stop="qeihuan(item)">切换分身</view>
					</view>
				</template>
				<template v-if="item.state==3&&!item.isPro">
					<view class="u-flex  u-m-t-20">
						<view class="status " v-if="userInfo.modelId==item.id" @click.stop="">使用中</view>
						<view class="btn-chongzhi font-bold " v-else @click.stop="qeihuan(item)">切换分身</view>
					</view>
				</template>
			</view>

		</view>

		<view v-if="!arr.length" class="u-flex u-row-center min-h-100 u-flex-col">
			<my-empty></my-empty>
		</view>

		<my-pop ref="myPoo">
			<view>
				<view class="u-font-32 u-p-30 u-text-center">
					确认要删除当前分身吗
				</view>
				<view class="u-flex u-row-between u-m-t-40">
					<view class="u-flex-1 u-p-r-30"><my-button @click="close" type="cancel">取消</my-button></view>
					<view class="u-flex-1 u-p-l-30"><my-button @click="delItem">确定</my-button></view>

				</view>
			</view>

		</my-pop>
		<my-pop ref="mypopChongzhi">
			<view>
				<view class="u-font-32 u-p-30 u-text-center">
					确认要重置该分身吗
				</view>
				<view class="u-flex u-row-center u-m-t-20">
					<view class="u-flex" style="border-radius: 32rpx;overflow: hidden;">
						<image :src="selItem.smallUrl" style="width: 320rpx;height: 320rpx;" mode="aspectFill"></image>
					</view>
				</view>
				<view class="u-flex u-row-between u-m-t-60">
					<view class="u-flex-1 u-p-r-30"><my-button @click="close" type="cancel">取消</my-button></view>
					<view class="u-flex-1 u-p-l-30"><my-button @click="confirmChongzhi">确定</my-button></view>

				</view>
			</view>

		</my-pop>
		<u-toast ref="uToast"></u-toast>
		<view class="fixed_b">
			<my-button @click="toCreate">制作新的分身</my-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				psize: 10,
				pnum: 1,
				isEnd: false,
				arr: [],
				selItem: '',
				SelIndex: '',
				isUser: false,
				userInfo: uni.getStorageSync('userInfo')

			};
		},
		filters: {
			timer: function(value) {
				return value.split('T')[0]
			}
		},
		onBackPress(options) {
		},
		methods: {
			toCreate(){
				const isXunlian=this.arr.some(v=>v.state==1||v.state==0)
				if(isXunlian){
					return this.$refs.uToast.show({
						title:'您有分身正在制作中，请稍后！'
					})
				}
				this.$u.route('/pages/control-fenshen/fenshen-first')
				// if(!this.userInfo.tried){
				// 	this.$u.route('/pages/control-fenshen/fenshen-first')
				// }else{
				// 	this.$u.route('/pages/control-fenshen/ceate-fenshen')
				// }
			},
			close() {
				this.$refs.mypopChongzhi.close()
			},
			confirmChongzhi() {
				const index = this.arr.findIndex(v => v.id == this.selItem.id)
				this.$api({
					url: 'model/reset/' + this.selItem.id
				}).then(res => {
					this.$refs.mypopChongzhi.close()
					this.$refs.uToast.show({
						title: res.message,
						type: res.code == 200 ? 'success' : 'error'
					})
					if (res.code == 200) {
						this.$set(this.arr, index, {
							...this.selItem,
							state: 0
						})
					}
				})
			},
			chongzhi(item) {
				if(this.userInfo.modelId==item.id){
					return this.$refs.uToast.show({
						title: '当前分身正在使用，不可重置！',
						type:  'error'
					})
				}
				this.selItem = item
				this.$refs.mypopChongzhi.open()
			},
			toDetail(item) {
				if (item.state == 3) {
					this.$u.route('/pages/control-fenshen/fensehn-detail?id=' + item.id)
				}
			},
			delItem() {
				this.$api({
					url: 'model/delete/' + this.selItem.id
				}).then(res => {
					this.$refs.uToast.show({
						title: res.message,
						type: res.code == 200 ? 'success' : 'error'
					})
					if (res.code == 200) {
						this.arr.splice(this.SelIndex, 1)
						this.$refs.myPoo.close()
						this.$api({
							url: "user/info"
						}).then(res => {
							this.userInfo = res.data
							uni.setStorageSync('userInfo', res.data)
						})
					}

				})
			},
			qeihuan(item) {
				this.$api({
					url: 'model/change/' + item.id,
				}).then(res => {
					this.$refs.uToast.show({
						title: res.message,
						type: res.code == 200 ? 'success' : 'error'
					})
					if (res.code == 200) {
						this.userInfo.modelId = item.id
						this.$api({
							url: "user/info"
						}).then(res => {
							this.userInfo = res.data
							uni.setStorageSync('userInfo', res.data)
						})
					}
				})
			},
			close() {
				this.selItem = ''
				this.$refs.myPoo.close()
			},
			del(item, index) {
				if(this.userInfo.modelId==item.id){
					return this.$refs.uToast.show({
						title: '当前分身正在使用，不可删除！',
						type:  'error'
					})
				}
				this.selItem = item
				this.SelIndex = index
				this.$refs.myPoo.open()
			}
		},
		onShow() {
			this.userInfo = uni.getStorageSync('userInfo')
			this.pnum = 1
			this.isEnd=false
			this.$api({
				url: 'model/list',
				data: {
					psize: this.psize,
					pnum: this.pnum
				}
			}).then(res => {
				this.arr = res.data
			})
		},
		onLoad(opt) {
			this.isUser = opt.isUser
			// this.$api({
			// 	url: 'model/list',
			// 	data: {
			// 		psize: this.psize,
			// 		pnum: this.pnum
			// 	}
			// }).then(res => {
			// 	this.arr = res.data
			// })
		}
	}
</script>

<style lang="scss" scoped>
	.blur {
		filter: blur(5px);
	}

	.fixed_b {
		left: 140rpx;
		right: 140rpx;
		bottom: 40rpx;
		background-color: transparent;
		z-index: 999;
	}

	.list {
		.item {
			display: flex;
			border: 1px solid rgb(32, 33, 29);
			border-radius: 30rpx;
			overflow: hidden;
			justify-content: center;
			align-items: center;
			position: relative;

			// background-color: rgb(255, 196, 40);
			.u-absolute {
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				z-index: 2;

				.pro {
					position: absolute;
					left: 10rpx;
					bottom: 10rpx;
				}

				.gou {
					position: absolute;
					right: 0;
					bottom: 0;
					padding-top: 10rpx;
					padding-left: 10rpx;
					border-radius: 30rpx 0 0 0;
					background-color: rgb(35, 33, 34);
				}
			}

			image {
				width: 320rpx;
				height: 320rpx;
			}
		}
	}

	.status,
	.btn-chongzhi {
		width: 130rpx;
		text-align: center;
		border: 1px solid transparent;
		border-radius: 100rpx;
		font-size: 24rpx;
		line-height: 48rpx;
	}

	.btn-chongzhi {
		border-color: #333;
		color: #333;
	}

	.status {
		color: #999;
		border-color: #999;
	}
</style>