<template>
	<view class="call-list" :class="['call-list-'+type]">
		<view v-for="(item,index) in list" :key="index" class="item" 
			@touchstart="touchStart($event, index)"
			@touchmove="touchMove($event, index)"
			@touchend="touchEnd($event, index)"
			:style="{'transform': 'translateX(' + swiperList[index] + 'px)', 'transition': swiperList[index] == 0 ? 'all 0.3s' : ''}">
			<view class="avatar" v-if="type!='xiaomi'">
				<image v-if="type=='ios'" :src="'/static/image/call/iosAvatar.png'||item.avatar" mode=""></image>
				<image v-else-if="type=='oppo'" :src="`/static/image/call/${type}StatusIcon${item.status}.png`" mode="widthFix">
				</image>
				<image v-else-if="type=='huawei'" :src="`/static/image/call/${type}StatusIcon${item.status}.png`"
					mode="widthFix"></image>
				<image v-else-if="type=='vivo'" :src="`/static/image/call/${type}StatusIcon${item.status}.png`" mode="widthFix">
				</image>

			</view>
			<view class="infoBox">
				<view class="left-box">
					<view class="leftInfo">
						<view class="title" :class="{'title-red':item.status==0}">
							<!-- ios -->
							<view class="notes" v-if="type=='ios'">
								{{item.name||item.phone}}
							</view>
							<!-- xiaomi -->
							<view class="notes" v-else-if="type=='xiaomi'">
								{{item.name||item.notes||item.phone}}
								<text v-if="!item.name&&item.notes">{{item.phone}}</text>
							</view>
							<view class="notes" v-else-if="type=='oppo'">
								{{item.name||item.phone}}
							</view>
							<view class="notes" v-else-if="type=='huawei'">
								{{item.name||item.phone}}
							</view>
							<view class="notes" v-else-if="type=='vivo'">
								{{item.name||item.phone}}
							</view>
						</view>
						<view class="info">

							<!-- 电话 -->
							<view class="phone" v-if="type=='vivo'">
								{{item.notes||item.phone}}
							</view>
							<!-- 卡几 -->
							<view class="kj" v-if="type=='oppo'||type=='huawei'">
								<image :src="`/static/image/call/${type}KJ${item.kj}.png`" mode=""></image>
							</view>
							<!-- 时间 -->
							<view class="time" v-if="type=='xiaomi'">
								{{item.time}}
							</view>
							<!-- icon -->
							<view class="status-icon" v-if="type=='ios'">
								<image src="/static/image/call/iosStatusIcon.png" mode=""></image>
							</view>
							<!-- 地址 -->
							<view class="address" v-if="type!='ios'">
								{{item.address}}
							</view>
							<!-- 运营商 -->
							<view class="yys">
								{{item.yys}}
							</view>
							<!-- 电话 -->
							<view class="phone" v-if="type=='ios'">
								{{item.phone}}
							</view>
							<!-- 备注 -->
							<view class="notes" v-if="type=='oppo'||type=='huawei'">
								{{item.notes}}
							</view>
						</view>
					</view>
				</view>
				<view class="right-box">
					<!-- 时间 -->
					<view class="time" v-if="type!='xiaomi'">
						{{item.time}}
					</view>
					<!-- 图标 -->
					<view class="icon">
						<image :src="`/static/image/call/${type}RightIcon.png`" mode=""></image>
					</view>
				</view>
			</view>
			<!-- 编辑按钮 -->
			<view class="edit-btn" @click="editItem(index)">
				<text class="edit-text">编辑</text>
			</view>
			<!-- 删除按钮 -->
			<view class="delete-btn" @click="deleteItem(index)">
				<text class="delete-text">删除</text>
			</view>
		</view>
	</view>

	<!-- 编辑弹窗 -->
	<view class="modal-mask" v-if="showEditModal" @click="closeEditModal">
		<view class="modal-content" @click.stop>
			<view class="modal-header">
				<text class="modal-title">编辑通话记录</text>
			</view>
			<view class="modal-body">
				<view class="form-item">
					<text class="form-label">姓名</text>
					<input class="form-input" v-model="editForm.name" placeholder="请输入姓名" />
				</view>
				<view class="form-item">
					<text class="form-label">电话</text>
					<input class="form-input" v-model="editForm.phone" placeholder="请输入电话" />
				</view>
				<view class="form-item">
					<text class="form-label">备注</text>
					<input class="form-input" v-model="editForm.notes" placeholder="请输入备注" />
				</view>
			</view>
			<view class="modal-footer">
				<view class="btn btn-cancel" @click="closeEditModal">取消</view>
				<view class="btn btn-confirm" @click="saveEdit">保存</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref, reactive, onMounted, watch } from 'vue';

	const props = defineProps({
		isHuise: {
			type: Boolean,
			default: true
		},
		type: {
			type: String,
			default: 'ios'
		}
	});

	const emit = defineEmits(['update:list']);

	const list = ref([
  {
    "avatar": "/static/avatar/1.png",
    "title": "13912345678",
    "name": "李强",
    "phone": "13912345678",
    "phoneNotes": "电话",
    "yys": "移动",
    "kj": "3",
    "address": "北京",
    "time": "星期一",
    "icon": "/static/icon/call.png",
    "status": 1,
    "notes": "工作往来"
  },
  {
    "avatar": "/static/avatar/2.png",
    "title": "15887654321",
    "name": "王芳",
    "phone": "15887654321",
    "phoneNotes": "电话",
    "yys": "联通",
    "kj": "2",
    "address": "上海",
    "time": "星期二",
    "icon": "/static/icon/call.png",
    "status": 4,
    "notes": "老朋友"
  },
  {
    "avatar": "/static/logo.png",
    "title": "18711223344",
    "name": "张伟",
    "phone": "18711223344",
    "phoneNotes": "电话",
    "yys": "移动",
    "kj": "1",
    "address": "广州",
    "time": "星期三",
    "icon": "/static/logo.png",
    "status": 0,
    "notes": "推销电话"
  },
  {
    "avatar": "/static/avatar/4.png",
    "title": "13644556677",
    "name": "刘洋",
    "phone": "13644556677",
    "phoneNotes": "电话",
    "yys": "电信",
    "kj": "5",
    "address": "深圳",
    "time": "星期四",
    "icon": "/static/icon/call.png",
    "status": 2,
    "notes": "快递小哥"
  },
  {
    "avatar": "/static/avatar/5.png",
    "title": "17788889999",
    "name": "陈静",
    "phone": "17788889999",
    "phoneNotes": "电话",
    "yys": "联通",
    "kj": "2",
    "address": "杭州",
    "time": "星期五",
    "icon": "/static/icon/call.png",
    "status": 3,
    "notes": "大学同学"
  },
  {
    "avatar": "/static/logo.png",
    "title": "15133334444",
    "name": "赵雷",
    "phone": "15133334444",
    "phoneNotes": "电话",
    "yys": "移动",
    "kj": "4",
    "address": "成都",
    "time": "星期一",
    "icon": "/static/logo.png",
    "status": 5,
    "notes": "骚扰电话"
  },
  {
    "avatar": "/static/avatar/7.png",
    "title": "18966667777",
    "name": "孙丽",
    "phone": "18966667777",
    "phoneNotes": "电话",
    "yys": "电信",
    "kj": "3",
    "address": "武汉",
    "time": "星期二",
    "icon": "/static/icon/call.png",
    "status": 1,
    "notes": "客户"
  },
  {
    "avatar": "/static/avatar/8.png",
    "title": "15555558888",
    "name": "周涛",
    "phone": "15555558888",
    "phoneNotes": "电话",
    "yys": "联通",
    "kj": "1",
    "address": "西安",
    "time": "星期三",
    "icon": "/static/icon/call.png",
    "status": 4,
    "notes": "家人"
  },
  {
    "avatar": "/static/logo.png",
    "title": "13011112222",
    "name": "吴杰",
    "phone": "13011112222",
    "phoneNotes": "电话",
    "yys": "电信",
    "kj": "5",
    "address": "南京",
    "time": "星期四",
    "icon": "/static/logo.png",
    "status": 0,
    "notes": "广告推销"
  },
  {
    "avatar": "/static/avatar/10.png",
    "title": "18899990000",
    "name": "郑爽",
    "phone": "18899990000",
    "phoneNotes": "电话",
    "yys": "移动",
    "kj": "2",
    "address": "长沙",
    "time": "星期五",
    "icon": "/static/icon/call.png",
    "status": 2,
    "notes": "同事"
  },
  {
    "avatar": "/static/logo.png",
    "title": "15712345678",
    "name": "王磊",
    "phone": "15712345678",
    "phoneNotes": "电话",
    "yys": "移动",
    "kj": "3",
    "address": "天津",
    "time": "星期一",
    "icon": "/static/logo.png",
    "status": 3,
    "notes": "未知号码"
  },
  {
    "avatar": "/static/avatar/12.png",
    "title": "15287654321",
    "name": "李娜",
    "phone": "15287654321",
    "phoneNotes": "电话",
    "yys": "联通",
    "kj": "4",
    "address": "苏州",
    "time": "星期二",
    "icon": "/static/icon/call.png",
    "status": 5,
    "notes": "健身房"
  },
  {
    "avatar": "/static/avatar/13.png",
    "title": "18799887766",
    "name": "张敏",
    "phone": "18799887766",
    "phoneNotes": "电话",
    "yys": "移动",
    "kj": "1",
    "address": "青岛",
    "time": "星期三",
    "icon": "/static/icon/call.png",
    "status": 1,
    "notes": "学校老师"
  },
  {
    "avatar": "/static/logo.png",
    "title": "13655443322",
    "name": "刘东",
    "phone": "13655443322",
    "phoneNotes": "电话",
    "yys": "电信",
    "kj": "5",
    "address": "大连",
    "time": "星期四",
    "icon": "/static/logo.png",
    "status": 4,
    "notes": "保险推销"
  },
  {
    "avatar": "/static/avatar/15.png",
    "title": "17722334455",
    "name": "陈龙",
    "phone": "17722334455",
    "phoneNotes": "电话",
    "yys": "联通",
    "kj": "2",
    "address": "厦门",
    "time": "星期五",
    "icon": "/static/icon/call.png",
    "status": 0,
    "notes": "外卖"
  },
  {
    "avatar": "/static/avatar/16.png",
    "title": "15166778899",
    "name": "赵雅",
    "phone": "15166778899",
    "phoneNotes": "电话",
    "yys": "移动",
    "kj": "3",
    "address": "宁波",
    "time": "星期一",
    "icon": "/static/icon/call.png",
    "status": 2,
    "notes": "亲戚"
  },
  {
    "avatar": "/static/logo.png",
    "title": "18911223344",
    "name": "孙浩",
    "phone": "18911223344",
    "phoneNotes": "电话",
    "yys": "电信",
    "kj": "4",
    "address": "郑州",
    "time": "星期二",
    "icon": "/static/logo.png",
    "status": 3,
    "notes": "房产中介"
  },
  {
    "avatar": "/static/avatar/18.png",
    "title": "15544332211",
    "name": "周梅",
    "phone": "15544332211",
    "phoneNotes": "电话",
    "yys": "联通",
    "kj": "1",
    "address": "沈阳",
    "time": "星期三",
    "icon": "/static/icon/call.png",
    "status": 5,
    "notes": "银行客服"
  },
  {
    "avatar": "/static/avatar/19.png",
    "title": "13099887766",
    "name": "吴刚",
    "phone": "13099887766",
    "phoneNotes": "电话",
    "yys": "电信",
    "kj": "2",
    "address": "济南",
    "time": "星期四",
    "icon": "/static/icon/call.png",
    "status": 1,
    "notes": "合作伙伴"
  },
  {
    "avatar": "/static/logo.png",
    "title": "18818818818",
    "name": "郑丽",
    "phone": "18818818818",
    "phoneNotes": "电话",
    "yys": "移动",
    "kj": "5",
    "address": "重庆",
    "time": "星期五",
    "icon": "/static/logo.png",
    "status": 4,
    "notes": "骚扰电话"
  }
]);

	const swiperList = ref([]);
	const startX = ref(0);
	const startY = ref(0);
	const deleteWidth = ref(140);

	const showEditModal = ref(false);
	const editForm = reactive({
		name: '',
		phone: '',
		notes: ''
	});
	const editIndex = ref(-1);

	onMounted(() => {
		swiperList.value = new Array(list.value.length).fill(0);
	});

	watch(() => props.isHuise, (newValue, oldValue) => {
		console.log(newValue);
	});

	const touchStart = (e, index) => {
		startX.value = e.changedTouches[0].clientX;
		startY.value = e.changedTouches[0].clientY;
		swiperList.value.forEach((item, i) => {
			if (i !== index) {
				swiperList.value[i] = 0;
			}
		});
	};

	const touchMove = (e, index) => {
		const moveX = e.changedTouches[0].clientX;
		const moveY = e.changedTouches[0].clientY;
		const disX = moveX - startX.value;
		const disY = moveY - startY.value;
		if (Math.abs(disX) > Math.abs(disY)) {
			e.preventDefault();
			let distance = Math.max(-deleteWidth.value, Math.min(0, disX));
			swiperList.value[index] = distance;
		}
	};

	const touchEnd = (e, index) => {
		const endX = e.changedTouches[0].clientX;
		const disX = endX - startX.value;
		if (disX < -deleteWidth.value / 2) {
			swiperList.value[index] = -deleteWidth.value;
		} else {
			swiperList.value[index] = 0;
		}
	};

	const deleteItem = (index) => {
		uni.showModal({
			title: '确认删除',
			content: '确定要删除这条通话记录吗？',
			confirmText: '删除',
			cancelText: '取消',
			success: (res) => {
				if (res.confirm) {
					list.value.splice(index, 1);
					swiperList.value.splice(index, 1);
					emit('update:list', list.value);
				}
			}
		});
	};

	const editItem = (index) => {
		editIndex.value = index;
		editForm.name = list.value[index].name;
		editForm.phone = list.value[index].phone;
		editForm.notes = list.value[index].notes;
		showEditModal.value = true;
	};

	const closeEditModal = () => {
		showEditModal.value = false;
		editIndex.value = -1;
	};

	const saveEdit = () => {
		if (!editForm.name && !editForm.phone) {
			uni.showToast({
				title: '请填写姓名或电话',
				icon: 'none'
			});
			return;
		}
		list.value[editIndex.value].name = editForm.name;
		list.value[editIndex.value].phone = editForm.phone;
		list.value[editIndex.value].notes = editForm.notes;
		emit('update:list', list.value);
		closeEditModal();
		swiperList.value[editIndex.value] = 0;
	};
