<template>
	<div class="center">
		<el-row>
			<el-col :span="12">
				<h2 class="bg-left titleup">{{ information }}检斤信息</h2>
			</el-col>
			<el-col :span="12" class="bg-right">
				<el-button class="bg-bottom" type="primary" size="small" @click="cancel"><img width="6" height="10"
						style="vertical-align: bottom; margin-right: 3px" src="../../../public/img/lujing.png"
						alt="" />返回</el-button>
			</el-col>
		</el-row>
		<div class="substance">
			<ws-form>
				<div style="width: 50%; margin: 0 auto">
					<div class="title">基本信息</div>
					<ws-info-table>
						<ws-form-item label="合同编号" span="1">
							<ws-select v-model="weighingList.contractNo" @change="contractChange" maxlength="100"
								:disabled="disabled1" size="small">
								<ws-option v-for="item in contractNoList" :key="item.contractNo"
									:label="item.contractNo" :value="item.contractNo" />
							</ws-select>
						</ws-form-item>
						<ws-form-item label="仓位号" span="1" prop="binNumber">
							<ws-select v-model="weighingList.binNumber" class="typeselect" :disabled="disabled">
								<ws-option v-for="item in getbinNumberList" :key="item.value" :label="item.value"
									:value="item.value" />
							</ws-select>
						</ws-form-item>
						<ws-form-item label="货名" span="1" prop="goodsName">
							<ws-input v-model="weighingList.goodsName" placeholder="请输入货名" maxlength="100" disabled
								size="small" />
						</ws-form-item>
						<ws-form-item label="囤位号" span="1" prop="storageNumber">
							<ws-input v-model="weighingList.storageNumber" placeholder="请输入囤位号" maxlength="100"
								size="small" :disabled="disabled" />
						</ws-form-item>
						<ws-form-item label="车牌号" span="1" prop="carNumber">
							<ws-input v-model="weighingList.carNumber" placeholder="请输入车牌号" maxlength="100" size="small"
								:disabled="disabled" />
						</ws-form-item>
						<ws-form-item label="出库类型" span="1" prop="inOutType">
							<ws-select v-model="weighingList.inOutType" :disabled="disabled">
								<ws-option v-for="item in inOutType" :key="item.value" :label="item.value"
									:value="item.value" />
							</ws-select>
						</ws-form-item>
						<ws-form-item label="毛重（公斤）" span="1" prop="grossWeight" v-if="this.types == 1">
							<ws-input v-model="weighingList.grossWeight" @input="calculation" placeholder="未获取到毛重"
								maxlength="100" size="small" :disabled="disabled2" />
						</ws-form-item>
						<el-button type="primary" v-if="this.types == 1" @click="chongxin">重新获取</el-button>

						<ws-form-item label="皮重（公斤）" span="1" prop="tare">
							<ws-input v-model="weighingList.tare" placeholder="未获取到皮重" maxlength="100" size="small"
								disabled />
						</ws-form-item>
						<el-button type="primary" v-if="this.types == 2 || this.types == null">重新获取</el-button>
						<!-- <div class="formItem"> -->
						<ws-form-item label="净重（公斤）" span="1" prop="netWeight" v-if="this.types == 1">
							<ws-input v-model="weighingList.netWeight" placeholder="请输入净重" maxlength="100" size="small"
								disabled />
						</ws-form-item>
						<!-- </div> -->
						<div class="bangdan">
							<el-form-item label="上传磅单" v-if="this.types == 1">
							</el-form-item>
							<el-upload action="https://www.zthymaoyi.com/upload/admin" list-type="picture-card"
								:on-success="(res, file) => {handlePictureCardPreview1(res)}"
								:on-remove="(value) => {handleRemove(value)}" class="photo2" v-if="this.types == 1">
								<i class="el-icon-plus"></i>
							</el-upload>
							<el-dialog :visible.sync="dialogVisible" v-if="this.types == 1">
								<img width="100%" :src="weighingList.addressUrl" alt="" />
							</el-dialog>
						</div>
					</ws-info-table>
					<div class="but">
						<el-button @click="cancel" type="primary">取消</el-button>
						<el-button @click="print" type="primary">保存</el-button>
					</div>
				</div>
			</ws-form>
		</div>
	</div>
