<!-- 
    filename: dialogForm.vue
    author: liubin
    date: 2023-08-15 10:32:36
    description: 弹窗的表单组件
-->

<template>
	<el-form
		ref="form"
		:model="form"
		:label-position="labelPosition"
		v-loading="formLoading">
		<el-row :gutter="20">
			<!-- 维修单号  -->
			<el-col :span="12">
				<el-form-item
					label="维修单号"
					prop="repairOrderNumber"
					:rules="[
						{ required: true, message: '维修单号不能为空', trigger: 'blur' },
					]">
					<el-input
						v-model="form.repairOrderNumber"
						@change="$emit('update', form)"
						placeholder="请输入维修单号"
						:disabled="disabled" />
				</el-form-item>
			</el-col>

			<!-- 维修工  -->
			<el-col :span="12">
				<el-form-item
					label="维修工"
					prop="repairman"
					:rules="[
						{ required: true, message: '维修工不能为空', trigger: 'blur' },
					]">
					<el-input
						v-model="form.repairman"
						@change="$emit('update', form)"
						placeholder="请输入维修工"
						:disabled="disabled" />
				</el-form-item>
			</el-col>

			<!-- 设备大类  -->
			<el-col :span="12">
				<el-form-item
					label="设备大类"
					prop="equipmentCategory"
					:rules="[
						{ required: true, message: '设备大类不能为空', trigger: 'blur' },
					]">
					<el-select
						v-model="form.equipmentCategory"
						:placeholder="`请选择设备大类`"
						:disabled="disabled"
						clearable
						filterable
						@change="handleEqTypeChange">
						<el-option
							v-for="opt in equipmentTypeOptions"
							:key="opt.value"
							:label="opt.label"
							:value="opt.value" />
					</el-select>
				</el-form-item>
			</el-col>

			<!-- 设备名称  -->
			<el-col :span="12">
				<el-form-item
					label="设备名称"
					prop="equipmentId"
					:rules="[
						{ required: true, message: '设备不能为空', trigger: 'blur' },
					]">
					<el-select
						v-model="form.equipmentId"
						:placeholder="`请选择设备`"
						:disabled="disabled"
						filterable
						clearable
						@change="$emit('update', form)">
						<el-option
							v-for="opt in equipmentOptions"
							:key="opt.value"
							:label="opt.label"
							:value="opt.value" />
					</el-select>
				</el-form-item>
			</el-col>

			<!-- 故障发生时间  -->
			<el-col :span="12">
				<el-form-item
					label="故障发生时间"
					prop="faultTime"
					:rules="[
						{
							required: true,
							message: '故障发生时间不能为空',
							trigger: 'blur',
						},
					]">
					<el-date-picker
						v-model="form.faultTime"
						type="datetime"
						:disabled="disabled"
						:placeholder="`请选择故障发生时间`"
						value-format="timestamp"
						format="yyyy-MM-dd HH:mm:ss"
						clearable
						@change="$emit('update', form)" />
				</el-form-item>
			</el-col>

			<!-- 故障级别  -->
			<el-col :span="12">
				<el-form-item
					label="故障级别"
					prop="faultLevel"
					:rules="[
						{
							required: true,
							message: '故障级别不能为空',
							trigger: 'blur',
						},
					]">
					<el-select
						v-model="form.faultLevel"
						placeholder="故障级别"
						:disabled="disabled"
						@change="$emit('update', form)">
						<el-option
							v-for="opt in getDictDatas(DICT_TYPE.FAULT_LEVEL)"
							:key="opt.value"
							:label="opt.label"
							:value="opt.value" />
					</el-select>
				</el-form-item>
			</el-col>

			<!-- 联系方式  -->
			<el-col :span="24">
				<el-form-item
					label="联系方式"
					prop="repairmanPhone"
					:rules="[
						{ required: true, message: '联系方式不能为空', trigger: 'blur' },
					]">
					<el-input
						v-model="form.repairmanPhone"
						@change="$emit('update', form)"
						placeholder="请输入联系方式"
						:disabled="disabled" />
				</el-form-item>
			</el-col>
		</el-row>
	</el-form>
</template>

