<template>
	<el-container>
		<el-drawer
			v-model="visible"
			title="进度详情"
			:size="'90%'"
			destroy-on-close
			@closed="$emit('closed')"
		>
			<!-- 任务信息 -->
			<el-header style="height: 20%; padding: 0">
				<el-card style="width: 100%; height: 100%">
					<template #header>
						<div class="card-header">
							<span>任务信息</span>
						</div>
					</template>
					<el-descriptions>
						<el-descriptions-item label="任务编号">{{
							currentMtask.number
						}}</el-descriptions-item>
						<el-descriptions-item label="产品全称">
							<span v-if="currentMtask.material_out_">{{
								currentMtask.material_out_name
							}}</span>
						</el-descriptions-item>
						<el-descriptions-item label="计划数量">{{
							currentMtask.count
						}}</el-descriptions-item>
						<el-descriptions-item label="计划日期">{{
							currentMtask.start_date
						}}</el-descriptions-item>
						<el-descriptions-item label="合格数">{{
							currentMtask.count_ok
						}}</el-descriptions-item>
					</el-descriptions>
				</el-card>
			</el-header>
			<!-- 日志 -->
			<el-main style="padding: 0">
				<el-container>
					<el-header>
						<div class="left-panel">
							<el-button
								type="primary"
								icon="el-icon-plus"
								@click="addMlog"
								v-if="currentMtask && currentMtask.state == 20"
								v-auth="'mlog.create'"
								>新增</el-button
							>
						</div>
					</el-header>
					<el-main style="padding: 0">
						<scTable
							ref="mtaskMlogs"
							:apiObj="apiObj"
							row-key="id"
							:params="params"
							stripe
							hidePagination
							hideDo
						>
							<el-table-column
								label="工序"
								prop="mgroup_name"
							></el-table-column>
							<el-table-column
								label="产品名称"
								prop="material_out_"
								min-width="120"
							>
								<template #default="scope">
									<span v-if="scope.row.material_out_">{{
										scope.row.material_out_.name
									}}</span>
								</template>
							</el-table-column>
							<el-table-column
								label="批次号"
								prop="batch"
							></el-table-column>
							<el-table-column
								label="加工设备"
								prop="equipment_name"
							></el-table-column>
							<el-table-column label="领用数量" prop="count_use">
							</el-table-column>
							<el-table-column label="加工数量" prop="count_real">
							</el-table-column>
							<el-table-column
								label="合格数量"
								prop="count_ok"
							></el-table-column>
							<el-table-column label="不合格数量" align="center">
								<el-table-column
									prop="count_n_hs"
									label="划伤"
								/>
								<el-table-column
									prop="count_n_zs"
									label="挫伤"
								/>
								<el-table-column
									prop="count_n_zz"
									label="杂质"
								/>
								<el-table-column
									prop="count_n_tw"
									label="条纹"
								/>
								<el-table-column
									prop="count_n_zdd"
									label="锥度大"
								/>
								<el-table-column prop="count_n_d" label="短" />
								<!-- 棒 -->
								<!-- <template v-if="currentMtask.material_out_.name=='光纤预制棒'"> -->
								<template
									v-if="material_name.indexOf('棒') > -1"
								>
									<el-table-column
										prop="count_n_dl"
										label="断裂"
									/>
									<el-table-column
										prop="count_n_qp"
										label="气泡"
									/>
									<el-table-column
										prop="count_n_bl"
										label="不亮"
									/>
									<el-table-column
										prop="count_n_hw"
										label="横纹"
									/>
									<el-table-column
										prop="count_n_yp"
										label="有皮"
									/>
									<el-table-column
										prop="count_n_bp"
										label="爆皮"
									/>
									<el-table-column
										prop="count_n_sc"
										label="色差"
									/>
								</template>
								<!-- 管 -->
								<!-- <template v-if="currentMtask.material_out_.name=='光纤预制管'"> -->
								<template
									v-if="material_name.indexOf('管') > -1"
								>
									<el-table-column
										prop="count_n_qx"
										label="气线"
									/>
									<el-table-column
										prop="count_n_js"
										label="结石"
									/>
									<el-table-column
										prop="count_n_tydd"
										label="椭圆度大"
									/>
									<el-table-column
										prop="count_n_sw"
										label="水雾"
									/>
									<el-table-column
										prop="count_n_bhpcd"
										label="壁厚偏差大"
									/>
									<el-table-column
										prop="count_n_wq"
										label="弯"
									/>
								</template>
							</el-table-column>
							<el-table-column
								label="操作人"
								prop="update_by_name"
							></el-table-column>
							<el-table-column
								label="操作"
								fixed="right"
								width="130"
							>
								<template #default="scope">
									<el-button
										link
										type="primary"
										@click="table_edit(scope.row)"
										v-auth="'mlog.update'"
										v-if="scope.row.submit_time == null"
										>编辑
									</el-button>
									<el-button
										link
										type="danger"
										@click="table_del(scope.row)"
										v-auth="'mlog.delete'"
										v-if="scope.row.submit_time == null"
										>删除
									</el-button>
									<el-button
										link
										type="primary"
										@click="mlog_submit(scope.row)"
										v-auth="'mlog.submit'"
										v-if="scope.row.submit_time == null"
										:loading="mlogHandleLoading"
										>提交
									</el-button>
									<el-button
										link
										type="danger"
										@click="mlog_revert(scope.row)"
										v-auth="'mlog.submit'"
										v-if="scope.row.submit_time != null"
										:loading="mlogHandleLoading"
										>撤回
									</el-button>
									<!-- <el-button
										link
										type="success"
										@click="table_show(scope.row)"
									>
										日志记录
									</el-button> -->
								</template>
							</el-table-column>
						</scTable>
					</el-main>
				</el-container>
			</el-main>
		</el-drawer>
		<save-dialog
			v-if="dialogSave"
			ref="saveDialog"
			:mtask="mtask"
			:mgroup="mgroup"
			:handledate="handledate"
			:material_out="material_out"
			:material_in="material_in"
			:material_name="material_name"
			@success="handleSaveSuccess"
			@closed="dialogSave = false"
		></save-dialog>
		<showDrawer
			ref="showDrawer"
			v-if="visibleDrawer"
			:mlogId="mlogId"
			@closed="visibleDrawer = false"
		>
		</showDrawer>
	</el-container>
