<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: DY
 * @LastEditTime: 2024-03-15 15:40:24
 * @Description:
-->
<template>
	<!-- <el-drawer
		:visible.sync="visible"
		:show-close="false"
		:wrapper-closable="false"
		class="drawer"
		size="50%"> -->
	<div class="container">
		<!-- <small-title slot="title" :no-padding="true">
			{{ isdetail ? '详情' : !dataForm.id ? '新增' : '编辑' }}
		</small-title> -->
		<div v-show="workOrderButton.length" class="buttonDiv">
			<div style="display: flex; width: 100%;">
				<el-button v-show="showScroll" type="text" style="width: 30px; background: white; margin-right: 8px" @click="scrollLeft" icon="el-icon-caret-left"></el-button>
				<div ref="content" class="scrollDiv">
					<el-button style="border: none" v-for="(work, index) in workOrderButton" :key="index" @click="init(work.id, true)">{{ work.name }}</el-button>
				</div>
				<el-button v-show="showScroll" type="text" @click="scrolRight" style="width: 30px; background: white; margin-left: 8px" icon="el-icon-caret-right"></el-button>
				<el-button style="float: right" type="primary" size="small" plain @click="goback()">
					<svg-icon icon-class="return"/>返回
				</el-button>
			</div>
		</div>
		<div class="content">
			<div class="card">
				<div class="boxTitle">
					<span class="blueTitle"></span>
					<span>工单编码：{{ dataForm.code }}</span>
				</div>
				<el-button v-if="!workOrderButton.length" style="float: right" type="primary" size="small" plain @click="goback()">
					<svg-icon icon-class="return"/>返回
				</el-button>
				<div class="formContent">
					<el-row :gutter="20">
						<el-col :span="3">
							<div class="blodTip">工单名称</div>
							<div class="lightTip">{{ dataForm.name }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">工单来源</div>
							<div class="lightTip">{{ dataForm.triggerOrigin === 1 ? 'MES-手动' : dataForm.triggerOrigin === 2 ? 'MES-订单下发' : 'ERP'}}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">所属订单</div>
							<div class="lightTip">
								<span v-for="(item, index) in orderList" :key="index" style="margin-right: 10px">{{ item.orderName }}</span>
							</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">产品名称</div>
							<div class="lightTip">{{ dataForm.productName }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">规  格</div>
							<div class="lightTip">{{ dataForm.specifications }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">计划生产数量</div>
							<div class="lightTip">{{ dataForm.planQuantity }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">预计用时(小时)</div>
							<div class="lightTip">{{ dataForm.remainingTime }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">计划投入数量</div>
							<div class="lightTip">{{ dataForm.planAssignQuantity }}</div>
						</el-col>
					</el-row>
					<el-row :gutter="20">
						<el-col :span="3">
							<div class="blodTip">优先级</div>
							<div class="lightTip">{{ fitlerP(dataForm.priority) }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">负责人</div>
							<div class="lightTip">{{ dataForm.workers }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">关联产线</div>
							<div class="lightTip">
								<span v-for="(item, index) in dataForm.productLineNames" :key="index" style="margin-right: 10px">{{ item }}</span>
							</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">物料计算方式</div>
							<div class="lightTip">{{ dataForm.materialMethod === 1 ? '产品基础' : dataForm.materialMethod === 2 ? '工艺扩展' : '' }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">关联工艺</div>
							<div class="lightTip">{{ dataForm.processFlowName }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">车间名称</div>
							<div class="lightTip">{{ (dataForm.roomNameDict || dataForm.roomNameDict === 0) ? getDictDataLabel('workshop', dataForm.roomNameDict) : '-' }}</div>
						</el-col>
					</el-row>
				</div>
			</div>

			<div class="card">
				<!-- <small-title
						style="margin: 16px 0; padding-left: 8px"
						:no-padding="true">
						生产信息
				</small-title> -->
				<div class="boxTitle">
					<span class="blueTitle"></span>
					<span>生产信息</span>
				</div>
				<div class="formContent">
					<el-row :gutter="20">
						<el-col :span="3">
							<div class="blodTip">工单创建时间</div>
							<div class="lightTip">{{ parseTime(dataForm.createTime) }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">计划开始时间</div>
							<div class="lightTip">{{ parseTime(dataForm.planStartTime) }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">计划完成时间</div>
							<div class="lightTip">{{ parseTime(dataForm.planFinishTime) }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">预计结束时间</div>
							<div class="lightTip">{{ parseTime(dataForm.computeFinishTime) }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">实际开始时间</div>
							<div class="lightTip">{{ parseTime(dataForm.startProduceTime) }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">实际完成时间</div>
							<div class="lightTip">{{ parseTime(dataForm.finishProduceTime) }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">工单状态</div>
							<div class="lightTip">{{ fitlerS(dataForm.status) }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">实际投入数量</div>
							<div class="lightTip">{{ dataForm.assignQuantity }}</div>
						</el-col>
					</el-row>
					<el-row :gutter="20">
						<el-col :span="3">
							<div class="blodTip">实际生产数量</div>
							<div class="lightTip">{{ dataForm.actualQuantity }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">废片数量</div>
							<div class="lightTip">{{ dataForm.nokQuantity }}</div>
						</el-col>
						<el-col :span="3">
							<div class="blodTip">检测瑕疵数</div>
							<div class="lightTip">{{ }}</div>
						</el-col>
					</el-row>
				</div>
			</div>

			<div class="card" style="padding-bottom: 16px; margin-bottom: 10px">
				<div class="boxTitle">
					<span class="blueTitle"></span>
					<span>订单相关信息</span>
				</div>
				<base-table
					:table-props="tableProps"
					:page="listQuery.pageNo"
					:limit="listQuery.pageSize"
					:table-data="orderList">
					<method-btn
						v-if="!isdetail"
						slot="handleBtn"
						:width="120"
						label="操作"
						:method-list="tableBtn"
						@clickBtn="handleClick" />
				</base-table>
				<!-- <pagination
					v-show="listQuery.total > 0"
					:total="listQuery.total"
					:page.sync="listQuery.pageNo"
					:limit.sync="listQuery.pageSize"
					:page-sizes="[5, 10, 15]"
					@pagination="getList" /> -->
			</div>

			<div class="card" style="padding-bottom: 16px;">
				<div class="boxTitle">
					<span class="blueTitle"></span>
					<span>预计用料信息</span>
				</div>
				<base-table
					:table-props="tableProps1"
					:page="listQuery1.pageNo"
					:limit="listQuery1.pageSize"
					:table-data="materialList" />
				<!-- <pagination
					v-show="listQuery1.total > 0"
					:total="listQuery1.total"
					:page.sync="listQuery1.pageNo"
					:limit.sync="listQuery1.pageSize"
					:page-sizes="[5, 10, 15]"
					@pagination="getList" /> -->
			</div>

			<!-- <div class="drawer-body__footer">
				<el-button type="primary" @click="goback()">关闭</el-button>
			</div> -->
		</div>
	</div>
</template>

<script>
// import basicAdd from '../../core/mixins/basic-add';
import { getCoreWO, getCoreWOMaPage, getConOrderList, getCoreWOListById } from "@/api/base/coreWorkOrder";
// import { orderList } from "@/api/base/orderManage";
import { getProcessFlowList } from '@/api/base/orderManage'
import SmallTitle from './SmallTitle';
import { publicFormatter } from "@/utils/dict";
import { parseTime } from '@/utils/ruoyi'

const tableBtn = [
	{
		type: 'edit',
		btnName: '编辑',
	},
	{
		type: 'delete',
		btnName: '删除',
	},
];
const tableProps = [
	{
		prop: 'orderName',
		label: '订单名称',
	},
	{
		prop: 'orderCode',
		label: '订单编码',
	},
	{
		prop: 'priority',
		label: '优先级',
		filter: (val) => ['', '低', '正常', '高'][val]
	},
	{
		prop: 'planAssignmentQuantity',
		label: '计划分配数量',
	},
	{
		prop: 'actualAssignmentQuantity',
		label: '实际分配数量',
	}
];

const tableProps1 = [
	{
		prop: 'material',
		label: '物料名称',
		filter: publicFormatter('material')
	},
	{
		prop: 'unit',
		label: '单位',
		filter: publicFormatter('unit_dict')
	},
	{
		prop: 'remaining',
		label: '剩余生产预计消耗'
	},
	{
		prop: 'num',
		label: '库存数量'
	}
];

export default {
	components: { SmallTitle },
	data() {
		return {
			tableBtn,
			tableProps,
			tableProps1,
			addOrUpdateVisible: false,
			urlOptions: {
				infoURL: getCoreWO
			},
			listQuery: {
				pageSize: 10,
				pageNo: 1,
				total: 0,
			},
			listQuery1: {
				pageSize: 10,
				pageNo: 1,
				total: 0,
			},
			dataForm: {},
			orderList: [],
			materialList: [],
			// orderArray: [],
			visible: false,
			isdetail: false,
			showScroll: false,
			workOrderButton: [],
			processFlowList: [],
			timer: null
			// tableH: this.tableHeight(510) / 2
		};
	},
	created() {
		this.getDict()
	},
	mounted() {
		if (this.$route.query.woIdString && this.$route.query.woIdString !== 'undefined') {
			const idList = this.$route.query.woIdString.split(',')
			getCoreWOListById(idList).then(res => {
				this.workOrderButton = res.data.map(work => {
					return {
						id: work.id,
						name: work.name
					}
				})
				const contentRef = this.$refs.content
				if (contentRef.scrollWidth > contentRef.clientWidth) {
					this.showScroll = true
				} else {
					this.showScroll = false
				}
				console.log(contentRef.scrollWidth, contentRef.clientWidth)
				this.init(this.workOrderButton[0].id, true)
			})
		} else {
			this.init(this.$route.query.id, true)
		}
	},
	methods: {
		scrolRight() {
			// let contentRef = this.$refs.content
			// console.log('你好啊啊', contentRef.scrollWidth, contentRef.clientWidth)
			this.stopScroll()
			this.timer = setTimeout(() => {
				this.$refs.content.scrollLeft += 50
			}, 100)
			// if (contentRef.scrollWidth > contentRef.clientWi)
		},
		scrollLeft() {
			this.stopScroll()
			this.timer = setTimeout(() => {
				this.$refs.content.scrollLeft -= 50
			}, 100)
		},
		stopScroll() {
			clearTimeout(this.timer)
		},
		getDict() {
			// 工艺
      getProcessFlowList().then(res => {
        this.processFlowList = res.data || []
      })
		},
		fitlerP(val) {
			if (val) {
				if (val === 1) {
					return '低'
				} else if (val ===  2) {
					return '正常'
				} else {
					return '高'
				}
			}
		},
		fitlerS(val) {
			if (val) {
				if (val === 1) {
					return '等待'
				} else if (val ===  2) {
					return '激活'
				} else if (val ===  3) {
					return '暂停'
				} else if (val ===  4) {
					return '完成'
				} else {
					return '作废'
				}
			}
		},
		initData() {
			this.orderList.splice(0);
			this.materialList.splice(0);
		},
		handleClick(raw) {
			if (raw.type === 'delete') {
				this.$confirm(
					`确定对${
						raw.data.attrName
							? '[名称=' + raw.data.attrName + ']'
							: '[序号=' + raw.data._pageIndex + ']'
					}进行删除操作?`,
					'提示',
					{
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
					}
				)
					.then(() => {
						deleteCoreProductAttr(raw.data.id).then(({ data }) => {
							this.$message({
								message: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.getList();
								},
							});
						});
					})
					.catch(() => {});
			} else {
				this.addNew(raw.data.id);
			}
		},
		getList() {
			// 获取订单列表
			getConOrderList({
				workOrderId: this.dataForm.id,
			}).then((response) => {
				this.orderList = response.data;
				// this.listQuery.total = response.data.total;
			});
			// 获取预使用原料列表
			console.log()
			if (this.dataForm.id) {
				getCoreWOMaPage({
					workOrderId: this.dataForm.id,
					pageNo: 1,
					pageSize: 100
				}).then((response) => {
					this.materialList = response.data.records.map((item) => {
						item.remaining = item?.num * this.dataForm.remainingQuantity || 0
						return item
					});
					// this.listQuery.total = response.data.length;
				});
			}
			// 获取订单相关信息
			// orderList({
			// 	workOrderId: this.dataForm.id
			// }).then((response) => {
			// 	this.orderArray = response.data;
			// });
		},
		init(id, isdetail) {
			this.initData();
			this.isdetail = isdetail || false;
			this.dataForm.id = id || undefined;
			this.visible = true;

			this.$nextTick(() => {

				if (this.dataForm.id) {
					// 获取工单详情
					this.urlOptions.infoURL(id).then(response => {
            this.dataForm = response.data
						// 工艺名称
						if (this.dataForm.processFlowId) {
							this.processFlowList.filter(item => {
								if (item.id === this.dataForm.processFlowId) {
									this.dataForm.processFlowName = item.name
								}
							})
						}
						// 获取订单列表和用料列表
						this.getList();
          });
				} else {
					if (this.urlOptions.isGetCode) {
            this.getCode()
          }
				}
			});
		},
		goback() {
			this.$router.go(-1);
			// this.initData();
		},
		goEdit() {
			this.isdetail = false;
		},
		// 新增 / 修改
		addNew(id) {
			this.addOrUpdateVisible = true;
			this.$nextTick(() => {
				this.$refs.addOrUpdate.init(id);
			});
		}
	}
};
</script>
<style scoped>
.scrollDiv {
	width: 100%;
	overflow-x: hidden;
	/* height: 50px; */
	display: flex;
	/* padding-bottom: -10px; */
}
.scrollDiv::-webkit-scrollbar-track {
	display: none;
}
.buttonDiv {
	margin-bottom: 8px;
	display: flex;
}
.formContent {
	font-size: 16px;
	line-height: 1.5;
	margin-bottom: 10px;
	width: 100%;
	padding: 0 14px;
}
.action_btn {
  float: right;
  margin: 5px 15px;
  font-size: 14px;
}
.add {
	color: #0b58ff;
}
.blodTip {
	height: 16px;
	font-size: 14px;
	font-weight: 600;
	color: rgba(0,0,0,0.85);
	margin-bottom: 8px;
}
.lightTip {
	/* height: 16px; */
	font-size: 14px;
	font-weight: 400;
	color: rgba(102,102,102,0.75);
	margin-bottom: 12px;
}
.container {
	background-color: rgb(242, 244, 249);
}
.card {
	padding: 16px 16px 0 16px;
	background-color: #fff;
  border-radius: 9px;
}
.boxTitle {
	display: inline-block;
	font-size: 16px;
	font-weight: 400;
	color: #000000;
	margin:0 10px 20px 0;
}
.blueTitle {
	content: '';
	display: inline-block;
	width: 4px;
	height: 18px;
	background-color: #0B58FF;
	border-radius: 1px;
	margin-right: 8px;
	vertical-align: bottom;
}
</style>
