<template>
	<view class="app-container">
		<no-data v-if="noData" :showText="'暂无工单信息'"></no-data>
		
		<block v-else>
			<view class="top-container">
				<uni-steps :options="steps" :active="orderState" active-color="#FF3F42"></uni-steps>
			</view>
			
			<view class="detail-container">
				<view class="card">
					<view class="title">订单信息</view>
					<view class="item">
						<view class="label">销售单号</view>
						<view class="value">{{detail.orderId || ''}}</view>
					</view>
					<view class="item">
						<view class="label">购买日期</view>
						<view class="value">{{detail.payTime || ''}}</view>
					</view>
					<view class="item">
						<view class="label">产品类型</view>
						<view class="value">{{detail.mainName || ''}}</view>
					</view>
					<block v-for="(item, index) in detail.goods" :key='index'>
						<view class="item">
							<view class="label">产品机型</view>
							<view class="value">{{item.pname}}</view>
						</view>
						<view class="item">
							<view class="label">产品数量</view>
							<view class="value">{{item.number}}</view>
						</view>
					</block>
				</view>
				<view class="card">
					<view class="title">服务信息</view>
					<view class="item">
						<view class="label">工单编号</view>
						<view class="value">{{detail.workerOrder || ''}}</view>
					</view>
					<view class="item">
						<view class="label">派单日期</view>
						<view class="value">{{detail.createTime || ''}}</view>
					</view>
					<view class="item">
						<view class="label">服务内容</view>
						<view class="value">{{detail.serverContent || ''}}</view>
					</view>
					<view class="item">
						<view class="label">服务网点</view>
						<view class="value">{{detail.installName || ''}}</view>
					</view>
					<view class="item">
						<view class="label">预约时间</view>
						<view class="value">{{detail.reserveTime || ''}}</view>
					</view>
					<view class="item">
						<view class="label">主要服务人员</view>
						<view class="value">{{detail.workerName || ''}} {{detail.workerPhone || ''}} <image src="/static/mine/call.png" v-if="detail.workerPhone" @tap="callPhone(detail.workerPhone)"></image></view>
					</view>
					<view class="item">
						<view class="label">辅助服务人员</view>
						<view class="value">{{detail.assistName || ''}}</view>
					</view>
				</view>
				<view class="card">
					<view class="title">其他信息</view>
					<view class="item">
						<view class="label">支架</view>
						<view class="value">{{detail.bracket || 0}}</view>
					</view>
					<view class="item">
						<view class="label">加长管</view>
						<view class="value">{{detail.pipe || 0}}</view>
					</view>
					<view class="item">
						<view class="label">空气开关</view>
						<view class="value">{{detail.switchFlag || 0}}</view>
					</view>
					<view class="item">
						<view class="label">一次成型墙孔</view>
						<view class="value">{{detail.hole || 0}}</view>
					</view>
					<view class="item">
						<view class="label">高空作业</view>
						<view class="value">{{detail.highAltitude || 0}}</view>
					</view>
					<view class="item item2">
						<view class="label">服务备注</view>
						<view class="value">{{detail.remark || ''}}</view>
					</view>
				</view>
				<view class="card" v-if="orderState > 0">
					<view class="title">辅材信息</view>
					
					<block v-if="detail.fcxxs && detail.fcxxs.length > 0">
						<view class="item">
							<view class="label">支付单号</view>
							<view class="value">{{detail.fcxxs[0].orderNo}}</view>
						</view>
						<view class="item">
							<view class="label">支付时间</view>
							<view class="value">{{detail.fcxxs[0].payTime}}</view>
						</view>
						<view class="goods">
							
							<view class="it" v-for="(item, index) in fc_list" :key='index'>
								<view class="name">{{item.goods_name}}</view>
								<view class="num">x{{item.number}}</view>
								<view class="price">¥{{item.sub_amount}}</view>
							</view>
							
							<view class="bottom">
								<view class="left">合计：</view>
								<view class="price">¥{{fc_total}}</view>
							</view>
						</view>
					</block>
					
					<block v-else>
						<no-data :showText="'暂无辅材信息'"></no-data>
					</block>
					
				</view>
			</view>
		
		</block>
		
		
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	
	export default {
		data() {
			return {
				id: null, // id
				detail: {}, // 详情数据
				steps: [{title: '已派单'}, {title: '服务中'}, {title: '已完工'}],
				orderState: 0,
				fc_list: [],
				noData: false,
			}
		},
		
		computed:{
			...mapState(['userInfo', 'isLogin', 'userId']),
			
			fc_total() {
				if(this.fc_list && this.fc_list.length > 0) {
					let total = 0;
					this.fc_list.forEach(item => {
						total = total + Number(item.sub_amount);
					})
					return total;
				}else {
					return 0
				}
			},
		},
		
		onLoad({id}) {
			this.id = id;
			this.getOrderDetail();
		},
		
		methods: {
			stateFilter(item) {
				let orderState = Number(item.orderState);
				if(item.serverContent == '安装服务') {
					if(orderState == 6) {
						return 1;
					}else if(orderState == 7 || orderState == 10) {
						return 2;
					}else {
						return 0;
					}
				} else {
					if(orderState < 7) {
						return 0;
					}else if(orderState == 7) {
						return 1;
					}else {
						return 2;
					}
				}
			},
			
			getOrderDetail() {
				this.$axios({
					url: '/order/work/list',
					method: 'get',
					params: {
						pageNo: 1,
						pageSize: 1,
						userId: this.userId,
						workerOrderNo: this.id,
					},
				}).then(res => {
					if(res.data && res.data.records) {
						this.noData = false;
						this.detail = res.data.records[0];
						this.orderState = this.stateFilter(this.detail);
						
						if(this.detail.fcxxs && this.detail.fcxxs.length > 0) {
							let list = [];
							for(let i=0; i<this.detail.fcxxs.length; i++) {
								if(this.detail.fcxxs[i].details && this.detail.fcxxs[i].details.length > 0) {
									this.detail.fcxxs[i].details.forEach(item => {
										list.push(item);
									})
								}
							}
							this.fc_list = list;
						}else {
							this.fc_list = [];
						}
					}else {
						this.noData = true;
					}
				})
			},
			
			callPhone(val) {
				uni.makePhoneCall({
				    phoneNumber: val
				});
			},
			
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		box-sizing: border-box;
	}
	.top-container {
		background: #FFFFFF;
		padding: 30rpx 0;
	}
	.detail-container {
		padding: 20rpx;
		.card {
			background: #FFFFFF;
			border-radius: 20rpx;
			margin-bottom: 20rpx;
			padding: 30rpx 20rpx 20rpx;
			.title {
				font-size: 32rpx;
				color: #333333;
				line-height: 32rpx;
				padding-bottom: 20rpx;
				font-weight: 600;
			}
			.item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 10rpx 0;
				&.item2 {
					flex-direction: column;
					align-items: flex-start;
					.value {
						padding-top: 10rpx;
					}
				}
				.label {
					color: #666666;
					flex-shrink: 0;
				}
				.value {
					color: #333333;
					display: flex;
					image {
						width: 40rpx;
						height: 40rpx;
						margin-left: 10rpx;
					}
				}
			}
			.goods {
				background: #F4F2F2;
				border-radius: 10rpx;
				padding: 10rpx 20rpx;
				margin-top: 10rpx;
				color: #666666;
				.it {
					display: flex;
					padding: 10rpx 0;
					.name {
						flex: 1;
					}
					.num {
						margin-left: 20rpx;
					}
					.price {
						margin-left: 20rpx;
					}
				}
				.bottom {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 10rpx 0;
					.price {
						color: #FF3F42;
					}
				}
			}
		}
	}
	
</style>
