<template>
	<view class="personal-pet">
		<view v-if="petList.length>0" class="personal-pet-list">
			<!-- <view class="personal-pet-list-add">
				<view style="font-size: 14px;color:#333;">
					全部宠物
				</view>
				<view class="personal-pet-list-add-btn"  @click="addPet">
					<uni-icons type="plus" size="18" color="#AAA" style="margin-right: 5px;"></uni-icons> 添加宠物
				</view>
			</view> -->
			
			<view v-for="(item,index) in petList" :key="index">
				<view :class="['personal-pet-list-item',item.gender=='男生'?'.personal-pet-list-item_backgroud_m':'.personal-pet-list-item_backgroud_f' ]">
					<view class="personal-pet-info">
						<view>
							<u-avatar :src="item.photo?item.photo:defaultPhoto" size="60" shape="circle"></u-avatar>
						</view>
						<view class="personal-pet-info-1">
							<view class="personal-pet-info-2">
								<view class="personal-pet-name">
									{{item.name}}
								</view>
								<view class="personal-pet-sex">
									<img :src="item.gender=='男生'?'../../static/images/details/boy.svg':'../../static/images/details/girl.svg'" alt="sex" 		
												style="width: 16px;height: 16px;"/>
								</view>
							</view>
							<view class="personal-pet-info-3" style="width: 100%;">
								<view class="ellipsis" style="max-width: 25%;" >
									{{item.breed || '未知'}}
								</view>
								<view class="personal-pet-info-age" style="max-width: 90px;">
									{{item.birthDate || '未知'}}
								</view>
								<view class="ellipsis" style="max-width: 25%;">
									{{item.bodyType || '未知'}}
								</view>
							</view>	
						</view>
					</view>
					<view class="personal-pet-info-disposition ellipsis">
						性格： {{item.personality}}
					</view>
					<view class="personal-pet-info-btns">
						<view class="personal-pet-info-btn" @click="editPet(item)">
							<u-icon name="edit-pen" color="#7d8196" size="16" style="margin-right: 5px;"></u-icon>
							<view style="margin-left: 5px;">
								编辑
							</view>
						</view>
						<view class="personal-pet-info-btn" @click="deletePet(item)">
							<u-icon name="trash" color="#7d8196" size="16"></u-icon>
							<view style="margin-left: 5px;">
								删除
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view v-else class="personal-pet-none">
			<img src="https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/pet/catdog.png" alt="pet"  
			style="width: 149px;height: 124px;" mode="widthFix"/>
			<view class="personal-pet-none-text">这里还没有您的宠物,请点击添加吧~</view>
		</view>
		
		<view class="personal-pet-add">
			<button class="personal-pet-add-btn" @click="addPet">
				<view style="font-size: 16px;font-weight: 500;">
					添加宠物
				</view>
			</button>
		</view>
		
		<view class="">
			<u-picker
				:showToolbar='false'
				:show="show"
				:columns="petTypes"
				@change="petTypeChange"
				@cancel="cancel"
				@confirm="confirm"
			></u-picker>
		</view>
		<u-modal :show="showDel" 
			@confirm="confirmDel" 
			@cancel="cancelDel" 
			ref="uModal" 
			showCancelButton 
			:asyncClose="true" 
			:content='delContent'></u-modal>
	</view>
</template>

