<template>
	<view class="content">
		<block v-if="orders.length > 0">
			<view v-for="(item, index) in orders" :key="index" class="order-block">
				<view class="order-header">
					<view class="order-header-no nowrap">{{item.order_no}}</view>
					<view class="order-header-price nowrap">￥{{item.total}}</view>
				</view>
				<view class="order-content">
					<view class="order-content-item">
						<label>订单类型</label>
						<view class="order-content-val order-content-type nowrap">
							<text class="order-type" v-for="(citem, cindex) in item.item_type" :key="cindex" v-if="citem.number > 0">{{citem.title}}×{{citem.number}}</text>
						</view>
					</view>
					<view class="order-content-item">
						<label>客户姓名</label>
						<view class="order-content-val nowrap">{{ item.user.nickname }}</view>
					</view>
					<view class="order-content-item">
						<label>客户电话</label>
						<view class="order-content-val nowrap">{{ item.user.username }}</view>
					</view>
					<view class="order-content-item">
						<label>服务包数量</label>
						<view class="order-content-val nowrap">×{{ item.services.length }}</view>
					</view>
					<view class="order-content-item">
						<label>创建时间</label>
						<view class="order-content-val nowrap">{{item.created_at}}</view>
					</view>
				</view>
				<view class="order-btns">
					<button class="order-left-btn" size="mini" @click="onCallPhone(item.user.username)">联系客户</button>
					<view class="order-right">
						<button size="mini" @click="$Router.push({name: 'WorkPerfectChoose', params: {orderId: item.business_order_id, type: 'perfect'}})">修改</button>
						<button size="mini" @click="$Router.push({name: 'OrderInfo', params: {orderId: item.business_order_id}})">查看</button>
					</view>
				</view>
			</view>
		</block>
		<block v-else>
			<view class="null">
				<u-empty
					mode="order"
					icon="http://cdn.uviewui.com/uview/empty/order.png"
				>
				</u-empty>
			</view>
		</block>
	</view>
</template>

<script>
	import { ordersEditorders } from '@/apis/interfaces/order.js'
	export default {
		data() {
			return {
				orders: [],
				page  : ''
			}
		},
		created() {
			this.getList()
		},
		onShow() {
			// 是否处理当前列表状态
			let isOrderId = this.$store.getters.getOrderId
			if(isOrderId != null){
				this.removeListVal(isOrderId)
			}
		},
		methods:{
			// 联系客户
			onCallPhone(phone){
				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			// 获取订单列表
			getList(){
				ordersEditorders().then(res => {
					let { data, page } = res;
					this.orders = data
					this.page   = page
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			},
			// 变更当前列表状态
			removeListVal(id){
				let ListArr 	= this.orders
				let ListIndex 	= ListArr.findIndex(val => val.business_order_id == id)
				if(ListIndex >= 0){
					this.orders.splice(ListIndex, 1)
				}
				this.$store.commit('setOrderId', null)
			},
		}
	}
</script>

<style lang="scss">
	.content{
		padding: 1rpx 0;
		box-sizing: border-box;
	}
	.null{
		height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	// 订单模块
	.order-block{
		background-color: white;
		margin: 30rpx;
		border-radius: 20rpx;
		.order-header{
			display: flex;
			justify-content: space-between;
			border-bottom: solid 1rpx #f6f6f6;
			padding: 15rpx 30rpx;
			line-height: 80rpx;
			font-size: 30rpx;
			&-no{
				color: gray;
				width: calc(100% - 200rpx);
			}
			&-price{
				font-weight: bold;
				color: red;
				width: 200rpx;
				text-align: right;
			}
		}
		.order-content{
			padding: 20rpx 30rpx;
			&-item{
				display: flex;
				justify-content: space-between;
				line-height: 70rpx;
				font-size: 30rpx;
				label{
					width: 200rpx;
					color: gray;
				}
				.order-content-val{
					width: calc(100% - 200rpx);
					text-align: right;
				}
				.order-content-type{
					text{
						margin-right: 30rpx;
						position: relative;
						display: inline-block;
						&::after{
							position: absolute;
							content: "/";
							width: 30rpx;
							text-align: center;
							font-size: 30rpx;
							top: 0;
							right: -30rpx;
						}
						&:last-child{
							margin-right: 0;
							&::after{
								display: none;
							}
						}
					}
				}
			}
		}
		.order-btns{
			display: flex;
			justify-content: space-between;
			border-top: solid 1rpx #f6f6f6;
			padding: 20rpx 30rpx;
			button[size="mini"]{
				margin: 0;
				padding: 0;
				height: 70rpx;
				line-height: 68rpx;
				border-radius: 40rpx;
				padding: 0 30rpx;
				font-size: 30rpx;
				background: white;
				color: $main-color;
				border:solid 1rpx $main-color;
				&::after{
					display: none;
				}
			}
			.order-right{
				button[size="mini"]{
					margin-left: 30rpx;
					background: $main-color;
					color: white;
				}
			}
		}
	}
</style>
