<template>
	<view class="device-detail">
		<app-top-push-notice />
		<!-- 顶部导航 -->
        <view class="tabback">
            <view class="rtjt" @click="btnback" style="font-size: 50rpx;">←</view>
            <view class="name">{{ $i18n.t('securityCompanyDetail') }}</view>
			<view style="width: 36rpx;"></view>
		</view>
		<view class="img">
			<image :src="xqobj.logo || 'https://api.ccttiot.com/smartmeter/img/static/uZiAQwh3lTliRkGXV3R0'" mode=""></image>
		</view>
		<view class="guanli">
			<view class="hezi" style="border-bottom: 1px solid #D8D8D8;">
				<view class="lt">
					<image src="https://api.ccttiot.com/smartmeter/img/static/uhAjj5mjJXEJsm5g8fiG" mode=""></image>
					<view class="">
							<text class="one">{{ $i18n.t('companyName') }}</text>
						<text class="two">{{xqobj.name || '--'}}</text>
					</view>
				</view>
				<view class=""></view>
			</view>
			<view class="hezi" style="border-bottom: 1px solid #D8D8D8;" @click="btntel">
				<view class="lt">
					<image src="https://api.ccttiot.com/smartmeter/img/static/uwlbogrtVgcOMQElD4Fw" mode=""></image>
					<view class="">
							<text class="one">{{ $i18n.t('companyPhone') }}</text>
						<text class="two">{{xqobj.phone || '--'}}</text>
					</view>
				</view>
				<image style="width: 64rpx;height: 64rpx;" src="https://api.ccttiot.com/smartmeter/img/static/uB6FO31UpSNWjbz0zVS7" mode=""></image>
			</view>
			<view class="hezi" style="border-bottom: 1px solid #D8D8D8;" @click="btnemail">
				<view class="lt">
					<image src="https://api.ccttiot.com/smartmeter/img/static/umwKb6l3vZpGW1xEwgDW" mode=""></image>
					<view class="">
							<text class="one">{{ $i18n.t('emailLabel') }}</text>
						<text class="two">{{xqobj.email || '--'}}</text>
					</view>
				</view>
				<image style="width: 64rpx;height: 64rpx;" src="https://api.ccttiot.com/smartmeter/img/static/uwEfRqaz2EHNqZ5soFAI" mode=""></image>
			</view>
			<view class="hezi" style="border-bottom: 1px solid #D8D8D8;">
				<view class="lt">
					<image src="https://api.ccttiot.com/smartmeter/img/static/uTHcfYtX8VZA6nbV4yin" mode=""></image>
					<view class="">
							<text class="one">{{ $i18n.t('websiteLabel') }}</text>
						<text class="two">{{xqobj.website == null ? '--' : xqobj.website}}</text>
					</view>
				</view>
				<image style="width: 64rpx;height: 64rpx;" src="https://api.ccttiot.com/smartmeter/img/static/uEXyvsA6LzF56VBUGve7" mode=""></image>
			</view>
		</view>
		<view class="invite-footer">
			<view class="invite-btn" @click="btnInviteCompany">{{ $i18n.t('inviteCompanyToSpace') }}</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
                xqobj:{},
				id:'',
				spaceId: '',
				inviteSubmitting: false
			}
		},
		computed: {
			// 计算当前语言，用于触发更新
			currentLanguage() {
				return this.$i18n.getCurrentLanguage();
			}
		},
		watch: {
			// 监听语言变化，强制更新组件
			currentLanguage() {
				console.log('安全公司详情页面语言变化:', this.currentLanguage);
				this.$forceUpdate();
			}
		},
		onLoad(option) {
			this.id = option.id
			this.spaceId = option.spaceId || uni.getStorageSync('kjid') || ''
            this.getxq()
		},
		onShow() {
			// 页面显示时强制更新
			this.$forceUpdate();
		},
		mounted() {
			// 监听语言变化事件
			uni.$on('languageChanged', this.handleLanguageChange);
		},
		beforeDestroy() {
			// 移除事件监听
			uni.$off('languageChanged', this.handleLanguageChange);
		},
		methods: {
			// 处理语言变化
			handleLanguageChange() {
				this.$forceUpdate();
			},
			// 点击拨打电话
			btntel(){
				if(!this.xqobj.phone){
					uni.showToast({
						title: this.$i18n.t('phoneNotExists') || '电话号码不存在',
						icon: 'none'
					})
					return
				}
				uni.makePhoneCall({
					phoneNumber: this.xqobj.phone,
					success: function() {
						console.log(this.$i18n.t('phoneCallSuccess'))
					},
					fail: function() {
						console.log(this.$i18n.t('phoneCallFailed'))
					}
				})
			},
			// 点击发送邮件
			btnemail(){
				// 检查邮箱是否存在
				if(!this.xqobj.email){
					uni.showToast({
						title: this.$i18n.t('emailNotExists'),
						icon: 'none'
					})
					return
				}
				// 使用 plus.runtime.openURL 打开邮件客户端
				// #ifdef APP-PLUS
				plus.runtime.openURL(`mailto:${this.xqobj.email}`, function(res) {
					console.log(this.$i18n.t('emailClientOpenedSuccess'))
				}, function(err) {
					console.log(this.$i18n.t('emailClientOpenFailed') + err.message)
					// 如果打开邮件客户端失败，显示提示
					uni.showToast({
						title: this.$i18n.t('cannotOpenEmailClient'),
						icon: 'none'
					})
				})
				// #endif
				// #ifdef H5
				// H5环境下使用 window.location.href
				window.location.href = `mailto:${this.xqobj.email}`
				// #endif
				// #ifdef MP-WEIXIN
				// 小程序环境下复制邮箱地址到剪贴板
				uni.setClipboardData({
					data: this.xqobj.email,
					success: function() {
					uni.showToast({
						title: this.$i18n.t('emailCopiedToClipboard'),
						icon: 'success'
					})
					},
					fail: function() {
					uni.showToast({
						title: this.$i18n.t('copyFailed'),
						icon: 'none'
					})
					}
				})
				// #endif
			},
			
			// 请求安全公司详情
			getxq(){
				this.$http.get(`/app/company/detail?id=${this.id}`).then(res =>{
					if(res.code == 200){
						this.xqobj = res.data[0]
					}
				})
			},
			// 邀请该公司为当前空间提供安防服务
			btnInviteCompany() {
				if (!this.id) {
					uni.showToast({ title: this.$i18n.t('inviteCompanyInvalid'), icon: 'none' })
					return
				}
				if (!this.spaceId) {
					uni.showToast({ title: this.$i18n.t('inviteCompanySpaceMissing'), icon: 'none' })
					return
				}
				if (this.inviteSubmitting) return
				this.inviteSubmitting = true
				const payload = {
					companyId: this.id,
					spaceId: this.spaceId
				}
				this.$http.post('/bst/companySpace/apply', payload).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.msg || this.$i18n.t('inviteCompanySuccess'),
							icon: 'success',
							duration: 2000
						})
					} else {
						uni.showToast({
							title: res.msg || this.$i18n.t('inviteCompanyFailed'),
							icon: 'none',
							duration: 3000
						})
					}
				}).catch(() => {
					uni.showToast({
						title: this.$i18n.t('inviteCompanyFailed'),
						icon: 'none'
					})
				}).then(() => {
					this.inviteSubmitting = false
				})
			},
			// 点击返回上一级
			btnback(){
				uni.navigateBack()
			}
		}
	}
