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

<template>
	<el-form
		ref="form"
		:model="form"
		:size="size"
		:label-position="labelPosition"
		v-loading="formLoading">
		<el-row :gutter="20">
			<el-col :span="12">
				<el-form-item
					label="保养计划名称"
					prop="name"
					:rules="[
						{ required: true, message: '请输入保养计划名称', trigger: 'blur' },
					]">
					<el-input
						v-model="form.name"
						@change="$emit('update', form)"
						:placeholder="`请输入保养计划名称`"
						:disabled="disabled" />
				</el-form-item>
			</el-col>

			<el-col :span="12">
				<el-form-item
					label="保养计划编号"
					prop="code"
					:rules="[
						{ required: true, message: '请输入保养计划编号', trigger: 'blur' },
					]">
					<el-input
						v-model="form.code"
						@change="$emit('update', form)"
						:placeholder="`请输入保养计划编号`"
						:disabled="disabled" />
				</el-form-item>
			</el-col>

			<el-col :span="12">
				<el-form-item label="部门" prop="departmentId">
					<el-select
						v-model="form.departmentId"
						:placeholder="`请选择部门`"
						:disabled="disabled"
						clearable
						filterable
						@change="$emit('update', form)">
						<el-option
							v-for="opt in departmentOptions"
							:key="opt.value"
							:label="opt.label"
							:value="opt.value" />
					</el-select>
				</el-form-item>

				<!-- <el-form-item
					label="设备大类"
					prop="equipmentCategory"
					:rules="[
						{ required: true, message: '请选择设备大类', trigger: 'blur' },
					]">
					<el-select
						v-model="form.equipmentCategory"
						:placeholder="`请选择设备大类`"
						:disabled="disabled"
						@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="lineId">
					<el-select
						v-model="form.lineId"
						:placeholder="`请选择产线`"
						:disabled="disabled"
						clearable
						filterable
						@change="$emit('update', form)">
						<el-option
							v-for="opt in lineOptions"
							:key="opt.value"
							:label="opt.label"
							:value="opt.value" />
					</el-select>
				</el-form-item>
				<!-- <el-form-item
					label="设备"
					prop="equipmentId"
					:rules="[{ required: true, message: '请选择设备', trigger: 'blur' }]">
					<el-select
						v-model="form.equipmentId"
						:placeholder="`请选择设备`"
						:disabled="disabled"
						@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="maintenancePeriod"
					:rules="[
						{
							type: 'number',
							message: '请输入正确的数字',
							trigger: 'blur',
							transform: (val) =>
								Number(val) && parseInt(val) === Number(val) && Number(val),
						},
						{ required: true, message: '保养频率不能为空', trigger: 'blur' },
					]">
					<el-input
						v-model="form.maintenancePeriod"
						@change="$emit('update', form)"
						:placeholder="`请输入保养频率`"
						:disabled="disabled" />
				</el-form-item>
			</el-col>

			<el-col :span="12">
				<el-form-item
					label="确认时限(h)"
					prop="confirmTimeLimit"
					:rules="[
						{ required: true, message: '请输入确认时限', trigger: 'blur' },
						{
							type: 'number',
							message: '请输入正确的数字',
							trigger: 'blur',
							transform: (val) => Number(val),
						},
					]">
					<el-input
						v-model="form.confirmTimeLimit"
						@change="$emit('update', form)"
						:placeholder="`请输入确认时限(小时)`"
						:disabled="disabled" />
				</el-form-item>
			</el-col>

			<!-- <el-col :span="12">
				<el-form-item label="保养类型" prop="maintainType">
					<el-select
						v-model="form.maintainType"
						:placeholder="`请选择保养类型`"
						:disabled="disabled"
						@change="$emit('update', form)">
						<el-option
							v-for="opt in getDictDatas(DICT_TYPE.MAINTAIN_TYPE)"
							:key="opt.value"
							:label="opt.label"
							:value="+opt.value" />
					</el-select>
				</el-form-item>
			</el-col> -->

			<el-col :span="12">
				<el-form-item
					label="保养时长(h)"
					prop="maintainDuration"
					:rules="[
						{ required: true, message: '保养时长不能为空', trigger: 'blur' },
						{
							type: 'number',
							message: '请输入正确的数字',
							trigger: 'blur',
							transform: (val) => Number(val),
						},
					]">
					<el-input
						v-model="form.maintainDuration"
						@change="$emit('update', form)"
						:placeholder="`请输入保养时长(小时)`"
						:disabled="disabled" />
				</el-form-item>
			</el-col>

			<el-col :span="12">
				<el-form-item
					label="首次保养时间"
					prop="firstMaintenanceTime"
					:rules="[
						{ required: true, message: '请选择首次保养时间', trigger: 'blur' },
					]">
					<el-date-picker
						v-model="form.firstMaintenanceTime"
						type="datetime"
						:disabled="edit"
						:placeholder="`请选择首次保养时间`"
						@change="$emit('update', form)"
						value-format="timestamp"></el-date-picker>
				</el-form-item>
			</el-col>

			<el-col :span="12">
				<el-form-item
					label="计划保养人员"
					prop="maintainer"
					:rules="[
						{ required: true, message: '请选择计划保养人员', trigger: 'blur' },
					]">
					<el-select
						v-model="form.maintainer"
						:placeholder="`请选择计划保养人员`"
						:disabled="disabled"
						multiple
						clearable
						filterable
						@change="$emit('update', form)">
						<el-option
							v-for="opt in maintainerOptions"
							: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="remark">
					<el-input
						v-model="form.remark"
						@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: {
		rows: {
			type: Array,
			default: () => [],
		},
		dataForm: {
			type: Object,
			default: () => ({}),
		},
		disabled: {
			type: Boolean,
			default: false,
		},
		edit: {
			type: Boolean,
			default: false,
		},
		hasFiles: {
			type: Boolean | Array,
			default: false,
		},
		labelPosition: {
			type: String,
			default: 'right',
		},
		size: {
			type: String,
			default: '',
		},
	},
	data() {
		return {
			form: {},
			formLoading: true,
			equipmentTypeOptions: [
				{ label: '安全设备', value: 1 },
				{ label: '消防设备', value: 2 },
				{ label: '特种设备', value: 3 },
			],
			lineList: [],
			maintainerList: [],
			departmentList: [],
		};
	},
	watch: {
		dataForm: {
			handler(val) {
				this.form = JSON.parse(JSON.stringify(val));
				if (typeof val.maintainer == 'string')
					this.form.maintainer = val.maintainer.split(',');
				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.edit && this.getCode('/base/equipment-maintain-plan/getCode');
		this.getList('maintainer');
		this.getList('department');
		this.getList('line');
	},
	computed: {
		departmentOptions() {
			return (this.departmentList || []).map((item) => ({
				id: item.id,
				label: item.name,
				value: item.id,
			}));
		},
		lineOptions() {
			return (this.lineList || []).map((item) => ({
				id: item.id,
				label: item.name,
				value: item.id,
			}));
		},
		maintainerOptions() {
			return (this.maintainerList || []).map((item) => ({
				id: item.id,
				label: item.name,
				value: item.name,
			}));
		},
	},
	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 getEquipmentList() {
			this.formLoading = true;
			const response = await this.$axios('/base/core-equipment/listAll');
			this.equipmentList = response.data || [];
			this.equipmentOptions = (response.data || []).map((item) => ({
				label: item.name,
				value: item.id,
			}));
			this.formLoading = false;
		},
		async getList(source = 'department') {
			const urls = [
				'/base/core-production-line/listAll',
				'/base/core-department/listAll',
				'/base/core-worker/listAll',
			];
			let res;
			switch (source) {
				case 'department':
					res = await this.$axios(urls[1]);
					this.departmentList = res.data || [];
					break;
				case 'maintainer':
					res = await this.$axios(urls[2]);
					this.maintainerList = res.data || [];
					break;
				case 'line':
					res = await this.$axios(urls[0]);
					this.lineList = res.data || [];
					break;
			}
			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)
		},
	},
};
</script>

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