<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" :loading="printLoading" :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,
					forColdTime: null,
					iceColdAddress: '',
					sort: null,
				},
				rules: {
					code: [{
						required: true,
						message: '请输入编号',
						trigger: ['blur', 'change'],
					}, 
					// {
					// 	required: true,
					// 	validator: (rule, value, callback) => {
					// 		if (!value) {
					// 			return callback(new Error('请输入数值'));
					// 		}
					// 		if (!/^(\d+)?(\.\d+)?$/.test(value)) {
					// 			return callback(new Error('请输入有效的数值'));
					// 		}
					// 		// 如果需要校验整数，可以使用下面的正则表达式
					// 		// if (!/^\d+$/.test(value)) {
					// 		//   return callback(new Error('请输入整数'));
					// 		// }
					// 		return callback();
					// 	},
					// 	trigger: 'blur'
					// },
					],
					label: {
						required: false,
						message: '请输入冰排备注',
						trigger: ['blur', 'change']
					},
					status: {
						required: true,
						message: '请选择状态',
						trigger: ['blur', 'change']
					},
					freezeClaim: {
						type: 'array',
						required: true,
						message: '请输入冷冻要求',
						trigger: ['blur', 'change']
					},
					suitableForCold: [{
						required: false,
						message: '请输入释冷温度',
						trigger: ['blur', 'change']
					}],
					forColdTime: [{
						required: false,
						message: '请输入释冷要求',
						trigger: ['blur', 'change']
					}, {
						required: false,
						validator: (rule, value, callback) => {
							if (!value && rule.required) {
								return callback(new Error('请输入释冷要求'));
							}
							if (value) {
								if (!/^(\d+)?(\.\d+)?$/.test(value)) {
									return callback(new Error('请输入有效的数值'));
								}
							}
							return callback();
						},
						trigger: ['blur', 'change']
					}],
					iceColdAddress: {
						required: false,
						message: '请输入释冷地点',
						trigger: ['blur', 'change']
					},
					sort: {
						required: false,
						validator: (rule, value, callback) => {
							if (!value && rule.required) {
								return callback(new Error('请输入排序编号'));
							}
							if (value) {
								if (!/^(\d+)?(\.\d+)?$/.test(value)) {
									return callback(new Error('请输入有效的数值'));
								}
							}
							return callback();
						},
						trigger: ['blur', 'change']
					}
				},
				list: [],
				isIceInfo: {},
				headline: '添加冰排',
				type: 1,
				printLoading: false,
			}
		},
		onShow() {
			var userInfo = this.$cache.getCache('userInfo')
			this.isIceInfo = userInfo.dept
			if (this.isIceInfo.isIceReleaseCold) {
				this.list = iceRaftRulesil()
			} else {
				var arrIce = iceRaftRulesil()
				this.list = removePointById(arrIce, ['suitableForCold', 'forColdTime', '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
						if (arrValue.freezeClaim) {
							this.recipientsModel.freezeClaim = 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: this.recipientsModel.freezeClaim,
						iceColdAddress: this.recipientsModel.iceColdAddress,
						forColdTime: Number(this.recipientsModel.forColdTime),
						suitableForCold: this.recipientsModel.suitableForCold,
						status: null,
						sort: Number(this.recipientsModel.sort),
					}
					if (this.recipientsModel.status == '正常') {
						params.status = '2'
					} else {
						params.status = '1'
					}
					params.codeList.push(this.recipientsModel.code)
					this.printLoading = true
					uni.showLoading({
						mask: true
					});
					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 {
								uni.$u.toast(res.msg)
							}
							this.printLoading = false
							uni.hideLoading();
						}).catch(() => {
							this.printLoading = false
							uni.hideLoading();
						})
					} else {
						this.$api.post('/api/ice-raft', params).then(res => {
							if (res.code == 200) {
								uni.navigateBack({
									delta: 1
								});
							} else {
								uni.$u.toast(res.msg)
							}
							this.printLoading = false
							uni.hideLoading();
						}).catch(() => {
							this.printLoading = false
							uni.hideLoading();
						})
					}
				} 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>