</script>

<style scoped lang="less">
	.guanli{
		background-color: #fff;
		margin-top: 60rpx;
		.hezi{
			display: flex;
			justify-content: space-between;
			height: 122rpx;
			align-items: center;
			padding: 0 32rpx;
			.lt{
				font-size: 30rpx;
				color: #3D3D3D;
				display: flex;
				align-items: center;
				view{
					.one{
						font-size: 26rpx;
						color: #727272;
					}
					.two{
						font-size: 30rpx;
						color: #3D3D3D;
						margin-top: 4rpx;
					}
					text{
						display: block;
					}
				}
				image{
					width: 64rpx;
					height: 64rpx;
					// background: #D8D8D8;
					border-radius: 6rpx 6rpx 6rpx 6rpx;
					margin-right: 50rpx;
				}
			}
		}
	}
	.img{
		width: 252rpx;
		height: 252rpx;
		background: #D8D8D8;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		margin: auto;
		margin-top: 60rpx;
		image{
			width: 252rpx;
			height: 252rpx;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
		}
	}
	.device-detail{
		background-color: #F3F5F6;
	}
	.tabback {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		height: 100rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
		background-color: #fff;
		margin-top: 80rpx;
		box-shadow: 0rpx 0rpx 0rpx 0rpx;
		.rtjt {
			font-size: 36rpx;
			color: #333;
		}
		.name {
			font-size: 36rpx;
			font-weight: bold;
			color: #333;
		}
	}

	.device-detail {
		min-height: 93vh;
		padding-bottom: calc(160rpx + env(safe-area-inset-bottom));
		box-sizing: border-box;
	}

	.invite-footer {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		padding: 24rpx 40rpx;
		padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
		background-color: #fff;
		box-shadow: 0 -4rpx 24rpx rgba(0, 0, 0, 0.06);
		box-sizing: border-box;
	}
	.invite-btn {
		width: 100%;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		background-color: #000;
		color: #fff;
		border-radius: 50rpx;
		font-size: 32rpx;
		font-weight: 500;
	}

</style>