<!--
 * @Descripttion: (出库货物记录表/wm_goods_out_production)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2024-03-22)
 * @LastEditors: (admin)
 * @LastEditTime: (2024-03-22)
-->
<template>
	<div class="app-container">
		<!-- :model属性用于表单验证使用 比如下面的el-form-item 的 prop属性用于对表单值进行验证操作 -->
		<el-form :model="queryParams" size="small" label-position="right" inline ref="queryForm" :label-width="labelWidth" v-show="showSearch" @submit.native.prevent>
			<el-form-item label="出库单号" prop="fkOutOrderId">
				<el-input v-model="queryParams.fkOutOrderId" placeholder="请输入出库单号" />
			</el-form-item>
			<el-form-item label="零件号" prop="partnumber">
				<el-input v-model="queryParams.partnumber" placeholder="请输入零件号" />
			</el-form-item>
			<el-form-item label="批次号" prop="packageCodeClient">
				<el-input v-model="queryParams.packageCodeClient" placeholder="请输入批次号" />
			</el-form-item>
			<el-form-item label="库位编号" prop="locationCode">
				<el-input v-model="queryParams.locationCode" placeholder="请输入库位编号" />
			</el-form-item>
			<!-- <el-form-item label="出库时间" prop="outTime">
				<el-date-picker v-model="form.outTime" type="datetime" placeholder="选择日期时间"></el-date-picker>
			</el-form-item> -->
			<el-form-item label=" ">
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<!-- 工具区域 -->
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" v-hasPermi="['wmsManagement:wmgoodsoutproduction:add']" icon="el-icon-plus" size="mini" @click="handleAdd">手动添加出库记录</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" v-hasPermi="['wmsManagement:wmgoodsoutproduction:add']" icon="el-icon-plus" size="mini" @click="handleBatchOut">按批次添加出库记录</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" :disabled="single" v-hasPermi="['wmsManagement:wmgoodsoutproduction:edit']" icon="el-icon-edit" size="mini" @click="handleUpdate">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" :disabled="multiple" v-hasPermi="['wmsManagement:wmgoodsoutproduction:delete']" icon="el-icon-delete" size="mini" @click="handleDelete">删除</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<!-- 数据区域 -->
		<el-table :data="dataList" v-loading="loading" ref="table" border highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="50" align="center" />
			<el-table-column prop="fkOutOrderId" label="出库单号" align="center" :show-overflow-tooltip="true" />
			<el-table-column prop="packageCodeClient" label="批次号" align="center" :show-overflow-tooltip="true" />
			<el-table-column prop="locationCode" label="库位编号" align="center" :show-overflow-tooltip="true" />
			<el-table-column prop="partnumber" label="零件号" align="center" :show-overflow-tooltip="true" />
			<el-table-column prop="description" label="描述" align="center" :show-overflow-tooltip="true" />
			<!-- <el-table-column prop="goodsNumLogic" label="标签零件数" align="center" /> -->
			<el-table-column prop="goodsNumAction" label="出库数量" align="center" />
			<el-table-column prop="entryWarehouseTime" label="入库时间" align="center" :show-overflow-tooltip="true" />
			<el-table-column prop="outTime" label="出库时间" align="center" :show-overflow-tooltip="true" />
			<el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true" />

			<el-table-column label="操作" align="center" width="140">
				<template slot-scope="scope">
					<el-button size="mini" v-hasPermi="['wmsManagement:wmgoodsoutproduction:edit']" type="success" icon="el-icon-edit" title="编辑" @click="handleUpdate(scope.row)"></el-button>
					<el-button size="mini" v-hasPermi="['wmsManagement:wmgoodsoutproduction:delete']" type="danger" icon="el-icon-delete" title="删除" @click="handleDelete(scope.row)"></el-button>
				</template>
			</el-table-column>
		</el-table>
		<pagination class="mt10" background :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改出库货物记录表对话框 -->
		<el-dialog :title="title" :lock-scroll="false" :visible.sync="open" :close-on-click-modal="false" width="60%">
			<el-form ref="form" :model="form" :rules="rules" :label-width="formLabelWidth">
				<el-row :gutter="20">
					<el-col :lg="12">
						<el-form-item label="出库单号" prop="fkOutOrderId">
							<el-select v-loading="loading1" v-if="opertype === 1" v-model="form.fkOutOrderId" filterable remote reserve-keyword placeholder="请输入出库单号" :remote-method="handlerOutOrderRemoteMethod">
								<el-option v-for="(item, index) in outOrderOptions" :key="index" :label="item.shipmentNum" :value="item.shipmentNum">
								</el-option>
							</el-select>
							<el-input v-if="opertype === 2" :disabled="opertype === 2" v-model="form.fkOutOrderId" placeholder="请输入出库单号" />
						</el-form-item>
					</el-col>

					<el-col :lg="24">
						<el-form-item label="选择箱">
							<el-select style="width: 600px;" v-loading="loading2" v-if="opertype === 1" v-model="PackageCodeSelect" filterable remote reserve-keyword placeholder="请输入零件号或批次号" :remote-method="handlerPackageCodeRemoteMethod" value-key="packageCodeClient" @change="PackageCodeChange">
								<el-option v-for="(item, index) in PackageCodeOptions" :key="index" :label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="批次号" prop="packageCodeClient">
							<el-input v-model="form.packageCodeClient" placeholder="请输入批次号" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="库位编号" prop="locationCode">
							<el-input v-model="form.locationCode" placeholder="请输入库位编号" />
						</el-form-item>
					</el-col>

					<el-col :lg="12">
						<el-form-item label="零件号" prop="partnumber">
							<el-input v-model="form.partnumber" placeholder="请输入零件号" />
						</el-form-item>
					</el-col>
					
					<el-col :lg="12">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="form.remark" placeholder="请输入备注" />
						</el-form-item>
					</el-col>
					
					<el-col :lg="12">
						<el-form-item label="原库存" prop="goodsNumLogic">
							<el-input v-model.number="form.goodsNumLogic" placeholder="请输入原库存" />
						</el-form-item>
					</el-col>

					<el-col :lg="12">
						<el-form-item label="出库数量" prop="goodsNumAction">
							<el-input v-model.number="form.goodsNumAction" placeholder="请输入出库数量" />
						</el-form-item>
					</el-col>

					<el-col :lg="12">
						<el-form-item label="仓库剩余" prop="goodsNumAction">
							{{ (form.goodsNumLogic - form.goodsNumAction) }}
						</el-form-item>
					</el-col>
					
					<el-col :lg="12">
						<el-form-item label="入库时间" prop="entryWarehouseTime">
							<el-date-picker v-model="form.entryWarehouseTime" type="datetime" placeholder="选择日期时间"></el-date-picker>
						</el-form-item>
					</el-col>

					<el-col :lg="12">
						<el-form-item label="出库时间" prop="outTime">
							<el-date-picker v-model="form.outTime" type="datetime" placeholder="选择日期时间"></el-date-picker>
						</el-form-item>
					</el-col>

					
				</el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="info" @click="cancel">取 消</el-button>
				<el-button type="primary" @click="submitForm">确 定</el-button>
			</div>
		</el-dialog>
		<TheBatchOutProductionDialog :visible.sync="TheBatchOutProductionDialogShow" @refresh="getList()"></TheBatchOutProductionDialog>
	</div>
