<template>
	<view>
		<form @submit="update">
			<view class="body">
				<input class="input" style="display:none;" name="machineCoding" :value=machineCoding />
				所属机器编码：<input class="input" v-model="machineCoding" />
				<button type="primary" @click="saoma1()" class="button1">
					<p class="p">扫码</p>
				</button>
			</view>
			<view class="body">
				<input class="input" style="display:none;" name="oldCoding" :value=oldCoding />
				旧模块编码：<input class="input" v-model="oldCoding" />
				<button type="primary" @click="saoma2()" class="button1">
					<p class="p">扫码</p>
				</button>
			</view>
			<view class="body">
				<input class="input" style="display:none;" name="coding" :value=coding />
				新模块编码：<input class="input" v-model="coding" />
				<button type="primary" @click="saoma3()" class="button1">
					<p class="p">扫码</p>
				</button>
			</view>
			<timeSelector showType="date" @btnConfirm="btnConfirm0" @btnCancel="btnCancel0">
				<view class="body">
					<!-- 更换日期：<input type="hidden" class="input" name="replateDate" :value="replateDate" /> -->
					更换日期：<view class="input">{{ replateDate }}</view>
				</view>
			</timeSelector>
			<view class="body">
				更换原因：<textarea class="input-two" name="replaceReason" />
			</view>
			<view class="submitButton">
				<button type="primary" formType="submit" class="button">
					<p class="p1">提交</p>
				</button>
			</view>
		</form>
	</view>
</template>

<script>
	import timeSelector from '@/components/wing-time-selector/wing-time-selector.vue';

	export default {
		components: {
			timeSelector
		},
		data() {
			// 初始化为今天的日期
			const now = new Date();
			const replateDateString = now.getFullYear() + '-' +
				(now.getMonth() + 1).toString().padStart(2, '0') + '-' +
				now.getDate().toString().padStart(2, '0');
			return {
				machineCoding: '',
				coding: '',
				oldCoding: '',
				replateDate: replateDateString,
				replaceReason: ''
			}
		},
		methods: {
			saoma1() {
				var that = this;
				// 允许从相机和相册扫码
				uni.scanCode({
					success(res) {
						that.machineCoding = res.result;
					}
				});
			},
			saoma2() {
				var that = this;
				// 允许从相机和相册扫码
				uni.scanCode({
					success(res) {
						that.oldCoding = res.result;
					}
				});
			},
			saoma3() {
				var that = this;
				// 允许从相机和相册扫码
				uni.scanCode({
					success(res) {
						that.coding = res.result;
					}
				});
			},
			btnConfirm0(e) {
				console.log('确定时间为：', new Date(e.key));
				this.replateDate = e.key;
			},
			btnCancel0() {
				this.replateDate = '';
			},

			update(event) {
				var serverUrl = this.serverurl;
				const {
					value: modules
				} = event.detail;
				var token = uni.getStorageSync("token");
				var replateDateObject = new Date(this.replateDate);
				var replateDateString = replateDateObject.getFullYear() + '-' +
					(replateDateObject.getMonth() + 1).toString().padStart(2, '0') + '-' +
					replateDateObject.getDate().toString().padStart(2, '0') + ' ' +
					replateDateObject.getHours().toString().padStart(2, '0') + ':' +
					replateDateObject.getMinutes().toString().padStart(2, '0') + ':' +
					replateDateObject.getSeconds().toString().padStart(2, '0');
				console.log('时间参数格式化为：', replateDateString);
				uni.request({
					url: serverUrl + "/TModules/replace",
					method: "POST",
					data: {
						"coding": modules.coding,
						"machineCoding": modules.machineCoding,
						"oldCoding": modules.oldCoding,
						"replateDate": replateDateString,
						"replaceReason": modules.replaceReason,
						"replacePersonnel": uni.getStorageSync("name"),
					},
					header: {
						'token': token
					},
					success: (Result) => {
						var res = Result;
						if (res.data.code == true) {}
						uni.showModal({
							title: '提示',
							content: res.data.message,
						});
					}
				});
			},
		}
	}
</script>

<style>
	.body {
		background-color: #FFFFFF;
		padding: 20upx 20upx 20upx 20upx;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
	}

	.input {
		padding-left: 10upx;
		background-color: #FFFFFF;
		width: 400upx;
		height: 55upx;
		box-shadow: 0upx 0upx 20upx #D3D3D3;
		border-radius: 5upx;
	}

	.button {
		margin: auto;
		width: 60%;
		height: 100upx;
	}

	.button1 {
		margin: auto;
		width: 80upx;
		height: 55upx;
	}

	.p {
		/* #ifdef H5 */
		top: -13%;
		/* #endif */
		width: 50upx;
		height: 30upx;
		font-size: 25upx;
		padding-right: 9upx;
		padding-top: 6upx;
		/* #ifndef H5 */
		/* padding-top: 10upx; */
		/* #endif */

		position: absolute;
		/* 水平居中 */
		left: 50%;
		-webkit-transform: translateX(-50%);
		transform: translateX(-50%);
	}

	.input-two {
		padding-left: 10upx;
		width: 450upx;
		box-shadow: 0upx 0upx 20upx #D3D3D3;
		border-radius: 5upx;
	}

	.p1 {
		/* #ifdef H5 */
		top: -13%;
		/* #endif */
		width: 80upx;
		height: 60upx;
		font-size: 40upx;
		padding-top: 6upx;
		/* #ifndef H5 */
		padding-top: 10upx;
		/* #endif */

		position: absolute;
		/* 水平居中 */
		left: 50%;
		-webkit-transform: translateX(-50%);
		transform: translateX(-50%);
	}
	
	.submitButton {
		margin-top: 40upx;
	}
</style>