<!-- 盘点--组装单 -->
<template>
	<div class="main-container order-mc-bg">
		<div class="sale-container">
			<!-- 顶部按钮组 -->
			<toolBtn :btnType="btnType" @actionhandel="actionhandel"></toolBtn>
			<!-- 列表 -->
			<div class="sale-page-body">
				<!-- 表头 -->
				<div class="sale-page-table-header" ref="getSaleTableHeader">
					<el-form :inline="true">
						<el-row class="form-row1">
							<el-col :span="20">
								<el-row :gutter="2">
									<el-col :span="4">
										<el-form-item
											class="Iisshow"
											label="单据编号"
										>
											<el-input
												class="f-12"
												placeholder="请输入单据编号"
												v-model.trim="search.billNo"
											></el-input>
											<div
												:data-clipboard-text="
													search.billNo
												"
												@click="saleOrderCopy"
												class="copyBtn"
											>
												<i
													class="el-icon-document-copy"
												/>
											</div>
										</el-form-item>
									</el-col>
									<el-col :span="4">
										<el-form-item label="单据状态">
											<el-select
												clearable
												placeholder="请选择"
												v-model="search.documentStatus"
											>
												<el-option
													:key="key + index"
													:label="value"
													:value="key"
													class="f-12"
													v-for="(
														value, key, index
													) in documentStatuslist"
												></el-option>
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="4">
										<el-form-item label="仓库">
											<el-select
												filterable
												placeholder="请选择"
												v-model="search.stockId"
											>
												<el-option
													:key="item.id"
													:label="item.name"
													:value="item.id"
													v-for="item in warehouseList"
												>
												</el-option>
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="4">
										<el-form-item
											class="Iisshow"
											label="产品"
										>
											<el-input
												class="f-12"
												placeholder="请输入物料名称/编码/SKU"
												v-model.trim="search.productKey"
											></el-input>
											<div
												:data-clipboard-text="
													search.productKey
												"
												@click="saleOrderCopy"
												class="copyBtn"
											>
												<i
													class="el-icon-document-copy"
												/>
											</div>
										</el-form-item>
									</el-col>
								</el-row>
							</el-col>
							<el-col :span="4" class="form-row-lastcol">
								<el-row :gutter="2">
									<el-col :span="24">
										<el-form-item class="form-group1">
											<el-button
												@click="resetSearch"
												size="small"
												>重置</el-button
											>
											<el-button
												@click="searchData"
												size="small"
												type="primary"
												>搜索</el-button
											>
										</el-form-item>
									</el-col>
								</el-row>
							</el-col>
						</el-row>
					</el-form>
				</div>
				<!-- 列表 -->
				<div class="sale-page-table-body">
					<el-table
						:data="tableData"
						:header-cell-class-name="headerRigthMethod"
						:max-height="heightNum"
						@row-dblclick="openDetailsDB"
						@selection-change="handleSelectionChange"
						border
						class="table-box"
						element-loading-spinner="el-icon-loading"
						element-loading-text="加载中，请稍后..."
						ref="tableInfoRef"
						style="width: 100%; font-size: 12px"
						v-loading="isLoading"
					>
						<el-table-column
							align="center"
							fixed
							type="selection"
							width="40"
						/>
						<el-table-column
							align="left"
							fixed
							label="单据编号"
							prop="billNo"
							width="187"
						>
							<template #default="scope">
								<span
									@click="jump(scope.row)"
									style="color: #4178d5"
									>{{ scope.row.billNo }}</span
								>
							</template>
						</el-table-column>

						<el-table-column
							align="left"
							label="成品编码"
							prop="materialNumberProduct"
							width="172"
						/>
						<el-table-column
							align="left"
							label="成品规格型号"
							prop="specificationProduct"
							width="280"
						/>
						<el-table-column
							align="left"
							label="成品名称"
							prop="materialNameProduct"
							width="150"
						/>
						<el-table-column
							align="center"
							label="成品单位"
							prop="unitNameProduct"
							width="172"
						/>
						<el-table-column
							align="right"
							label="成品数量"
							prop="qtyProduct"
							width="100"
						/>
						<el-table-column
							align="left"
							label="成品仓库"
							prop="stockNameProduct"
							width="120"
						/>
						<el-table-column
							align="left"
							label="成品客户"
							prop="customerNameProduct"
							width="172"
						/>
						<el-table-column
							align="left"
							label="成品客户仓库"
							prop="customerStockNameProduct"
							width="120"
						/>
						<el-table-column
							align="left"
							label="子物料编码"
							prop="materialNumber"
							width="172"
						/>
						<el-table-column
							align="left"
							label="子物料规格型号"
							prop="specification"
							width="280"
						/>
						<el-table-column
							align="left"
							label="子物料名称"
							prop="materialName"
							width="150"
						/>
						<el-table-column
							align="center"
							label="子物料单位"
							prop="unitName"
							width="172"
						/>
						<el-table-column
							align="right"
							label="子物料数量"
							prop="qty"
							width="100"
						/>
						<el-table-column
							align="left"
							label="子物料仓库"
							prop="stockName"
							width="120"
						/>
						<!-- <el-table-column prop="subStockName" label="子物料仓位" align="left" width="200" /> -->
						<el-table-column
							align="left"
							label="子物料客户"
							prop="customerName"
							width="172"
						/>
						<el-table-column
							align="left"
							label="子物料客户仓库"
							prop="customerStockName"
							width="120"
						/>
						<el-table-column
							align="left"
							label="日期"
							prop="date"
							width="83"
						/>
						<el-table-column
							align="center"
							label="单据状态"
							prop="documentStatus"
							width="72"
						/>
						<el-table-column
							align="center"
							label="单据类型"
							prop="billType"
							width="72"
						/>
						<el-table-column
							align="center"
							label="备注"
							prop="note"
							width="225"
						/>
					</el-table>
				</div>
			</div>
			<!-- 底部 -->
			<div class="sale-page-footer">
				<div class="footer-right">
					<el-pagination
						:currentPage="search.pageNo"
						:page-size="search.pageSize"
						:page-sizes="[50, 100, 150]"
						:total="orderTotal"
						@current-change="handleCurrentChange"
						@size-change="handleSizeChange"
						layout="slot,sizes, total,prev,pager, next,jumper"
						popper-class="每页显示"
					>
						<span class="total-select-item">
							共选中
							<text
								style="
									text-align: center;
									color: #4178d5;
									padding: 0 1px;
								"
								>{{
									multipleSelection
										? multipleSelection.length
										: 0
								}}</text
							>行
						</span>
					</el-pagination>
				</div>
				<div class="footer-pagination-total-page">
					<span
						>共{{ Math.ceil(orderTotal / search.pageSize) }}页</span
					>
				</div>
			</div>
		</div>
		<!-- 操作结果 -->
		<el-dialog title="操作结果" v-model="dialogVisible" width="40%">
			<div class="sale-page-table-body">
				<el-table
					:data="tableInfo"
					border
					class="table-box"
					height="300"
					id="tableInfo-table"
				>
					<el-table-column label="序号" prop="date" width="50">
						<template #default="scope">
							<span>{{ scope.$index + 1 }}</span>
						</template>
					</el-table-column>
					<el-table-column
						label="组装订单号"
						prop="orderNo"
						width="200"
					></el-table-column>
					<el-table-column label="指令" prop="a" width="110">
						<template #default>
							<span>{{
								actionType === 'submit'
									? '提交'
									: actionType === 'revoke'
									? '撤销'
									: actionType === 'audit'
									? '审核'
									: actionType === 'de-approval'
									? '反审'
									: actionType === 'delet'
									? '删除'
									: actionType === 'finish'
									? '完工'
									: actionType === 'close-case'
									? '结案'
									: actionType === 'de-excute-start'
									? '反执行至开工'
									: actionType === 'de-approval'
									? '反审'
									: actionType === 'delet'
									? '删除'
									: actionType === 'finish'
									? '完工'
									: actionType === 'close-case'
									? '结案'
									: actionType === 'de-excute-finish'
									? '反执行至完工'
									: ''
							}}</span>
						</template>
					</el-table-column>
					<el-table-column label="结果" width="80">
						<template #default="scope">
							<span :style="{ 'txt-Red': scope.row.isSuccess }">{{
								scope.row.isSuccess ? '成功' : '失败'
							}}</span>
						</template>
					</el-table-column>
					<el-table-column label="信息" prop="msg">
						<template #default="scope">
							<span class="txt-Red">{{ scope.row.msg }}</span>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<span
						class="f-12 ext-black-light"
						style="margin-right: 25px"
						>共{{
							tableInfo && tableInfo.length > 0
								? tableInfo.length
								: 0
						}}条</span
					>
					<el-button
						@click="
							handelhtmlToExcel('tableInfo-table', '操作结果');
							dialogVisible = false;
						"
						size="small"
						>导 出
					</el-button>
					<el-button
						@click="dialogVisible = false"
						size="small"
						type="primary"
						>关 闭</el-button
					>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<script>
