<template>
	<view>
		<view class="head" v-if="$isRight(order)">
			<view class="lf-row-between lf-color-gray list">
				<view>采购单 {{ order.p_sn }}</view>
			</view>
			<view class="lf-row-between list">
				<image :src="order.supplier.logo" class="image"></image>
				<view class="info">
					<view class="lf-font-32 lf-color-black lf-font-bold">{{ order.supplier.supplier_name }}</view>
					<view class="lf-font-24 lf-color-555">{{ order.supplier.address }}</view>
				</view>
			</view>
		</view>
		<self-line></self-line>
		<view class="lf-p-l-32 lf-p-r-32 lf-border-box lf-bg-white">
			<!-- <lf-ysteps :stepList="stepList" color="#11D189"></lf-ysteps> -->
			<lf-stepbar :list="order.state_log" v-if="order.state_log"></lf-stepbar>
		</view>
		<self-line></self-line>
		<view class="head">
			<view class="lf-row-between list">
				<view>车辆</view>
				<view class="lf-font-bold"></view>
			</view>
			<view class="lf-row-between list">
				<view>司机</view>
				<view class="lf-font-bold"></view>
			</view>
			<view class="lf-row-between list">
				<view>联系电话</view>
				<view class="lf-font-bold"></view>
			</view>
			<view class="list">
				<view>证明材料</view>
				<view class="lf-flex-wrap lf-m-t-10">
					<image :src="item.voucher_pic" @click="lookImage(index)" class="ms-img" v-for="(item, index) in order.voucher" :key="item.id"></image>
				</view>
			</view>
		</view>
		<self-line></self-line>
		<view class="lf-m-t-30 lf-m-l-32">
			<view class="lf-font-32 lf-color-black lf-font-bold lf-m-b-20">物资明细</view>
			<wyb-table :first-line-fixed="true" contentBgColor="#ecfaf5" :headers="headers" :contents="contents" width="max-content" height="350rpx"></wyb-table>
		</view>
		<view style="height: 100rpx;"></view>
		<!-- 操作按钮 -->
		<view class="fixed-bottom" v-if="type != 3">
			<view v-if="type == 1" class="lf-row-flex-end">
				<button class="btn btn1">编辑</button>
				<button class="btn btn2">立即发单</button>
			</view>
			<view v-else-if="type == 2" class="lf-row-flex-end">
				<button class="btn btn2" style="background-color: #FF0000;">撤销订单</button>
			</view>
			<view v-else-if="type == 4" class="lf-row-flex-end">
				<button class="btn btn1">退单</button>
				<button class="btn btn1 lf-m-l-20">上传凭证</button>
				<button class="btn btn2" @click="$url('/pages/purchase/receipt?p_sn='+ order.p_sn)">确认收货</button>
			</view>
			<view v-else-if="type == 5" class="lf-row-between">
				<button class="btn btn1">复用采购单</button>
				<view class="lf-font-32" style="color: #11D189;">已完成</view>
			</view>
		</view>
	</view>
</template>

<script>
	import lfYsteps from '@/components/lf-ysteps/lf-ysteps';
	import wybTable from '@/components/wyb-table/wyb-table';
	export default {
		components: { lfYsteps, wybTable },
		data(){
			return {
				headers: [{
						key: 'name',
						label: '菜名'
					},{
						key: 'spec',
						label: '规格'
					},{
						key: 'pre_tax_price',
						label: '税前价格'
					},{
						key: 'after_tax_price',
						label: '税后价格'
					}],
				contents: [],
				order: {},
				p_sn: '',
				type: 4
			}
		},
		onLoad(options){
			// TODO 1、凭证上传流程变动：当采购单状态为已发货状态时，食堂可以上传凭证图片；
			// TODO 2、食堂收货时，实到数、实收数默认填充采购数量；
			this.p_sn = options.p_sn || '';
			// TODO 显示车辆等信息，显示table信息
			this.getData();
		},
		methods: {
			getData(){
				this.$http(this.API.API_CANTEEN_PURCHASEDETAIL, {
					p_sn: this.p_sn
					// p_sn: '802316269455228606'
				}).then(res => {
					console.log("xxx",res)
					this.order = res.data.order;
					let list = res.data.order.sheet || [];
					let contents = list.map(item => {
						return {
							name: item.material.m_name,
							spec: item.spec.name,
							pre_tax_price: {edit: true, value: item.tax_standard_price},
							after_tax_price: {edit: true, value: item.non_tax_standard_price},
							spec_id: item.spec.id,
							material_id: item.material.id
						}
					})
					this.contents = contents;
				})
			},
			// 预览图片
			lookImage(index){
				this.$u.throttle(() => {
					let images = this.order.voucher.map(item => item.voucher_pic);
					uni.previewImage({
						urls: images,
						current: index
					})
				}, 200);
			},
			// 改变订单状态 TODO 
			orderStateChange(state){
				this.$http(this.API.API_CANTEEN_PURCHASEUPDATE, {
					p_sn: this.p_sn,
					state: state
				}).then(res => {
					console.log("orderStateChange", res);
					this.$msg('操作成功');
				})
			}
		}
	}
</script>

<style lang="scss" scoped="scoped">
	.head{
		padding: 0 32rpx;
		width: 750rpx;
		box-sizing: border-box;
		height: auto;
		.list{
			padding: 30rpx 0;
			border-bottom: 1rpx solid #e5e5e5;
			font-size: 28rpx;
			color: #555555;
			&:last-child{
				border-bottom: none;
			}
			.image{
				width: 140rpx;
				height: 140rpx;
				border-radius: 10rpx;
				background-color: #EEEEEE;
			}
			.info{
				display: flex;
				flex-direction: column;
				justify-content: space-around;
				width: 530rpx;
				height: 140rpx;
			}
		}
	}
	.fixed-bottom{
		position: fixed;
		bottom: 0rpx;
		left: 0rpx;
		z-index: 99;
		width: 750rpx;
		height: 98rpx;
		display: flex;
		// justify-content: flex-end;
		align-items: center;
		border-top: 1rpx solid #E5E5E5;
		background-color: #FFFFFF;
		box-sizing: border-box;
		padding: 0 32rpx;
		.btn{
			width: 212rpx;
			height: 82rpx;
			border-radius: 41rpx;
			margin: 0;
			padding: 0;
			font-size: 32rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		.btn1{
			border: 2rpx solid #555555;
			opacity: .5;
		}
		.btn2{
			background: #11D189;
			color: #FFFFFF;
			margin-left: 20rpx;
		}
	}
	.fixed-bottom>view{
		width: 100%;
	}
	.ms-img{
		width: 160rpx;
		height: 160rpx;
		margin-right: 15rpx;
		margin-top: 15rpx;
		background-color: #EEEEEE;
		&:nth-of-type(4n){
			margin-right: 0rpx;
		}
	}
</style>
