<template>
	<view>
		<view class="body">
			<input class="input" style="display:none;" name="code" :value=code />
			编码：<input class="input" v-model="code" />
			<button type="primary" @click="saoma()" class="" size="mini">
				<!-- <p class="p">扫码</p> -->
				扫码
			</button>
			<button type="primary" @click="getMessage(code)" class="" size="mini">
				<!-- <p class="p">获取</p> -->
				获取
			</button>
		</view>

		<view class="">
			<t-table>
				<t-tr class="tr">
					机器编码：<input class="input" v-model="coding" disabled="true"/>
				</t-tr>
				<t-tr class="tr">
					机器铭牌：{{nameplate != null ? nameplate : ''}}
				</t-tr>
				<t-tr class="tr">
					设备编码：{{clientId != null ? clientId : ''}}
				</t-tr>
				<t-tr class="tr">
					订单编码：{{orderNo != null ? orderNo : ''}}
				</t-tr>
				<t-tr class="tr">
					客户编码：{{customerNo != null ? customerNo : ''}}
				</t-tr>
				<t-tr class="tr">
					机器名称：{{name != null ? name : ''}}
				</t-tr>
				<t-tr class="tr">
					总装开始时间：{{startDate != null ? startDate : ''}}
				</t-tr>
				<t-tr class="tr">
					工序状态：{{weiname}}
				</t-tr>
				<t-tr class="tr">
					模块状态：{{Modulesname}}
				</t-tr>
				<t-tr class="tr">
					总装结束时间：{{endDate != null ? endDate : ''}}
				</t-tr>
				<t-tr class="tr">
					刷卡器：{{cardReader != null ? cardReader : ''}}
				</t-tr>
			</t-table>
		</view>
		<view class="line"></view>
		<view class="">
			<uni-list-item title="工序查询" @click="gongxu()" />
		</view>
		<view class="line"></view>
		<!-- <view class="line"></view> -->
		<view class="">

			<view v-for="(item,index) in groupedModules" :key="index">
				<t-table class="table">
					<t-tr class="tr table">
						名称：{{item.name}}
					</t-tr>
					<t-tr class="tr" v-for="(item1,index1) in item.items" :key="index1">
						<t-td>编码：{{item1.coding}}</t-td>
						<t-td class="td">
							<button type="primary" @click="tran(item1.coding)" class="button1">
								<p class="p">详情</p>
							</button>
						</t-td>
					</t-tr>
				</t-table>
				<view class="line"></view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				code: '',
				coding: '',
				nameplate: '',
				name: '',
				startDate: '',
				clientId: '',
				customerNo: '',
				orderNo: '',
				cardReader: '',
				endDate: '',
				modulesList: [],
				Modulesname: '',
				weiname: '',
				groupedModules: [], // 用于存储分组后的结果
			}
		},
		onShow() {

		},
		onLoad: function(option) {
			const item = JSON.parse(decodeURIComponent(option.item));
			var coding = item.coding;
			this.getMessage(coding);
		},
		methods: {
			saoma() {
				var that = this;
				// 允许从相机和相册扫码
				uni.scanCode({
					success(res) {
						that.code = res.result;
					}
				});
			},
			gongxu() {
				var item = {
					coding: null,
					type: 'get'
				};
				var coding = this.coding;
				item.coding = coding;
				uni.navigateTo({
					url: '../Insert/workingProcedure?item=' + encodeURIComponent(JSON.stringify(item))
				});
			},
			groupModules() {
			    const grouped = {};
				console.log("grouped")
			    this.modulesList.forEach(item => {
			        if (!grouped[item.name]) {
			          grouped[item.name] = { name: item.name, items: [] };
			        }
			        grouped[item.name].items.push(item);
			    });
			    this.groupedModules = Object.values(grouped); // 将分组结果赋值给 groupedModules
				console.log("groupedModules", this.groupedModules)
			},
			getMessage(coding) {
				var that = this;
				var serverUrl = that.serverurl;
				var token = uni.getStorageSync("token");
				uni.request({
					url: serverUrl + "/TMachine/selectMachine?coding=" + coding,
					method: "GET",
					header: {
						'token': token
					},
					success: (Result) => {
						// console.log(Result);
						var res = Result;
						if (res.data.code == true) {
							//当机器已录入时，会先把所有录入模块给列出来
							if (res.data.message == 'all') {
								var tmachine = res.data.data.tmachine;
								that.name = tmachine.name;
								that.nameplate = tmachine.nameplate;
								that.clientId = tmachine.clientId;
								that.customerNo = tmachine.customerNo;
								that.orderNo = tmachine.orderNo;
								that.cardReader = tmachine.cardReader;
								that.coding = tmachine.coding;
								that.startDate = tmachine.startDate;
								that.endDate = tmachine.endDate;
								that.modulesList = res.data.data.modulesList;
								that.groupModules()
								// console.log("groupedModules", that.groupedModules)
							} else {
								var codes = res.data.data;
								that.name = codes.name;
								that.nameplate = codes.nameplate;
								that.clientId = codes.clientId;
								that.customerNo = codes.customerNo;
								that.orderNo = codes.orderNo;
								that.cardReader = codes.cardReader;
								that.producePersonnel = uni.getStorageSync("name");
							}

						} else {
							uni.showModal({
								title: '提示',
								content: res.data.message,
							});
						}
						this.jiancha();
					}
				});
			},
			tran(coding) {
				uni.setStorageSync("selectCoding", coding);
				uni.navigateTo({
					url: 'selectModules',
				});
			},
			jiancha() {
				var token = uni.getStorageSync("token");
				uni.request({
					url: this.serverurl + "/TWorkingProcedure/jiancha",
					method: "POST",
					data: {
						"machineCoding": this.coding
					},
					header: {
						'token': token
					},
					success: (Result) => {
						var res = Result;
						if (res.data.code == false) {
							var re = res.data.data.toString();
							this.weiname = re + '未安装';
						} else {
							this.weiname = '已全部安装';
						}
						this.jianchaModules();
					}
				});
			},
			jianchaModules() {
				var token = uni.getStorageSync("token");
				uni.request({
					url: this.serverurl + "/TModules/jianchaModules?machineCoding=" + this.coding,
					method: "GET",
					header: {
						'token': token
					},
					success: (Result) => {
						var res = Result;
						if (res.data.code == false) {
							var re = res.data.data.toString();
							this.Modulesname = re + '未安装';
						} else {
							this.Modulesname = '已全部安装';
						}
					}
				});
			}
		}
	}
</script>

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

	.table {
		padding-top: 30upx;

	}

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

	.td {
		padding-left: 250upx;

	}

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

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

	.button1 {
		margin: auto;
		width: 100upx;
		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%);
	}

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

	.line {
		background: #ECECEC;
		height: 10upx;
	}
</style>