<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_add2"
					v-auth="'mlog.create'"
				>快速报工</el-button>
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_add"
					v-auth="'mlog.create'"
				>新增</el-button>
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_rework"
					v-auth="'mlog.create'"
				>返工</el-button>
			</div>
			
			<div class="right-panel">
				<el-input
					style="margin-right: 5px"
					v-model="query.search"
					placeholder="名称"
					clearable
				></el-input>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="handleQuery"
				></el-button>
				<el-checkbox
					v-model="showHidden"
					label="已提交"
					@change="hiddenChange"
				/>
			</div>
		</el-header>
		<el-main>
			<scTable
				ref="table"
				:apiObj="apiObj"
				row-key="id"
				:params="params"
				:query="params"
				@row-click="table_detail"
			>
				<el-table-column
					label="#"
					type="index"
					width="50"
					fixed
				></el-table-column>
				<el-table-column
					label="工艺路线"
					prop="material_out_name"
					min-width="130"
					fixed
				>
				<template #default="scope">
					<el-tag v-if="scope.row.is_fix" type="warning">返工</el-tag>
					<span v-else>{{scope.row.material_out_name}}</span>
				</template>
				</el-table-column>
				<el-table-column
					label="预计工时"
					prop="hour_work"
					width="80"
				></el-table-column>
				<el-table-column label="批次号" min-width="100">
					<template #default="scope">
						<div v-if="scope.row.mlogb_full!==null">
							<span v-for="item in scope.row.mlogb_full" :key="item.id">
								<span v-if="item.material_in!==null">{{ item.batch }}、</span>
							</span>
						</div>
					</template>
				</el-table-column>
				<el-table-column
					label="生产设备"
					prop="equipment_name"
					min-width="130"
				></el-table-column>
				<el-table-column label="进度" prop="belong_dept_name"  v-if="mgroup_name=='黑化'||mgroup_name=='退火'">
					<template #default="scope">
						<el-progress id="progressbwlq" :percentage="customMethod(scope.row,1)" :stroke-width="20" :color="customMethod(scope.row,2)" :text-inside="true"><span>{{customMethod(scope.row,3)}}</span></el-progress>
					</template>
				</el-table-column>
				<el-table-column
					label="部门/工段"
					prop="belong_dept_name"
				>
					<template #default="scope">
						{{scope.row.belong_dept_name}}/{{scope.row.mgroup_name}}
					</template>
				</el-table-column>
				<el-table-column label="是否保温" v-if="route_code=='zlybcl'">
					<template #default="scope">
						<span v-if="scope.row.oinfo_json_">{{scope.row.oinfo_json_.是否保温}}</span>
					</template>
				</el-table-column>
				<el-table-column label="温度" v-if="route_code=='zlybcl'">
					<template #default="scope">
						<span v-if="scope.row.oinfo_json_">{{scope.row.oinfo_json_.温度}}</span>
					</template>
				</el-table-column>
				<el-table-column label="压力" v-if="route_code=='zlybcl'">
					<template #default="scope">
						<span v-if="scope.row.oinfo_json_">{{scope.row.oinfo_json_.压力}}</span>
					</template>
				</el-table-column>
				<el-table-column label="刻度" v-if="route_code=='zlybcl'">
					<template #default="scope">
						<span v-if="scope.row.oinfo_json_">{{scope.row.oinfo_json_.刻度}}</span>
					</template>
				</el-table-column>
				<el-table-column label="压板用时" v-if="route_code=='zlybcl'">
					<template #default="scope">
						<span v-if="scope.row.oinfo_json_">{{scope.row.oinfo_json_.压板用时}}</span>
					</template>
				</el-table-column>
				<el-table-column
					label="处理人"
					prop="handle_user_name"
					width="80"
				></el-table-column>
				<el-table-column label="保温剩余时间" v-if="mgroup_name=='黑化'||mgroup_name=='退火'">
					<template #default="scope">
						{{ getRemaTime(scope.row) }}
					</template>
				</el-table-column>
				<el-table-column
					label="开始时间"
					prop="work_start_time"
				></el-table-column>
				<el-table-column
					label="结束时间"
					prop="work_end_time"
				></el-table-column>
				<el-table-column label="提交人">
					<template #default="scope">
						<span v-if="scope.row.submit_user_name">{{ scope.row.submit_user_name }}</span>
					</template>
				</el-table-column>
				<el-table-column
					label="提交时间"
					prop="submit_time"
				></el-table-column>
				<el-table-column
					label="操作"
					fixed="right"
					align="center"
					width="220"
				>
					<template #default="scope">
						<el-button
							type="success"
							v-if="route_code=='ladansi'||route_code=='yicifusi'||route_code=='ercifusi'||route_code=='zlybcl'"
							@click.stop="table_monitor(scope.row)"
							>监测</el-button
						>
						<el-button
							@click="table_detail(scope.row)"
							type="primary"
							>详情</el-button
						>
						<el-button
							v-auth="'mlog.delete'"
							type="danger"
							v-if="scope.row.submit_time == null"
							@click.stop="table_del(scope.row, scope.$index)"
							>删除</el-button
						>
						<el-button
							v-else
							type="danger"
							@click.stop="mlogRevert(scope.row)"
							>撤回</el-button
						>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<save-dialog
			v-if="dialog.save"
			ref="saveDialog"
			:process="process"
			:mgroup="mgroupId"
			:dept="deptId"
			:mgroupName = "mgroupName"
			:mgroupMtype="mgroupMtype"
			@success="handleSaveSuccess"
			@closed="dialog.save = false"
		>
		</save-dialog>
		<saves-dialog
			v-if="dialog.save2"
			ref="saveDialog2"
			:process="process"
			:mgroup="mgroupId"
			:dept="deptId"
			:mgroupName = "mgroupName"
			:mgroupMtype="mgroupMtype"
			@success="handleSaveSuccess2"
			@closed="dialog.save2 = false"
		>
		</saves-dialog>
		<detail-drawer
			v-if="dialog.detail"
			ref="detailDialog"
			:mlogId="mlogId"
			:process="process"
			:mtask="mtask"
			:dept = "deptId"
			:mgroup="mgroupId"
			:processType="processType"
			@closed="detailClose"
		>
		</detail-drawer>
		<monitor-drawer
			v-if="dialog.monitor"
			ref="monitorDialog"
			:endTime="endTime"
			:startTime="startTime"
			:equipmentId="equipmentId"
		></monitor-drawer>
	</el-container>
