<template>
    <div class="accountEdit_page">
		<div class="accountEdit_page_head">
			<div>{{ $t('account.UpdateAvatar') }}</div>
			<div class="upload_item">
				<div class="upload_file_item">
					<a-upload
						:capture="null"
						list-type="picture-card"
						:before-upload="beforeUpload"
						v-model:file-list="fileList"
						:customRequest="function(){}"
						:maxCount="1"
						accept=".jpg,.png,.jpeg,.bmp"
						@change="fileUploadChange"
						>
						<div
							class="upload_tip_block"
						>
							<!-- <i class="fi fi-br-upload"></i> -->
							<img :src="uploadUrl?uploadUrl:userDetail.avatar" alt="">
						</div>
					</a-upload>
				</div>
			</div>
		</div>
		<div class="accountEdit_page_body">
			<div class="accountEdit_page_body_item">
				<div class="accountEdit_page_body_item_name">{{$t('account.userName')}}:</div>
				<div class="accountEdit_page_body_item_inut">
					<input type="text" disabled :value="userDetail.userName">
				</div>
			</div>
			<div class="accountEdit_page_body_item">
				<div class="accountEdit_page_body_item_name">{{$t('account.email')}}:</div>
				<div class="accountEdit_page_body_item_inut">
					<input type="text" disabled :value="userDetail.email">
				</div>
			</div>
			<div class="accountEdit_page_body_item">
				<div class="gallery_btn" @click="setSubmit">
					{{$t('account.Submit')}}
				</div>
			</div>
		</div>
		<Cropper ref="Cropper" @handleCropperSuccess="handleCropperSuccess" @closeCropper="deletUploadFile()" :cropperFileData="cropperFileData" :isRound="true"></Cropper>
    </div>
</template>
<script lang="ts">
import { defineComponent,computed,ref,reactive,nextTick,toRefs,createVNode, onMounted} from 'vue'
import { Https } from "@/tool/https";
import { Modal,message } from 'ant-design-vue';
import { useStore } from "vuex";
import Cropper from '@/component/HomePage/Cropper.vue'
import { useI18n } from 'vue-i18n'
export default defineComponent({
    components:{
		Cropper,
    },
    setup() {
        const store = useStore();
		let userDetail:any= computed(()=>{
			return store.state.UserHabit.userDetail
		})
		let accountHomeData:any = reactive({
            cropperFileData:{name:'',uid:''}, //裁剪的原始文件数据
            uploadUrl:'',
			uploadFile:undefined,
			token:'',
			fileList:[]
		})
		let Cropper = ref()
		// provide('exhibitionList',exhibitionList)
		let handleCropperSuccess = (event:any)=>{
            let {file, fileData,base64} =event
			accountHomeData.fileList[0].status = 'done'
			accountHomeData.uploadUrl = base64
			accountHomeData.uploadFile = file
            Cropper.value.closeCropper()
		}
		let beforeUpload=(file:any,fileList:any)=>{
            const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/jpg' || file.type === 'image/bmp';
            if (!isJpgOrPng) {
                message.info(useI18n().t('PrintboardUpload.jsContent1'));
            }
            const isLt2M = file.size / 1024 / 1024 < 5;
            if (!isLt2M) {
                message.info(useI18n().t('PrintboardUpload.jsContent2'));
            }
            if(isJpgOrPng && isLt2M){
            // }else{
            //     return (isJpgOrPng && isLt2M) || Upload.LIST_IGNORE;
            }
            
        }
		let  fileUploadChange = (data:any)=>{
            let file = data.file
			// file.id = res.data.id?res.data.id:""
			var reader = new FileReader();
			reader.onload = (e:any) => {
				let data_new;
				if (typeof e.target.result === 'object') {
				// 把Array Buffer转化为blob 如果是base64不需要
				data_new = window.URL.createObjectURL(new Blob([e.target.result]));
				} else {
				data_new = e.target.result;
				}
				Cropper.value.getOptionImg(data_new)
				
				
			};
			// 转化为base64
			// reader.readAsDataURL(file)
			// 转化为blob
			reader.readAsArrayBuffer(file.originFileObj);
			Cropper.value.changeShowModal(true)
        }
		let deletUploadFile = () => {
			accountHomeData.fileList = []
            // let index = -1
            // this.fileList.forEach((ele:any,index1:any) => {
            //     if(this.cropperFileData.uid === ele.uid){
            //         index = index1
            //     }
            // });
            // if(index > -1){
            //     this.fileList.splice(index, 1)
            // }
        }
		let setSubmit = ()=>{
			if(!accountHomeData.uploadFile)return
			let param = new FormData(); 
			param.append('file',accountHomeData.uploadFile);
			let config:any = {headers:{'Content-Type':'multipart/form-data','Accept':'*/*'  }}
			Https.axiosPost(Https.httpUrls.uploadAvatar,param,config)
			.then((rv)=>{
				let data = {
					avatar : rv
				}
				store.commit("upUserDetail", data)
				message.success('提交成功')
			})

		}
        return{
           ...toRefs(accountHomeData),
		   userDetail,
		   Cropper,
		   handleCropperSuccess,
		   beforeUpload,
		   fileUploadChange,
		   deletUploadFile,
		   setSubmit,
        }
    },
    data(){
        return{
            
        }
    },
})
</script>
<style lang="less" scoped>
.accountEdit_page{
	.accountEdit_page_head{
		display: flex;
		align-items: center;
		position: relative;
		img{
			width: 10rem;
			object-fit: contain;
			height: 10rem;
			border-radius: 50%;
			background: #fff;
		}
		.accountEdit_page_head_upload{
			width: auto;
		}
		.upload_item{
			:deep(.ant-upload-list-picture-card-container){
				display: none !important;
			}
		}
		
		margin-bottom: 5rem;
	}
	.accountEdit_page_body{
		width: 85rem;
		.accountEdit_page_body_item{
			display: flex;
			margin-bottom: 5rem;
			flex-direction: column;
			width: 100%;
			.gallery_btn{
				margin-left: auto;
			}
			input,textarea{
				padding-left: 2.7rem;
				border-radius: 1.6rem;
				border: 2px solid #D0D0D0;
				width: 100%;
				font-size: 2rem;
			}
			input{
				height: 7rem;
			}
			.accountEdit_page_body_item_name{
				color: #000;
				text-align: left;
				font-size: 2rem;
			}
			.accountEdit_page_body_item_inut{
				margin-top: 1.5rem;
				flex: 1;
			}
		}
		.accountEdit_page_body_item:last-child{
			justify-content: center;
		}
	}
}
</style>
