<template>
	<view class="container">
		<uni-nav-bar title="注册列表" backgroundColor='#03803B' color="#fff" left-icon="left" fixed statusBar
			@clickLeft='goBack()' @clickRight="addDetails" rightWidth="150rpx">
			<block slot="right">
				<view style="font-size: 26rpx;">
					+注册轮胎保
				</view>
			</block>
		</uni-nav-bar>
		<view style="background-color: #fff;">
			<uni-search-bar bgColor="#F6F6F6 " v-model="searchValue" placeholder="请输入轮胎保信息" @confirm="searchChange"
				 cancelButton="none" @clear="searchChange">
			</uni-search-bar>
		</view>
		<z-paging ref="paging" v-model="dataList" @query="getList" :fixed="false" :auto-show-back-to-top="true"
			empty-view-text="暂无数据" :hide-empty-view="false" :auto-show-system-loading="true">
			<view class="list" v-for="(item, index) in dataList" :key="index">
				<uni-swipe-action style="margin-top: 30rpx;">
					<uni-swipe-action-item>
						<view class="tab-bar-item" @click="inEdit(item)">
							<view class="list-item">
								<view style="font-weight: 800;">
									{{item.insuranceNo}}
								</view>
								<view style="color: #B5B4B4;">
									{{item.createTime}}
								</view>

							</view>
							<view class="list-item">
								<view style="font-weight: 800;color: #03803B;">
									{{item.vehicleNumber||'暂无车牌'}}
								</view>
								<view class="">
									轮胎规格: {{item.tyreSpecs||'-'}}
								</view>
							</view>
							<view class="list-item">
								<view class="">
									轮胎:{{item.tireQuantity||0}}条
								</view>
								<view style="display: flex;align-items: center;color: #03803B;">
									<image style="width: 32rpx;height: 32rpx;margin-right: 18rpx;"
										src="@/static/images/home/insurance/ap.png" />
									<text v-if="item.status==0" style="color:#e6a23c">
										录入
									</text>
									<text v-if="item.status==1" style="color:#e6a23c">
										在保
									</text>
									<text v-if="item.status==2" style="color:#409EFF">
										受理中
									</text>
									<text v-if="item.status==3" style="color:#67c23a">
										已通过
									</text>
									<text v-if="item.status==4" style="color:#f56c6c">
										已拒绝
									</text>
								</view>
							</view>
							<view class="list-item-end">
								<view style="color: #B5B4B4;">
									有效截至日期:{{item.insuranceTime||'暂无日期'}}
								</view>
							</view>
						</view>
						<template v-slot:right>
							<view @click.stop="onClick(item)"
								style="width: 120rpx;background-color:#e43d33;display: flex;justify-content: center;align-items: center;color: #fff;">
								<text>删除</text>
							</view>
						</template>
					</uni-swipe-action-item>
				</uni-swipe-action>
			</view>
		</z-paging>

	</view>
</template>

<script>
	import {
		getList,
		remove
	} from '@/api/home/insurance.js'
	export default {
		data() {
			return {
				searchValue:null,
				dataList: [],
			}
		},
		onLoad() {},
		onShow() {
			if (uni.getStorageSync('isUpdate')) {
				this.$nextTick(() => {
					if (this.$refs.paging) {
						this.$refs.paging.reload();
					}
				});
				uni.removeStorageSync('isUpdate')
			}
		},
		filters: {
			statusMap(value) {
				// 状态 0录入，1已提交（在保），2受理中（理赔提交），3已通过（工厂审核通过）
				const statusList = ['录入', '在保', '受理中', '已通过']
				return statusList[value] || '未知状态'
			}
		},
		methods: {
			onClick(row) {
				if(row.status!=0){
					uni.showToast({
						title: '非录入状态不允许删除',
						icon: 'none',
						duration: 2000
					});
					return
				}
				let _this = this
				uni.showModal({
					title: '提示',
					content: '是否确认删除？',
					success: function(res) {
						if (res.confirm) {
							uni.showLoading({
								title: '加载中',
								mask: true
							});
							remove(row.id).then(res => {
									uni.showToast({
										title: '操作成功',
										icon: 'none',
										duration: 2000
									});
									_this.$refs.paging.reload();
								})
								.finally(() => {
									uni.hideLoading()
								});
						}
					}
				});
			},
			addDetails() {
				uni.navigateTo({
					url: '/pages/home/insurance/rules'
				});
			},
			inEdit(row) {
				uni.navigateTo({
					url: '/pages/home/insurance/details?id=' + row.id,
				});
			},
			searchChange() {
				this.$refs.paging.reload()
			},
			async getList(pageNo, pageSize) {
				try {
					const obj = {
						current: pageNo,
						size: pageSize
					}
					const res = await getList(obj)
					this.$refs.paging.complete(res.data.records)
				} catch (e) {
					this.$refs.paging.complete(false)
					uni.showToast({
						title: '加载失败',
						icon: 'none'
					})
				}
			},
			goBack() {
				uni.navigateBack({
					delta: 1,
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.scroll-view {
		flex: 1;
		height: 100%;
		/* 关键样式：防止内容被裁剪 */
		overflow-anchor: none;
	}

	.container {
		height: 100vh;
		display: flex;
		flex-direction: column;
	}

	.loading,
	.no-more {
		text-align: center;
		padding: 20px;
		color: #999;
	}

	.uni-lastmsg {
		width: 80rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		display: block;
		/* 需明确设置display */
	}

	.uni-lastmsg2 {
		width: 140rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		display: block;
		/* 需明确设置display */
	}

	.nav-right {
		width: 37rpx;
		height: 42rpx;
	}

	.list {
		width: 100%;

		.tab-bar-item {
			background-color: #fff;
			padding: 0rpx 62rpx;
			font-size: 28rpx;
			line-height: 32rpx;
			color: #5F5F5F;
			font-weight: 400;

			.list-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				width: 100%;
				height: 100rpx;
				border-bottom: 2px solid #F6F6F6;
			}

			.list-item-end {
				display: flex;
				justify-content: space-between;
				align-items: center;
				width: 100%;
				height: 100rpx;
			}
		}
	}
</style>