<template>
	<view class="content">
		<com-navbar :leftIcon="false" bgColor="#fff" title="需求" :titleStyle="{ color: '#000000', fontSize: '34rpx' }" />
		<view></view>
		<view class="content-tabs" :style="{ top: `calc(${globalData.statusBarHeight}rpx + 122rpx)` }">
			<view class="content-tabs-text" v-for="(v,index) in list" @click="jump(index)" :key="index">
				<view class="" :style="subscript == index?'color: #000000;' : 'color: #444444;'">
					{{v.name}}
				</view>
				<view v-show="subscript == index" class="content-tabs-text-area"></view>
			</view>
		</view>
		<template v-if="statusNeedList && statusNeedList.length">
			<comindex :list="statusNeedList" class="content-mindex" :subscript='subscript' @getVal="handleStatus">
			</comindex>
		</template>
		<view v-else class="diagram">
			<image src="@/static/icon/qst.png" mode=""></image>
			<view class="">
				暂无数据
			</view>
		</view>
		<com-tabbar :list=" [{
					name: '首页',
					icon: '/static/tabbar/tb1.png', 
					selectIcon:'/static/tabbar/td1.png', 
					url: '/pages/home/index',
					type: 'type'
				}, {
					name: '我的需求', 
					icon: '/static/tabbar/tb2.png',
					selectIcon: '/static/tabbar/td2.png', 
					url: '/pages/demand/index',
					type: 'demand'
				}, {
					name: '发布', 
					icon: '/static/tabbar/fb.png',
					selectIcon:'/static/tabbar/fb.png', 
					url: '/pages/release/index', 
					type: 'type'
				}, {
					name: '动态',
					icon: '/static/tabbar/tb3.png', 
					selectIcon: '/static/tabbar/td3.png', 
					url: '/pages/order/index',
					type: 'type'
				}, {
					name:'我的' ,
					icon:'/static/tabbar/tb4.png' ,
					selectIcon:'/static/tabbar/td4.png', 
					url: '/pages/my/index',
					type: 'type'
				}]"></com-tabbar>
	</view>
</template>

<script>
	import {
		myNeedList,
		orderDetailList,
		removeOrderFunction,
		refundFunction,
		applyRefund
	} from '../../api/system/index.js'
	import apiBaseConfig from '@/config/index.js';
	import uniGo from '@/utils/unIGO.js';
	import comindex from '@/pages/demand/components/index.vue'
	export default {
		data() {
			return {
				imgBgUrl: apiBaseConfig.imgBgUrl,
				globalData: getApp().globalData,
				title: 'Hello',
				arr: [],
				list: [{
					name: '已发布'
				}, {
					name: '已驳回'
				}, {
					name: '待支付'
				}, {
					name: '已接单'
				}],
				subscript: '0', //状态
				needInfo: {
					pageSize: 10,
					pageNum: 1,
					status: 0,
					deleteStatus: 0,
					orderByColumn: 'modifyTime',
					isAsc: 'desc'
				},
				statusNeedList: []
			}
		},
		components: {
			comindex
		},
		onLoad(option) {
			if (option.id) {
				this.subscript = option.id
			}
		},
		onShow() {
			this.getMyNeedList()
		},
		methods: {
			async getMyNeedList() {
				let that = this
				that.needInfo.status = that.subscript
				that.statusNeedList = []
				if (that.subscript == 3) {
					that.$delete(that.needInfo, "status")
					that.$set(that.needInfo, 'statusList', '3,5,6')
				} else {
					that.$delete(that.needInfo, "statusList")
				}
				const data_back = await orderDetailList(that.needInfo)
				const {
					code,
					total,
					rows
				} = data_back
				if (code == 200) {
					if (total > 0) {
						that.statusNeedList = rows
					} else {
						that.statusNeedList = []
					}
				}

			},
			jump(ind) {
				let that = this
				this.subscript = ind
				that.getMyNeedList()
			},
			// 取消订单接口
			async handleCancle(id) {
				let that = this
				const back_data = await removeOrderFunction({
					id: id
				})
				console.log(back_data, '取消')
				const {
					code
				} = back_data
				if (code == 200) {
					uni.showToast({
						title: '取消成功!',
						icon: 'none',
						duration: 2000,
						success() {
							setTimeout(() => {
								that.getMyNeedList()
							}, 1000)
						}
					})
				}
			},
			// 立即退款接口
			async handleRefund(id, price) {
				const data_back = await refundFunction({
					refundeAmount: price,
					orderId: id
				})
				console.log(data_back, '退款')
				const {
					code
				} = data_back
				if (code == 200) {
					uni.showToast({
						title: '退款成功!',
						icon: 'none',
						duration: 2000,
						success() {
							setTimeout(() => {
								that.getMyNeedList()
							}, 1000)
						}
					})
				}
			},
			//申请退款接口
			async applyRefundFun(id){
				let that = this
				const data_back = await applyRefund({id:id,status:'5'})
				const {code} = data_back
				if(code === 200){
					uni.showToast({
						title: '提交申请成功!',
						icon: 'none',
						duration: 2000,
						success() {
							setTimeout(() => {
								that.getMyNeedList()
							}, 1000)
						}
					})
				}
			},
			// 取消订单2 立即退款3
			handleStatus(val, id, price) {
				let that = this
				if (val == 2) {
					that.handleCancle(id)
				} else if (val == 3) {
					// that.handleRefund(id, price)
					that.applyRefundFun(id)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		&-tabs {
			display: flex;
			justify-content: space-around;
			width: 100%;
			position: fixed;
			padding-top: 20rpx;
			padding-bottom: 20rpx;
			top: -1rpx;
			z-index: 999;
			background-color: #f3f4f6;

			&-text {
				>view:nth-child(1) {
					font-size: 24rpx;
				}

				&-area {
					width: 75rpx;
					height: 6rpx;
					background: #2C66FF;
				}
			}
		}

		&-mindex {
			margin-top: 60rpx;
			width: 100%;
			margin-bottom: 150rpx;
		}
	}

	.diagram {
		text-align: center;
		margin-top: 335rpx;

		>image {
			width: 396rpx;
			height: 243rpx;
		}

		>view {
			margin-top: 20rpx;
			font-size: 24rpx;
			font-weight: 400;
			color: #444444;
		}
	}
</style>
