<template>
	<view class="mine-evaluate-list">
		<fixed>
			<view class="fill-base b-1px-b">
				<view class="pl-lg pr-lg">
					<view class="space-lg"></view>
					<uni-segmented-control :current="typeInd" :values="typeList"
						@clickItem="handerTabChange($event,'typeInd')" style-type="button" :active-color="primaryColor">
					</uni-segmented-control>
					<view class="space-sm"></view>
				</view>
				<tab @change="handerTabChange($event,'activeIndex')" :list="tabList" :activeIndex="activeIndex"
					:activeColor="primaryColor" :width="100/tabList.length + '%'" height="100rpx" msgRight="110rpx">
				</tab>
			</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">
						{{typeList[typeInd].id == 3 ? item.store_info.title : 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" v-if="typeList[typeInd].id != 3">
				<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>
					<block v-if="typeList[typeInd].id == 1">
						<view class="f-caption c-caption mt-sm"> 数量：{{`${item.num}/${item.unit}`}} </view>
						<view class="f-caption c-caption"> 配送周期：{{item.send_cycle}} </view>
					</block>
					<block v-if="typeList[typeInd].id == 2">
						<view class="f-caption c-caption mt-sm"> 租赁期限：{{item.end_time}} 到期 </view>
						<view class="f-caption c-caption"> 租赁面积：{{item.area}}㎡ </view>
					</block>
				</view>
			</view>
			<view class="flex-between f-paragraph mt-md pt-md b-1px-t" v-if="typeList[typeInd].id == 1">
				<view class="c-desc max-380 ellipsis">认养编号 {{item.claim_code}}</view>
				<view class="flex-y-baseline c-title">总计：<view class="f-title c-warning">¥{{item.pay_price}}</view>
				</view>
			</view>
			<block v-if="typeList[typeInd].id == 3">
				<view class="order-item flex-center" :class="[{'mt-md':aindex!=0}]"
					v-for="(aitem,aindex) in item.order_goods" :key="aindex">
					<image mode="aspectFill" lazy-load class="avatar box-shadow-mini radius-24"
						:src="aitem.goods_cover">
					</image>
					<view class="flex-1 ml-lg">
						<view class="f-paragraph c-title text-bold max-490 ellipsis"> {{aitem.goods_name}}
						</view>
						<view class="f-caption c-title" :class="[{'flex-center':aitem.refund_num*1>0}]">
							<view class="ellipsis" :style="{maxWidth:aitem.refund_num*1>0?'300rpx':'490rpx'}">
								{{aitem.spe_name}}
							</view>
							<view class="flex-1 c-warning text-right" v-if="aitem.refund_num*1>0">
								已退x{{aitem.refund_num}}
							</view>
						</view>
						<view class="flex-y-baseline mt-sm">
							<view class="flex-1 f-paragraph c-warning">¥{{aitem.goods_price}} </view>
							<view class="f-caption c-caption">x {{aitem.goods_num}} </view>
						</view>
					</view>
				</view>
				<view class="flex-between mt-lg pt-md b-1px-t">
					<view class="c-title ml-sm mr-sm"> {{`共${item.all_goods_num}件`}}</view>
					<view class="flex-y-baseline f-desc c-title"> 实付：
						<view class="f-title c-warning">¥{{item.true_price}}</view>
					</view>
				</view>
			</block>

			<view class="f-paragraph mt-md pt-md b-1px-t" v-if="activeIndex==1">
				<view class="flex-between c-caption">
					<view class="flex-warp">
						<i class="iconfont icon-star-fill icon-font-color mr-sm"
							:style="{backgroundImage: aindex < item.eva_info.star ? '-webkit-linear-gradient(90deg, #FDCD47, #FFC000)' : '-webkit-linear-gradient(90deg, #eee, #eee)'}"
							v-for="(aitem,aindex) in 5" :key="aindex"></i>
					</view>
					<view>{{item.eva_info.create_time}}</view>
				</view>
				<view class="mt-sm mb-md f-paragraph c-paragraph">
					<text decode="emsp" style="word-break:break-all;">{{item.eva_info.text || '没有填写评价哦'}}</text>
				</view>
				<view class="flex-warp mt-md" v-if="item.eva_info.imgs && item.eva_info.imgs.length > 0">
					<block v-for="(aitem,aindex) in item.eva_info.imgs" :key="aindex">
						<view class="eva-img radius-10 rel" v-if="aindex < 3">
							<image @tap.stop="toPreviewImage(index,aindex)" mode="aspectFill" class="eva-img radius-10"
								:src="aitem">
							</image>
							<view class="more f-caption c-base abs" v-if="aindex == 2 && item.eva_info.imgs.length > 3">
								+{{item.eva_info.imgs.length - 3}}</view>
						</view>
					</block>
				</view>
			</view>
			<view class="flex-between mt-lg">
				<view></view>
				<view class="flex-warp">
					<view @tap.stop="toChangeItem(index,1)"
						class="common-btn flex-center f-caption c-base radius-4 ml-lg"
						:style="{background:primaryColor}" v-if="activeIndex==1">查看评价
					</view>
					<view @tap.stop="toChangeItem(index)" class="common-btn flex-center f-caption c-base radius-4 ml-lg"
						:style="{background:activeIndex==0?primaryColor:subColor}">{{activeIndex==0?'去评价':'删除评价'}}
					</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="confirmChangeOrder" ref="del_item" title="删除评价" desc="请确认是否删除评价，删除后将无法恢复" text="确认删除"
			:info="popupInfo">
		</common-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from "vuex"
	import uniSegmentedControl from "@/components/uni-segmented-control.vue"
	export default {
		components: {
			uniSegmentedControl
		},
		data() {
			return {
				typeInd: 0,
				typeList: [{
					id: 1,
					title: '认养订单',
					method: 'claim'
				}, {
					id: 2,
					title: '土地订单',
					method: 'land'
				}, {
					id: 3,
					title: '商城订单',
					method: 'shop'
				}],
				activeIndex: 0,
				tabList: [{
					id: 1,
					title: '待评价',
					number: 0
				}, {
					id: 2,
					title: '已评价'
				}],
				statusType: {
					'-1': '已取消',
					1: '待支付',
					2: '认养中',
					3: '配送中',
					7: '已完成',
				},
				param: {
					page: 1
				},
				list: {
					data: []
				},
				loading: true,
				lockTap: false,
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			userInfo: state => state.user.userInfo,
		}),
		async onLoad() {
			this.$util.showLoading()
			await this.initIndex()
		},
		onUnload() {
			this.$util.back()
		},
		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: {
			...mapActions(['getUserInfo']),
			async initIndex() {
				await this.getList()
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex()
			},
			async getList() {
				let {
					list: oldList,
					typeInd,
					typeList,
					activeIndex,
					tabList,
				} = this
				let {
					id: type,
				} = typeList[typeInd]
				let param = this.$util.deepCopy(this.param)
				param.type = type
				let methodModel = activeIndex == 0 ? 'noEvaluateOrderList' : 'userEvaluateList'
				let newList = await this.$api.mine[methodModel](param)
				if (activeIndex == 1) {
					newList.data = newList.data.map(item => {
						let new_item = Object.assign({}, item.order_info, {
							eva_info: item
						});
						delete new_item.eva_info.order_info
						return new_item
					})
				}
				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				if (activeIndex == 0) {
					this.tabList[0].number = newList.total
				}
				this.$util.hideAll()
			},
			handerTabChange(index, key) {
				this[key] = index;
				if (key == 'typeInd') {
					this.activeIndex = 0
				}
				this.$util.showLoading()
				this.param.page = 1
				this.list.data = []
				this.getList()
			},
			toPreviewImage(index, aindex) {
				let {
					imgs: urls
				} = this.list.data[index].eva_info
				this.$util.previewImage({
					current: urls[aindex],
					urls
				})
			},
			toChangeItem(index, type = 0) {
				let {
					activeIndex
				} = this
				if (activeIndex == 0 || type == 1) {
					let {
						id,
						order_id,
						eva_info = {
							id: 0
						}
					} = this.list.data[index]
					let {
						id: eva_id
					} = eva_info
					let {
						method
					} = this.typeList[this.typeInd]
					let param = type == 1 ? `&eva_id=${eva_id}` : ``
					let url = `/mine/pages/evaluate/edit?id=${id}${param}&type=${method}`
					this.$util.goUrl({
						url
					})
					return
				}
				this.toChangeOrder(index, 'del_item')
			},
			// 删除评价
			async toChangeOrder(index, key) {
				let {
					eva_info,
					goods_cover = '',
				} = this.list.data[index]
				let {
					id,
					order_code,
				} = eva_info
				this.popupInfo = {
					id,
					name: `订单编号：${order_code}`,
					image: goods_cover,
					index,
					refs_key: key,
				}
				this.$refs[key].open()
			},
			async confirmChangeOrder() {
				let {
					id,
					index,
					refs_key
				} = this.popupInfo
				if (this.lockTap) return;
				this.lockTap = true;
				this.$util.showLoading()
				try {
					await this.$api.mine.evaluateUpdate({
						id,
						status: -1
					})
					this.$util.hideAll()
					this.list.data.splice(index, 1)
					this.$util.showToast({
						title: `删除成功`
					})
					this.tabList[0].number = this.tabList[0].number + 1
					this.$util.back()
					this.lockTap = false
					this.$refs[refs_key].close()
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
						this.$util.hideAll()
					}, 2000)
				}
			},
		}
	}
</script>


<style lang="scss">
	.mine-evaluate-list {
		.common-btn {
			background: #CCCCCC;
		}
	}
</style>
