<template>
	<div class="app-container">
	  <el-form :model="queryParams" ref="queryRef"  v-show="showSearch" label-width="90">
		<el-row >
		  <!-- format="YYYY-MM-DD " -->
		  <el-col :span="7">
			<el-form-item label="操作时间" prop="rkTime">
			  <el-date-picker
				v-model="rkTime"
				type="datetimerange"
				start-placeholder="开始时间"
				end-placeholder="结束时间"
				format="YYYY/MM/DD HH:mm:ss"
				value-format="YYYY-MM-DD HH:mm:ss"
			  />
			</el-form-item>
		  </el-col>
		  <el-col :span="4">
			<el-form-item label="项目号" prop="xmNo">
			  <el-input
				v-model="queryParams.xmNo"
				placeholder="请输入项目号"
				clearable
				@keyup.enter="handleQuery"
			  />
			</el-form-item>
		  </el-col>
		  <el-col :span="4">
			<el-form-item label="订单编号" prop="sapNo">
			  <el-input
				v-model="queryParams.sapNo"
				placeholder="请输入订单编号"
				clearable
				@keyup.enter="handleQuery"
			  />
			</el-form-item>
		  </el-col>
		  <el-col :span="4">
			<el-form-item label="入库单据号" prop="billNo">
			  <el-input
				v-model="queryParams.billNo"
				placeholder="请输入入库单据号"
				clearable
				@keyup.enter="handleQuery"
			  />
			</el-form-item>
		  </el-col>
		  <el-col :span="4">
			<el-form-item label="库存状态" prop="operationType">
			  <el-select v-model="queryParams.operationType" placeholder="请选择" clearable @change="changeStore">
				  <el-option v-for="dict in storeStatusList" :key="dict.id" :label="dict.statusName" :value="dict.id" />
			  </el-select>
			</el-form-item>
		  </el-col>
		</el-row>
		<el-row >
			<!-- <el-col :span="7">
				<el-form-item label="出库时间" prop="lyTime">
				<el-date-picker
					v-model="lyTime"
					type="datetimerange"
					start-placeholder="开始时间"
					end-placeholder="结束时间"
					format="YYYY/MM/DD HH:mm:ss"
					value-format="YYYY-MM-DD HH:mm:ss"
				/>
				</el-form-item>
			</el-col> -->
			<el-col :span="7">
				<el-form-item label="出库单据号" prop="billNoCk">
				<el-input
					v-model="queryParams.billNoCk"
					placeholder="请输入出库单据号"
					clearable
					@keyup.enter="handleQuery"
				/>
				</el-form-item>
		  	</el-col>
		  
		  <el-col :span="4">
			<el-form-item label="物料号" prop="wlNo">
			  <el-input
				v-model="queryParams.wlNo"
				placeholder="请输入物料号"
				clearable
				@keyup.enter="handleQuery"
			  />
			</el-form-item>
		  </el-col>
		  <el-col :span="4">
			<el-form-item label="项目描述" prop="xmMs">
			  <el-input
				v-model="queryParams.xmMs"
				placeholder="请输入项目描述"
				clearable
				@keyup.enter="handleQuery"
			  />
			</el-form-item>
		  </el-col>
		  <!-- <el-col :span="4">
			<el-form-item label="供应商编码" prop="gysNo">
			  <el-input
				v-model="queryParams.gysNo"
				placeholder="请输入供应商编码"
				clearable
				@keyup.enter="handleQuery"
			  />
			</el-form-item>
		  </el-col> -->
		  <el-col :span="4">
			<el-form-item label="存放位置" prop="pcode">
			  <el-input
				v-model="queryParams.pcode"
				placeholder="请输入存放位置"
				clearable
				@keyup.enter="handleQuery"
			  />
			  
			</el-form-item>
		  </el-col>
		  <el-col :span="4">
			<el-form-item label="入库类型" prop="rkType">
			  <el-select v-model="queryParams.rkType" placeholder="请选择入库类型" clearable >
				  <el-option v-for="dict in storeTypeList" :key="dict.id" :label="dict.typeName" :value="dict.typeCode" />
			  </el-select>
			</el-form-item>
		  </el-col>
		</el-row>
		<el-row>
		  <el-col :span="7">
			<el-form-item label="供应商名称" prop="gysMc">
			  <el-input
				v-model="queryParams.gysMc"
				placeholder="请输入供应商名称"
				clearable
				@keyup.enter="handleQuery"
			  />
			</el-form-item>
		  </el-col>
		  <!-- <el-col :span="4">
			<el-form-item label="物资类型" prop="wlType">
			  <el-select v-model="queryParams.wlType" placeholder="请选择物资类型" clearable >
				  <el-option v-for="dict in wzTypeList" :key="dict.id" :label="dict.typeName" :value="dict.typeCode" />
			  </el-select>
			</el-form-item>
		  </el-col> -->
		  <el-col :span="4">
			<el-form-item label="所属大库" prop="parentWarehouseCode">
				<el-select v-model="queryParams.parentWarehouseCode" placeholder="请选择大库" clearable @change="selectBigWarehouse" @clear="clearFun">
				  <el-option v-for="dict in warehouseOptions" :key="dict.id" :label="dict.warehouseName" :value="dict.warehouseCode" />
				</el-select>
			</el-form-item>
		  </el-col>
		  <el-col :span="4">
			<el-form-item label="所属小库" prop="warehouseCode">
				<el-select v-model="queryParams.warehouseCode" placeholder="请选择小库" clearable >
				  <el-option v-for="dict in warehouseList" :key="dict.id" :label="dict.warehouseName" :value="dict.warehouseCode" />
				</el-select>
			</el-form-item>
		  </el-col>
		  <el-col :span="4">
			<el-form-item label="物料描述" prop="wlMs">
			  <el-input
				v-model="queryParams.wlMs"
				placeholder="物料描述"
				clearable
				@keyup.enter="handleQuery"
			  />
			</el-form-item>
		  </el-col>
		  <el-col :span="4">
			<el-form-item label="出库类型" prop="ckType">
			  <el-select v-model="queryParams.ckType" placeholder="请选择入库类型" clearable >
				  <el-option v-for="dict in ckTypeList" :key="dict.id" :label="dict.typeName" :value="dict.typeCode" />
			  </el-select>
			</el-form-item>
		  </el-col>
		  
		</el-row>
	  </el-form>
	  <el-row :gutter="10" class="mb8">


		<el-col :span="1.5">
		  <el-button
			type="warning"
			plain
			icon="Download"
			@click="handleExport"
			v-hasPermi="['wisdom:stock:export']"
		  >导出</el-button>
		</el-col>
		<el-col :span="5" >
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-col>
		<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
	  </el-row>
  
	  <el-table v-loading="loading"  :data="stockList" show-overflow-tooltip show-summary :summary-method="getSummaries" border @selection-change="printSelectionChange">
		<el-table-column label="序号" align="center" type="index" width="70" />
		<el-table-column label="详细类型" align="center" prop="detailType" width="150"/>
		<el-table-column label="操作类型" align="center" prop="operationType" width="100">
		  <template #default="scope">
			<dict-tag :options="dictTagData()" :value="scope.row.operationType" />
		  </template>
		</el-table-column>
		<el-table-column label="单据号" align="center" prop="billNo" width="180" />
		<el-table-column label="物料号" align="center" prop="wlNo" width="100"/>
		<el-table-column label="物料描述" align="center" prop="wlMs" width="150"/>
		<el-table-column label="单位" align="center" prop="dw" />
		<el-table-column label="数量" align="center" prop="realQty" width="120"/>
		<el-table-column label="项目号" align="center" prop="xmNo" width="150"/>
		<el-table-column label="项目描述" align="center" prop="xmMs" width="150"/>
		<el-table-column label="库位码" align="center" prop="pcode" width="120"/>
		<el-table-column label="订单编号" align="center" prop="sapNo" width="150"/>
		<el-table-column label="供应商名称" align="center" prop="gysMc" width="150"/>
		<el-table-column label="操作时间" align="center" prop="rkTime" width="150">
		  <template #default="scope">
			<span>{{ scope.row.operationType == '入库' ? parseTime(scope.row.rkTime, '{y}-{m}-{d} {h}:{i}:{s}') : parseTime(scope.row.lyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
		  </template>
		</el-table-column>
		
		<!-- <el-table-column label="是否移库" align="center" prop="isMove" >
          <template #default="scope">
            <span>{{ isMoveFun(scope.row.hasMoved)  }}</span>
          </template>
        </el-table-column>
		<el-table-column label="物资类型" align="center" prop="wlTypeName" />
		<el-table-column label="入库类型" align="center" prop="rkTypeName" width="150"/>
		<el-table-column label="出库类型" align="center" prop="ckTypeName" width="150"/>
		<el-table-column label="是否派送" align="center" prop="isDelivery" >  
			<template #default="scope">
			<span v-if="scope.row.isDelivery == '0' || !scope.row.isDelivery">否</span>
			<span v-else-if="scope.row.isDelivery === '1'">是</span>
			<span v-else-if="scope.row.isDelivery === '2'">配送中</span>
			<span v-else-if="scope.row.isDelivery === '3'">配送完成</span>
			</template>
		</el-table-column>
		<el-table-column label="托盘码" align="center" prop="trayCode" />
		<el-table-column label="身份码" align="center" prop="entityId" width="150"/>
		<el-table-column label="所属大库" align="center" prop="parentWarehouseName" width="150"/>
		<el-table-column label="所属小库" align="center" prop="warehouseName" width="150"/>
		<el-table-column label="入库时间" align="center" prop="rkTime" width="150">
		  <template #default="scope">
			<span>{{ parseTime(scope.row.rkTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
		  </template>
		</el-table-column>
		<el-table-column label="出库时间" align="center" prop="lyTime" width="150">
		  <template #default="scope">
			<span>{{ parseTime(scope.row.lyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
		  </template>
		</el-table-column>
		<el-table-column label="库龄" align="center" prop="stockAge" />
		<el-table-column label="备注" align="center" prop="remark" width="150"/>
		<el-table-column label="出库备注" align="center" prop="ckRemark" width="150"/>
		<el-table-column label="一次封样号" align="center" prop="fycde1" width="150"/>
		<el-table-column label="二次封样号" align="center" prop="fycde2" width="150"/>
	   -->
		</el-table>
		<!-- <el-table v-loading="loading"  :data="stockList" show-overflow-tooltip show-summary :summary-method="getSummaries" border @selection-change="printSelectionChange">
		<el-table-column label="序号" align="center" type="index" width="70" />
		<el-table-column label="单据号" align="center" prop="billNo" width="180">
			<template #default="scope">
				<span>{{ scope.row.isChuku == 0 || scope.row.isChuku == 5 ? scope.row.billNo : scope.row.billNoCk }}</span>
			</template>
		</el-table-column>
		<el-table-column label="库存状态" align="center" width="100">
		  <template #default="scope">
			<dict-tag :options="dictTagData()" :value="scope.row.isChuku" />
		  </template>
		</el-table-column>
		<el-table-column label="订单编号" align="center" prop="sapNo" width="150"/>
		<el-table-column label="项目号" align="center" prop="xmNo" width="150"/>
		<el-table-column label="项目描述" align="center" prop="xmMs" width="150"/>
		<el-table-column label="物料号" align="center" prop="wlNo" width="100"/>
		<el-table-column label="物料描述" align="center" prop="wlMs" width="150"/>
		<el-table-column label="供应商名称" align="center" prop="gysMc" width="150"/>
		<el-table-column label="单位" align="center" prop="dw" />
		<el-table-column label="是否移库" align="center" prop="isMove" >
          <template #default="scope">
            <span>{{ isMoveFun(scope.row.hasMoved)  }}</span>
          </template>
        </el-table-column>
		<el-table-column label="库位码" align="center" prop="pcode" width="120"/>
		<el-table-column label="物资类型" align="center" prop="wlTypeName" />
		<el-table-column label="数量" align="center" prop="realQty" width="120"/>
		<el-table-column label="入库类型" align="center" prop="rkTypeName" width="150"/>
		<el-table-column label="出库类型" align="center" prop="ckTypeName" width="150"/>
		<el-table-column label="是否派送" align="center" prop="isDelivery" >  
			<template #default="scope">
			<span v-if="scope.row.isDelivery == '0' || !scope.row.isDelivery">否</span>
			<span v-else-if="scope.row.isDelivery === '1'">是</span>
			<span v-else-if="scope.row.isDelivery === '2'">配送中</span>
			<span v-else-if="scope.row.isDelivery === '3'">配送完成</span>
			</template>
		</el-table-column>
		<el-table-column label="托盘码" align="center" prop="trayCode" />
		<el-table-column label="身份码" align="center" prop="entityId" width="150"/>
		<el-table-column label="所属大库" align="center" prop="parentWarehouseName" width="150"/>
		<el-table-column label="所属小库" align="center" prop="warehouseName" width="150"/>
		<el-table-column label="入库时间" align="center" prop="rkTime" width="150">
		  <template #default="scope">
			<span>{{ parseTime(scope.row.rkTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
		  </template>
		</el-table-column>
		<el-table-column label="出库时间" align="center" prop="lyTime" width="150">
		  <template #default="scope">
			<span>{{ parseTime(scope.row.lyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
		  </template>
		</el-table-column>
		<el-table-column label="库龄" align="center" prop="stockAge" />
		<el-table-column label="备注" align="center" prop="remark" width="150"/>
		<el-table-column label="出库备注" align="center" prop="ckRemark" width="150"/>
		<el-table-column label="一次封样号" align="center" prop="fycde1" width="150"/>
		<el-table-column label="二次封样号" align="center" prop="fycde2" width="150"/>
	  	</el-table> -->
	  
	  <pagination
		v-show="total>0"
		:total="total"
		v-model:page="queryParams.pageNum"
		v-model:limit="queryParams.pageSize"
		@pagination="getList"
	  />




	  <!-- 图片预览 -->
	  <el-dialog v-model="dialogVisible">
		<img w-full :src="dialogImageUrl" alt="Preview Image" />
	  </el-dialog>
  

	</div>
  </template>
  
  <script setup name="Record">
  // wisdomDelStock
  import { listStock,getSumInfoI} from "@/api/wisdom/stockSearch";
  import { getToken } from "@/utils/auth";
  import {warehousingDict} from "@/api/information/warehousingtype";  //入库类型
  import {listOuttype} from "@/api/information/outtype";  //出库类型
  import {materialtypeDict} from "@/api/information/materialtype";    //物资类型
//   import {warehouseDict} from "@/api/information/warehouseinfo";    //所属仓库
  import {warehouseAll} from "@/api/information/warehouseinfo";    //所属仓库
  
  import {personListDict} from "@/api/system/user";    //理货员
  import {getKwList} from "@/api/information/pcdedetail";    //库位下拉数据
  import { parseTime } from '@/utils/manage'
  import { Plus } from '@element-plus/icons-vue'
  
  const headers = ref({
	Authorization: "Bearer " + getToken()
  });
  const { proxy } = getCurrentInstance();
  
  const stockList = ref([]);
  const open = ref(false);
  const openSwitch = ref(false);  //移库
  const loading = ref(true);
  const showSearch = ref(true);
  const idEdit = ref(0);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  const planLoading = ref(true);
  const planList = ref([]); //从供应计划中添加列表
  const checkPlanList = ref([]);  //已选中的要入库的数据
  const storeTypeList = ref([]);  //入库类型下拉数据
  const ckTypeList = ref([]);
  const wzTypeList = ref([]);   //物资类型下拉数据
  const warehouseOptions = ref([]);  //大库下拉数据
  const warehouseList = ref([]);  //所属仓库下拉数据
  const personList = ref([]);   //理货员下拉数据
  const orderNum = ref("")    //搜索条件：订单号
  const kwOptions = ref([]);  //库位下拉
  const showContent = ref(false); // 控制打印内容是否显示
  const dateStr = ref("")   //打印单据生成的日期
  const printList = ref([]) //要打印的数据列表
  const storeStatusList = ref([
	{id:'入库',statusName:"已入库"},
	{id:'出库',statusName:"已出库"}
  ])
  const moveReason = ref("")  //移库原因
  const rkTime = ref([])	//入库时间
//   const lyTime = ref([])	//出库时间
  const isExamine = ref(0) //是否需要开启审核 1开启；0不开启
  const actionUrl = import.meta.env.VITE_APP_BASE_API+"/wisdom/signature/upload"
  const dialogImageUrl = ref('')
  const dialogVisible = ref(false)
  const srcList = ref([])
  const autoFill = ref(false) //是否自动填充备注
  const kzms = ref("")
  const totalMoney = ref(null)
  
  const data = reactive({
	form: {},
	storeData:{},   //入库数据
	switchData:[],  //移库数据
	queryParams: {
	  pageNum: 1,
	  pageSize: 10,
	  billNo:null,
	  billNoCk:null,
	  isChuku:null,
	  isChukuList:[0,1,3],
	  // rkTime: [],
	  startTime:null,
	  endTime:null,
	  lyStartTime:null,
	  lyEndTime:null,
	  xmNo: null,
	  sapNo: null,
	  xmMs:"",
	  wlNo:"",
	  gysNo:"",
	  pcode:"",
	  rkType:"",
	  ckType:"",
	  gysMc:"",
	  wlType:"",  //物资类型
	  cangku:"",
	  wlMs:"",
	  ids:[],
	  parentWarehouseCode:"",
	  warehouseCode:"",
	},
	rules: {
	  rkType: [{ required: true, message: "入库类型不能为空", trigger: "change" }],
	  wlType: [{ required: true, message: "物资类型不能为空", trigger: "change" }],
	  cangku: [{ required: true, message: "所属仓库不能为空", trigger: "change" }],
	  lihuoY: [{ required: true, message: "理货员不能为空", trigger: "change" }]
	},
	switchRules:{
	  toCangku:[{ required: true, message: "目标仓库不能为空", trigger: "change" }],
	  toPcode:[{ required: true, message: "目标存放位置不能为空", trigger: "change" }],
	},

  });
  
  const { queryParams, storeData, rules,form,switchData,switchRules } = toRefs(data);

  function selectBigWarehouse(val){
  warehouseOptions.value.filter(item => {
    if (item.warehouseCode === val) {
      warehouseList.value = item.children;
      
    }
  });

}
function clearFun(){
  warehouseList.value = [];
  queryParams.value.parentWarehouseCode = null
  queryParams.value.warehouseCode = null
  // queryParams.value.warehouseName = null
  
}
  function isChukuFun(isChuku){
	// scope.row.isChuku==0?"已入库":(scope.row.isChuku==1?"已出库":"审批中")
	if(isChuku == 0){
		return "已入库"
	} else if(isChuku == 1){
		return "已出库"
	}else if(isChuku == 3){
		return "借料出库"
	}else if(isChuku == 2){
		return "审批中"
	}else if(isChuku == 4){
		return "入库撤销"
	}else if(isChuku == 5){
		return "出库撤销"
	}
  }
  function isMoveFun(isMove){
	if(isMove == 0){
	  return "否"
	} else if(isMove == 1){
	  return "是"
	}
  }
  function changeStore(val){
	// totalMoney.value = null
	if(val==0){
	  queryParams.value.isChukuList = [0,5]
	}else if(val==1){
	  queryParams.value.isChukuList = [1,3]
	}else{
	  queryParams.value.isChukuList = [0,1,3]
	}
  }
  //供应计划中没有，手动添加的数据
  function addLineFun(){
	const info = {
	  dw:'',
	  entityId:'',  //身份码
	  gysMc:'',
	  gysNo:'',
	  wlNo:'',
	  wlMs:'',
	  htDj:'',    //单价
	  pcode:'',
	  trayCode:'',
	  realQty:'',
	  sapNo:'',
	  xmNo:'',
	  xmMs:'',
	  gysMc:'',
	  remark:'',
	  gysJhId:null,
	  isnew:true
	}
	
	planList.value.push(info)
  }
  //根据物料号查询物料信息
//   function searchInfo(wlNo,index){
// 	// console.log(33333)
// 	// console.log(index)
// 	// return
// 	wlSearch(wlNo).then(res=>{
// 	  if(res.rows.length>0){
// 		let info = res.rows[0]
// 		planList.value[index].wlMs = info.desMat
// 		planList.value[index].dw = info.unt
// 		planList.value[index].htDj = info.uprc
// 	  }else{
// 		proxy.$modal.msgWarning("暂无物料信息");
// 		return
// 	  }
	  
// 	  // console.log(111111)
// 	  // console.log(res)
// 	})
//   }
  function preview(url){
	srcList.value = [url]
  }
  //勾选自动填充备注
  function autoFillFun(){
	if(autoFill.value && !kzms.value){
	  autoFill.value = false
	  proxy.$modal.msgWarning("扩展描述不能为空");
	  return
	}
	if(autoFill.value && kzms.value && planList.value.length>0){
	  for(let i=0;i<planList.value.length;i++){
		planList.value[i].remark = kzms.value
	  }
	}else if(!autoFill.value){
	  for(let i=0;i<planList.value.length;i++){
		planList.value[i].remark = ""
	  }
	}
	// console.log(autoFill.value)
  }
  // 上传成功回调
  function handleAvatarSuccess(res,id,pcode){
	// console.log(222222)
	// console.log(id)
	// console.log(pcode)
	for(let i=0;i<planList.value.length;i++){
	  if(planList.value[i].id ==id && planList.value[i].pcode==pcode){
		planList.value[i].photoUrl = res.url
	  }
	}
	// console.log(222222)
	// console.log(res)
	// console.log(row)
  }
  //删除图片
  function handleRemove(res,id,pcode){
	for(let i=0;i<planList.value.length;i++){
	  if(planList.value[i].id ==id && planList.value[i].pcode==pcode){
		planList.value[i].photoUrl = ""
	  }
	}
	// console.log(33333333)
	// console.log(planList.value)
	// console.log(res)
	// console.log(id)
	// console.log(pcode)
  }
  //预览图片
  function handlePreview(uploadFile){
	// console.log(222222)
	// console.log(res)
	dialogImageUrl.value = uploadFile.response.url
	dialogVisible.value = true
  }
  function tableRowClassName({row, rowIndex}) {
	// console.log(row)
	  // 根据每行的数据，判断isError来增加class属性
	  if (row.isAdd) {
		return 'warning-row'
	  } 
	  // else {
	  //   return 'success-row'
	  // }
	  return '';
  }
  function getSumInfo(){
	//统计信息
	getSumInfoI(queryParams.value).then(response => {
	  	totalMoney.value = response.sumMoney
	});
  }
  /** 查询库存单据主列表 */
  function getList() {
	loading.value = true;
	listStock(queryParams.value).then(response => {
	stockList.value = response.rows
	total.value = response.total;
	// stockList.value = response.dataList.rows
	// total.value = response.dataList.total;
	// totalMoney.value = response.sumMoney
	//   stockList.value = response.rows;
	//   total.value = response.total;
	  loading.value = false;
	});
  }

  //库位字典
  function pcodeList(){
	getKwList().then(response=>{
	  kwOptions.value = response.data
	})
  }
  //所属仓库
  function warehouseFun(){
	//所属仓库下拉数据
	// warehouseDict().then(response=>{
	//   warehouseList.value = response.data
	// })
	warehouseAll().then(response=>{
		warehouseOptions.value = response.data
	})
	
  }
  //物资类型
  function materialFun(){
	//获取物资类型下拉数据
	materialtypeDict().then(response=>{
	  wzTypeList.value = response.data
	})
  }
  
  // 取消按钮
  function cancel() {
	open.value = false;
	reset();
  }
  //移库取消
  function switchCancel(){
	openSwitch.value = false
	switchData.value = {}
  }
  // 表单重置
  function reset() {
	planList.value = []
	storeData.value = {}
	proxy.resetForm("stockRef");

	
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
	//入库时间
	if(rkTime.value && rkTime.value.length>0){
	//   queryParams.value.startTime = rkTime.value[0]
	//   queryParams.value.endTime = rkTime.value[1]

	  queryParams.value.statDate = rkTime.value[0]
	  queryParams.value.endDate = rkTime.value[1]
	}else{
	//   queryParams.value.startTime = ""
	//   queryParams.value.endTime = ""

	  queryParams.value.statDate = ""
	  queryParams.value.endDate = ""
	}
	//出库时间
	// if(lyTime.value && lyTime.value.length>0){
	//   queryParams.value.lyStartTime = lyTime.value[0]
	//   queryParams.value.lyEndTime = lyTime.value[1]
	// }else{
	//   queryParams.value.lyStartTime = ""
	//   queryParams.value.lyEndTime = ""
	// }
	queryParams.value.pageNum = 1;
	if(printList.value.length>0){
	  queryParams.value.ids = printList.value.map( item => item.id )	
	}
	// console.log(queryParams.value)
	getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
	proxy.resetForm("queryRef");
	rkTime.value = []
	// lyTime.value = []
	// queryParams.value.startTime = ""
	// queryParams.value.endTime = ""
	queryParams.value.parentWarehouseCode = null
	queryParams.value.warehouseCode = null
	handleQuery();
  }
  
  //新增入库 多选框选中数据
  function handleSelectionChange(selection) {
  
	checkPlanList.value = selection
	// console.log(selection)
	// ids.value = selection.map(item => item.id);
	
  }
  
  //打印单据、移库、修改  多选框选中数据
  function printSelectionChange(selection){
	printList.value = selection
	queryParams.value.ids = selection.map(item => item.id)
	single.value = selection.length != 1;
	multiple.value = !selection.length;
	// console.log(queryParams.value.ids)
  }
  function getSummaries(param) {
    // const { columns, data } = param;
	// // console.log(columns)
    // const sums = [];
    // columns.forEach((column, index) => {
    //   if (index === 0) { // 第一列不进行合计操作，通常是序列号或选择框等非数值列。
    //     sums[index] = '总计'; // 这里可以设置为其他文字或空字符串。
    //     return;
    //   } else if (index === 2) { // 第二列是金额列，进行求和操作。
	// 	if(queryParams.value.isChuku==0){
	// 		sums[index] = '入库金额：';
	// 	}else if(queryParams.value.isChuku==1){
	// 		sums[index] = '出库金额：';
	// 	}else{
	// 		sums[index] = '总金额：';
	// 	}
        
	//   }else if(index === 3){
	// 	sums[index] = totalMoney.value
	//   }
	// //   else if(index === 4){
	// // 	sums[index] = '出库金额：'
	// //   }else if(index === 5){
	// // 	sums[index] = 4444
	// //   }
	// })
	//   return sums
	}
	
  
  /** 新增按钮操作 */
  function handleAdd() {
	reset();
	open.value = true;
	title.value = "添加库存单据";
	//获取入库类型下拉数据
	warehousingDict().then(response=>{
	  storeTypeList.value = response.data
	})
	//理货员下拉数据
	personListDict().then(response=>{
	  personList.value = response.data
	})
  }
  function lhyList(){
	//理货员下拉数据
	personListDict().then(response=>{
	  personList.value = response.data
	})
  }
  function rklxList(){
	//获取入库类型下拉数据
	warehousingDict().then(response=>{
	  storeTypeList.value = response.data
	})
	  //获取出库类型下拉数据
	listOuttype({pageNum:1,pageSize:50}).then(response=>{
		ckTypeList.value = response.rows
	})
  }
  //从供应计划中添加 搜索
  function searchPlan(){
	if(!orderNum.value){
	  proxy.$modal.msgWarning("采购订单号不能为空");
	  return
	}
	//查供应计划
	getPlan(orderNum.value).then(response=>{
	  let planData = response.data
	  if(planData.length>0){
		
		for(let i=0;i<planData.length;i++){
		  planData[i].realQty = planData[i].jhQty
		  planData[i].gysJhId = planData[i].id
		  if(autoFill.value && kzms.value != ""){
			planData[i].remark = kzms.value
		  }
		  planData[i].isnew = false
		}
	  }else{
		proxy.$modal.msgWarning("暂无数据");
		return
	  }
	  // console.log(planData)
	  planList.value = planData
	  // planLoading.value = false
	})
	
  }
  //添加数量
  function addNum(row){
	let addRow = {}
	Object.assign(addRow,row)
	addRow.pcode = ''
	addRow.realQty = ''
	addRow.entityId = ''
	addRow.trayCode = ''
	addRow.remark = ''
	addRow.photoUrl = ""
	// console.log(addRow)
	addRow.isAdd = true
	// console.log(row)
	planList.value.push(addRow)
  }
  // function selectKw(row){
  //   console.log(row)
  // }
  /** 修改按钮操作 */
  function handleUpdate(row) {
	reset();
	idEdit.value = row.id
	getStock(row.id).then(response => {
	  let dataInfo = response.data
	  dataInfo.isnew = true
	  storeData.value.rkType = dataInfo.rkType
	  storeData.value.wlType = dataInfo.wlType
	  storeData.value.cangku = dataInfo.cangku
	  storeData.value.lihuoY = parseInt(dataInfo.lihuoY) 
	  planList.value = [dataInfo]
	  // form.value = response.data;
	  open.value = true;
	  title.value = "修改库存单据";
	});
  }
  function closeDialog(){
	idEdit.value = 0
  }
  
  /** 提交按钮 */
  function submitForm() {
	
	// console.log(submitData);return
	// // /pc/wisdom/stock/add
	// console.log(planList);return
	proxy.$refs["stockRef"].validate(valid => {
	  
	  if (valid) {
		// if(planList.value.filter(x=>x.realQty=="").length>0){
		//   proxy.$modal.msgError("入库数量不能为空");
		//   return
		// }
		// console.log(storeData.value.rkType)
		// return
		if(checkPlanList.value.length==0){
		  proxy.$modal.msgError("请勾选数据");
		  return
		}
		if(checkPlanList.value.filter(x=>x.realQty=="").length>0){
		  proxy.$modal.msgError("入库数量不能为空");
		  return
		}
		if(isExamine.value==1 && checkPlanList.value.filter(x=>!x.photoUrl).length>0){
		  proxy.$modal.msgError("现场图片不能为空");
		  return
		}
		let isNumTrue = false	  //入库数量是否正确
		let iscfwz = false      //库位是否为空
		let bigData = checkPlanList.value
		let smallData = checkPlanList.value
		for(let i=0;i<bigData.length;i++){
		  // let sumNum = 0
		  //库位
		  if(!bigData[i].pcode){
			iscfwz = true
		  }
		  //入库数量
		  // for(let j=0;j<smallData.length;j++){
		  //   if(bigData[i].id == smallData[j].id){
		  //     sumNum += Number(smallData[j].realQty)*1000
		  //   }
		  // }
		  // if(bigData[i].jhQty != sumNum/1000){
		  //   isNumTrue = true
		  //   break;
		  // }
		}
		// if(isNumTrue){
		//   proxy.$modal.msgError("入库数量不正确");
		//   return
		// }
		//发现场入库类型可以不用选库位
		if(storeData.value.rkType!="RK025" && iscfwz){
		  proxy.$modal.msgError("请选择库位");
		  return
		}
		if(isExamine.value==1 && !storeData.value.approverId){
		  proxy.$modal.msgError("审批人不能为空");
		  return
		}
		// console.log(checkPlanList.value)
		// return
		
		
		// console.log(submitData)
		// return
		if(idEdit.value!=0){  //修改
		  let submitData = {
			cangku:storeData.value.cangku,    //所属仓库
			lihuoY:storeData.value.lihuoY,    //理货员
			rkType:storeData.value.rkType,    //入库类型
			wlType:storeData.value.wlType,    //物资类型
			approverId:storeData.value.approverId,  //审批人
			id:idEdit.value,
			// rkList:planList.value
			...checkPlanList.value[0]
		  }
		  // console.log(submitData)
		  // return
		  updateStock(submitData).then(response=>{
			proxy.$modal.msgSuccess("修改成功");
			open.value = false;
			getList();
		  })
		}else{  //新增
		  let submitData = {
			cangku:storeData.value.cangku,    //所属仓库
			lihuoY:storeData.value.lihuoY,    //理货员
			rkType:storeData.value.rkType,    //入库类型
			wlType:storeData.value.wlType,    //物资类型
			approverId:storeData.value.approverId,  //审批人
			id:idEdit.value,
			// rkList:planList.value
			rkList:checkPlanList.value
		  }
		  addStock(submitData).then(response => {
			  proxy.$modal.msgSuccess("新增成功");
			  open.value = false;
			  getList();
		  });
		}
		
	  }
	});
  }
  //移库弹框
  function handleSwitch(){
	switchData.value = []
	moveReason.value = ""
	if(printList.value.length!=1){
	  proxy.$modal.msgError("请勾选一条数据");
	  return
	}
	if(printList.value[0].isChuku==1){
	  proxy.$modal.msgError("已出库，不允许移库");
	  return
	}
	//库位下拉数据
	pcodeList()
	openSwitch.value = true
	const {
	  realQty:realQty,
	  pcode:toPcode,
	  trayCode:toTrayCode,
	  cangku:toCangku
	} = printList.value[0]
	// console.log({realQty,toPcode})
	// switchData.value = printList.value
	switchData.value[0] = {
	  realQty,
	  toPcode,
	  toTrayCode,
	  toCangku
	}
	// console.log(printList.value)
  }
  //减一行
  function handleDelete(index){
	// console.log(index)
	switchData.value.splice(index, 1);
  }
  //加一行
  function addLine(){
	const addData = {
	  realQty:0,
	  toPcode:"",
	  toTrayCode:"",
	  toCangku:""
	}
	switchData.value.push(addData)
  }
  //移库提交
  function submitMoveForm(){
	if(switchData.value.length==0){
	  proxy.$modal.msgError("请添加数据");
	  return
	}
	if(switchData.value.filter(item=>item.realQty==0).length>0){
	  proxy.$modal.msgError("数量不能为空");
	  return
	}
	if(switchData.value.filter(item=>!item.toPcode).length>0){
	  proxy.$modal.msgError("存放位置不能为空");
	  return
	}
	if(switchData.value.filter(item=>!item.toCangku).length>0){
	  proxy.$modal.msgError("仓库不能为空");
	  return
	}
	// if(switchData.value.length==1 && printList.value[0].realQty==switchData.value[0].realQty && printList.value[0].pcode==switchData.value[0].toPcode && printList.value[0].cangku==switchData.value[0].toCangku){
	//   proxy.$modal.msgError("请选择新的存放位置");
	//   return
	// }
	if(switchData.value.filter(x=>x.toPcode==printList.value[0].pcode).length>0){
	  proxy.$modal.msgError("不允许选择原来的库位【"+printList.value[0].pcode+"】");
	  return
	}
	let sumNum=0
	for(let i=0;i<switchData.value.length;i++){
	  sumNum += Number(switchData.value[i].realQty)*1000
	}
	if(printList.value[0].realQty<sumNum/1000){
	  proxy.$modal.msgError("移库数量不正确");
	  return
	}
	let submitData = {
	  fromRkId:printList.value[0].id,
	  moveReason:moveReason.value,
	  targets:switchData.value
	}
	moveHouse(submitData).then(response=>{
	  if(response.code==200){
		proxy.$modal.msgSuccess("操作成功");
		//数据列表
		getList();
		openSwitch.value = false
	  }else{
		proxy.$modal.msgError("操作失败");
	  }
	  
	})
	// console.log(submitData)
  }
  function cancelMove(){
	openSwitch.value = false
  }
  //移库提交
  // function submitSwitchData(){
  //   proxy.$refs["switchRef"].validate(valid => {
  //     if (valid){
  //       // console.log(switchData.value)
  //       let submitData = {}
  //       submitData.rkId = switchData.value.id
  //       submitData.entityId = switchData.value.entityId   //实物ID(身份码)
  //       submitData.fromCangku = switchData.value.cangku   //原仓库
  //       submitData.fromPcode = switchData.value.pcode   //原库位码(原存放位置)
  //       submitData.fromTrayCode = switchData.value.trayCode   //原托盘码(原容器码)
  //       submitData.toCangku = switchData.value.toCangku   //目标仓库
  //       submitData.toPcode = switchData.value.toPcode   //目标库位码
  //       submitData.toTrayCode = switchData.value.toTrayCode   //目标托盘码
  //       submitData.moveReason = switchData.value.moveReason   //移库原因
  //       moveHouse(submitData).then(response=>{
  //         if(response.code==200){
  //           proxy.$modal.msgSuccess("操作成功");
  //           //数据列表
  //           getList();
  //           openSwitch.value = false
  //         }else{
  //           proxy.$modal.msgError("操作失败");
  //         }
		  
  //       })
  //     }
  //   })
	
  // }
  
  /** 删除按钮操作 */
  // function handleDelete(row) {
  //   const _ids = row.id || ids.value;
  //   proxy.$modal.confirm('是否确认删除库存单据主编号为"' + _ids + '"的数据项？').then(function() {
  //     return delStock(_ids);
  //   }).then(() => {
  //     getList();
  //     proxy.$modal.msgSuccess("删除成功");
  //   }).catch(() => {});
  // }
  
  // 撤销入库
  const handleStockDelete = (row) => {
	// dialogDelStock.value = true
	proxy.$modal.confirm('确定撤销吗？').then(function() {
	  if(row.isChuku==0){
		let data = {
		  id:row.id,
		  realQty:row.realQty,
		  gysJhId:row.gysJhId
		}
		return wisdomDelStock(data);
	  }else if(row.isChuku==1){
		return ckDelStock(row.id)
	  }
	  
	}).then(() => {
	  getList();
	  proxy.$modal.msgSuccess("操作成功");
	}).catch(() => {});
  }
  
  const dialogDelStock = ref(false)
  const delStockFormRef = ref(null)
  const delStockForm = ref({
	billNo:''
  })
  const delStockRules = ref({
	billNo: [{ required: true, message: '请输入单据号', trigger: ['blur'] }],
  })
  const submitDelStock = () => {
	proxy.$refs["delStockFormRef"].validate(valid => {
	  if (valid){
		wisdomDelStock(delStockForm.value.billNo).then(response=>{
		  if(response.code==200){
			proxy.$modal.msgSuccess("操作成功");
			//数据列表
			getList();
			dialogDelStock.value = false
		  }else{
			proxy.$modal.msgError("操作失败");
		  }
		})
	  }
	})
  }
  const cancelDelStock = () => {
	dialogDelStock.value = false
  }
  /** 导出按钮操作 */
  function handleExport() {
	// wisdom/stock/export
	let obj = JSON.parse(JSON.stringify(queryParams.value))
	delete obj.pageNum
	delete obj.pageSize
	proxy.download('wisdom/stock/exportPageList', {
	  ...obj
	}, `出入库记录_${parseTime(new Date().getTime(), '{y}年{m}月{d}日 {h}_{i}_{s}')}.xlsx`)
	}
  //打印单据
  function handlePrint(){
	if(printList.value.length==0){
	  proxy.$modal.msgError("请勾选数据");
	  return
	}
	const date = new Date();
	const year = date.getFullYear();
	const month = date.getMonth() + 1; // 月份从0开始，需要加1
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	dateStr.value = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second
	showContent.value = true
  }
  getSumInfo()
  //数据列表
  getList();
  //库位下拉数据
  pcodeList()
  //所属仓库
  warehouseFun()
  //物资类型
  materialFun()

  //出、入库类型下拉数据
  rklxList()
  //理货员下拉数据
  lhyList()

  const isChukuList = ref([
  { value: '入库', label: '入库', elTagType: "success", elTagClass: null },
  { value: '出库', label: '出库', elTagType: "warning", elTagClass: null },
])
const dictTagData = () => {
  return isChukuList.value.map(item => ({
    ...item,
    value: String(item.value)
  }));
}
  </script>
  <style scoped>
  /* .el-table .warning-row {
	--el-table-tr-bg-color: var(--el-color-warning-light-9);
  }
  .el-table .success-row {
	--el-table-tr-bg-color: var(--el-color-success-light-9);
  }
  .titleBox{
	text-align: center;
  }
  .topBox{
	margin-bottom: 10px;
	display: flex;
  }
  .bottomBox{
	margin-top: 20px;
  }
  .oneLineBox{
	display: flex;
  }
  .everyBox{
	width: 30%;
  }
  .examine{
	margin-top: 20px;
	height: 100px;
	background-color: #ededed;
	padding: 20px 0 0 20px;
	border-radius: 10px;
  } */
  /* ::v-deep .el-table .cell {
    white-space: nowrap;
	} */
  </style>
  