</script>

<style lang="scss" scoped>
	.call-list {
		width: 100vw;
		overflow: hidden;
		box-sizing: border-box;
		background-color: #fff;

		.item {
			display: flex;
			position: relative;
			width: 100%;
			.avatar {
				width: 80rpx;
				height: 80rpx;

				image {
					width: 100% !important;
					height: 100% !important;
				}
			}

			.infoBox {
				width: calc(100% - 80rpx);
				display: flex;
				justify-content: space-between;
				align-items: center;
				background-color: #fff;
				z-index: 2;

				.left-box {
					display: flex;
					justify-content: flex-start;
					align-items: center;

					.leftInfo {
						.title {
							font-size: 32rpx;
							color: #333;
							margin-bottom: 10rpx;
						}

						.info {
							display: flex;
							font-size: 28rpx;
							color: #666;

							view {
								margin-right: 10rpx;
							}

							.kj {
								line-height: 0;
								width: 22rpx;
								height: 22rpx;

								image {
									width: 100%;
									height: 100%;
								}
							}

							.status-icon {
								line-height: 0;
								width: 22rpx;
								height: 22rpx;

								image {
									width: 100%;
									height: 100%;
								}
							}
						}

					}
				}

				.right-box {
					display: flex;
					justify-content: flex-end;
					align-items: center;

					.time {
						font-size: 28rpx;
						color: #666;
					}

					.icon {
						width: 40rpx;
						height: 40rpx;
						overflow: hidden;
						margin-left: 20rpx;

						image {
							width: 100%;
							height: 100%;
						}
					}
				}
			}

			.edit-btn {
				position: absolute;
				top: 0;
				right: -140rpx;
				width: 70rpx;
				height: 100%;
				background-color: #007AFF;
				display: flex;
				justify-content: center;
				align-items: center;
				z-index: 1;
				.edit-text {
					color: #fff;
					font-size: 28rpx;
				}
			}

			.delete-btn {
				position: absolute;
				top: 0;
				right: -70rpx;
				width: 70rpx;
				height: 100%;
				background-color: #FF3B30;
				display: flex;
				justify-content: center;
				align-items: center;
				z-index: 1;
				.delete-text {
					color: #fff;
					font-size: 28rpx;
				}
			}
		}
	}

	.call-list-ios {
		.edit-btn{
			right: -140rpx !important;
			width: 70rpx !important;
			height: 100% !important;
			background-color: #007AFF;
		}
		.delete-btn{
			right: -70rpx !important;
			width: 70rpx !important;
			height: 100% !important;
			background-color: #FF3B30;
		}
		.item {
			align-items: center;
			justify-content: space-between;
			height: 132rpx;
		}

		.avatar {
			width: 84rpx;
			height: 84rpx;
			border-radius: 50%;
			margin-left: 32rpx;
		}

		.infoBox {
			width: calc(100% - 136rpx) !important;
			padding: 24rpx 32rpx 24rpx 0;
			height: 100%;
			box-sizing: border-box;

			box-shadow: inset 0 -0.3px 0 0 #C2C2C2;

			.left-box {
				.title {
					font-weight: 400;
					font-size: 16px;
					color: #1A1A1A;
					line-height: 16px;
				}

				.title-red {
					color: #FC3E30 !important;
				}

				.info {
					align-items: center;
				}

				.info .phone {
					font-weight: 400;
					font-size: 14px;
					color: #838383;
					line-height: 20px;
				}

				.info .yys {
					height: 12px;
					background: #C7C7C7;
					border-radius: 2px 2px 2px 2px;
					padding: 0 4rpx;
					font-weight: 400;
					font-size: 10px;
					color: #FFFFFF;
					line-height: 10px;
					text-align: center;
				}
			}

			.right-box {
				.time {
					font-weight: 400;
					font-size: 14px;
					color: #838383;
					line-height: 14px;
				}

				.icon {
					width: 40rpx;
					height: 40rpx;
					overflow: hidden;
					margin-left: 14rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}
	}

	.call-list-xiaomi {
		.item {
			height: 140rpx;
		}

		.infoBox {
			width: 100% !important;
			padding: 34rpx 56rpx 28rpx 56rpx;
			height: 100%;
			box-sizing: border-box;

			.title {
				font-weight: 400;
				font-size: 16px;
				color: #1A1A1A;

				text {
					font-size: 13px !important;
					color: #767676 !important;
				}
			}

			.title-red {
				color: #EE0115 !important;
			}

			.info {

				view {
					font-size: 13px;
					color: #767676;
				}
			}
		}
	}

	.call-list-oppo {
		.item {
			padding: 32rpx 36rpx 0 36rpx !important;
			justify-content: space-between;
			height: 140rpx;
		}

		.infoBox {
			width: calc(100% - 46rpx) !important;
			padding-bottom: 36rpx;
			height: 100%;
			box-sizing: border-box;
			box-shadow: inset 0 -0.3px 0 0 #C2C2C2;

			.title {
				font-weight: 400;
				font-size: 16px;
				color: #1A1A1A;
			}

			.title-red {
				color: #DB2C22 !important;
			}

			.info {

				view {
					font-size: 13px;
					color: #767676;
				}

				.notes {
					color: #F17A30;
				}
			}

			.right-box {
				.time {
					color: #767676;
					font-size: 13px;
				}
			}
		}

		.avatar {
			width: 26rpx !important;
			height: 26rpx !important;
			border-radius: 0 !important;

			image {
				width: 100% !important;
				height: 100% !important;
			}
		}

		.info {
			display: flex;
			align-items: center;
		}
	}

	.call-list-vivo {
		.item {
			padding: 36rpx 52rpx 0 36rpx !important;
			justify-content: space-between;
			height: 140rpx;

			.infoBox {
				width: calc(100% - 52rpx) !important;
				padding-bottom: 32rpx;
				height: 100%;
				box-sizing: border-box;
				box-shadow: inset 0 -0.3px 0 0 #C2C2C2;

				.title {
					font-weight: 400;
					font-size: 16px;
					color: #1A1A1A;
				}

				.title-red {
					color: #F04E51 !important;
				}

				.info {

					view {
						font-size: 12px;
						color: #8C8C8C;
					}
				}

				.right-box {
					.time {
						color: #8C8C8C;
						font-size: 14px;
					}
				}
			}
		}

		.avatar {
			width: 32rpx !important;
			height: 32rpx !important;
			border-radius: 0 !important;

			image {
				width: 100% !important;
				height: 100% !important;
			}
		}
	}

	.call-list-huawei {
		.item {
			padding: 24rpx 32rpx 0 32rpx !important;
			justify-content: space-between;
			height: 120rpx;
		}

		.infoBox {
			width: calc(100% - 52rpx) !important;
			padding-bottom: 24rpx;
			height: 100%;
			box-sizing: border-box;
			box-shadow: inset 0 -0.3px 0 0 #C2C2C2;

			.title {
				font-weight: 400;
				font-size: 16px;
				color: #1A1A1A;
			}

			.title-red {
				color: #E83F28 !important;
			}

			.info {

				view {
					font-size: 13px;
					color: #696969;
				}
			}

			.right-box {
				.time {
					color: #696969;
					font-size: 13px;
				}
			}
		}

		.avatar {
			width: 32rpx !important;
			height: 32rpx !important;
			border-radius: 0 !important;

			image {
				width: 100% !important;
				height: 100% !important;
			}
		}

		.info {
			display: flex;
			align-items: center;
		}
	}

	.modal-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 999;
	}

	.modal-content {
		width: 600rpx;
		background-color: #fff;
		border-radius: 16rpx;
		overflow: hidden;
	}

	.modal-header {
		padding: 40rpx;
		text-align: center;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.modal-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}

	.modal-body {
		padding: 40rpx;
	}

	.form-item {
		margin-bottom: 30rpx;
	}

	.form-label {
		display: block;
		font-size: 28rpx;
		color: #666;
		margin-bottom: 16rpx;
	}

	.form-input {
		width: 100%;
		height: 80rpx;
		border: 1rpx solid #ddd;
		border-radius: 8rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		box-sizing: border-box;
	}

	.modal-footer {
		display: flex;
		border-top: 1rpx solid #f0f0f0;
	}

	.btn {
		flex: 1;
		height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 32rpx;
	}

	.btn-cancel {
		border-right: 1rpx solid #f0f0f0;
		color: #666;
	}

	.btn-confirm {
		color: #007AFF;
	}
</style>
