<template>
	<view class="center">
		<view class="c-row b-b">
			<text class="tit">名头</text>
			<view class="con-list">
				<input placeholder="请填写名头" name="input" v-model="deptList.procurementPlanNo"></input>
			</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">标题</text>
			<view class="con-list">
				<input placeholder="请填写标题,2-16个字" name="input" v-model="deptList.title"></input>
			</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">货名</text>
			<view class="con-list">
				<input placeholder="请填写货名" name="input" v-model="deptList.goodsName"></input>
			</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">采购量（吨）</text>
			<view class="con-list">
				<input placeholder="请填写采购重量" name="input" v-model="deptList.plannedPurchaseVolume"></input>
			</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">最小成交量（吨）</text>
			<view class="con-list">
				<input placeholder="请填写最小成交量" name="input" v-model="deptList.minimumVolume"></input>
			</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">类型</text>
			<picker @change="typeChange" :value="typesIndex" :range="typesType" class="con-list"
				>
				<view class="picker" v-model="deptList.procurementPlanType">
					{{typesIndex>-1?typesType[typesIndex]:'请选择类型'}}
				</view>
			</picker>
		</view>
		<view class="c-row b-b">
			<text class="tit">价格类型</text>
				<view class="con-list">
					<!-- <input placeholder="请填写价格类型" name="input"  v-model="deptList.priceType"></input> -->
					{{deptList.priceType}}
				</view>
		</view>
		<view class="c-row b-b" v-if="this.typesIndex != 1">
			<text class="tit">单价（元/吨）</text>
			<view class="con-list">
				<input placeholder="请填写单价（元/吨）" name="input" v-model="deptList.unitPrice"></input>
			</view>
		</view>
		<view class="c-row b-b" v-if="this.typesIndex == 1">
			<text class="tit">基差（元/吨）</text>
			<view class="con-list">
				<input placeholder="请填写单价（元/吨）" name="input" v-model="deptList.basisPrice"></input>
			</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">收货地区</text>
			<view class="con-list">
				<input placeholder="请填写收货地区" name="input" v-model="deptList.receiveArea"></input>
			</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">买方手机号</text>
			<view class="con-list">
				<input placeholder="请填写卖方手机号" name="input" v-model="deptList.buyerPhone"></input>
			</view>
		</view>
		<view class="c-row b-b">
			<button type="default">获取验证码</button>
			<view class="con-list">
				<input placeholder="请填写6位短信验证码" name="input" v-model="deptList.title1"></input>
			</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">水分（%）<= </text>
					<view class="con-list">
						<input placeholder="请填写水分占比" name="input" v-model="deptList.waterContent"></input>
					</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">容重（g/L）<= </text>
					<view class="con-list">
						<input placeholder="请填写容重" name="input" v-model="deptList.bulkDensity"></input>
					</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">热损伤（%）<= </text>
					<view class="con-list">
						<input placeholder="请填写热损伤占比" name="input" v-model="deptList.jiaorenli"></input>
					</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">杂质（%）<= </text>
					<view class="con-list">
						<input placeholder="请填写杂质占比" name="input" v-model="deptList.impurity"></input>
					</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">霉变粒（%）<= </text>
					<view class="con-list">
						<input placeholder="请填写霉变粒占比" name="input" v-model="deptList.mildewGrain"></input>
					</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">不完整粒（%）<= </text>
					<view class="con-list">
						<input placeholder="请填写不完整粒占比" name="input" v-model="deptList.imperfectGrain"></input>
					</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">蛋白（%）<= </text>
					<view class="con-list">
						<input placeholder="请填写蛋白占比" name="input" v-model="deptList.protein"></input>
					</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">粒型 </text>
			<picker @change="liChange" :value="liIndex" :range="liType" class="con-list" v-model="deptList.grain">
				<view class="con-list">
					{{liIndex>-1?liType[liIndex]:'请选择粒型'}}
				</view>
			</picker>
		</view>
		<view class="c-row b-b">
			<text class="tit">品级 </text>
			<picker @change="pinChange" :value="pinIndex" :range="pinType" class="con-list">
				<view class="con-list">
					{{pinIndex>-1?pinType[pinIndex]:'请选择品级'}}
				</view>
			</picker>
		</view>
		<view class="c-row b-b">
			<text class="tit">产出年份</text>
			<picker @change="nianChange" :value="nianIndex" :range="nianType" class="con-list">
				<view class="con-list">
					{{nianIndex>-1?nianType[nianIndex]:'请选择产出年份'}}
				</view>
			</picker>
		</view>
		<view class="c-row b-b">
			<text class="tit">包装方式</text>
			<picker @change="baoChange" :value="baoIndex" :range="baoType" class="con-list">
				<view class="con-list">
					{{baoIndex>-1?baoType[baoIndex]:'请选择包装方式'}}
				</view>
			</picker>
		</view>
		<view class="c-row b-b">
			<text class="tit">袋装备注</text>
				<view class="con-list">
					<input placeholder="请填写袋装备注" name="input" v-model="deptList.baggingNotes"></input>
				</view>
		</view>
		<view class="c-row b-b">
			<text class="tit">运费承担方</text>
			<picker @change="chengChange" :value="chengIndex" :range="chengType" class="con-list">
				<view class="con-list">
					{{chengIndex>-1?chengType[chengIndex]:'请选择运费承担方'}}
				</view>
			</picker>
		</view>

		<button @click="commit()">发布</button>
	</view>
</template>

