<template>
	<view style="background-color: #f7f7f7;padding-top: 2rpx;height: calc(100vh - 90rpx);">
		
		<view>
			
			<view class="sim_info" v-if="iccidObj.data_iccid">
				<view class="sim_info_loding" v-if="loding">
					<image src="../../static/images/cb/6286299.gif" mode="" class="img"></image>
				</view>
				<view class="sim_info_title">
					<image :src="$imageURL+'/bigdata_app'+'/image/cb/sim1.png'" mode=""></image>
					<p>数据SIM卡流量</p>
				</view>
				<view class="sim_info_text">
					<view class="sim_info_iccid" @click="copy(siminfo['data'].iccid)">
						<p>ICCID：{{siminfo['data'].iccid}}</p>
						<image src="../../static/images/ba167c2774bc7a63381dc3aaf1ef95c.png" mode="" class="tishi">
						</image>
					</view>
					<view class="" v-if="!isDataFault">
						<p>总流量：{{siminfo['data'].total}}M</p>
						<p>状态：{{siminfo['data'].status}}</p>
						<p>到期时间：{{siminfo['data'].expire}}</p>
						<p  v-if="myuser_type == 1">厂商名称：{{siminfo['data'].company}}</p>
						<p class="infoDeclear">
							<span>已使用：<span :class="[siminfo['data'].used/siminfo['data'].total < 1 ?'green':'red']">{{siminfo['data'].used.toFixed(2)}}M</span>/{{siminfo['data'].total}}M</span>
							<span>剩余：<span :class="[siminfo['data'].used/siminfo['data'].total < 1 ?'green':'red']">{{(siminfo['data'].total - siminfo['data'].used).toFixed(2)}}M</span></span>
						</p>
						<u-line-progress :percent="(siminfo['data'].used/siminfo['data'].total) * 100" height="12" :show-percent="false" :active-color="(siminfo['data'].used/siminfo['data'].total) < 1 ? '#14A478' : '#f56c6c'"></u-line-progress>
					</view>
				</view>
			</view>
			<template v-if="iccidObj.photo_iccid || iccidObj.is_editor">
				<view class="sim_info">
					<view class="sim_info_loding" v-if="loding2">
						<image src="../../static/images/cb/6286299.gif" mode="" class="img"></image>
					</view>
					<view class="sim_info_title">
						<image mode="widthFix" :src="$imageURL+'/bigdata_app/image/cb/sim2.png'"></image>
						图片sim卡流量
					</view>
					<view class="sim_info_text">
						<view class="sim_info_iccid">
							<p>ICCID：{{siminfo['photo'].iccid}}</p>
							<image @click="copy(siminfo['photo'].iccid)" src="../../static/images/ba167c2774bc7a63381dc3aaf1ef95c.png" mode="" class="tishi">
							</image>
							<text class="yficonfont icon-xiugai" @click="editFun(siminfo['photo'].iccid)"></text>
							<!-- <image  src="../../static/images/ba167c2774bc7a63381dc3aaf1ef95c.png" mode="" class="tishi">
							</image> -->
						</view>
						<view class="contenB" v-if="!isPhotoFault">
							<p>总流量：{{siminfo['photo'].total}}M</p>
							<p>状态：{{siminfo['photo'].status}}</p>
							<p>到期时间：{{siminfo['photo'].expire}}</p>
							<p v-if="myuser_type == 1">厂商名称：{{siminfo['photo'].company}}</p>
							<p class="infoDeclear">
								<span>已使用：<span :class="[siminfo['photo'].used/siminfo['photo'].total < 1 ?'green':'red']">{{siminfo['photo'].used.toFixed(2)}}M</span>/{{siminfo['photo'].total}}M</span>
								<span>剩余：<span :class="[siminfo['photo'].used/siminfo['photo'].total < 1 ?'green':'red']">{{(siminfo['photo'].total - siminfo['photo'].used).toFixed(2)}}M</span></span>
							</p>
							<u-line-progress :percent="(siminfo['photo'].used/siminfo['photo'].total) * 100" height="12" :show-percent="false" :active-color="(siminfo['photo'].used/siminfo['photo'].total) < 1 ? '#14A478' : '#f56c6c'"></u-line-progress>
						</view>
					</view>
				</view>
			</template>
			<u-modal v-model="editFlag" width="90%" :async-close="true" :show-cancel-button="true"
				@confirm="editFunConfim">
				<view class="slot-content">
					<view class="expireBox">
						<u-form>
							<u-form-item label="新iccid号" :label-width="140">
								<u-input v-model="editSim" type="text" :border="true" />
							</u-form-item>
						</u-form>
					</view>
				</view>
			</u-modal>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				myuser_type:'',
				iccid: '',
				iccidObj: {
					data_iccid: '',
					photo_iccid: ''
				},
				siminfo: {
					data: {
						expire: "",
						iccid: "",
						status: "",
						total: 0,
						used: 0,
						company:''
					},
					photo: {
						expire: "",
						iccid: "",
						status: "",
						total: 0,
						used: 0,
						company:''
					}
				},
				editSim:'',
				editFlag: false,
				isPhotoFault: false,
				isDataFault: false,
				loding: true,
				loding2: true,
			}
		},
		methods: {
			async editFun(iccid) {
				// this.editSim = iccid;
				this.editSim = '';
				this.editFlag = true;
			},
			async editFunConfim() {
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.send_control.device_sim_update',
					data: {
						d_id: this.id,
						simid:this.editSim
					}
				})
				if(res.code) {
					this.editFlag = false;
					this.loding2 = false;
					uni.showToast({
					    title: '修改成功',
					    duration: 2000,
						icon:"none"
					});
					this.simdata(this.editSim, 'photo');
				} else {
					uni.showToast({
					    title: res.msg,
					    duration: 2000,
						icon:"none"
					});
				}
				console.log(res);
			},
			async simid(data) { //获取sim卡号
				this.loding2 = true
			
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.send_control.device_sim_new',
					data: {
						d_id: data
					}
				})
			
				if(res.data_iccid == '' && res.photo_iccid == '') {
					if(res.is_editor) {
						this.isPhotoFault = true;
						this.iccidObj = res;
						this.siminfo.photo.iccid = ''
						// this.siminfo = {
						// 	photo: {
						// 		iccid: ''
						// 	}
						// }
						this.loding2 = false;
					} else {
						uni.showToast({
							title: '此设备暂无SIM卡信息!',
							duration: 2000,
							icon:"none"
						});
					}
					return
				}
				this.iccidObj = res;
				if (res.data_iccid) {
					this.simdata(res.data_iccid, 'data')
				}
				if (res.photo_iccid) {
					this.simdata(res.photo_iccid, 'photo')
				} else if(res.is_editor) {
					this.isPhotoFault = true;
					this.iccidObj = res;
					this.siminfo.photo.iccid = ''
					// this.siminfo = {
					// 	photo: {
					// 		iccid: ''
					// 	}
					// }
					this.loding2 = false;
				}
				console.log('sim=====',this.siminfores);

			},
			async simdata(data, type) { //获取sim信息
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.send_control.sim_query_new',
					data: {
						iccid: data
					}
				})
				if(type == 'data') {
					this.loding = false;
				} else {
					this.loding2 = false;
				}
				if(res.code) {
					uni.showToast({
					    title: res.msg,
					    duration: 5000,
						icon:"none"
					});
					this.siminfo[type] = {
						iccid: res.iccid
					}
					if(type == 'data') {
						this.isDataFault = true;
					} else {
						this.isPhotoFault = true;
					}
					return
				}
				if(type == 'data') {
					this.isDataFault = false;
				} else {
					this.isPhotoFault = false;
				}
				this.siminfo[type] = res;
				console.log('sim----',this.siminfo[type])
			},
			clickLeft() {
				uni.navigateBack({
					delta: 1
				})
			},
			copy(item) {
				uni.setClipboardData({
					data: item,
					success: function() {
						console.log('success');
					}
				});
			},
		},
		onLoad(option) {
			console.log(option)
			this.id = option.id ? option.id : option.d_id
			this.loding = true
			this.simid(this.id);
			uni.getStorage({
				key:"myuser_type",
				success:(res)=>{
					this.myuser_type = res.data
				}
			})
		}
	}