import { mapState } from 'vuex';
import {
	transformTimestamp,
	exporthandel,
	htmlToExcel,
} from '@/utils/pubulic.js';
import { orderCopy } from '@/utils/takeMethods';
export default {
	name: 'AssemblyOrderOrder',
	data() {
		return {
			orderTotal: 0,
			actionType: '', //判断点击的顶部那个按钮
			tableInfo: [], //操作结果
			isLoading: false, //表格加载数据
			documentStatuslist: [], // 单据状态
			multipleSelection: [], // 列表选中集合
			btnType: [
				'save',
				'copy',
				'close-case',
				'split-order',
				'close',
				'de-close',
				'close',
				'print-aterial-list',
				'finish',
				'close-case',
				'de-excute-start',
				'de-excute-finish',
				'finish',
				'push-down',
				'print-brower',
				'query',
			], // 不需要显示的顶部按钮类型
			warehouseList: [], // 仓库数据
			dialogVisible: false, // 操作弹框显示
			// 搜索条件
			search: {
				orgId: '', // 组织Id(所有列表页都需要 全局)
				billNo: '', //单据编号（采购入库单号）
				documentStatus: '', //单据状态
				stockId: '', // 仓库ID
				productKey: '', // 产品（关键字）：物料表中的->>编码、名称、规格型号
				pageNo: 1, // 页码
				pageSize: 50, // 页数
			},
			tableData: [],
			selectItem: 0,
			rowShow: false,
			heightNum: 0,
			windowHeight: 0,
			isDown: true,
		};
	},
	computed: {
		...mapState({
			orgId: (state) =>
				state && state.org && state.org.orgId ? state.org.orgId : null,
		}),
		transformTime: function () {
			return function (value) {
				return transformTimestamp(value);
			};
		},
	},
	created() {
		this.windowHeight = document.documentElement.clientHeight;
		this.heightNum =
			this.windowHeight - (40 + 30 + 54 + 34 + 34 + 34 + 16 + 16);
		this.search.orgId = this.orgId;
		this.Init();
	},
	activated() {
		// 键盘监听事件
		document.addEventListener('keyup', this.keyupFn, false);
	},
	deactivated() {
		document.removeEventListener('keyup', this.keyupFn, false);
	},
	watch: {
		orgId(value) {
			if (value) {
				this.search.orgId = value;
				this.search.pageNo = 1;
				// 监听组织切换 ，数据初始化
				this.getwarehouse();
				this.getassemblyOrder();
			}
		},
	},
	methods: {
		keyupFn(e) {
			if (e.keyCode === 13) {
				// enter见搜索
				this.searchData();
			}
		},
		// 初始化
		Init() {
			this.getwarehouse();
			this.getassemblyOrder();
			this.getStatus();
		},
		// 订单号 复制按钮
		saleOrderCopy() {
			orderCopy();
		},
		// 操作弹框导出
		handelhtmlToExcel(domid, title) {
			htmlToExcel(domid, title);
		},
		// 重置
		resetSearch() {
			this.search = {
				orgId: this.orgId, // 组织Id(所有列表页都需要 全局)
				billNo: '', //单据编号（采购入库单号）
				documentStatus: '', //单据状态
				stockId: '', // 仓库ID
				productKey: '', // 产品（关键字）：物料表中的->>编码、名称、规格型号
				pageNo: 1, // 页码
				pageSize: 50, // 页数
			};
			this.getassemblyOrder();
		},
		// 双击计入
		openDetailsDB(row, column) {
			let typeStr1 =
				row.documentStatus === '重新审核' ||
				row.documentStatus === '创建'
					? 'edit'
					: 'see';
			this.$router.push({
				path: '/warehouse/other/assemblyOrderSet',
				query: { type: typeStr1, productId: row.id },
			});
		},
		// 点击生产订单查看或者是编辑
		jump(item) {
			// 只有创建和重新审核的单据才能做修改，其余都是查看
			let typeStr =
				item.documentStatusKey === 'a' || item.documentStatusKey === 'd'
					? 'edit'
					: 'see';
			this.$router.push({
				path: '/warehouse/other/assemblyOrderSet',
				query: { type: typeStr, productId: item.id },
			});
		},
		// 获取仓库
		getwarehouse() {
			// 获取仓库
			this.warehouseList = [];
			this.$http
				.get('SysConfig/GetWarehouseByOrg/' + this.orgId)
				.then((req) => {
					if (req.status === 200) {
						this.warehouseList = req.data;
					}
				});
		},
		// 获取需要状态列表---[单据状态，领料状态，业务状态]
		getStatus() {
			this.$http.get('Assembly/status/get').then((r) => {
				if (r.status === 200) {
					this.documentStatuslist = r.data.documentStatus;
				}
			});
		},
		// 获取数据
		getassemblyOrder() {
			this.isLoading = true;
			this.$http.post('Assembly/list', this.search).then((res) => {
				if (res.status === 200) {
					this.isLoading = false;
					this.tableData = res.data;
					this.orderTotal = res.totalCount;
				}
			});
		},
		// 顶部公共按钮操作
		actionhandel(type) {
			this.actionType = type;
			// 点击新增
			if (type === 'add') {
				this.$router.push({
					path: '/warehouse/other/assemblyOrderSet',
					query: { type: 'add', id: '', sjtag: Math.random() * 36 },
				});
			}
			if (this.multipleSelection && this.multipleSelection.length > 0) {
				let nweArysize = 0;
				// 单据id集合 （有去重）
				let newArry = [
					...new Set(
						this.multipleSelection.map((item) => {
							return item.id;
						})
					),
				];
				// 取单据订单号
				let orderNoArry = [
					...new Set(
						this.multipleSelection.map((item) => {
							return item.billNO;
						})
					),
				];
				this.OrderNoNames = [];
				this.OrderNoNames = orderNoArry;
				// 明细id集合 （有去重）
				let detailsIds = [
					...new Set(
						this.multipleSelection.map((item) => {
							return item.detailId;
						})
					),
				];
				this.OrdedetailIds = [];
				this.OrdedetailIds = detailsIds;
				// 单据id 集合的size
				nweArysize = new Set(
					this.multipleSelection.map((item) => {
						return item.productId;
					})
				).size;
				// 点击复制
				if (type === 'copy') {
					if (nweArysize !== 1 && nweArysize > 0) {
						this.$message.error({
							message: '只能克隆单号相同的生产订单',
							showClose: true,
						});
					} else {
						this.$router.push({
							path: '/warehouse/other/assemblyOrderSet',
							query: {
								type: 'copy',
								productId: this.multipleSelection[0].id,
							},
						});
					}
				}
				// 点击提交
				if (type === 'submit') {
					this.toolarFn('Assembly/submit/status', newArry);
				}
				// 点击撤销
				if (type === 'revoke') {
					this.toolarFn('Assembly/revoke', newArry);
				}
				// 点击审核
				if (type === 'audit') {
					this.toolarFn('Assembly/check', newArry);
				}
				// 点击反审核
				if (type === 'de-approval') {
					this.toolarFn('Assembly/recheck', newArry);
				}
				// 点击删除
				if (type === 'delet') {
					this.toolarFn('Assembly/delete', newArry);
				}
			} else {
				//按列表字段导出,点击全部导出
				if (
					type !== 'add' &&
					type !== 'refresh' &&
					type !== 'list-export' &&
					type !== 'all-export'
				) {
					this.$message.error({
						message: '请先勾选',
						showClose: true,
					});
				}
			}
			if (type === 'list-export' || type === 'all-export') {
				exporthandel(
					type === 'list-export'
						? 'Assembly/Export'
						: 'Assembly/ExportAll',
					this.search
				);
			}
			// 点击刷新
			if (type === 'refresh') {
				this.getassemblyOrder();
			}
		},
		// 顶部操作方法-公共部分封装下
		toolarFn(path, newArry, detailsIds) {
			let formdata = detailsIds
				? {
						ids: newArry,
						detailsIds: detailsIds,
				  }
				: { ids: newArry };
			this.$http.post(path, formdata).then((res) => {
				if (res.status === 200) {
					this.dialogVisible = true;
					this.tableInfo = res.data;
					this.search.pageNo = 1;
					this.getassemblyOrder();
				}
			});
		},
		// 下单日期改变
		time1Chang(value) {
			if (value && value.length > 0) {
				this.search.beginDate = value[0];
				this.search.endDate = value[1];
			} else {
				this.search.beginDate = '';
				this.search.endDate = '';
			}
		},
		// 搜索事件
		searchData() {
			this.search.pageNo = 1;
			this.getassemblyOrder();
		},

		headerRigthMethod({ row, column, rowIndex, columnIndex }) {},
		handleSelectionChange(val) {
			this.multipleSelection = val;
		},
		handleSizeChange(val) {
			this.search.pageNo = 1;
			this.search.pageSize = val;
			this.Init();
		},
		handleCurrentChange(val) {
			this.search.pageNo = val;
			this.Init();
		},
	},
};
</script>
<style lang="scss" scoped>
@import '@/assets/style/saleOrder/saleOrderCommon.scss';
@import '@/assets/style/saleOrder/orderList.scss';
@import '@/assets/style/saleOrder/common.scss';
</style>
