<template>
	<el-container>
		<el-main>
			<scTable
				ref="table"
				:apiObj="apiObj"
				:query="params"
				row-key="id"
				:params="params"
			>
				<el-table-column label="任务列表" type="index" width="80"></el-table-column>
				<el-table-column label="产品名" prop="material_out_name" show-overflow-tooltip>
				</el-table-column>
				<el-table-column label="任务编号" prop="number" width="140" show-overflow-tooltip>
				</el-table-column>
				<el-table-column label="任务量" prop="count"> </el-table-column>
				<el-table-column label="开始时间" prop="start_date">
				</el-table-column>
				<el-table-column label="结束时间" prop="end_date">
				</el-table-column>
				<el-table-column label="合格数" prop="count_ok">
				</el-table-column>
				<!-- <el-table-column label="分配人数" prop="mtaskb">
					<template #default="scope">
						<span v-if="scope.row.mtaskb.length>0">
							{{scope.row.mtaskb.length}}
						</span>
					</template>
				</el-table-column> -->
				<el-table-column label="可用批" v-if="mgroupName!='切片'" width="80">
					<template #default="scope">
						<el-button 
							link
							size="small"
							type="primary"
							v-if="scope.row.state == 20"
							@click="viewBatches(scope.row)">
							查看
						</el-button>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" width="60">
					<template #default="scope">
						<el-button
							link
							size="small"
							v-auth="'mtask.submit'"
							type="primary"
							v-if="scope.row.state == 20 "
							@click="mtask_submit(scope.row)"
							>提交
						</el-button>
						<!-- <el-button
							link
							size="small"
							v-auth="'mtask.submit'"
							type="primary"
							v-if="scope.row.state == 20 "
							@click="mtask_deliver(scope.row)"
							>分配
						</el-button> -->
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<deliver-drawer
			v-if="deliverShow"
			ref="detailDialog"
			:mgroup="mgroupId"
			:dept="deptId"
			@success = "refreshMtask"
			@closed="detailClose"
		>
		</deliver-drawer>
		<el-dialog v-model="dialogVisible">
			<scTable hidePagination hideDo :apiObj="m_apiObj" :query="m_query" ref="mTable">
				<el-table-column
					label="物料名称"
					prop="material_name"
					min-width="150"
				>
					<template #default="scope">
						{{ scope.row.material_name }}
						<span v-if="scope.row.material_origin != null"
							>（{{ scope.row.material_origin_name }}）</span
						>
					</template>
				</el-table-column>
				<el-table-column
					label="批次号"
					prop="batch"
					min-width="120"
				></el-table-column>
			</scTable>
		</el-dialog>
	</el-container>
</template>
<script>
import deliverDrawer from "./mtask_deliver.vue";
export default {
	props: {
		deptId: {
			type: String,
			default: "",
		},
		mgroupId: {
			type: String,
			default: "",
		},
		mgroupName: {
			type: String,
			default: "",
		},
		processId: {
			type: String,
			default: "",
		},
		mgroupcode: {
			type: String,
			default: "",
		},
	},
	components: {
		deliverDrawer,
	},
	name: "mtask",
	data() {
		return {
			dialogVisible: false,
			m_apiObj: null,
			m_query: null,
			apiObj: null,
			params: { mgroup: "",state:20 },
			query: {},

			tableData: [],
			selection: [],
			cates_: {
				section: "工序",
				other: "其他",
			},
			type_: {
				10: "primary",
				20: "primary",
				30: "primary",
				34: "danger",
				40: "success",
			},
			// state_: {
			// 	10: "创建中",
			// 	20: "已下达",
			// 	30: "生产中",
			// 	34: "已终止",
			// 	40: "已提交",
			// },
			deliverShow:false,
		};
	},
	mounted() {
		let that = this;
		that.params.mgroup = that.mgroupId;
		that.apiObj = this.$API.pm.mtask.list;
	},
	methods: {
		viewBatches(row){
			this.dialogVisible = true;
			this.$nextTick(() => {
				this.m_query = {mtaskx: row.id};
				this.m_apiObj = this.$API.wpm.wmaterial.list;
				this.$refs.mTable.queryData(this.m_query);
			});
		},
		elTagType(state) {
			return this.type_[state];
		},
		mtask_submit(row){
			this.$confirm('确定提交该任务吗？提交后不可更改相关信息', "提示", {
				type: "warning",
			}).then(() => {
				this.$API.pm.mtask.submit.req(row.id).then((res) => {
					this.$message.success("操作成功");
					this.$refs.table.refresh();
					this.mtaskClick(this.currentMtask);
				}).catch((err) => {});
			});
		},
		mtask_deliver(row){
			this.deliverShow = true;
			this.$nextTick(() => {
				this.$refs.detailDialog.open(row);
			});
		},
		refreshMtask(){
			this.$refs.table.refresh();
		},
		//表格选择后回调事件
		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>
