<template>
	<view class="mould-page">
		<navigation title="换模换产"></navigation>
		<view class="list-box" v-if="dataList.length>0">
			<view class="list-item" v-for="(item, index) in dataList" :key="index">
				<view class="item-text">所在设备：{{ item.equipmentName || '-' }}</view>
				<view class="item-text">模具名称：{{ item.mouldName || '-' }}</view>
				<view class="item-text">模具编码：{{ item.mouldcCode || '-' }}</view>
				<view class="item-text">安装时间：{{ timeFormat(item.startTime) }}</view>
				<view class="btn-box">
					<view class="btn-item" @click="deleteInfo(item.recordId)" v-if="item.modulId">
						<view>卸载</view>
					</view>
					<view class="btn-item" @click="editInfo(item.id, item.equipmentName)">
						<view>换模</view>
					</view>
				</view>
			</view>
		</view>
		<empty v-else text="暂无数据"></empty>
		<!-- 输入框（卸载） -->
		<u-modal :show="show" title="卸载" :showCancelButton="true" @cancel="cancelStop" @confirm="confirmStop">
			<view class="slot-content">
				<u--form labelPosition="left" :model="formData" :rules="rules" ref="form" labelWidth="140rpx">
					<u-form-item label="卸载时间" prop="endTime" borderBottom @click="showTimePicker = true">
						<u--input v-model="formData.endTime" placeholder="请选择卸载时间" border="none" disabled
							disabledColor="#ffffff"></u--input>
					</u-form-item>
				</u--form>
			</view>
		</u-modal>
		<u-datetime-picker :show="showTimePicker" v-model="defaultTime" mode="datetime" @cancel="showTimePicker=false"
			@confirm="timePickerConfirm($event)"></u-datetime-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showTimePicker: false,
				defaultTime: Number(new Date()),
				dataList: [],
				show: false,
				formData: {
					recordId: '',
					endTime: '',
				},
				rules: {
					endTime: {
						type: 'string',
						required: true,
						message: '请选择卸载时间',
						trigger: ['blur', 'change']
					},
				},
			}
		},
		onShow() {
			this.getList()
		},
		methods: {
			getList() {
				uni.showLoading({
					title: '正在加载...'
				})
				this.$api.listCurrentModule().then(res => {
					this.dataList = res.data
					uni.hideLoading()
				}).catch(err => {})
			},
			timeFormat(val) {
				if (val) {
					return this.dayjs(val).format('YYYY-MM-DD HH:mm:ss')
				} else {
					return '-'
				}
			},
			// 换模
			editInfo(id, equipmentName) {
				uni.navigateTo({
					url: '/subPackages/mould/edit?id=' + id + '&equipmentName=' + equipmentName
				});
			},
			// 卸载
			deleteInfo(id) {
				this.formData.recordId = id
				this.show = true
				this.formData.endTime = ''
			},
			timePickerConfirm(e) {
				this.formData.endTime = this.dayjs(e.value).format('YYYY-MM-DD HH:mm:ss')
				this.showTimePicker = false
			},
			confirmStop(val) {
				let that = this
				this.$refs.form.validate().then(res => {
					this.show = false
					this.$api.stopModule(this.formData).then(res => {
						uni.showToast({
							title: '卸载成功！',
							duration: 2000
						});
						setTimeout(function() {
							that.getList()
						}, 2000);
					})
				}).catch(errors => {
					console.log('校验失败')
				})
			},
			cancelStop() {
				this.showTimePicker = false
				this.show = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.mould-page {

		.list-box {
			padding: 0 20rpx;
			color: $uni-text-color-list;

			.list-item {
				background-color: $uni-bg-color;
				padding: 20rpx;
				border-radius: 20rpx;
				margin-top: 28rpx;

				.item-text {
					margin-top: 10rpx;
				}
			}

			.btn-box {
				display: flex;
				justify-content: flex-end;
				margin-top: 10rpx;

				.btn-item {
					width: 175rpx;
					height: 50rpx;
					line-height: 50rpx;
					text-align: center;
					border-radius: 24rpx;
					margin-left: 20rpx;
					border: 1px solid #43adfd;
					color: #43adfd;
				}
			}
		}
	}
</style>