</template>
<script>
	import {
		grossWeight,
		getbinNumber,
		contractList,
		tareAdd,
		tareEdit,
		getweighing
	} from '@/model/outboundManagement/index'
	export default {
		data() {
			return {
				inspect: {},
				types: 3,
				information: '添加皮重',
				weighingList: {
					carNumber: '',
					storageNumber: '',
					buckleMiscellaneous: 0,
					compId: sessionStorage.getItem('ws-pf_compId'),
				},
				disabled: false,
				disabled1: false,
				disabled2: false,
				dialogVisible: false,
				getbinNumberList: [],
				warehouseName: '',
				contractNoList: [],
				inOutType: [{
						value: '销售出库',
						type: 1
					},
					{
						value: '移库出库',
						type: 3
					},
					{
						value: '暂存出库',
						type: 5
					},
					{
						value: '贸易服务出库',
						type: 7
					},
				],
				relationId: '',
				warehouseId: '', //仓库id
			}
		},
		activated() {
			this.types = this.$route.query.typeNo
			this.weighingList.number = this.$route.query.number
			this.weighingList.binNumber = this.$route.query.binNumber
			this.weighingList.storageNumber = this.$route.query.storageNumber
			this.weighingList.carNumber = this.$route.query.carNumber
			this.weighingList.goodsName = this.$route.query.goodsName
			this.weighingList.inOutType = this.$route.query.inOutType
			this.warehouseName = this.$route.query.warehouseName
			let id = this.$route.query.id
			this.relationId = this.$route.query.relationId
			this.warehouseId = this.$route.query.warehouseId
			if (this.types == 1) {
				this.information = '毛重'
				this.disabled = true
				this.disabled1 = true
			} else if (this.types == 2) {
				this.information = '编辑皮重'
				this.disabled1 = true
				this.disabled2 = true
			}
			this.getList(id)
		},
		methods: {
			chongxin() {
				if (this.types == 2 || this.types == 3) {
					return
				}
			},
			handlePictureCardPreview1(file) {
				this.weighingList.addressUrl += file.url + ','
				this.addressUrls.push(file.url)
			},
			cancel() {
				this.weighingList = {}
				this.$router.push({
					path: 'weighingManagement'
				})
			},
			calculation() {
				if (this.weighingList.grossWeight && this.weighingList.tare) {
					let count = 0
					this.weighingList.netWeight =
						this.weighingList.grossWeight -
						this.weighingList.tare
				}
			},
			contractChange(e) {
				for (let i = 0; i < this.contractNoList.length; i++) {
					if (e == this.contractNoList[i].contractNo) {
						this.weighingList.goodsName = this.contractNoList[i].goodsName
						this.weighingList.contractPrice = this.contractNoList[i].contractPrice
					}
				}
			},
			getList() {
				if (this.types == 2 || this.types == 1) {
					getweighing({
							id: this.$route.query.id
						})
						.toPromise()
						.then((response) => {
							this.weighingList = response
						})
				}
				// 仓位
				getbinNumber({
						id: this.warehouseId
					})
					.toPromise()
					.then((response) => {
						this.getbinNumberList = []
						for (let i = 0; i < response.warehousePositionInfoList.length; i++) {
							this.getbinNumberList.push({
								value: response.warehousePositionInfoList[i].binNumber,
							})
						}
					})
				//合同编号
				contractList({
						compId: sessionStorage.getItem('ws-pf_compId'),
						flag: 5
					})
					.toPromise()
					.then((response) => {
						this.contractNoList = response
					})
			},
			print() {
				if (this.types == null || this.types == 2) {
					if (!this.weighingList.contractNo) {
						this.$message({
							message: '合同编号不能为空',
							type: 'warning',
						})
						return
					}
					if (!this.weighingList.binNumber) {
						this.$message({
							message: '仓位号不能为空',
							type: 'warning',
						})
						return
					}
					if (!this.weighingList.storageNumber) {
						this.$message({
							message: '囤位号不能为空',
							type: 'warning',
						})
						return
					}
					if (
						this.weighingList.storageNumber.length < 1 ||
						this.weighingList.storageNumber.length > 10
					) {
						this.$message({
							message: '囤位号不超过10个字符！',
							type: 'warning',
						})
						return
					}
					if (!this.weighingList.carNumber) {
						this.$message({
							message: '车牌号不能为空',
							type: 'warning',
						})
						return
					}
					if (
						this.weighingList.carNumber.length != 7
					) {
						this.$message({
							message: '车牌号输入错误',
							type: 'warning',
						})
						return
					}
					if (!this.weighingList.inOutType) {
						this.$message({
							message: '出库类型不能为空',
							type: 'warning',
						})
						return
					}
					// if (!this.weighingList.tare) {
					// 	this.$message({
					// 		message: '皮重不能为空',
					// 		type: 'warning',
					// 	})
					// 	return
					// }
					// if (
					// 	isNaN(this.weighingList.tare) ||
					// 	(String(this.weighingList.tare).indexOf('.') != -1 &&
					// 		String(this.weighingList.tare).length -
					// 		(String(this.weighingList.tare).indexOf('.') + 1) >
					// 		1) ||
					// 	this.weighingList.tare < 1 ||
					// 	this.weighingList.tare > 100000
					// ) {
					// 	this.$message({
					// 		message: '皮重输入错误！',
					// 		type: 'warning',
					// 	})
					// 	return
					// }
				}
				if (this.types == 1) {
					if (!this.weighingList.grossWeight) {
						this.$message({
							message: '毛重不能为空',
							type: 'warning',
						})
						return
					}
					if (
						isNaN(this.weighingList.grossWeight) ||
						(String(this.weighingList.grossWeight).indexOf('.') != -1 &&
							String(this.weighingList.grossWeight).length -
							(String(this.weighingList.grossWeight).indexOf('.') + 1) >
							1) ||
						this.weighingList.grossWeight < 1 ||
						this.weighingList.grossWeight > 100000
					) {
						this.$message({
							message: '毛重输入错误！',
							type: 'warning',
						})
						return
					}
				}
				this.weighingList.warehouseName = this.warehouseName
				this.weighingList.secretaryWeigher = sessionStorage.getItem('ws-pf_staffName')
				this.$confirm('确定保存检斤信息？', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
					})
					.then(() => {
						if (this.types == 1) {
							this.weighingList.relationId = this.relationId
							grossWeight(this.weighingList)
								.toPromise()
								.then((response) => {
									this.$notify.success({
										title: '成功',
										message: '提交成功',
									})
									this.$router.push({
										path: 'weighingManagement'
									})
								})
								.catch((response) => {})
						} else if (this.types == 2) {
							this.weighingList.warehouseId = this.warehouseId
							tareEdit(this.weighingList)
								.toPromise()
								.then((response) => {
									this.$notify.success({
										title: '成功',
										message: '提交成功',
									})
									this.$router.push({
										path: 'weighingManagement'
									})
								})
								.catch((response) => {})
						} else if (this.types == null) {
							this.weighingList.warehouseId = this.warehouseId
							tareAdd(this.weighingList)
								.toPromise()
								.then((response) => {
									this.$notify.success({
										title: '成功',
										message: '提交成功',
									})
									this.$router.push({
										path: 'weighingManagement'
									})
								})
								.catch((response) => {})
						}
					})
					.catch(() => {
						return false
					})
			},
		},
	}
