<style lang="scss">
	.container{
		min-height:100%;
		background:#F6F6F6;
		padding:30rpx 0;
		.card{
			margin-top:0;
			&.info{
				view{
					display: flex;
					justify-content: space-between;
					height:80rpx;
					align-items: center;
					&+view{
						border-top:2rpx solid rgba(216, 216, 216, 0.3);
					}
				}
			}
			&.detail{
				.item{
					border-top:2rpx solid rgba(216, 216, 216, 0.6);
					padding:18rpx 0;
					&>.info{
						display: flex;
						margin-bottom:14rpx;
						text{
							&:first-child{
								color:#fff;
								font-size: 20rpx;
								width:80rpx;
								height:40rpx;
								line-height: 40rpx;
								text-align: center;
								background:#10C176;
								border-radius: 0px 20rpx 0px 20rpx;
								margin-right:16rpx;
							}
							&:nth-child(2){
								// line-height: 40rpx;
								&:after{
									content:"("attr(data-unit)")";
									color:#999;
									margin-left:7rpx;
									font-size:20rpx;
								}
							}
							&:nth-child(3){
								margin-left:auto;
								color:#999;
								font-size:24rpx;
								display: flex;
    							align-items: center;
								&:before{
									color:#10C176;
									font-size: 30rpx;
								}
							}
						}
					}
					&>.company{
						display: flex;
						text{
							color:var(--c);
							font-size:20rpx;
							
						}
					}
				}
				&>.plant-xinzeng{
					border-top:2rpx solid rgba(216, 216, 216, 0.6);
					text-align: center;
					color:#10C176;
					padding-top:20rpx;
					&:before{
						margin-right:12rpx;
					}
				}
			}
			&.type{
				.title_mast{
					margin-bottom:0;
					&:after{
						content:attr(data-type);
						color:#FBA83C;
						margin-left:auto;
					}
				}
			}
		}
	}
</style>
<template>
	<view class="container">
		<view class="card info">
			<view>
				<text>申请人</text> <text>{{formData.applicantName}}</text>
			</view>
			<view>
				<text>所在基地</text> <text>{{formData.baseName}}</text>
			</view>
			<view>
				<text>申请时间</text> <text>{{formData.applicantTime}}</text>
			</view>
		</view>

		<view class="card detail">
			<view class="title_mast">采购详情</view>
			
			<view class="item" v-for="(v,k) in formData.plantPurchaseSubList" :key="k">
				<view class="info">
					<text>{{v.detailedParentName}}</text>
					<text :data-unit="v.goodsSpecification">{{v.goodsName}}</text>
					<text class="plant-shanchu" @click="formData.plantPurchaseSubList.splice(k,1)">删除</text>
				</view>
				<view class="company">
					<text style="--c:#FBA83C;flex-shrink: 0;">{{v.detailedType}}</text>
					<text style="--c:#707070;flex-grow:1;text-align:center;">{{v.supplier}}</text>
				</view>
			</view>
			<u-empty v-if="formData.plantPurchaseSubList.length==0" width="70%" text=' ' icon="/static/noData.png"/>

			<view class="plant-xinzeng" @click="toAddGoods">添加物品</view>
		</view>

		<!-- <view class="card type">
			<view class="title_mast" data-type="审核中">申请状态</view>
		</view> -->

		<button class="cu-btn round bg-green shadow submit" @click="submit">确定提交</button>
	</view>
</template>
<script>
import request from '@/common/request'
export default {
	data(){
		return{
			formData:{
				applicantId: "",
				applicantName: "",
				applicantTime: "",
				baseName: "",
				companyId: "",
				plantPurchaseSubList: [],
				// sysOrgCode: "",
			},
		}
	},
	onLoad(){
		this.init()
	},
	methods:{
		async init(){
			var res=await request("/api/purchase/purchaseUserShow",{
					params:{ plantBaseInfoId:this.$store.state.baseInfo.id }
				})
			if(res.statu){
				this.formData.applicantId=this.$store.getters['userInfo'].id
				this.formData.applicantName=this.$store.getters['userInfo'].realname
				this.formData.applicantTime=new Date().format('yyyy-MM-dd')
				this.formData.baseName=res.data.baseName
				this.formData.companyId=this.$store.getters['userInfo'].companyId
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取采购申请信息失败!",
					showCancel:false,
				})
			}
		},
		async submit(){
			if(this.formData.plantPurchaseSubList.length<=0){
				uni.showToast({title:"请选择采购详情!",icon:"none"})
				return false
			}
			var res=await request("/api/purchase/add",{
				method:"post",
				body: this.formData
			})
			if(res.statu){
				uni.reLaunch({url:"/pages/home/my?toPage=record"})
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"保存失败!",
					showCancel:false,
				})
			}
		},
		toAddGoods(){
			uni.navigateTo({
				url:`/pages/purchase/agrProdList`,
				events:{submit: e=>{
					if(!this.formData.plantPurchaseSubList.some(v=>{
						if(v.goodsName==e.goodsName && v.goodsSpecification==e.goodsSpecification){
							v.num=parseFloat(e.num)+parseFloat(v.num)
							return true
						}
						return false
					})){
						this.formData.plantPurchaseSubList.push(e)
					}
				}}
			})
		},
	}
}
</script>