<script>
	import {
	    mapState 
	} from 'vuex';
	export default {
		data() {
			return {
				types: '',
				typesType: ["现货", "期货"],
				typesIndex: 0,
				deptList: {
					priceType: "定价采购",
					procurementPlanType:"现货",
					grain:"塔粮",
					grade:"不限（默认）",
					outputYear:"2020",
					packingType:"不限（默认）",
					freightPayer:"",
				},
				insertProcurementPlanInfo: {},
				liIndex: 0,
				liType: ['塔粮', '筛粮', '中粒', '大粒'],
				pinIndex: 0,
				pinType: ['不限（默认）', '一等品', '二等品', '三等品', '等外'],
				nianIndex: 3,
				nianType: ['2023', '2022', '2021', '2020', '2019', '2018', '2017', '2016', '2015', '2014', '2013', '2012',
					'2011'
				],
				baoIndex: 0,
				baoType: ['不限（默认）', '散装', '大袋', '小袋'],
				chengIndex: -1,
				chengType: ['可议', '买方承担', '卖方承担']
			}
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		methods: {
			typeChange(e) {
				this.typesIndex = e.detail.value
				this.types = this.typesType[this.typesIndex];
				this.deptList.procurementPlanType = this.typesType[this.typesIndex];
				if (this.typesIndex == 0) {
					this.deptList.priceType = '定价采购'
				} else {
					this.deptList.priceType = '期货盘面价+基差'
				}

			},
			commit() {
				this.insertProcurementPlanInfo = this.deptList
				// if (!this.deptList.goodsName) {
				// 	this.$api.msg('货名1不能为空')
				// 	return
				// }
			// 	if (!this.deptList.title) {
			// 		this.$api.msg('标题不能为空')
			// 		return
			// 	}
				if (!this.deptList.goodsName) {
					this.$api.msg('货名不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.plannedPurchaseVolume) {
					this.$api.msg('采购量不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.minimumVolume) {
					this.$api.msg('最小成交量不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.procurementPlanType) {
					this.$api.msg('类型不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.priceType) {
					this.$api.msg('价格类型不能为空')
					return
				}
				if(this.insertProcurementPlanInfo.procurementPlanType == '现货'){
					if (!this.insertProcurementPlanInfo.unitPrice) {
						this.$api.msg('单价不能为空')
						return
					}
				}else if(this.insertProcurementPlanInfo.procurementPlanType == '期货'){
					if (!this.insertProcurementPlanInfo.basisPrice) {
						this.$api.msg('基差（元/吨）不能为空')
						return
					}
				}
			
				if (!this.insertProcurementPlanInfo.buyerPhone) {
					this.$api.msg('买方手机号不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.waterContent) {
					this.$api.msg('水分不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.bulkDensity) {
					this.$api.msg('容重不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.jiaorenli) {
					this.$api.msg('热损伤不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.impurity) {
					this.$api.msg('杂质不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.mildewGrain) {
					this.$api.msg('霉变粒不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.imperfectGrain) {
					this.$api.msg('不完整粒不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.bulkDensity) {
					this.$api.msg('蛋白不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.grain) {
					this.$api.msg('粒型不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.grade) {
					this.$api.msg('品级不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.outputYear) {
					this.$api.msg('产出年份不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.packingType) {
					this.$api.msg('包装方式不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.baggingNotes) {
					this.$api.msg('装袋备注不能为空')
					return
				}
				if (!this.insertProcurementPlanInfo.freightPayer) {
					this.$api.msg('运费承担方不能为空')
					return
				}
		         this.insertProcurementPlanInfo.commonId = this.userInfo.id
				this.$api.doRequest('post', '/procurementPlanInfo/api/insertProcurementPlanInfo', this
						.insertProcurementPlanInfo).then(res => {
						console.log("添加成功")
						this.procurementPlanInfos = res.data.data
					})
					.catch(res => {
						uni.showToast({
							title: res.errmsg,
							icon: 'none',
							duration: 2000
						})
					});
			},
			liChange(e) {
				this.liIndex = e.detail.value
				this.litypes = this.liType[this.liIndex];
				this.deptList.grain = this.liType[this.liIndex]
			},
			pinChange(e) {
				this.pinIndex = e.detail.value
				this.pintypes = this.pinType[this.pinIndex];
				this.deptList.grade = this.pinType[this.pinIndex]
			},
			nianChange(e) {
				this.nianIndex = e.detail.value
				this.niantypes = this.nianType[this.nianIndex];
				this.deptList.outputYear = this.baoType[this.baoIndex]
			},

			baoChange(e) {
				this.baoIndex = e.detail.value
				this.baotypes = this.baoType[this.baoIndex];
				this.deptList.packingType = this.chengType[this.chengIndex];
			},
			chengChange(e) {
				this.chengIndex = e.detail.value
				this.chengtypes = this.chengType[this.chengIndex];
					this.deptList.freightPayer = this.chengType[this.chengIndex];
			}
		}
	}
</script>

<style>
	.center {
		padding: 10px 20px;
	}

	.c-row {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		padding: 20rpx 30rpx;
		position: relative;
	}

	.con-list {
		-webkit-box-flex: 1;
		-webkit-flex: 1;
		flex: 1;
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-orient: vertical;
		-webkit-box-direction: normal;
		-webkit-flex-direction: column;
		flex-direction: column;
		color: #303133;
		line-height: 40rpx;
		text-align: right;
		padding-right: 20rpx;
		font-size: 14px;
	}
</style>
