<template>
	<view>
		<view class="w750 padding flex-col align-center " style="min-height: 100%;">
			<view class="response padding bg-white margin-bottom border-bottom" style="border-radius: 20rpx;"
				v-for="(item, index) in userPostList" :key="index" @click="edit(item.carId)">

				<view class="flex-row  margin-bottom-xs align-center">
					<view>
						<u--image class="goodImg" radius="10rpx" width="240rpx" height="140rpx" :showLoading="true"
							:src=" baseUrl + firstSubstring(item.carPics)"></u--image>
					</view>
					<view class="flex-col justify-between margin-bottom-xs margin-left-xs response"
						style="height: 140rpx;">
						<view class="text-lg text-bold">
							<text class="margin-right-xs">{{item.carBrand}}</text> <text>{{item.carModel}}</text>
						</view>
						<view>
							<text class="text-bold text-orange text-lg">
								{{ formatAmountToWan(item.carPrice) + ' 万' }}</text>
						</view>
						<view class="flex-row justify-between text-sm">
							<text>上户：{{item.carRecordDate}}</text>
							<text>公里：{{item.carKilometers}}</text>
							<!-- <text>上户地点：{{ getMappingArea(item.province,item.city,item.county) }} </text> -->
						</view>
					</view>

				</view>

				<view class="flex-row justify-between align-center margin-bottom-xs">
					<view>
						<text class="text-sm margin-bottom-xs">{{item.createTime}}</text>
					</view>
					<view class="flex-row">
						<button class="cu-btn line-black text-red sm round margin-right-sm"
							@click.stop="edit(item.carId)">编辑</button>
						<button class="cu-btn line-red text-red sm round" @click.stop="del(item.carId)">删除</button>
					</view>
				</view>


			</view>
			<u-loadmore :status="status" line fontSize="22rpx" />
			<view v-show="userPostList.length===0">
				<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" textSize="22rpx"
					iconSize="120rpx">
				</u-empty>
			</view>
		</view>
	</view>
</template>

<script>
	import Decimal from "decimal.js"; // 具体文件中引入
	export default {
		data() {
			return {
				imagesUrl: getApp().globalData.config.imagesUrl,
				baseUrl: getApp().globalData.config.baseUrl,
				userPostList: [],
				user: {},
				role: '',
				isLoadMore: false, //是否加载中
				params: {
					pageSize: 10,
					pageNum: 1
				},
				status: 'loadmore',
			}
		},
		onLoad() {
			this.getConfig()
		},
		onReachBottom() {
			if (!this.isLoadMore) { //此处判断，上锁，防止重复请求
				this.status = 'loading';
				this.isLoadMore = true
				this.params.pageNum++
				console.log(this.params.pageNum);
				this.getConfig()
			}
		},
		methods: {
			// 获取配置:用户发布次数
			async getConfig() {
				const user = await this.$request({
					url: '/getInfo',
				})
				if (user.code == 200 && user.hasOwnProperty('user')) {
					this.user = user.user
					if (user.roles.length > 0 && user.roles[0] == 'dealers') {
						this.role = user.roles[0]
					}
				}
				// 获取已发布次数
				const getUserPostNum = await this.$request({
					url: '/system/car/getUserPostNum',
					data: this.params
				})
				this.userPostList = this.userPostList.concat(getUserPostNum.rows);
				console.log(getUserPostNum.rows.length);
				if (getUserPostNum.rows.length < this.params.pageSize) { //判断接口返回数据量小于请求数据量，则表示此为最后一页
					this.status = 'nomore';
					this.isLoadMore = true
				} else {
					this.isLoadMore = false
				}

				console.log(getUserPostNum);
			},
			// 按逗号分隔字符串，获取第一个元素，用于图片展示
			firstSubstring(string) {
				if (string.includes(',')) {
					return string.split(',')[0];
				} else {
					return string;
				}
			},
			// 格式化金额，转为万元单位
			formatAmountToWan(num) {
				num = Number(num);
				if (num == 0) {
					return num + "";
				} else {
					return new Decimal(num).div(new Decimal(10000)).toString();
				}
			},

			// 翻译省市区代码
			async getMappingArea(province, city, county) {
				const provinceRes = await this.$request({
					url: '/shop/region/getInfoWx/' + province,
				});
				const cityRes = await this.$request({
					url: '/shop/region/getInfoWx/' + city,
				});
				const countyRes = await this.$request({
					url: '/shop/region/getInfoWx/' + county,
				});
				return provinceRes.data.name + cityRes.data.name + countyRes.data.name;
			},
			del(carId) {
				this.$modal.confirm('即将删除这辆二手车信息，是否继续?').then(async () => {
					const res = await this.$request({
						url: '/system/car/removeWx/' + carId,
						method: 'DELETE'
					});
					this.$modal.msgSuccess('删除成功')
					this.getConfig()
				})
			},
			edit(carId) {
				this.$tab.navigateTo('/subCarPages/edit/edit?carId=' + carId)
			}

		}
	}
</script>

<style>
	page {
		/* background: #242A38; */
	}
</style>