<template>
	<mobile-frame>
		<view class="info">
			<view class="one">
				<uni-forms ref="form" :modelValue="form" :rules="rules" label-width="auto">
					<uni-forms-item label="快递类型查询" name="transport_type">
						<uni-easyinput type="text" @change="searchOther" v-model="form.transport_type" placeholder="请输入快递类型" />
					</uni-forms-item>
					<uni-forms-item label="快递类型" name="customer_transport_type">
						<picker class="picker" mode="selector" :range="typeList" @change="typeChange" range-key="label">
							<view>{{form.type_name||'请选择快递类型'}}</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item label="维护快递单号" name="customer_transport_no">
						<uni-easyinput type="text" v-model="form.customer_transport_no" placeholder="请输入快递单号" />
					</uni-forms-item>
				</uni-forms>
				<view class="btn">
					<button type="primary" @click="onSubmit('form')" size="small">提交</button>
				</view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				form: {},
				rules: {
					customer_transport_type: {
						rules: [{
							required: true,
							errorMessage: '请输入快递类型',
						}]
					},
					customer_transport_no: {
						rules: [{
							required: true,
							errorMessage: '请输入快递单号',
						}]
					}
				},
				typeList: [],
			};
		},
		onLoad: function(e) {
			const that = this;
			that.$set(that, `id`, e.id || '');
			that.watchLogin();
		},
		methods: {
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: async (res) => {},
					fail: (err) => {
						uni.navigateTo({
							url: `/pages/login/index`
						})
					}
				})
			},
			typeChange(e) {
				const that = this;
				let data = that.typeList[e.detail.value];
				that.$set(that.form, `customer_transport_type`, data.value);
				that.$set(that.form, `type_name`, data.label);
			},
			// 提交保存
			async onSubmit(ref) {
				const that = this;
				that.$refs[ref].validate().then(async params => {
					let form = that.form;
					let transport = {};
					transport.customer_transport_no = params.customer_transport_no;
					transport.customer_transport_type = params.customer_transport_type;
					form.transport = transport;
					const arr = await that.$api(`/afterSale/${that.id}`, 'POST', form);
					if (arr.errcode == '0') {
						uni.showToast({
							title: `维护信息成功`,
							icon: 'success',
						});
						uni.navigateBack({
							detail: 1
						})
					} else {
						uni.showToast({
							title: arr.errmsg,
							icon: 'none',
						})
					}
				})
			},
			// 查询其他信息
			async searchOther(e) {
				const that = this;
				let res = await that.$api(`/dictData`, 'GET', {
					code: "transport_type",
					label: e
				});
				if (res.errcode == '0') {
					that.$set(that, `typeList`, res.data);
				}
			}
		}
	}
</script>

<style lang="scss">
	.info {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			padding: 2vw;

			.uni-input {
				border: #f1f1ff 1px solid;
				padding: 2vw 2vw;
				border-radius: 1vw;
			}

			.picker {
				border: 1px solid #3333;
				border-radius: 5px;
				padding: 2vw;
			}

			.btn {
				text-align: center;

				button {
					margin: 0 2vw 2vw 2vw;
					background-color: var(--ff0Color);
					color: var(--fffColor);
				}

				.name {
					color: var(--f85Color);
					font-size: var(--font14Size);
				}
			}
		}
	}

	.uni-forms-item {
		margin-bottom: 6vw !important;
		display: flex;
		flex-direction: row;
	}
</style>
