<template>
	<!-- 添加运单 -->
	<view>
		<u-navbar :title="headline" autoBack placeholder></u-navbar>
		<x-form ref="recipients" :list="list" :model="recipientsModel" :rules="rules"></x-form>
		<view style="width: 100%;height: 150rpx;"></view>
		<view class="btn_print" @click="printWaybill">
			<u-button type="primary" :text="headline"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		iceRaftRulesil
	} from "./waybill.js";
	export default {
		name: 'addIceRaft',
		data() {
			return {
				recipientsModel: {
					id: null,
					code: '',
					label: '',
					status: '',
					freezeClaim: '',
					suitableForCold: null,
					iceColdAddress: '',
				},
				rules: {
					code: {
						required: true,
						message: '请输入编号',
						trigger: ['blur', 'change']
					},
					label: {
						required: true,
						message: '请输入冰排备注',
						trigger: ['blur', 'change']
					},
					status: {
						required: true,
						message: '请选择状态',
						trigger: ['blur', 'change']
					},
					freezeClaim: {
						required: true,
						message: '请输入冷冻要求',
						trigger: ['blur', 'change']
					},
					iceColdAddress: {
						required: true,
						message: '请输入释冷地点',
						trigger: ['blur', 'change']
					},
					suitableForCold: {
						required: true,
						message: '请输入释冷温度',
						trigger: ['blur', 'change']
					},
				},
				list: [],
				isIceInfo: {},
				headline: '添加冰排',
				type: 1,
			}
		},
		onShow() {
			var userInfo = this.$cache.getCache('userInfo')
			this.isIceInfo = userInfo.dept
			console.log(this.isIceInfo.isIceReleaseCold,25)
			if (this.isIceInfo.isIceReleaseCold) {
				this.list = iceRaftRulesil()
			} else {
				var arrIce = iceRaftRulesil()
				this.list = removePointById(arrIce, ['suitableForCold', 'iceColdAddress'])
			}

			function removePointById(arr1, arr2) {
				for (let i = 0; i < arr2.length; i++) {
					for (let j = 0; j < arr1.length; j++) {
						if (arr2[i] == arr1[j].field) {
							let indexs = arr1.indexOf(arr1[j]);
							arr1.splice(indexs, 1);
						}
					}
				}
				return arr1
			}
		},
		onLoad(option) {
			if (option.list) {
				let arrValue = JSON.parse(option.list)
				if (arrValue.id) {
					this.$nextTick(() => {
						this.headline = '编辑冰排'
						this.recipientsModel = arrValue
						this.recipientsModel.freezeClaim = String(arrValue.freezeClaim)
						this.recipientsModel.suitableForCold = String(arrValue.suitableForCold)
						if (arrValue.status == '2') {
							this.recipientsModel.status = '正常'
						} else {
							this.recipientsModel.status = '停用'
						}
					})
				} else {
					this.headline = '添加冰排'
				}
			}
		},
		mounted() {},
		methods: {
			// tianjia运单
			async printWaybill() {
				let flag1 = await this.$refs['recipients'].validateForm();
				if (flag1) {
					let params = {
						codeList: [],
						label: this.recipientsModel.label,
						freezeClaim: Number(this.recipientsModel.freezeClaim),
						iceColdAddress: this.recipientsModel.iceColdAddress,
						suitableForCold: Number(this.recipientsModel.suitableForCold),
						status: null,
					}
					if (this.recipientsModel.status == '正常') {
						params.status = '2'
					} else {
						params.status = '1'
					}
					params.codeList.push(this.recipientsModel.code)
					if (this.recipientsModel.id) {
						params.id = this.recipientsModel.id
						params.code = this.recipientsModel.code
						delete params.codeList
						this.$api.put('/api/ice-raft', params).then(res => {
							if (res.code == 200) {
								uni.navigateBack({
									delta: 1
								});
							}
						})
					} else {
						this.$api.post('/api/ice-raft', params).then(res => {
							if (res.code == 200) {
								uni.navigateBack({
									delta: 1
								});
							}
						})
					}
				} else {
					uni.$u.toast('请先完善表单')
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff !important;
	}

	.title_waybill {
		font-size: 32rpx;
		padding: 20rpx;
	}

	.title_waybill1 {
		position: relative;
		margin: 20rpx;
		border-bottom: 1rpx;
		padding-bottom: 20rpx;
	}

	.card_Address {
		position: relative;
	}

	.card_flex_il {
		display: flex;
		align-items: center;
		padding: 20rpx;
	}

	.title_sou {
		font-size: 32rpx;
	}

	.card_Address:before {
		content: " ";
		position: absolute;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 1px;
		border-top: 1px solid #e7e6e4;
		-webkit-transform-origin: 0 0;
		transform-origin: 0 0;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
	}

	.card_courier {
		min-height: 600rpx;
		padding-bottom: 20rpx;
	}

	.scroll-view {
		max-height: 60vh;
		overflow: hidden;
	}

	.headline {
		padding: 30rpx 30rpx 0rpx 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		padding-bottom: 30rpx;
		font-size: 32rpx;
		font-weight: 600;
	}

	.search_card {
		padding: 0rpx 30rpx 10rpx 30rpx;
	}

	.courier_item {
		margin: 20rpx 20rpx;
		width: calc(100% - 80rpx);
		padding: 20rpx;
		border-radius: 20rpx;
		// box-shadow: 0 2rpx 10rpx 0 rgba(0, 0, 0, .1);
		border: 1rpx solid #e7e6e4;
	}

	.courier_name {
		display: flex;
		flex-direction: column;
	}

	.title_name {
		font-weight: 600;
		font-size: 32rpx;
		margin-bottom: 10rpx;
	}

	.title_phone {
		font-size: 28rpx;
	}

	.xd_title {
		width: auto;
		font-size: 28rpx;
		font-weight: 500;
		padding: 15rpx 30rpx;
		border-radius: 35rpx;
		// border: 3rpx solid #82848a;
		background-color: #E4E7ED;
	}
</style>