<template>
	<view>
		<form @submit="add">
			<view class="body">
				<input class="input" style="display:none;" name="coding" :value=coding >
				编码：</input>
				<input class="input" v-model="coding" />
				<button type="primary" @click="saoma()" class="button1" size="mini">
					<!-- <p class="p">扫码</p> -->
					扫码
				</button>
				<button type="primary" @click="getMessage(coding)" class="button1" size="mini"> 
					<!-- <p class="p">获取</p> -->
					获取
				</button>
			</view>
			<view class="">
				<t-table>
					<t-tr class="tr">
						名称：{{name}}
					</t-tr>
					<t-tr class="tr">
						所属机器编码：<input class="input" v-model="machineCoding" disabled="true"/>
					</t-tr>
					<t-tr class="tr">
						版本号：{{versions}}
					</t-tr>
					<t-tr class="tr">
						生产人员：{{producePersonnel}}
					</t-tr>
					<t-tr class="tr">
						生产日期：{{produceDate}}
					</t-tr>
					<t-tr class="tr">
						出货日期：{{shipmentDate}}  
						<button v-show="shipmentDate==''" type="primary" @click="chuhuo(coding)"
							class="button1" size="mini">
							出货
						</button>
					</t-tr>
					<view class="" v-if="installPersonnel!=''&&installPersonnel!=null">
						<t-tr class="tr">
							装配人员：{{installPersonnel}}
						</t-tr>
						<t-tr class="tr">
							装配日期：{{installDate}}
						</t-tr>
					</view>
					<view class="" v-if="replateDate!=''&&replateDate!=null">
						<!-- <view class="" v-if="replacePersonnel!=''&&replacePersonnel!=null"> -->
						<t-tr class="tr">
							本模块更换人员：{{replacePersonnel}}
						</t-tr>
						<t-tr class="tr">
							本模块更换原因：<input class="input" v-model="replaceReason" />
							<button type="primary" @click="updateReplaceReason()" class="button1" size="mini">
								<!-- <p class="p">更正</p> -->
								更正
							</button>
						</t-tr>
						<t-tr class="tr">
							本模块更换日期：{{replateDate}}
						</t-tr>
					</view>
					<view class="" v-if="oldReplateDate!=''&&oldReplateDate!=null">
						<!-- <view class="" v-if="replacePersonnel!=''&&replacePersonnel!=null"> -->
						<t-tr class="tr">
							旧模块更换人员：{{oldReplacePersonnel}}
						</t-tr>
						<t-tr class="tr">
							旧模块更换原因：{{oldReplaceReason}}
						</t-tr>
						<t-tr class="tr" v-if="oldCoding!=null">
							旧模块编号：{{oldCoding}}
						</t-tr>
						<t-tr class="tr">
							旧模块更换日期：{{oldReplateDate}}
						</t-tr>
					</view>
					<t-tr class="tr">
						状态：{{status}}
					</t-tr>
					<t-tr class="ttr">
						操作：
						<button v-if="status!=''" type="primary" @click="change(coding,machineCoding)"
							class="button1" size="mini">
							更换
						</button>
						<button v-if="level>2" type="primary" @click="del(coding)" class="button1" size="mini">
							删除
						</button>
					</t-tr>
				</t-table>
			</view>
			<view class="title">配件</view>
			<!-- 			<view class="lis" v-for="item in desList">
				{{item}}
			</view> -->
			<view class="lis" v-for="(item,index) in desList" :key="index">
				{{item}}
			</view>
		</form>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from 'vuex';
	import timeSelector from '@/components/wing-time-selector/wing-time-selector.vue';
	export default {
		components: {
			timeSelector
		},
		data() {
			return {
				oldCoding: '',
				replateDate: '',
				replaceReason: '',
				replacePersonnel: '',
				shipmentDate:'',
				oldReplateDate: '',
				oldReplaceReason: '',
				oldReplacePersonnel: '',
				installDate: '',
				installPersonnel: '',
				produceDate: '',
				coding: '',
				name: '',
				versions: '',
				producePersonnel: '',
				machineCoding: '',
				status: '',
				level: 0,
				desList: {}
			}
		},
		onShow() {
			var token = uni.getStorageSync("token");
			if (token.length > 2) {
				var coding = uni.getStorageSync("selectCoding");
				if (coding.length > 2) {
					this.coding = coding;
					this.getMessage(coding);
				}
			}
			var level = uni.getStorageSync("level");
			this.level = level;

		},
		methods: {
			saoma() {
				var that = this;
				// 允许从相机和相册扫码
				uni.scanCode({
					success(res) {
						that.coding = res.result;
					}
				});
			},
			del(coding) {
				uni.showModal({
					title: '删除',
					content: '确定执行操作？',
					success: (res) => {
						if (res.confirm) {
							var that = this;
							var serverUrl = that.serverurl;
							var token = uni.getStorageSync("token");
							uni.request({
								url: serverUrl + "/TModules/deleteModules?coding=" + coding,
								method: "GET",
								header: {
									'token': token
								},
								success: (Result) => {
									// console.log(Result);
									var res = Result;
									uni.showModal({
										title: '提示',
										content: res.data.message,
									});
								}
							});
						}
					}
				});

			},
			chuhuo(coding){
				uni.showModal({
					title: '出货',
					content: '确定执行操作？',
					success: (res) => {
						if (res.confirm) {
							var that = this;
							var serverUrl = that.serverurl;
							var token = uni.getStorageSync("token");
							uni.request({
								url: serverUrl + "/TModules/chuhuo?coding=" + coding,
								method: "GET",
								header: {
									'token': token
								},
								success: (Result) => {
									// console.log(Result);
									var res = Result;
									uni.showModal({
										title: '提示',
										content: res.data.message,
									});
								}
							});
						}
					}
				});
			},
			updateReplaceReason(){
				var that = this;
				var serverUrl = that.serverurl;
				var token = uni.getStorageSync("token");
				uni.request({
					url: serverUrl + "/TModules/updateReplaceReason",
					method: "POST",
					header: {
						'token': token
					},
					data:{
						"coding":that.coding,
						"replaceReason":that.replaceReason
					},
					success: (Result) => {
						// console.log(Result);
						var res = Result;
						uni.showModal({
							title: '提示',
							content: res.data.message,
						});
					}
				});
			},
			change(coding, machineCoding) {
				uni.navigateTo({
					url: '/pages/select/update?coding=' + coding + '&machineCoding=' + machineCoding,
				});
			},
			getMessage(coding) {
				var that = this;
				var serverUrl = that.serverurl;
				var token = uni.getStorageSync("token");
				uni.request({
					url: serverUrl + "/TModules/getModules?coding=" + coding,
					method: "GET",
					header: {
						'token': token
					},
					success: (Result) => {
						console.log("1");
						var res = Result;
						if (res.data.code == true) {
							var codes = res.data.data;
							that.name = codes.name;
							var co = codes.coding;
							if (co.substring(0, 4) != '1000') {
								that.versions = co.substring(8, 10);
								that.producePersonnel = codes.producePersonnel;
								that.produceDate = codes.createDate;
								var sta = codes.status;
								if (sta == '0' || sta == null) {
									that.status = "未安装";
								}
								if (sta == '1') {
									that.status = "已安装";
								}
								if (sta == '2') {
									that.status = "已替换";
								}
								that.machineCoding = codes.machineCoding;
							} else {
								that.versions = co.substring(co.length - 3, co.length);
								// that.producePersonnel = "";
								// that.produceDate = "";
								var sta = codes.status;
								if (sta == '0') {
									that.status = "未安装";
								}
								if (sta == '1') {
									that.status = "已安装";
								}
								if (sta == '2') {
									that.status = "已替换";
								}
								that.producePersonnel = codes.producePersonnel;
								that.produceDate = codes.createDate;
								if (codes.machineCoding != null) {
									that.machineCoding = codes.machineCoding;
								}

							}

							if (codes.flagCode.length > 1) {
								that.desList = codes.flagCode.split(",");
							}
							if (codes.shipmentDate != null) {
								that.shipmentDate = codes.shipmentDate;
							}
							that.installDate = codes.installDate;
							that.installPersonnel = codes.installPersonnel;
							that.replateDate = codes.replateDate;
							that.replaceReason = codes.replaceReason;
							that.replacePersonnel = codes.replacePersonnel;
							that.oldCoding = codes.oldCoding;
							that.oldReplateDate = codes.oldReplateDate;
							that.oldReplaceReason = codes.oldReplaceReason;
							that.oldReplacePersonnel = codes.oldReplacePersonnel;
							// if (co.substring(0, 4) != '1000') {
							// 	that.oldCoding = codes.oldCoding;
							// } else {
							// 	that.oldCoding = '';
							// }
						} else {
							uni.showModal({
								content: res.data.message,
							});
						}
					}
				});
			}
		}
	}
</script>

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

	.ttr {
		padding-left: 30upx;
		padding-top: 13upx;
		font-size: 30upx;
		padding-bottom: 13upx;
		display: flex;
		/* flex-direction: row; */
		gap: 20upx;
		justify-content: flex-start;
		
	}

	.tr {
		padding-left: 30upx;
		padding-top: 13upx;
		font-size: 30upx;
		padding-bottom: 13upx;
	}

	.input {
		/* padding: 10upx 20upx 10upx 0upx; */
		padding-left: 10upx;
		/* padding-top: 10upx; */
		background-color: #FFFFFF;
		height: 50upx;
		box-shadow: 0upx 0upx 20upx #D3D3D3;
		border-radius: 5upx;
		margin-right: 10upx;
	}

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

	.button1 {
		
	}

	.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%);
	}

	.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%);
	}

	.radio {
		padding-top: 20upx;
		padding-left: 30upx;
	}

	.lis {
		padding-left: 90upx;
		font-size: 30upx;
		padding-top: 5upx;
	}

	.title {
		padding-left: 20upx;
		font-size: 35upx;
	}
</style>