</template>
<script>
	import {
		listWmGoodsOutProduction,
		addWmGoodsOutProduction,
		delWmGoodsOutProduction,
		updateWmGoodsOutProduction,
		getWmGoodsOutProduction,
	} from '@/api/wmsManagement/wmGoodsOutProduction.js'
	import { listWmGoodsNowProduction,dictWmGoodsNowProduction } from '@/api/wmsManagement/wmGoodsNowProduction.js'
	import { listWmOutOrder } from '@/api/wmsManagement/wmOutOrder.js'
	import TheBatchOutProductionDialog from './components/TheBatchOutProductionDialog/TheBatchOutProductionDialog.vue'
	export default {
		name: 'wmgoodsoutproduction',
		components: {
			TheBatchOutProductionDialog
		},
		data() {
			return {
				labelWidth: '100px',
				formLabelWidth: '100px',
				// 选中id数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 遮罩层
				loading: false,
				// 显示搜索条件
				showSearch: true,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					sort: undefined,
					sortType: undefined,
				},
				// 弹出层标题
				title: '',
				// 操作类型 1、add 2、edit
				opertype: 0,
				// 是否显示弹出层
				open: false,
				TheBatchOutProductionDialogShow: false,
				// 表单参数
				form: {},
				PackageCodeSelect: {},
				columns: [
					{ index: 0, key: 'id', label: `雪花id`, checked: true },
					{ index: 1, key: 'fkNowProductionId', label: `成品库当前货物表主键`, checked: true },
					{ index: 2, key: 'fkOutOrderId', label: `出库单号`, checked: true },
					{ index: 3, key: 'packageCode', label: `箱子编号（MES）`, checked: true },
					{ index: 4, key: 'packageCodeClient', label: `箱子编号（客户）`, checked: true },
					{ index: 5, key: 'packageCodeOriginal', label: `箱子编号（原始码）`, checked: true },
					{ index: 6, key: 'locationCode', label: `库位编号`, checked: true },
					{ index: 7, key: 'partnumber', label: `零件号`, checked: true },
					{ index: 8, key: 'goodsNumLogic', label: `箱子中货物数量（理论）`, checked: true },
					{ index: 9, key: 'goodsNumAction', label: `箱子中货物数量（实际）`, checked: false },
					{ index: 10, key: 'entryWarehouseTime', label: `入库时间`, checked: false },
					{ index: 11, key: 'outTime', label: `出库时间`, checked: false },
					{ index: 12, key: 'remark', label: `备注`, checked: false },
					{ index: 13, key: 'updatedBy', label: `更新人`, checked: false },
					{ index: 14, key: 'updatedTime', label: `更新时间`, checked: false },
					{ index: 15, key: 'createdBy', label: `创建人`, checked: false },
					{ index: 16, key: 'createdTime', label: `创建时间`, checked: false },
				],
				// 数据列表
				dataList: [],
				// 总记录数
				total: 0,
				// 提交按钮是否显示
				btnSubmitVisible: true,
				// 表单校验
				rules: {
					fkOutOrderId: [{ required: true, message: '出库单号不能为空', trigger: 'blur' }],
					partnumber: [{ required: true, message: '零件号不能为空', trigger: 'blur' }],
					packageCodeClient: [{ required: true, message: '批次号不能为空', trigger: 'blur' }],
					locationCode: [{ required: true, message: '库位编号不能为空', trigger: 'blur' }],
				},
				// 出库单列表
				loading1: false,
				outOrderOptions: [],
				// 仓库货物列表
				loading2: false,
				PackageCodeOptions: [],
			}
		},
		created() {
			// 列表数据查询
			this.getList()

			var dictParams = []
		},
		methods: {
			// 查询数据
			getList() {
				this.loading = true
				listWmGoodsOutProduction(this.queryParams).then((res) => {
					if (res.code == 200) {
						// this.dataList = res.data.result
						// this.total = res.data.totalNum
						this.dataList = res.data.item1
						this.total = res.data.item2
						this.loading = false
					}
				})
			},
			// 取消按钮
			cancel() {
				this.open = false
				this.reset()
			},
			// 重置数据表单
			reset() {
				this.form = {
					id: undefined,
					fkNowProductionId: undefined,
					fkOutOrderId: undefined,
					packageCode: undefined,
					packageCodeClient: undefined,
					packageCodeOriginal: undefined,
					locationCode: undefined,
					partnumber: undefined,
					goodsNumLogic: undefined,
					goodsNumAction: undefined,
					entryWarehouseTime: undefined,
					outTime: undefined,
					remark: undefined,
					updatedBy: undefined,
					updatedTime: undefined,
					createdBy: undefined,
					createdTime: undefined,
				}
				this.outOrderOptions = [];
				this.PackageCodeSelect = '';
				this.PackageCodeOptions = [];
				this.resetForm('form')
			},
			// 重置查询操作
			resetQuery() {
				this.timeRange = []
				this.resetForm('queryForm')
				this.handleQuery()
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map((item) => item.id)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			// 自定义排序
			sortChange(column) {
				if (column.prop == null || column.order == null) {
					this.queryParams.sort = undefined
					this.queryParams.sortType = undefined
				} else {
					this.queryParams.sort = column.prop
					this.queryParams.sortType = column.order
				}

				this.handleQuery()
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1
				this.getList()
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset()
				this.open = true
				this.title = '手动添加出库记录'
				this.opertype = 1
			},
			// 批量出库
			handleBatchOut() {
				this.TheBatchOutProductionDialogShow = true;
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const Ids = row.id || this.ids

				this.$confirm('是否确认删除数据项？')
					.then(function () {
						return delWmGoodsOutProduction(Ids)
					})
					.then(() => {
						//this.handleQuery()
						this.getList()
						this.msgSuccess('删除成功')
					})
					.catch(() => {})
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset()
				const id = row.id || this.ids
				getWmGoodsOutProduction(id).then((res) => {
					const { code, data } = res
					if (code == 200) {
						this.open = true
						this.title = '修改数据'
						this.opertype = 2

						this.form = {
							...data,
						}
					}
				})
			},
			/** 提交按钮 */
			submitForm: function () {
				console.log(JSON.stringify(this.form))
				this.$refs['form'].validate((valid) => {
					if (valid) {
						if (this.form.id != undefined && this.opertype === 2) {
							updateWmGoodsOutProduction(this.form)
								.then((res) => {
									this.msgSuccess('修改成功')
									this.open = false
									this.getList()
								})
								.catch((err) => {
									//TODO 错误逻辑
								})
						} else {
							addWmGoodsOutProduction(this.form)
								.then((res) => {
									console.log(res);
									this.msgSuccess('新增成功')
									
									// this.reset();
									// this.open = false
									this.getList()
								})
								.catch((err) => {
									//TODO 错误逻辑
									console.log(err);
								})
						}
					}
				})
			},
			// 远程搜索工单号
			handlerOutOrderRemoteMethod(query) {
				if (query !== '') {
					const data = {
						pageNum: 1,
						pageSize: 30,
						shipmentNum: query,
						type: 1,
						status: -1
					}
					this.loading1 = true
					setTimeout(() => {
						this.loading1 = false
					}, 30000)
					listWmOutOrder(data).then((res) => {
						if (res.code == 200) {
							this.outOrderOptions = res.data.result
							this.loading1 = false
						}
						else{
							this.loading1 = false
						}
					})
				} else {
					this.outOrderOptions = []
				}
			},
			// 远程搜索箱
			handlerPackageCodeRemoteMethod(query) {
				if (query !== '') {
					const data = {
						pageNum: 1,
						pageSize: 30,
						query: query,
					}
					this.loading2 = true
					setTimeout(() => {
						this.loading2 = false
					}, 30000)
					dictWmGoodsNowProduction(data).then((res) => {
						if (res.code == 200) {
							this.PackageCodeOptions = res.data.result
							this.loading2 = false
						}else{
							this.loading2 = false
						}
					})
					
				} else {
					this.PackageCodeOptions = []
				}
			},
			PackageCodeChange(select) {
				console.log('select', select)
				this.form.fkNowProductionId = select.id
				this.form.partnumber = select.partnumber
				// 原仓库库存
				this.form.goodsNumLogic = select.goodsNumAction
				this.form.locationCode = select.locationCode
				// 出库数量
				this.form.goodsNumAction = select.goodsNumAction
				this.form.packageCodeClient = select.packageCodeClient
			},
		},
	}
</script>