<template>
	<view class="farmer-order-land-list">
		<fixed>
			<tab @change="handerTabChange" :list="tabList" :activeIndex="activeIndex" :activeColor="primaryColor"
				:width="100/tabList.length + '%'" height="100rpx" msgRight="40rpx"></tab>
			<view class="b-1px-b"></view>
		</fixed>
		<view @tap.stop="goDetail(index)" class="order-item fill-base mg-big pd-lg radius-16 box-shadow"
			v-for="(item,index) in list.data" :key="index">
			<view class="flex-between pb-lg  c-title">
				<view class="f-title flex-y-baseline"><i class="iconfont icon-dianpu mr-sm"></i>
					<view class="text-bold max-400 ellipsis">{{item.farmer_info.title}}</view>
				</view>
				<view class="f-paragraph"
					:style="{color:item.pay_type == 1? subColor: item.pay_type == 2? primaryColor: ''}">
					{{statusType[item.pay_type]}}
				</view>
			</view>
			<view class="flex-center" :class="[{'mt-lg':index!=0}]">
				<image mode="aspectFill" lazy-load class="avatar radius-24" :src="item.goods_cover"></image>
				<view class="flex-1 ml-lg">
					<view class="f-title c-title text-bold ellipsis" style="max-width: 498rpx;"> {{item.goods_name}}
					</view>
					<view class="f-caption c-caption mt-sm"> 租赁期限：{{item.end_time}} 到期 </view>
					<view class="f-caption c-caption"> 租赁面积：{{item.area}}㎡ </view>
				</view>
			</view>
			<view class="flex-between mt-md">
				<view></view>
				<view class="flex-warp" v-if="item.pay_type == 1">
					<view @tap.stop="toChangeOrder(index,'cancel_item')"
						class="common-btn disabled flex-center f-caption c-title radius-4">取消订单
					</view>
					<view @tap.stop="toPay(index)" class="common-btn flex-center f-caption c-base radius-4 ml-lg"
						:style="{background:primaryColor}">去支付
					</view>
				</view>
				<view class="flex-warp" v-if="(item.pay_type > 1 && !item.have_eva) || item.pay_type == 2">
					<view @tap.stop="goDetail(index,1)" class="common-btn flex-center f-caption c-base radius-4 ml-lg"
						:style="{background:primaryColor}" v-if="item.pay_type == 2">我要配送
					</view>
					<view @tap.stop="toEvaluate(index)" class="common-btn flex-center f-caption c-base radius-4 ml-lg"
						:style="{background:subColor}" v-if="!item.have_eva">去评价
					</view>
				</view>
			</view>
		</view>

		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1"></abnor>

		<view class="space-footer"></view>
		<common-popup @confirm="confirmCancel" ref="cancel_item" type="CANCEL_ORDER" :info="popupInfo"></common-popup>
	</view>
</template>

<script>
	import {
		mapState,
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				options: {},
				activeIndex: 0,
				tabList: [{
					id: 0,
					title: '全部'
				}, {
					id: 2,
					title: '租赁中',
					number: 0
				}, {
					id: 7,
					title: '已完成'
				}],
				statusType: {
					'-1': '已取消',
					1: '待支付',
					2: '租赁中',
					7: '已完成',
				},
				param: {
					page: 1,
				},
				list: {
					data: []
				},
				loading: true,
				popupInfo: {},
				lockTap: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
		}),
		onLoad(options) {
			this.$util.showLoading()
			this.options = options
			let {
				tab = 0
			} = options
			this.activeIndex = tab
			this.initIndex()
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return
			this.param.page = this.param.page + 1
			this.loading = true
			this.getList()
		},
		methods: {
			initIndex() {
				this.getList()
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex()
			},
			handerTabChange(index) {
				this.activeIndex = index
				this.$util.showLoading()
				this.param.page = 1
				this.list.data = []
				this.getList()
			},
			async getList() {
				let {
					list: oldList,
					param,
					tabList,
					activeIndex
				} = this
				param.pay_type = tabList[activeIndex].id
				let newList = await this.$api.land.orderList(param);

				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				let {
					rent_count,
				} = newList.count
				this.tabList[1].number = rent_count
				this.$util.hideAll()
			},
			// 取消订单
			async toChangeOrder(index, key) {
				let {
					id,
					order_code,
					goods_cover: image,
				} = this.list.data[index]
				this.popupInfo = {
					id,
					name: `订单编号：${order_code}`,
					image,
					index,
				}
				this.$refs[key].open()
			},
			async confirmCancel() {
				let {
					id,
					index,
				} = this.popupInfo
				if (this.lockTap) return;
				this.lockTap = true;
				this.$util.showLoading()
				try {
					await this.$api.land.cancelOrder({
						id
					})
					this.$util.hideAll()
					if (this.activeIndex == 0) {
						this.list.data[index].pay_type = -1
					} else {
						this.list.data.splice(index, 1)
					}
					this.$util.showToast({
						title: `取消成功`
					})
					this.lockTap = false
					this.$refs.cancel_item.close()
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
						this.$util.hideAll()
					}, 2000)
				}
			},
			// 去支付
			async toPay(index) {
				if (this.lockTap) return;
				this.lockTap = true;
				this.$util.showLoading()
				let {
					id,
					pay_model
				} = this.list.data[index]
				try {
					let {
						pay_list
					} = await this.$api.land.landRePayOrder({
						id
					})
					this.$util.hideAll()
					if (pay_list) {
						if (pay_model == 3) {
							pay_list = {
								orderInfo: pay_list,
								provider: 'alipay'
							}
						}
						try {
							await this.$util.pay(pay_list)
							this.lockTap = false;
							if (this.activeIndex == 0) {
								this.list.data[index].pay_type = 2
							} else {
								this.list.data.splice(index, 1)
							}
							let {
								number
							} = this.tabList[1]
							this.tabList[1].number = number + 1
						} catch (e) {
							this.lockTap = false;
							return;
						}
					}
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
						this.$util.hideAll()
					}, 2000)
				}
			},
			// 去评价
			toEvaluate(index) {
				let {
					id
				} = this.list.data[index]
				let url = `/mine/pages/evaluate/edit?id=${id}&type=land`
				this.$util.goUrl({
					url
				})
			},
			// 订单详情
			goDetail(index, tab = 0) {
				let {
					id
				} = this.list.data[index]
				let url = `/land/pages/order/detail?id=${id}&tab=${tab}`
				this.$util.goUrl({
					url
				})
			}
		}
	}
</script>


<style lang="scss">
</style>
