<template>
	<view>
		<uni-collapse>
			<uni-forms ref="form" :modelValue="formData">
				<uni-forms-item label="模具编码" :labelWidth='90' name="mouldUuid">
					<uni-easyinput v-model="formData.mouldUuid" suffixIcon="scan" @iconClick="scanBarMouldUuid"
						@confirm="submit" type="text" />
				</uni-forms-item>
				<uni-forms-item label="状态" :labelWidth='90' name="status">
					<u-radio-group v-model="formData.status" placement="row">
						<u-radio v-for="(item, index) in taskStatusOptions" :key="index" :label="item.text"
							:name="item.value">
						</u-radio>
					</u-radio-group>
				</uni-forms-item>
				<u-button type="primary" @click="submit">查询</u-button>
				<uni-collapse-item title="查询结果" :open="true">
					<uni-card :is-shadow="false" v-for="(item,index) in breakdownList" :key="index" is-full>
						<view><strong>任务编码</strong> : {{item.taskCode}}</view>
						<view><strong>任务名称</strong> : {{item.taskName}}</view>
						<view><strong>任务类型</strong> : <uni-tag :text="taskText(item.taskType)" type="primary"
								v-if="item.taskType"></uni-tag></view>
						<view><strong>故障编码</strong> : {{item.breakdownCode}}</view>
						<view><strong>故障名称</strong> : {{item.breakdownName}}</view>
						<view><strong>状态</strong> : <uni-tag :text="statusText(item.status)" type="primary"
								v-if="item.status"></uni-tag></view>
						<view><strong>模具编号</strong> : {{item.mouldUuid}}</view>
						<view><strong>故障类型</strong> : <uni-tag :text="breakText(item.type)" type="primary"
								v-if="item.type"></uni-tag></view>
						<view><strong>维修人</strong> : {{item.operatorName}}</view>
						<view><strong>创建人</strong> : {{item.createBy}}</view>
						<view><strong>创建时间</strong> : {{item.createTime}}</view>
						<view><strong>修改人</strong> : {{item.updateBy}}</view>
						<uni-row>
							<uni-col :span="18">
								<view><strong>修改时间</strong> : {{item.updateTime}}</view>
								<view><strong>备注</strong> : {{item.remark}}</view>
							</uni-col>
							<uni-col :span="6"> <u-button type="primary"
									@click="handleFinish(item)">完成</u-button></uni-col>
						</uni-row>
					</uni-card>
				</uni-collapse-item>
			</uni-forms>
		</uni-collapse>
	</view>
</template>

<script>
	import {
		listBreakdown,
		getBreakdown,
		delBreakdown,
		addBreakdown,
		updateBreakdown,
		finishBreakdown
	} from "@/api/tpmMould/breakdown";
	import {
		listRepairTask,
		getRepairTask,
		delRepairTask,
		addRepairTask,
		updateRepairTask,
		finishRepairTask,
	} from "@/api/tpmMould/repairTask";
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";
	export default {
		mounted() {
			getDicts("tpm_breakdown_type").then(res => {
				this.breakdownTypeOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
			getDicts("tpm_task_status").then(res => {
				this.taskStatusOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
			getDicts("tpm_repair_type").then(res => {
				this.repairTypeOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
			this.submit();
		},
		data() {
			return {
				repairTypeOptions: [],
				breakdownTypeOptions: [],
				taskStatusOptions: [],
				formData: {
					mouldUuid: null,
					status: "0"
				},
				breakdownList: [],
			}
		},
		methods: {
			statusText(status) {
				let obj = this.taskStatusOptions.find(item => item.value == status)
				if (obj !== undefined) {
					return this.taskStatusOptions.find(item => item.value == status).text
				}
			},
			breakText(status) {
				let obj = this.breakdownTypeOptions.find(item => item.value == status)
				if (obj !== undefined) {
					return this.breakdownTypeOptions.find(item => item.value == status).text
				}
			},
			taskText(status) {
				let obj = this.repairTypeOptions.find(item => item.value == status)
				if (obj !== undefined) {
					return this.repairTypeOptions.find(item => item.value == status).text
				}
			},
			scanBarMouldUuid(code) {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.mouldUuid = res.result;
						_this.submit()
					}
				});
			},
			submit() {
				listBreakdown({
					mouldUuid: this.formData.mouldUuid,
					status: this.formData.status
				}).then(res => {
					if (res.rows.length > 0) {
						this.breakdownList = res.rows;
					} else {
						this.$modal.msg("未查询到相关信息，请重新输入！")
						this.formData.mouldUuid = null;
					}
				})
			},
			handleFinish(item) {
				finishBreakdown([item.id]).then(response => {
					this.submit();
					this.$modal.msgSuccess("完成成功");
				});
			}
		}
	}
</script>

<style>
	.text {
		text-align: center;
	}
</style>