<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		style="width: 80%;"
		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="关联任务">
								<el-select
									v-model="form.mtask"
									placeholder="关联任务"
									clearable
									style="width: 100%"
									@change="changeMtask"
								>
									<el-option
										v-for="item in options"
										:key="item.id"
										:label="item.number"
										:value="item.id"
									>
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="工艺步骤">
								<el-select
									v-model="form.route"
									placeholder="工艺步骤"
									clearable
									filterable
									style="width: 100%"
								>
									<el-option
										v-for="item in routeOptions"
										:key="item.id"
										:label="item.name + (item.routepack_name ? '（' + item.routepack_name + '）' : '')"
										:value="item.id"
									>
										<span style="display: flex; width: 100%; min-width: 0" :title="item.name + (item.routepack_name ? '（' + item.routepack_name + '）' : '')">
											<span style="flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap">{{ item.name }}</span>
											<span v-if="item.routepack_name" style="max-width: 45%; flex-shrink: 0; overflow: hidden; color: var(--el-text-color-secondary); text-overflow: ellipsis; white-space: nowrap">（{{ item.routepack_name }}）</span>
										</span>
									</el-option>
								</el-select>
							</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 = {
	route: "",
	mtask:"",
	mgroup:"",
	note:'',
	is_fix:false,
};

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: {
				mtask: [
					{
						required: true,
						message: "请选择任务",
						trigger: "blur",
					},
				],
			},
			is_fix:false,
			visible: false,
			isSaveing: false,
			options: [],
			routeOptions: [],
			setFiltersVisible: false,
		};
	},
	mounted() {
		this.$nextTick(() => {
		    this.getRoute();
			this.getMtask();
		})
		
	},
	methods: {
		disabledDateFn(time) {
			return time.getTime() > new Date().getTime();
		},
		getMtask() {
			let that = this;
			this.$API.pm.mtask.list
				.req({ page: 0, mgroup: that.mgroup, state: 20 })
				.then((res) => {
					that.options = 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;
					if(res.length==1){
						that.form.route = res[0].id;
					}
				});
		},
		//显示
		open(mode = "add",text) {
			this.mode = mode;
			if(text){
				this.is_fix = true;
			}
			this.visible = true;
			return this;
		},
		//表单注入数据
		setData(data) {
			console.log("data", data);
			Object.assign(this.form, data);
			this.getRoute(data.id);
		},
		changeMtask(){
			let that = this;
			that.options.forEach((item) => {
			    if(item.id == that.form.mtask){
					that.form.route = item.route;
				}
			})
		},
		//表单提交方法
		submit() {
			let that = this;
			console.log(this.form)
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					if (that.mode === "add") {
						that.form.is_fix = that.is_fix;
						that.form.mgroup = that.mgroup;
						that.$API.wpm.fmlog.create.req(that.form).then((res) => {
							that.isSaveing = false;
							that.$emit("success");
							that.visible = false;
							that.$message.success("操作成功");
						}).catch(()=>{
							that.isSaveing = false;
						});
					} else {
						that.$API.wpm.fmlog.update.req(that.form.id, that.form).then((res) => {
							that.isSaveing = false;
							that.$emit("success");
							that.visible = false;
							that.$message.success("操作成功");
						}).catch(()=>{
							that.isSaveing = false;
						});
					}
				}
			});
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

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