<template>
	<view class="p-person-avatar g_flex_column_center g_w_all g_h_all g_bg_0 g_kuaishou">
		<view>
			<view
				:style="{
					width: size.width + 'px',
					height: size.width + 'px',
				}"
				style="overflow: hidden"
				class="g_flex_c"
			>
				<img
					:src="avatar"
					:style="{
						width: size.width + 'px',
						height: size.width + 'px',
					}"
				/>
			</view>
			<view class="g_flex_row_center g_mt_30">
				<view v-if="!isEdit" class="g_flex_row_center">
					<!-- || MP-WEIXIN -->
					<!-- #ifdef APP-PLUS || H5 || MP-TOUTIAO || MP-KUAISHOU  -->
					<button aria-role="button" class="g_w_110 g_h_32 g_flex_c g_radius_17 g_c_f" style="border: 1px solid #fff; background-color: transparent; font-size: 14px" @click="handleOpenAvatar">更换图片</button>
					<!-- #endif -->
					<!-- #ifdef MP-WEIXIN -->
					<!-- <button open-type="chooseAvatar" @chooseavatar="handleStartChangeAvatar" aria-role="button" class="g_w_110 g_h_32 g_flex_c g_radius_17 g_c_f" style="border: 1px solid #fff; background-color: transparent; font-size: 14px">更换图片</button> -->
					<button @click="handleOpenAvatar" aria-role="button" class="g_w_110 g_h_32 g_flex_c g_radius_17 g_c_f" style="border: 1px solid #fff; background-color: transparent; font-size: 14px">更换图片</button>
					<!-- #endif -->
				</view>
				<view v-if="isEdit" class="g_flex_row_center">
					<view class="g_w_110 g_h_32 g_flex_c g_radius_17 g_c_f g_mr_16" style="border: 1px solid #fff" @click="handleCancel">取消</view>
					<view class="g_w_112 g_h_34 g_flex_c g_radius_17 g_c_f g_bg_f40" style="border: 2px solid #ff4400" @click="handleConfirm">确认</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	onReady() {
		uni.setNavigationBarColor({
			backgroundColor: "#000000",
			frontColor: "#ffffff",
		});
	},
	onLoad(options) {
		let that = this;
		console.log("options", options);
		this.info = options;
		this.avatar = options.value;
		uni.setNavigationBarTitle({
			title: `修改${options.label}`,
		});
	},
	onShareAppMessage() {
		return this.G.shareFun();
	},
	data() {
		return {
			avatar: "",
			isEdit: false,
			size: {
				width: uni.getSystemInfoSync().windowWidth,
				height: uni.getSystemInfoSync().windowHeight,
			},
		};
	},
	methods: {
		uploadPhoto(callback=()=>{}) {
			if (uni.getSystemInfoSync().platform === 'ios'){
				callback();
			}else{
				this.checkPermission(()=>{
					callback();
				});
			}
		},
		// 检查权限
		checkPermission(callback=()=>{}) {
				let that = this;
				// 先检查是否已经拥有权限
				try {
						var hasCameraPermission = plus.android.checkPermission("android.permission.CAMERA");
						var hasStoragePermission = plus.android.checkPermission("android.permission.WRITE_EXTERNAL_STORAGE");
						
						// 如果已经拥有所有权限，直接执行回调
						if (hasCameraPermission && hasStoragePermission) {
								callback();
								return;
						}
				} catch (e) {
						// 如果检查权限的方法不可用，继续执行原有逻辑
						console.log("检查权限方法不可用，继续执行原有逻辑");
				}
				
				// 没有权限或无法检查权限，显示授权提示弹窗
				uni.showModal({
						content: '这里的上传图片需要进行【拍摄照片，访问相册，存储照片】授权，否则无法上传图片。',
						title: '上传图片',
						success: (resula) => {
								if(resula.confirm){
										plus.android.requestPermissions(
												["android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"],
												function(resultObj) {
														var result = 0;
														for (var i = 0; i < resultObj.granted.length; i++) {
																result++;
														}
														if (resultObj.granted.length > 0) {
																// 权限获取成功，执行上传操作
																callback();
														} 
												}, function(error) {
														uni.showToast({
																title: "权限请求失败",
																icon: "none"
														});
												}
										);
								}
						},
						fail: (res) => {},
						complete: (res) => {},
				})
		},
		handleOpenAvatar() {
			let that = this;
			that.uploadPhoto(()=>{
				uni.showActionSheet({
					itemList: ['从相册选择','使用相机'],
					success: function (res) {
						if(res.tapIndex == 0){
							console.log('点击相册')
							// 相册
							that.setAvatar(['album'])
						}else{
							console.log('点击拍照')
							// 拍照
							that.setAvatar(['camera'])
						}
					},
					fail: function (res) {}
				});
			});
		},
		setAvatar($type) {
			let that = this;
			that.G.uploadImg(
				(res) => {
					console.log("获取数据", res);
					that.avatar = res.image;
					that.isEdit = true;
				},
				"default",
				1,
				$type
			);
		},
		handleCancel() {
			let that = this;
			that.isEdit = false;
			// that.avatar =  this.avatar;
		},
		handleConfirm() {
			let that = this;
			that.G.Put(
				"/yishoudan/agency/updateField",
				{
					fieldName: this.info.fieldName,
					fieldValue: that.avatar,
				},
				(res) => {
					that.isEdit = false;
					uni.showToast({
						title: "修改成功",
						icon: "success",
					});
					that.G.checkToken();
					uni.$emit("changeCompanyDesp");
					setTimeout(() => {
						uni.navigateBack();
					}, 1500);
				}
			);
		},
	},
};
</script>

<style></style>
