<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button
					type="primary"
					icon="el-icon-plus"
					@click="table_add"
					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>
			</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"
				></el-table-column>
				<el-table-column
					label="工艺路线"
					prop="material_out_name"
					min-width="130"
				>
				</el-table-column>
				<el-table-column
					label="预计工时"
					prop="hour_work"
					width="80"
				></el-table-column>
				<el-table-column
					label="生产设备"
					prop="equipment_name"
					min-width="150"
				></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="处理人"
					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">
						<el-icon v-if="scope.row.submit_time != null" color="green">
							<CircleCheckFilled />
						</el-icon>
					</template>
				</el-table-column>
				<el-table-column
					label="提交时间"
					prop="submit_time"
				></el-table-column>
				<el-table-column
					label="操作"
					fixed="right"
					align="center"
					width="150"
				>
					<template #default="scope">
						<!-- <el-button
							link
							size="small"
							v-auth="'mlog.update'"
							v-if="scope.row.submit_time == null"
							type="primary"
							@click.stop="table_edit(scope.row)"
							>编辑</el-button
						> -->
						<el-button
							link
							size="small"
							@click="table_detail(scope.row)"
							type="primary"
							>详情</el-button
						>
						<el-button
							link
							size="small"
							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
							link
							v-else
							size="small"
							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="processId"
			:mgroup="mgroupId"
			:dept="deptId"
			:mgroup_name = "mgroup_name"
			@success="handleSaveSuccess"
			@closed="dialog.save = false"
		>
		</save-dialog>
		<detail-drawer
			v-if="dialog.detail"
			ref="detailDialog"
			:mlogId="mlogId"
			:mtask="mtask"
			@closed="detailClose"
		>
		</detail-drawer>
	</el-container>
</template>
<script>
import saveDialog from "./mlog_form.vue";
import detailDrawer from "./mlog_detail.vue";

export default {
	props: {
		mgroupName: {
			type: String,
			default: "",
		},
		mgroupId:{
			type: String,
			default: "",
		}
	},
	name: "mlog",
	components: {
		saveDialog,
		detailDrawer,
	},
	data() {
		return {
			apiObj: null,
			params: { mgroup: "" },
			query: {},
			dialog: {
				save: false,
				detail: false,
			},
			tableData: [],
			selection: [],
			mtask: "",
			mlogId: "",
			deptId: null,
			processId: "",
			processCate: "",
			mgroup_name:''
		};
	},
	watch: {
		mgroupName: {
			handler: function (newval,odlval) {
				let that = this;
				that.params.mgroup = "";
				that.apiObj = null;
				that.mgroup_name  = newval;
				// that.getMgroupInfo();
			},
		},
	},
	mounted() {
		let that = this;
		that.params.mgroup =that.mgroupId;
		that.apiObj = that.$API.wpm.mlog.list;
		// this.mgroup_name  = this.mgroupName;
		// this.getMgroupInfo();
		// console.log("mgroupName",this.mgroupName);
	},
	methods: {
		getMgroupInfo(){
			let that = this;
			that.$API.mtm.mgroup.item.req(that.mgroupId).then((res) => {
				if (res.length < 1) {
					that.$message.error("获取工段错误");
					return;
				}
				that.mgroupId = res.id;
				that.deptId = res.belong_dept;
				that.processId = res.process;
				that.processCate = res.process_cate;
				that.params.mgroup = res.id;
				that.apiObj = that.$API.wpm.mlog.list;
				that.$refs.table.refresh();
			});
		},
		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();
				console.log('nowTime',nowTime)
				console.log('startTiem',startTiem)
				let remaTime = nowTime - startTiem;
				console.log('remaTime',remaTime)
				let temp = Math.floor(remaTime/1000*60);
				console.log('temp',temp)
				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_add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("add");
			});
		},
		//编辑日志
		table_edit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				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
				}
				
			}
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		handleQuery() {
			this.$refs.table.queryData(this.query);
		},
		//本地更新数据
		//新增岗位后更新数据
		handleSaveSuccess(data, mode) {
			this.dialog.save = true;
			this.$refs.table.refresh();
		},
	},
};
</script>
<style scoped>

</style>
