<template>
	<view style="background-color: #fff;">
		<!-- <uni-collapse> -->
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<!-- <uni-collapse-item title="新增模具维修" :open="true"> -->
			<uni-forms-item label="任务号" :labelWidth='90' name="taskCode">
				<uni-easyinput type="text" disabled v-model="formData.taskCode" />
			</uni-forms-item>
			<uni-forms-item label="任务名" :labelWidth='90' name="taskName">
				<uni-easyinput type="text" v-model="formData.taskName" disabled />
			</uni-forms-item>
			<uni-forms-item label="类型" :labelWidth='90' name="type" v-if="!formData.taskId">
				<uni-data-select v-model="formData.type" :localdata="repairTypeOptions" />
			</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>
			<uni-forms-item label="模具编码" :labelWidth='90' name="mouldUuid">
				<uni-easyinput suffixIcon="scan" @iconClick="scanBarMouldUuid" v-model="formData.mouldUuid" type="text"
					@confirm="confirmMouldUuid" />
			</uni-forms-item>
			<uni-forms-item label="模具名称" :labelWidth='90' name="mouldName">
				{{formData.mouldName}}
			</uni-forms-item>
			<uni-forms-item label="备注" :labelWidth='90' name="remark">
				<uni-easyinput v-model="formData.remark" type="text" />
			</uni-forms-item>
			<u-divider :text="'新增故障'" v-if="formData.taskId"></u-divider>
			<view v-if="formData.taskId">
				<uni-forms-item label="故障号" :labelWidth='90' name="breakdownCode">
					<uni-easyinput v-model="formData.breakdownCode" disabled type="text" />
				</uni-forms-item>
				<uni-forms-item label="故障名" :labelWidth='90' name="breakdownName">
					<uni-easyinput v-model="formData.breakdownName" type="text" />
				</uni-forms-item>
				<uni-forms-item label="故障类型" :labelWidth='90' name="type" v-if="formData.taskId">
					<uni-data-select v-model="formData.type" :localdata="breakdownTypeOptions" />
				</uni-forms-item>
				<uni-forms-item label="维修人" :labelWidth='90' name="operatorCode">
					<uni-easyinput suffixIcon="scan" @iconClick="scanBarOperatorCode" @confirm="confirmOperatorCode"
						v-model="formData.operatorCode" type="text" style="margin-bottom: 10px;" />
					<uni-data-select v-model="formData.operatorCode" :localdata="empList" />
				</uni-forms-item>
			</view>
			<!-- </uni-collapse-item> -->
		</uni-forms>
		<!-- </uni-collapse> -->
		<u-button type="success" v-if="!formData.taskId" @click="taskSubmit">新建任务</u-button>
		<u-button type="primary" v-if="formData.taskId" @click="breakSubmit">新建故障</u-button>
	</view>
</template>

<script>
	import {
		listMould
	} from "@/api/tpmMould/mould";
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";
	import {
		listRepairTask,
		getRepairTask,
		delRepairTask,
		addRepairTask,
		updateRepairTask,
		finishRepairTask,
	} from "@/api/tpmMould/repairTask";
	import {
		listEmployee
	} from "@/api/mes/jobIn";
	import {
		listBreakdown,
		getBreakdown,
		delBreakdown,
		addBreakdown,
		updateBreakdown,
		finishBreakdown
	} from "@/api/tpmMould/breakdown";
	export default {
		mounted() {
			getDicts("tpm_repair_type").then(res => {
				this.repairTypeOptions = 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_breakdown_type").then(res => {
				this.breakdownTypeOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
			listEmployee().then((res) => {

				this.empList = res.rows.map(item => {
					item.text = item.empCode + ':' + item.name
					item.value = item.empCode
					return item
				});
			})
		},
		data() {
			return {
				empList: [],
				repairTypeOptions: [],
				breakdownTypeOptions: [],
				taskStatusOptions: [],
				formData: {
					taskCode: null,
					taskName: "模具维修",
					type: null,
					status: "0",
					mouldUuid: null,
					mouldName: null,
					remark: null,
					breakdownCode: null,
					breakdownName: null,
					operatorId: null,
					operatorCode: null,
					mouldId: null,
					taskId: null,
					taskType: null
				},
				rules: {
					// saleOutTaskCode: {
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请输入销售出库任务单!'
					// 	}]
					// },
				}
			}
		},

		methods: {
			deleteDetail(index) {
				this.formData.wmsSaleOutDetailList.splice(index, 1);
			},
			clickDetail(itemIndex, {
				position,
				index
			}) {
				if (index == 0) {
					this.deleteDetail(itemIndex);
				}
			},
			//扫描模具编码带出模具信息
			confirmMouldUuid() {
				if (this.formData.mouldUuid) {
					listMould({
						mouldUuid: this.formData.mouldUuid
					}).then(res => {
						if (res.rows.length > 0) {
							this.formData.mouldId = res.rows[0].id
							this.formData.mouldName = res.rows[0].mouldName
						} else {
							this.$modal.msg("未查询到该模具，请重新输入！")
						}
					})
				}
			},
			scanBarMouldUuid() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.mouldUuid = res.result;
						_this.confirmMouldUuid()
					}
				});
			},
			scanBarOperatorCode() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.operatorCode = res.result;
						_this.confirmOperatorCode()
					}
				});
			},
			confirmOperatorCode() {
				listEmployee({
					empCode: this.formData.operatorCode
				}).then(async res => {
					if (res.rows.length == 0) {
						this.$modal.msg("未查询到该人员，请重新输入！")
						this.formData.operatorCode = null;
					}
				})
			},
			taskSubmit() {
				const _this = this;
				this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定新建该任务吗？',
						success: function(res) {
							if (res.confirm) {
								console.log(_this.formData)
								if (_this.formData.taskName == null || _this.formData.taskName == "") {
									_this.$modal.msg("任务名不得为空，请输入！")
								} else {
									_this.$modal.loading('提交中')
									addRepairTask(_this.formData).then(async res => {
										let obj = res.mouldRepairTask
										_this.formData.taskId = obj.id;
										_this.formData.taskCode = obj.taskCode;
										_this.formData.taskType = obj.type;
										_this.$modal.msgSuccess("新建任务成功!");
										_this.$modal.closeLoading();
									});
								}
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},
			breakSubmit() {
				const _this = this;
				this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定新建该故障吗？',
						success: function(res) {
							if (res.confirm) {
								if (_this.formData.breakdownName == null || _this.formData
									.breakdownName == "") {
									_this.$modal.msg("故障名不得为空，请输入！")
								} else {
									let obj = _this.empList.find(item => _this.formData.operatorCode ==
										item.value)
									if (obj !== undefined) {
										_this.formData.operatorId = obj.id
									}
									console.log(_this.formData)
									_this.$modal.loading('提交中')
									addBreakdown(_this.formData).then(async res => {
										_this.$modal.msgSuccess("新建故障成功!");
										_this.$modal.closeLoading();
										setTimeout(() => {
											_this.$tab.switchTab(
												"/pages/work/index");
										}, 500);
									});
								}
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			},
		}
	}
</script>

<style>
	.divider {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 10px;
		margin-bottom: 10px;
		height: 1px;
		background-color: #F1F1F1;
	}

	.divider span {
		padding: 5px;

	}
</style>