<script>
export default {
	name: 'DialogForm',
	model: {
		prop: 'dataForm',
		event: 'update',
	},
	emits: ['update'],
	components: {},
	props: {
		dataForm: {
			type: Object,
			default: () => ({}),
		},
		disabled: {
			type: Boolean,
			default: false,
		},
		labelPosition: {
			type: String,
			default: 'top',
		},
	},
	data() {
		return {
			allSpeicalEquipments: [],
			uploadOpen: false,
			form: {},
			formLoading: true,
			dataLoaded: false,
			equipmentList: [],
			equipmentOptions: [],
			equipmentTypeOptions: [
				{ label: '安全设备', value: 1 },
				{ label: '消防设备', value: 2 },
				{ label: '特种设备', value: 3 },
			],
			workerOptions: [],
			planOptions: [],
		};
	},
	watch: {
		dataForm: {
			handler(val) {
				this.form = JSON.parse(JSON.stringify(val));
				if (this.form.equipmentCategory != null) {
					setTimeout(() => {
						this.equipmentOptions = this.equipmentList
							.filter((item) => item.special)
							.filter(
								(item) => item.specialType === this.form.equipmentCategory
							)
							.map((item) => ({ label: item.name, value: item.id }));
					}, 1000);
				}
				if (this.hasFiles) {
					if (typeof this.hasFiles == 'boolean' && this.hasFiles) {
						this.form.files = this.form.files ?? [];
					} else if (Array.isArray(this.hasFiles)) {
						this.hasFiles.forEach((prop) => {
							this.form[prop] = this.form[prop] ?? [];
						});
					}
				}
			},
			deep: true,
			immediate: true,
		},
	},
	mounted() {
		this.initOptions();
		// this.getCode('/base/equipment-maintain-plan/getCode');
	},
	methods: {
		/** 模拟透传 ref  */
		validate(cb) {
			return this.$refs.form.validate(cb);
		},
		resetFields(args) {
			return this.$refs.form.resetFields(args);
		},
		// getCode
		// async getCode(url) {
		// 	this.formLoading = true;
		// 	const response = await this.$axios(url);
		// 	this.formLoading = false;
		// 	this.form.code = response.data || '';
		// },

		// initialize
		async initOptions() {
			this.initEquipment();
			// this.initWorker();
			// this.initPlan();
		},

		async initEquipment(type = 'special-equipment') {
			this.formLoading = true;
			const response = await this.$axios('/base/core-equipment/listAll');
			this.equipmentList = response.data || [];
			const equipmentOptions = (response.data || [])
				.filter((item) => (type == 'special-equipment' ? item.special : true))
				.map((item) => ({
					label: item.name,
					value: item.id,
				}));
			this.equipmentOptions = equipmentOptions;
			this.allSpeicalEquipments = equipmentOptions;
			this.formLoading = false;
		},

		// async initWorker() {
		// 	this.formLoading = true;
		// 	const response = await this.$axios({
		// 		url: '/base/core-worker/listAll',
		// 	});
		// 	this.workerOptions = (response.data || []).map((item) => ({
		// 		label: item.name,
		// 		value: item.id,
		// 	}));
		// 	this.formLoading = false;
		// },
		// async initPlan() {
		// 	this.formLoading = true;
		// 	const response = await this.$axios({
		// 		url: '/base/equipment-maintain-plan/page',
		// 		params: { pageNo: 1, pageSize: 100, speical: true },
		// 	});
		// 	this.planOptions = (response.data?.list || []).map((item) => ({
		// 		label: item.name,
		// 		value: item.id,
		// 	}));
		// 	this.formLoading = false;
		// },

		// handlers
		handleEqTypeChange(type) {
			this.form.equipmentId = null;
			if (type) {
				this.equipmentOptions = this.equipmentList
					.filter((item) => item.special)
					.filter((item) => item.specialType === type)
					.map((item) => ({ label: item.name, value: item.id }));
			} else
				this.equipmentOptions = this.equipmentList.map((item) => ({
					label: item.name,
					value: item.id,
				}));
			// this.$emit('update', this.form)
		},

		handleEqChange() {
			this.$emit('update', this.form);
		},

		// 修改 是否计划保养 时
		handlePlanChange(val) {
			console.log('handlePlanChange', val);
			this.form.equipmentCategory = null;
			this.form.equipmentId = null;
			this.$emit('update', { ...this.form, relatePlan: val });
			this.initEquipment(val == 1 ? 'special-equipment' : null);
		},

		handleEditorInput(html) {
			this.$emit('update', {
				...this.form,
				maintenanceDes: html,
			});
		},

		// upload
		handleFilesOpen() {},

		beforeUpload() {},

		handleUploadSuccess(response, file) {
			this.$modal.msgSuccess('上传成功');
			console.log('file', file);
			this.form.files.push({
				fileName: file.name,
				fileUrl: response.data,
			});
			this.$emit('update', this.form);
		},

		handleDeleteFile(file) {},
	},
};
</script>

<style scoped lang="scss">
.el-date-editor,
.el-select {
	width: 100%;
}

.upload-area {
	position: relative;
	overflow: hidden;
	transition: height 0.3s ease-out;
}

.upload-in-dialog {
	margin-right: 24px;
	position: relative;
	float: left;
}

.close-icon {
	position: absolute;
	top: 0;
	right: 12px;
	z-index: 100;
	transition: transform 0.3s ease-out;
}

.close-icon.open {
	transform: rotateZ(90deg);
}

.dialog__upload_component__close {
	color: #ccc;
}
.dialog__upload_component__close:hover {
	/* color: #777; */
	color: red;
}

.height-48 {
	height: 35px !important;
}

:deep(.record-add__editor) {
	.ql-picker-label {
		display: flex;
	}
}
</style>