<script>
	import { getDictList } from "@/api/system/user.js"
	import { getPetList,delPet } from "@/api/system/pet"
	export default{
		data(){
			return{
				defaultPhoto:'https://catmdogf.oss-cn-shanghai.aliyuncs.com/CMDF/front/personal/pet/catdog.png',
				petList:[],
				show:false,
				showDel:false,
				petTypes:[],
				delContent:'',
				deleteId:'',
				petType:''
			}
		},
		onShow(options) {
			this.getPetList();
		},
		onPullDownRefresh() {
			this.getPetList();
		},
		mounted() {
			this.getPetTypeList();
			this.getPetList();
		},
		methods:{
			getPetList(){
				this.petList=[]
				getPetList().then(res=>{
					if(res&&res.content){
						this.petList=res.content
						this.showDel = false;
					} 
				})
				
			},
			getPetTypeList(){
				getDictList('pet_type').then(res=>{
					if (res.code == 200) {
						let petType = res.data.map(e=>e.dictLabel)
						this.petTypes=[petType]
						console.log(this.petTypes)
					} else {
						this.$modal.showToast('获取pet type失败')
					}
				})
			},
			addPet(){
				this.show = true;
			},
			cancel() {
				this.show = false
			},
			petTypeChange(e){
				console.log(e)
				this.petType=e.value[0]
			},
			confirm(e) {
				this.show = false
				console.log(this.petType) 
				if(this.petType==='猫猫'){
					uni.navigateTo({
						url: `/pages/personalCenter/petInfo?petType=cat&optionType=add`
					});
				}
				if(this.petType==='狗狗'){
					uni.navigateTo({
						url: `/pages/personalCenter/petInfo?petType=dog&optionType=add`
					});
				}
				if(!this.petType){
					if(e.value[0]==='猫猫'){
						uni.navigateTo({
							url: `/pages/personalCenter/petInfo?petType=cat&optionType=add`
						});
					}
					if(e.value[0]==='狗狗'){
						uni.navigateTo({
							url: `/pages/personalCenter/petInfo?petType=dog&optionType=add`
						});
					}
				}
			},
			editPet(item){
				if(item.petType=='猫猫' || item.petType=='cat'){
					uni.navigateTo({
						url: `/pages/personalCenter/petInfo?petType=cat&optionType=edit&petId=${item.id}`
					});
				}
				if(item.petType=='狗狗'|| item.petType=='dog'){
					uni.navigateTo({
						url: `/pages/personalCenter/petInfo?petType=dog&optionType=edit&petId=${item.id}`
					});
				}				
			},
			deletePet(item){
				this.delContent = "确定要删除"+item.name+'?';
				this.showDel = true;
				this.deleteId=item.id;
			},
			confirmDel(){
				delPet(this.deleteId).then(res=>{
					console.log(res);
					this.$modal.showToast('删除成功');
					this.getPetList()
				})
				
				
			},
			cancelDel(){
				this.showDel = false;
				this.deleteId='';
			}
		}
	}
</script>

<style lang="scss">
	.personal-pet{
		position: relative;
		height: 100%;
		padding-bottom: 90px;
		.personal-pet-add{
			background-color: #FFFFFF;
			padding:10px 20px 40px;
			width: 100%;
			height: 90px;
			position: fixed;
			bottom: 0;
			z-index: 100;
			display: flex;
			.personal-pet-add-btn{
				width: 100%;
				border-radius: 6px;
				background: #FFB13F;
				font-size: 16px;
				color: #FFFFFF;
			}
		}

		.personal-pet-list{
			.personal-pet-list-add{
				width: 100%;
				height: 44px;
				background-color: #FFFFFF;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 15px;
				.personal-pet-list-add-btn{
					font-size: 14px;
					color:#AAA;
					display: flex;
					align-items: center;
				}
				
			}
			.personal-pet-list-item_backgroud_m{
				background: linear-gradient(179deg, #EDF5FE 0.75%, #FFF 34.11%);
			}
			.personal-pet-list-item_backgroud_f{
				background: linear-gradient(179deg, #FFF4F6 0.75%, #FFF 34.11%);
			}
			.personal-pet-list-item{
				margin: 10px 10px 0 10px;
				border-radius: 5px;
				padding: 20px 10px 10px;
				.personal-pet-info{
					display: flex;
					align-items: center;
					justify-content: flex-start;
					.personal-pet-info-1{
						margin-left: 10px;
						.personal-pet-info-2{
							display: flex;
							flex-wrap: wrap;
							.personal-pet-name{
								color: #333;
								font-size: 16px;
								margin-right: 10px;
							}
						}
						.personal-pet-info-3 {
							display: flex;
							align-items: baseline;
							font-size: 14px;
							margin-top: 5px;
							color: #7D8196;
							.personal-pet-info-age{
								padding: 0 10px; 
								margin: 0 10px;
								border-left: solid 2px #7D8196;
								border-right: solid 2px #7D8196;
							}
							
						}
						
					}
					
				}
				.personal-pet-info-disposition{
					padding: 10px; 
					color: #7D8196;
					font-size: 14px;
					background: #f9f9f9;
					border-radius: 5px;
					margin-top: 10px;
				}
				.personal-pet-info-btns{
					display: flex;
					justify-content: flex-end;
					margin-top: 10px;
					.personal-pet-info-btn{
						display: flex;
						font-size: 14px;
						color: #7D8196;
						margin-left: 20px;
					}
				}
			}
			
		}
		.personal-pet-none{
			display:flex;
			justify-content: center;
			align-items: center;
			flex-wrap: wrap;
			margin-top: 40%;
			.personal-pet-none-text{
				color: #666;
				text-align: center;
				font-size: 14px;
				width: 100%;
				margin-top: 10px;
			}
		}
		
	}
	
</style>