<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:size="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-container v-loading="loading">
			<el-main style="padding: 0 20px 20px 20px">
				<el-form
					ref="dialogForm"
					:model="form"
					:rules="rules"
					label-position="right"
					label-width="80px"
					style="padding: 0 10px"
				>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="生产类型" prop="mtype">
								<el-select
									v-model="form.mtype"
									placeholder="生产类型"
									clearable
									:disabled="mode == 'edit'"
									style="width: 100%"
								>
									<el-option
										v-for="item in typeOptions"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item
								label="外协单位"
								prop="supplier"
								v-if="form.mtype == 20"
							>
								<el-select
									v-model="form.supplier"
									placeholder="外协单位"
									clearable
									:disabled="mode == 'edit'"
									style="width: 100%"
								>
									<el-option
										v-for="item in supplierOptions"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="工艺路线" prop="route">
								<el-select
									v-model="form.route"
									placeholder="工艺路线"
									clearable
									filterable
									style="width: 100%"
									:disabled="mode == 'edit'"
								>
									<el-option
										v-for="item in routeOptions"
										:key="item.id"
										:label="item.process_name"
										:value="item.id"
									>
										<span
											>{{ item.routepack_name }}-{{
												item.process_name
											}}</span
										>
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="生产设备">
								<el-select
									v-model="form.equipment"
									placeholder="生产设备"
									clearable
									filterable
									:disabled="mode == 'edit'"
									style="width: 100%"
								>
									<el-option
										v-for="item in options"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									>
									<span style="float:left">{{item.name}}</span>
									<span style="float:right">{{item.number}}</span>
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item
								label="开始时间"
								prop="work_start_time"
							>
								<el-date-picker
									:disabled="mode == 'edit'"
									v-model="form.work_start_time"
									type="datetime"
									value-format="YYYY-MM-DD HH:mm:ss"
									style="width: 100%"
								/>
							</el-form-item>
						</el-col>

						<el-col :md="12" :sm="24">
							<el-form-item label="结束时间" prop="work_end_time">
								<el-date-picker
									v-model="form.work_end_time"
									type="datetime"
									value-format="YYYY-MM-DD HH:mm:ss"
									style="width: 100%"
									:disabledDate="disabledDateFn"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="mgroupName=='黑化'||mgroupName=='退火'">
							<el-form-item label="保温时间" required>
								<el-input-number
									v-model="form.reminder_interval_list[0]"
									:min="0"
									:disabled="mode == 'edit'"
									controls-position="right"
								/>(min)
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="mgroupName=='黑化'||mgroupName=='退火'">
							<el-form-item label="冷却时间" required>
								<el-input-number
									v-model="form.reminder_interval_list[1]"
									:min="0"
									:disabled="mode == 'edit'"
									controls-position="right"
								/>(min)
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="操作人">
								<ehsSelect
									v-model="form.handle_user"
									:showName="form.handle_user_name"
									:apiObj="this.$API.system.user.list"
									:params="{ depts: dept }"
								></ehsSelect>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="备注">
								<el-input
									v-model="form.note"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit"
					>保存</el-button
				>
				<el-button @click="visible = false">取消</el-button>
			</el-footer>
		</el-container>
	</el-dialog>
</template>

<script>
const defaultForm = {
	mtype: "",
	supplier:'',
	route: "",
	equipment: "",
	handle_user: null,
	work_start_time: "",
	work_end_time:'',
	note:'',
	reminder_interval_list: [0,0],
};

export default {
	props: {
		process: {
			type: String,
			default: "",
		},
		mgroupName:{
			type: String,
			default: "",
		},
		mgroup: {
			type: String,
			default: "",
		},
		dept: {
			type: String,
			default: "",
		},
	},
	computed: {
		title() {
			return this.titleMap[this.mode];
		},
	},
	emits: ["success", "closed"],

	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增日志",
				edit: "编辑日志",
				show: "查看日志",
			},
			//表单数据
			form: Object.assign({}, defaultForm),
			//验证规则
			rules: {
				supplier: [
					{
						required: true,
						message: "请选择供应商",
						trigger: "blur",
					},
				],
				work_start_time: [
					{
						required: true,
						message: "请选择生产开始时间",
						trigger: "blur",
					},
				],
				route: [
					{
						required: true,
						message: "请选择工艺路线",
						trigger: "blur",
					},
				],
				mtype: [
					{
						required: true,
						message: "请选择生产类型",
						trigger: "blur",
					},
				],
			},
			typeOptions: [
				{ id: 10, name: "自产" },
				{ id: 20, name: "外协" },
			],
			visible: false,
			isSaveing: false,
			options: [],
			routeOptions: [],
			userOptions: [],
			supplierOptions: [],
			setFiltersVisible: false,
		};
	},
	mounted() {
		this.getRoute();
		this.getUser();
		this.getSupplier();
		this.getEquipment();
		
	},
	methods: {
		disabledDateFn(time) {
			return time.getTime() > new Date().getTime();
		},
		//获取
		getUser() {
			this.$API.system.user.list.req({ depts: this.dept }).then((res) => {
				this.userOptions = res.results;
			});
		},
		getEquipment() {
			this.$API.em.equipment.list
				.req({ page: 0, type: 10,belong_dept:this.dept })
				.then((res) => {
					this.options = res;
				});
		},
		getSupplier() {
			let that = this;
			let obj = {};
			obj.page = 0;
			obj.can_outsource = true;
			that.$API.pum.supplier.list.req(obj).then((res) => {
				that.supplierOptions = res;
			});
		},
		getRoute(id) {
			let that = this;
			that.$API.mtm.route.list
				.req({ process: that.process, page: 0, routepack__state: 30 })
				.then((res) => {
					that.routeOptions = res;
				});
		},
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单注入数据
		setData(data) {
			console.log("data", data);
			Object.assign(this.form, data);
			this.getRoute(data.id);
		},

		//表单提交方法
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					that.form.mgroup = that.mgroup;
					if (that.mode === "add") {
						that.$API.wpm.mlog.init.req(that.form).then((res) => {
							that.isSaveing = false;
							that.$emit("success");
							that.visible = false;
							that.$message.success("操作成功");
						});
					} else {
						let obj = {};
						obj.work_end_time = that.form.work_end_time;
						obj.handle_user = that.form.handle_user;
						obj.note = that.form.note;
						that.$API.wpm.mlog.change
							.req(that.form.id, that.form)
							.then((res) => {
								that.isSaveing = false;
								that.$emit("success");
								that.visible = false;
								that.$message.success("操作成功");
							});
					}
				}
			});
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style>
.form_unit {
	position: absolute;
	right: -25px;
}
</style>