</script>
<style lang="scss" scoped>
	.center {
		background: #f6f7fc;
	}

	.ws-info-table {
		border: none;
	}

	.ws-info-table .el-form-item {
		width: 50%;
		border: none;
	}

	.formItem {
		width: 100%;
		display: inline;
	}

	/deep/.ws-info-table .el-form-item .el-form-item__label {
		width: 30%;
		text-align: center;
		background: #ffffff;
		font-size: 14px;
		color: #8890b1;
	}

	/deep/.ws-info-table .el-form-item .el-form-item__content {
		border: none;
	}

	.title {
		font-size: 16px;
		font-weight: 600;
		margin: 10px;
	}

	.titleup {
		position: relative;
	}

	.title::before {
		content: '';
		display: inline-block;
		width: 5px;
		height: 15px;
		background: #8890b1;
		margin-right: 5px;
	}

	.substance {
		background: #ffffff;
		width: 100%;
		border-radius: 4px;
		margin: 0 auto;
		padding-top: 20px;
	}

	/deep/.el-table .el-table__header .cell,
	.el-table .el-table__body .cell {
		text-align: center;
	}

	/deep/.el-table--enable-row-transition .el-table__body td {
		text-align: center;
	}

	.bg-left {
		padding-left: 30px;
	}

	.bg-right {
		padding-right: 10px;
		text-align: right;
	}

	.bg-bottom {
		margin: 15px 0px;
	}

	.titleup {
		position: relative;
	}

	/deep/ .substance .bangdan {
		width: 100%;
		padding-top: 6%;
		padding-bottom: 6%;
	}

	// // .photo2{
	// //   margin-top: 10px;
	// // }
	.el-upload--picture-card {
		background-color: #fbfdff;
		border: 1px dashed #c0ccda;
		border-radius: 6px;
		box-sizing: border-box;
		width: 148px;
		height: 148px;
		line-height: 146px;
		vertical-align: top;
		margin-left: -289px;
	}

	.titleup::before {
		content: '';
		display: inline-block;
		width: 5px;
		height: 30px;
		background: #5473e8;
		position: absolute;
		left: 0;
	}

	.but {
		text-align: center;
		margin: 20px auto;
	}

	.butCss {
		opacity: 0;
	}
</style>