</template>
<script>
import saveDialog from "./mlog_form.vue";
import savesDialog from "./mlog_form2.vue";
import detailDrawer from "./mlog_detail.vue";
import monitorDrawer from "./monitor_detail.vue";

export default {
	props: {
		mgroupName: {
			type: String,
			default: "",
		},
		mgroupId:{
			type: String,
			default: "",
		},
		deptId: {
			type: String,
			default: "",
		},
		process: {
			type: String,
			default: "",
		},
		mgroup_code: {
			type: String,
			default: "",
		},
		mgroupMtype: {
		    type: Number,
			default: 10,
		},
		processType: {
		    type: String,
			default: "",
		}
	},
	name: "mlog",
	components: {
		saveDialog,
		detailDrawer,
		monitorDrawer,
		savesDialog
	},
	data() {
		return {
			apiObj: null,
			params: { mgroup: "" ,submit_time__isnull:true,with_mlogb:'yes',
				query: " { id,is_fix,equipment, material_out_name, equipment_name, reminder_interval_list ,mstate_json,belong_dept_name, mgroup_name,handle_user_name,work_start_time,work_end_time,submit_time,oinfo_json,mlogb_full,submit_user_name}"
			},
			query: {submit_time__isnull:true},
			dialog: {
				save: false,
				save2: false,
				detail: false,
				monitor:false,
			},
			tableData: [],
			selection: [],
			mtask: "",
			mlogId: "",
			mgroup_name: "",
			route_code: "",
			equipmentId: "",
			showHidden:false,
		};
	},
	watch: {
		mgroupName: {
			handler: function (newval,odlval) {
				let that = this;
				that.mgroup_name = newval;
				that.params.mgroup = "";
				that.apiObj = null;
				that.getMgroupInfo();
			},
		},
	},
	mounted() {
		let that = this;
		that.route_code = this.$route.path.split("/")[2];
		that.params.mgroup =that.mgroupId;
		that.apiObj = that.$API.wpm.mlog.list;
		// this.getMgroupInfo();
	},
	methods: {
		getMgroupInfo(){
			let that = this;
			that.$API.mtm.mgroup.item.req(that.mgroupId).then((res) => {
				if (res.length < 1) {
					that.$message.error("获取工段错误");
					return;
				}
				that.deptId = res.belong_dept;
				that.processId = res.process;
				that.mgroupMtype = res.mtype;
				that.params.mgroup = res.id;
				that.apiObj = that.$API.wpm.mlog.list;
			});
		},
		getRemaTime(row){
			if(row.work_start_time !== null){
				let times = 0;
			    let nowTime = new Date().getTime();
				let startTiem = new Date(row.work_start_time).getTime();
				let remaTime = nowTime - startTiem;
				let temp = Math.floor(remaTime/1000*60);
				if(remaTime>0&&row.reminder_interval_list[0]){
					if(temp<row.reminder_interval_list[0]){
						times = row.reminder_interval_list[0]-temp;
					}else{
						return '/';
					}
					return times
				}else{
					return '/';
				}
			}
		},
		detailClose() {
			this.dialog.detail = false;
			this.$refs.table.refresh();
		},
		table_monitor(row){
			let that= this;
			if(row.equipment!==null){
				that.equipmentId = row.equipment;
				that.endTime = row.work_end_time;
				that.startTime = row.work_start_time;
				that.dialog.monitor = true;
				this.$nextTick(() => {
					this.$refs.monitorDialog.open("add");
				});
			}else{
				that.$message.error("该日志未关联设备，请选择设备后重新查看");
			}
		},
		//添加日志
		table_add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add");
			});
		},
		//添加日志
		table_add2() {
			this.dialog.save2 = true;
			this.$nextTick(() => {
				this.$refs.saveDialog2.open("add");
			});
		},
		table_rework(){
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add","rework");
			});
		},
		//编辑日志
		table_edit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				if(row.is_fix){
					this.$refs.saveDialog.open("edit","rework").setData(row);
				}else{
					this.$refs.saveDialog.open("edit").setData(row);
				}
				
			});
		},
		//日志详情
		table_detail(row) {
			this.mlogId = row.id;
			this.dialog.detail = true;
			this.$nextTick(() => {
				this.$refs.detailDialog.open();
			});
		},
		//日志删除
		table_del(row) {
			this.$confirm(`确定删除吗？`, "提示", {
				type: "warning",
			}).then(() => {
				var id = row.id;
				this.$API.wpm.mlog.delete.req(id).then((res) => {
					if (res.err_msg) {
						this.$message.error(res.err_msg);
					} else {
						this.$refs.table.refresh();
						this.$message.success("删除成功");
					}
				});
			});
		},
		//日志撤回
		mlogRevert(row) {
			this.$confirm(`确定撤回该日志吗？`, "提示", {
				type: "warning",
			}).then(() => {
				var id = row.id;
				this.$API.wpm.mlog.revert.req(id).then((res) => {
					if (res.err_msg) {
						this.$message.error(res.err_msg);
					} else {
						this.$refs.table.refresh();
						this.$message.success("撤回成功");
					}
				});
			});
		},
		
		customMethod(row,index){
			let color = '',context = '';
			let percent = 0;
			if(row.reminder_interval_list.length>0){
				let data = new Date();
				//当前时间
				let currentTime = data.getTime();
				//开始时间
				let startTime = new Date(row.work_start_time).getTime();
				//结束时间
				let endTime = new Date(row.work_end_time).getTime();
				//已开始工作的时长
				let times = (currentTime-startTime)/1000/60;
				//总时长
				let sum = row.reminder_interval_list[0]+row.reminder_interval_list[1];
				if(endTime!==0&&(currentTime>endTime)){
					percent = 100;
					color = row.mstate_json[1].color;
					context = '已完成';
				}else{
					if(times>0){
						percent = Math.round((times/sum)*100);
					}else{
						percent = 0;
					}
					if(times>0){
						if(times>row.reminder_interval_list[0]){//冷却中
							color = row.mstate_json[1].color;
							context = row.mstate_json[1].name;
						}else if(times>row.reminder_interval_list[1]){//已完成
							color = row.mstate_json[1].color;
							context = '已完成';
						}else{//保温中
							color = row.mstate_json[0].color;
							context = row.mstate_json[0].name;
						}
					}else{
						color = "#ffffff";
					}
				}
				if(index==1){
					return percent
				}else if(index==2){
					return color
				}else{
					return context
				}
				
			}
		},
		hiddenChange(val) {
			if (val) {
				this.query.submit_time__isnull = false;
			} else {
				this.query.submit_time__isnull = true;
			}
			this.$refs.table.queryData(this.query);
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		handleQuery() {
			this.$refs.table.queryData(this.query);
		},
		//新增后更新数据
		handleSaveSuccess(data, mode) {
			this.dialog.save = true;
			this.$refs.table.refresh();
		},
		handleSaveSuccess2(data) {
			console.log(data);
			this.dialog.save = true;
			this.$refs.table.refresh();
			this.mlogId = data;
			this.dialog.detail = true;
			this.$nextTick(() => {
				this.$refs.detailDialog.open();
			});
		},
	},
};
</script>
<style scoped>

</style>
