<template>
	<view>
		<view>
			<uni-easyinput suffixIcon="scan" placeholder="请输入设备编码" @iconClick="scanBar"
				v-model="queryParams.equipmentCode" type="text" @confirm="scanBarCode" />
			<button type="primary" @click="reset"
				style="margin-top: 5px;text-align: center;display: block;width: 30%;">重置</button>
			<view class="cu-card article ">
				<view class="cu-item shadow borderBottom" v-for="(item,index) in list" :key="index">
					<view class="title">
						<view class="text-cut">任务名 : {{item.taskName}}</view>
						<view>
							<button type="primary" size="mini" @click="detail(item)" style="float: right;">查看明细</button>

						</view>
						<view>
							<button type="success" size="mini"
								style="float: right;margin-right: 10px;background-color: #5AC725;color: #fff;"
								@click="handleFinish(item)">完成</button>
						</view>
					</view>
					<view class="content">
						<view class="desc">
							<view class="text-content">
								<view>明细号 : {{item.taskItemCode}}</view>
								<view>明细名 : {{item.taskItemName}}</view>
								<view>状态 : <uni-tag :text="tagText(item.status)" type="primary"
										v-if="item.status"></uni-tag></view>
								<view>设备编码 : {{item.equipmentCode}}</view>
								<view>时间 : {{item.createTime}}</view>

							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		listCheckTaskItem,
		getCheckTaskItem
	} from "@/api/tpm/index.js";
	import {
		listTaskItem,
		getTaskItem,
		delTaskItem,
		addTaskItem,
		updateTaskItem,
		finishCheckTaskItem,
		finishCheckTaskItemDetail,
		repairCheckTaskItemDetail,
		tpmCheckTaskEditemOnly,
		listEmployee
	} from "@/api/tpm/checkTaskItem";
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";
	import {
		getUserProfile
	} from "@/api/system/user";

	export default {
		mounted() {
			this.getList()
			getDicts("tpm_task_status").then(res => {
				this.taskStatusOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})

			console.log(this.$store)
		},
		data() {
			return {
				list: {},
				queryParams: {
					equipmentCode: null,
					pageNum: 1,
					pageSize: 100,
					orderByColumn: "id",
					isAsc: "desc",
				},
				formData: {},
				taskStatusOptions: [],
				rightOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#ff2a17'
					}
				}, ],
			}
		},
		methods: {
			tagText(status) {
				let obj = this.taskStatusOptions.find(item => item.value == status)
				if (obj !== undefined) {
					return this.taskStatusOptions.find(item => item.value == status).text
				}
			},
			async handleFinish(item) {
			    try { 
					console.log("1234");
			        // 获取用户ID
			        let useId = null;
			        const res = await listEmployee({"name": this.$store.state.user.name});
					console.log(res,"res1")
			        
			        if (res && res.rows && res.rows.length > 0) {
			            useId = res.rows[0]["id"] ?? null;
			        }
			
			        // if (useId) {
			            // 调用 tpmCheckTaskEditemOnly 方法
			            const verifierRes = await tpmCheckTaskEditemOnly({verifierId: useId, id: item.id});
			            console.log(verifierRes, "verifierRes");
			
			            // 调用 finishCheckTaskItem 方法
			            const finishRes = await finishCheckTaskItem([item.id]);
			            this.$modal.msgSuccess("完成成功!");
			            this.getList();
			   //      } else {
						// console.log("无")
			   //          // this.$modal.msgError("没有找到该员工!");
			   //      }
			    } catch (error) {
			        // console.error("Error in handleFinish:", error);
			        // this.$modal.msgError("操作失败，请稍后重试!");
			    }
			},

			getList() {
				listCheckTaskItem(this.queryParams).then(response => {
					this.list = response.rows;
				});
			},
			reset() {
				this.queryParams.equipmentCode = null;
				this.getList();
			},
			scanBarCode() {
				if (this.queryParams.equipmentCode) {
					this.getList();
				}
			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.queryParams.equipmentCode = res.result;
						_this.scanBarCode();
					}
				});
			},
			detail(item) {
				getCheckTaskItem(item.id).then(async res => {
					this.$tab.navigateTo('/pages/tpm/checkTaskItem?formData=' + JSON.stringify(res.data));
				})
			},
		}
	}
</script>

<style>
</style>