</script>

<style lang="scss" scoped="scoped">
	.expireBox{
		padding: 0 20rpx;
	}
	.green{
		color: #14A478;
	}
	.red{
		color: #f56c6c;
	}
	.sim_info {
		position: relative;
		width: 90%;
		margin: 30rpx auto;
		// box-shadow: 0 0 10rpx #bcb9ca;
		background: #fff;
		padding: 30rpx 20rpx 50rpx;
		border-radius: 20rpx;
		box-sizing: border-box;

		.sim_info_loding {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			background-color: rgba(0, 0, 0, 0.3);
			border-radius: 20rpx;
			height: 100%;
			display: flex;
			justify-content: space-around;
			align-items: center;

			.img {
				width: 200rpx;
				height: 175rpx;

			}
		}

		.sim_info_title {
			width: 100%;
			display: flex;

			image {
				width: 24rpx;
				height: 32rpx;
				margin: 6rpx 20rpx 0 0;
			}

			p {
				font-weight: 700;
			}
		}

		.sim_info_text {
			width: 100%;
			padding-left: 44rpx;
			box-sizing: border-box;
			p {
				height: 50rpx;
				font-size: 26rpx;
				line-height: 50rpx;
			}
			
			.infoDeclear{
				display: flex;
				justify-content: space-between;
				align-items: center;
			}
			.sim_info_iccid {
				display: flex;
				align-items: center;
				.tishi {
					width: 30rpx;
					height: 30rpx;
					color: #000;
					margin:0 20rpx;
				}
			}
		}
	}
</style>