<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="12" :xs="24" v-if="mgroupMtype==20">
							<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="12" :xs="24" v-if="mgroupMtype == 20">
							<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-col :md="12" :sm="12" :xs="24" v-if="mode=='add'&&mlogtype!=='rework'">
							<el-form-item label="关联任务" prop="mtask">
								<el-select
									v-model="form.mtask"
									placeholder="关联任务"
									clearable
									style="width: 100%"
									@change="changeMtask"
								>
									<el-option
										v-for="item in mtaskOptions"
										:key="item.id"
										:label="item.number"
										:value="item.id"
									>
									</el-option>
								</el-select>
							</el-form-item>
						</el-col> -->
						<el-col :md="12" :sm="12" :xs="24" v-if="mlogtype!=='rework'">
							<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.routepack_name"
										:value="item.id"
									>
										<span>{{ item.routepack_name }}</span>
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="12" :xs="24" v-if="route_code!=='niuzhuan'">
							<el-form-item label="生产设备">
								<el-select
									v-model="form.equipment"
									placeholder="生产设备"
									clearable
									filterable
									style="width: 100%"
								>
									<el-option
										v-for="item in options"
										:key="item.id"
										:label="item.label"
										: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="12" :xs="24" v-for="item in testitems" :key="item.id">
							<el-form-item :label="item.name">
								<el-input-number
									v-if="item.field_type=='input-number'"
									v-model="item.value"
									:min="0"
									class="width-100"
									controls-position="right"
								>
								</el-input-number>
								<el-input-number
									v-if="item.field_type=='input-int'"
									v-model="item.value"
									:min="0"
									class="width-100"
									controls-position="right"
								>
								</el-input-number>
								<el-input
									v-if="item.field_type=='input-text'"
									v-model="item.value"
									class="width-100"
								>
								</el-input>
								<el-select
									v-if="item.field_type=='select-text'"
									v-model="item.value"
									clearable
									class="width-100"
								>
									<el-option
										v-for="item0 in item.choices"
										:key="item0"
										:label="item0"
										:value="item0"
									>
									</el-option>
								</el-select>
								<el-select
									v-if="item.field_type=='selects-text'"
									v-model="item.value"
									clearable
									multiple
									class="width-100"
								>
									<el-option
										v-for="item1 in item.choices"
										:key="item1"
										:label="item1"
										:value="item1"
									>
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="12" :xs="24" v-if="route_code!=='niuzhuan'">
							<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%"
									popper-append-to-body
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="12" :xs="24" v-if="route_code!=='niuzhuan'">
							<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"
									popper-append-to-body
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="12" :xs="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="12" :xs="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: 10,
	supplier:'',
	route: "",
	equipment: "",
	handle_user: null,
	work_start_time: "",
	work_end_time:null,
	note:'',
	mgroup:'',
	mtask:'',
	oinfo_json:{},
	is_fix:false,
};

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

	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增日志",
				edit: "编辑日志",
				show: "查看日志",
			},
			test_file:'',
			fileList:[],
			//表单数据
			form: Object.assign({}, defaultForm),
			//验证规则
			rules: {
				supplier: [
					{
						required: true,
						message: "请选择供应商",
						trigger: "blur",
					},
				],
				work_start_time: [
					{
						required: true,
						message: "请选择生产开始时间",
						trigger: "blur",
					},
				],
				work_end_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: "外协" },
			],
			mlogtype:"",
			route_code:"",
			visible: false,
			isSaveing: false,
			options: [],
			testitems:[],
			routeOptions: [],
			userOptions: [],
			mtaskOptions:[],
			supplierOptions: [],
			setFiltersVisible: false,
		};
	},
	mounted() {
		this.route_code = this.$route.path.split("/")[2];
		this.form.work_start_time = this.$TOOL.dateFormat(new Date());
		this.getRoute();
		this.getUser();
		this.getSupplier();
		this.getEquipment();
		this.getMtask();
		this.gettestitem();
	},
	methods: {
		disabledDateFn(time) {
			return time.getTime() > new Date().getTime();
		},
		gettestitem(){
			let that = this;
			that.$API.qm.testitem.list.req({ process: that.process,type:20 }).then((res) => {
				res.results.forEach((item) => {
					let obj = {};
					Object.assign(obj, item);
					obj.value = '';
				    that.testitems.push(obj);
				})
			});
		},
		//获取
		getUser() {
			this.$API.system.user.list.req({ depts: this.dept }).then((res) => {
				this.userOptions = res.results;
			});
		},
		getEquipment() {
			let that = this;
			this.$API.em.equipment.list.req({ page: 0, type: 10,mgroup:that.mgroup}).then((res) => {
				res.forEach((item) => {
					let obj = {};
					Object.assign(obj, item);
					obj.label = item.name+'-'+item.number;
					that.options.push(obj);
				});
			});
		},
		//获取任务
		getMtask() {
			let that = this;
			this.$API.pm.mtask.list
				.req({ page: 0, mgroup: that.mgroup, state: 20 })
				.then((res) => {
					that.mtaskOptions = 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",type) {
			this.mode = mode;
			this.visible = true;
			if(type!==''&&type!==undefined&&type!==null){
				this.mlogtype = type;
				this.form.mtype=10;
				this.form.is_fix=true;
			}
			return this;
		},
		//表单注入数据
		setData(data) {
			let that = this;
			console.log("data", data);
			Object.assign(this.form, data);
			if(data.test_file!==null){
				this.form.test_file = data.test_file;
				this.fileList = [{name:data.test_file,url:data.test_file}];
			}
			if(data.oinfo_json!==null){
				if(that.testitems.length>0){
					that.testitems.forEach((item) => {
						item.value = data.oinfo_json[item.id];
					});
				}else{
					setTimeout(() => {
					    that.testitems.forEach((item) => {
							item.value = data.oinfo_json[item.id];
						});
					},500)
				}
			}
			this.getRoute(data.id);
		},
		changeMtask(){
			let that = this;
			that.mtaskOptions.forEach((item) => {
			    if(item.id == that.form.mtask){
					that.form.route = item.route;
				}
			})
		},
		fileUPSuccess(res) {
			let that = this;
			console.log('res',res);
			this.test_file = res.path;
		},
		//表单提交方法
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					that.form.mgroup = that.mgroup;
					let oinfo_json = {};
					that.testitems.forEach((item) => {
					    oinfo_json[item.id] = item.value;
					})
					that.form.oinfo_json = oinfo_json;
					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("操作成功");
						}).catch(()=>{
							that.isSaveing = false;
						});
					} else {
						let obj = {};
						obj.work_end_time = that.form.work_end_time;
						obj.handle_user = that.form.handle_user;
						obj.note = that.form.note;
						if(that.mgroupName=='黑化'){
							that.form.test_file = that.test_file;
						}
						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("操作成功");
						}).catch(()=>{
							that.isSaveing = false;
						});
					}
				}
			});
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

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