</template>
<script>
import saveDialog from "./mlog6_form.vue";
import showDrawer from "./mlog_drawer.vue";
export default {
	components: {
		saveDialog,
		showDrawer,
	},
	props: {
		mtaskId: {
			type: String,
			default: "",
		},
	},
	data() {
		return {
			mlogHandleLoading: false,
			dialog: {
				save: false,
			},
			state_: {
				10: "创建中",
				20: "已下达",
				30: "生产中",
				40: "已提交",
			},
			hideDo: true,
			currentMtask: {},
			apiObj: null,
			params: {
				mtask: "",
			},
			mlogId: "",
			handledate: "",
			material_in: "",
			material_out: "",
			visible: false,
			dialogSave: false,
			visibleDrawer: false,
		};
	},
	mounted() {
		console.log(this.mtaskId);
		this.getMtask();
	},
	methods: {
		open() {
			this.visible = true;
			this.params.mtask = this.mtaskId;
			return this;
		},
		getMtask() {
			let that = this;
			this.$API.pm.mtask.item.req(that.mtaskId).then((res) => {
				that.currentMtask = res;
				this.mgroup = res.mgroup;
				this.mtask = res.id;
				this.material_in = res.material_in;
				this.material_out = res.material_out;
				this.material_name = res.material_out_name;
				this.handledate = res.start_date;

				this.params.mtask = this.mtaskId;
				this.apiObj = this.$API.wpm.mlog.list;
			});
		},
		table_show(row) {
			this.visibleDrawer = true;
			this.mlogId = row.id;
			this.$nextTick(() => {
				this.$refs.showDrawer.open();
			});
		},

		handleQueryMtask() {
			this.$refs.table.refresh();
		},
		//添加
		addMlog() {
			let that = this;
			that.mgroup = that.currentMtask.mgroup;
			that.material_out = that.currentMtask.material_out;
			that.dialogSave = true;
			that.$nextTick(() => {
				that.$refs.saveDialog.open("add");
			});
		},
		//编辑
		table_edit(row) {
			this.dialogSave = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row);
			});
		},
		//删除
		table_del(row) {
			this.$confirm(`确定删除吗？`, "提示", {
				type: "warning",
			})
				.then(() => {
					this.$API.wpm.mlog.delete
						.req(row.id)
						.then((res) => {
							this.$message.success("删除成功");
							this.$refs.mtaskMlogs.refresh();
							return res;
						})
						.catch((err) => {
							return err;
						});
				})
				.catch(() => {});
		},

		//本地更新数据
		handleSaveSuccess(data, mode) {
			this.$refs.mtaskMlogs.refresh();
		},
		resetQuery() {
			this.query = {};
		},
		mlog_submit(row) {
			let that = this;
			this.$API.wpm.mlog.submit
				.req(row.id)
				.then((res) => {
					that.$message.success("操作成功");
					that.getMtask(that.mtask);
					that.$refs.mtaskMlogs.refresh();
				})
				.catch((err) => {});
		},
		mlog_revert(row) {
			let that = this;
			that.mlogHandleLoading = true;
			that.$API.wpm.mlog.revert
				.req(row.id)
				.then((res) => {
					that.$message.success("撤回成功");
					that.$refs.mtaskMlogs.refresh();
					that.getMtask(that.mtask);
					that.mlogHandleLoading = false;
				})
				.catch((e) => {
					that.mlogHandleLoading = false;
				});
		},
	},
};
</script>
<style scoped>
.tabsHeader {
	display: inline-block;
	position: absolute;
	right: 20px;
	margin-top: 4px;
	z-index: 100;
}

.right-panel > * + * {
	margin-left: 10px;
}

.el-main.nopadding {
	padding: 0 20px 0 20px;
}

.tableTitle {
	height: 40px;
	line-height: 40px;
	font-size: 14px;
	box-sizing: border-box;
	border-bottom: 1px solid #eeeeee;
}
</style>
