<template>
	<view class='container'>
		<view style="background: linear-gradient( 180deg, #D9F5E8 0%, #FFFFFF 100%);padding-bottom: 200rpx;">
			<u-navbar :autoBack="true" placeholder="true" title="历史工单" bgColor="transparent">
			</u-navbar>
		</view>
		<view style="margin-top: -200rpx;">
			<view class="flex mlr-10">
				<view @click="onClick(index)" class="h-150 mlr-10 br-12 flex-column flex-between-center"
					style="flex: 1;background: #FFFFFF;"
					:style="{ color: index==0?'#FF682D':index==1?'#EEA616':index==2?'#1FCA7C':'#23BBEE'}"
					v-for="(item, index) in 4" :key="index">
					<text class="mt-20 fs-36">{{numberList[(index+1)]}}</text>
					<text class="mb-20 fs-24">{{index==0?'待处理':index==1?'待支付':index==2?'已完成':index==3?'已取消':''}}</text>
					<view v-if="index==tabIndex"
						style="background: #27CC81;height: 10rpx;width: 100%;border-radius: 12rpx;">
					</view>
					<view v-else style="background: #FFFFFF;height: 10rpx;width: 100%;border-radius: 12rpx;">
					</view>
				</view>
			</view>
		</view>
		<view class="overflow-y">
			<scroll-view scroll-y style="height: 100%;" v-if="list.length!=0">
				<view v-for="(item, index) in list" :key="index" @click="onCz('详情',index)">
					<view class="flex-column br-12 mt-30 mlr-24 pad-20"
						style="box-shadow: 0px 0px 15px 1px rgba(142,142,142,0.16);background: white;">
						<view class="flex vh-center mt-20 fs-24">
							<image class="h-40 w-40 mr-15" src="/static/icon_gd.png"> </image>
							<text class="fs-32 fw-700" style="flex: 1;">{{item.type==1?'居家报修':'公区报事'}}</text>
							<text
								:style="{ color: item.state==1?'#FF682D':item.state==2?'#EEA616':item.state==3?'#1FCA7C':'#23BBEE'}">
								{{item.state==1?'待处理':item.state==2?'待支付':item.state==3?'已完成':item.state==4?'已取消':''}}
							</text>
						</view>
						<text class="fs-28 br-6 pad-20 mtb-20"
							style="background: #EBF5F0;">{{item.repairsDescription}}</text>
						<scroll-view scroll-x="true" scroll-with-animation="true">
							<view class="flex">
								<view class="w-140 h-140 mr-15"
									v-for="(itemImg, index2) in $wskj.splitList(item.repairsImg)" :key="index2">
									<image class="w-140 h-140" :src="itemImg"></image>
								</view>
							</view>
						</scroll-view>
						<view class="flex vh-center h-60 mt-20 fs-24">
							<text style="flex: 1;color: #000;">{{item.repairsTime}}</text>
							<text @tap.stop="onCz('关闭报修',index)" v-if="item.state==1"
								style="color: #23BBEE;">{{item.type==1?'关闭报修':'关闭报事'}}</text>
							<text @tap.stop="onCz('去支付',index)" v-if="item.state==2" style="color: #EEA616;">去支付</text>
							<text style="color: #27CC81;margin-left: 30rpx;">{{item.type==1?'报修详情':'报事详情'}}</text>
						</view>
					</view>
				</view>

			</scroll-view>
			<view style="padding-top: 300rpx;" v-if="list.length==0">
				<u-empty mode="data"></u-empty>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: 0,
				tabIndex: 0,
				list: [],
				numberList: '',
			};
		},
		onLoad(e) {
			this.type = e.type
		},
		onShow() {
			this.getList()
			this.getNumberList()
		},
		methods: {
			onClick(index) {
				this.tabIndex = index;
				this.getList()
			},
			onCz(e, index) {
				if (e == "详情") {
					this.$wskj.to("/pagesMain/pages/bxxq?id=" + this.list[index].repairsId)
					return
				}
				if (e == "关闭报修") {
					this.$wskj.showModal('提示', '确定关闭报修？', true, (confirmed) => {
						if (confirmed) {
							this.gb(this.list[index].repairsId);
						}
					});
					return
				}
				if (e == "去支付") {
					this.pay(index)
					return
				}
			},
			pay(index) {
				const params = {
					url: '/api/payRecord/pay',
					loadingTip: '',
					data: {
						orderId: this.list[index].repairsId,
						amount: this.list[index].money,
						payBizCode: "MAINTAIN", //PRESTORE（预存）MAINTAIN（维修）
					}
				}
				this.$request(params).then(res => {
					uni.requestPayment({
						provider: 'wxpay',
						appid: res.data.data.appId, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
						nonceStr: res.data.data.nonceStr, // 随机字符串
						package: res.data.data.package, // 固定值
						timeStamp: res.data.data.timeStamp, // 时间戳（单位：秒）
						paySign: res.data.data.paySign, // 签名，这里用的 MD5/RSA 签名
						signType: 'MD5',
						success: (res) => {
							this.getData()
						},
						fail: (err) => {
							console.log(err);
						}
					});
				})
			},
			getNumberList() {
				const params = {
					url: '/api/repairs/userCountAll',
					method: 'GET',
				}
				this.$request(params).then(res => {
					this.numberList = res.data

				})
			},
			getList() {
				const params = {
					url: '/api/repairs/userListAll?state=' + (this.tabIndex + 1),
					method: 'GET',
				}
				this.$request(params).then(res => {
					this.list = res.data

				})
			},
			gb(id) {
				const params = {
					url: '/api/repairs/cancel/' + id,
					method: 'PUT',
				}
				this.$request(params).then(res => {
					this.getList()
					this.getNumberList()

				})
			}
		},
	};
</script>

<style scoped>
	.container {
		height: 100vh;
		display: flex;
		flex-direction: column;
		background: #fff;
	}
</style>