<template>
	<view class="followRecord_wrap">
		<rxl-timeline class="followRecord_timeLine_wrap" v-if="!isEmpty(dataList)">
			<rxl-timeline-item :timestamp="key" :size="10" color="#108cff" v-for="(followList,key,index) in dataList"
				:key="key+index">
				<view slot="body">
					<view class="body_wrap" v-for="(item) in followList" :key="item.id">
						<view class="body_top">
							<view class="body_top_left">
								<image src="@/static/case/icon-avatar.png" mode=""></image>
								<text class="follow">{{item.createNickname}}</text>
							</view>
							<view class="body_top_right" @click="handleDelete(item)">
								删除
							</view>
						</view>
						<view class="body_center">
							{{item.content}}
						</view>
						<view class="body_bottom">
							<u-icon name="clock" color="#C0C0C7"></u-icon>
							<text class="date">{{item.createTime}}</text>
						</view>
					</view>
				</view>
			</rxl-timeline-item>
		</rxl-timeline>
		<view v-else class="empty_wrap">
			暂无跟进记录
		</view>
		
		<u-modal :show="showModal"
			@confirm="confirm" 
			ref="uModal" 
			:asyncClose="true" 
			showCancelButton
			@cancel="showModal = false"
			cancelColor="#409eff"
			:confirmText="'删除'" confirmColor="#f8836c">
			<view class="modal_box">
				确定要删除跟进记录
			</view>
		</u-modal>
	</view>
</template>
<script>
	import {
		isEmpty
	} from "lodash";
	export default {
		props: {
			clueId: {
				required: true
			},
			orderId: {
				type: String | Number,
				required: false
			},
			type: {
				type: String,
				required: true
			}
		},
		data() {
			return {
				list: [0, 1],
				dataList: {},
				loading: false,
				
				showModal: false,
				currentDeleteFollowId : undefined
			}
		},
		methods: {
			handleDelete(item){
				const { id } = item;
				this.currentDeleteFollowId = id;
				this.showModal = true;
			},
			isEmpty,
			async getData() {
				this.loading = true;
				let data;
				
				// 根据type类型调用不同接口
				if (this.type === '1') {
					// 线索跟进
					data = await uni.$u.api.getClueFollowList({
						clueId: this.clueId
					});
				} else if (this.type === '2') {
					// 订单跟进(通过orderId)
					data = await uni.$u.api.getOrderFollowList({
						orderId: this.orderId
					});
				} else if (this.type === '3') {
					// 订单跟进(通过clueId)
					data = await uni.$u.api.getOrderFollowListByClueId({
						clueId: this.clueId
					});
				} else if (this.type === '4') {
					// 重复线索跟进
					data = await uni.$u.api.getDuplicateClueFollowByClueId({
						clueId: this.clueId
					});
				} else {
					// 重复订单跟进
					data = await uni.$u.api.getDuplicateOrderFollowListByClueId({
						clueId: this.clueId
					});
				}
				
				this.dataList = data.data;
				this.loading = false;
			},
			async confirm() {
				try {
					// 根据type类型调用不同删除接口
					if (this.type === '1' || this.type === '4') {
						// 线索跟进和重复线索跟进使用deleteClueFollow
						await uni.$u.api.deleteClueFollow([this.currentDeleteFollowId]);
					} else {
						// 订单跟进(通过orderId)、订单跟进(通过clueId)、重复订单跟进使用deleteOrderFollow
						await uni.$u.api.deleteOrderFollow([this.currentDeleteFollowId]);
					}
					
					uni.$u.toast("删除成功");
					this.getData();
				} catch (error) {
					console.error('删除跟进记录失败:', error);
					uni.$u.toast("删除失败");
				} finally {
					this.showModal = false;
					this.currentDeleteFollowId = undefined;
				}
			},
		},
		mounted() {
			this.getData();
		},
		beforeDestroy(){
			uni.$off('addFollowSuccess');  
		},
		created() {
			uni.$on('addFollowSuccess',()=>{
				this.getData();
			});
		}
	}
</script>
<style lang="scss" scoped>
	.followRecord_wrap {
		min-height: 400px;
		background: #f5f6f8;
		position: relative;

		.empty_wrap {
			text-align: center;
			color: #999999;
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%, -50%);
		}
	}

	.followRecord_timeLine_wrap {
		background: #ffffff;

		.timeline-item {
			&:last-child {
				::v-deep .left-line .timeline-item-tail {
					display: none;
				}
			}
		}
	}

	.body_wrap {
		margin-bottom: 20rpx;
		background-color: #f6f7f8;
		padding: 20rpx;
		border-radius: 20rpx;

		&:last-child {
			margin-bottom: 0;
		}

		.body_top {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.body_top_left {
				display: flex;
				align-items: center;

				image {
					width: 46rpx;
					height: 46rpx;
					margin-right: 12rpx;
				}

				.follow {
					color: #202020;
					font-size: 30rpx;
					margin-right: 12rpx;
				}

				.debt {
					color: #999999;
					font-size: 24rpx;
				}
			}

			.body_top_right {
				color: #108CFF;
				font-size: 24rpx;
			}
		}

		.body_center {
			margin: 30rpx 0;
			padding-left: 56rpx;
			overflow: hidden;
		}

		.body_bottom {
			display: flex;
			padding-left: 56rpx;

			::v-deep .u-icon__icon {
				font-size: 24rpx !important;
			}

			.date {
				color: #c0c0c7;
				font-size: 22rpx;
				margin-left: 10rpx;
			}
		}
	}
</style>