<template>
	<div class="app-container">
	  <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
		<!-- 搜索项 -->
		<el-form-item label="库位编号" prop="pcode">
		  <el-input v-model="queryParams.pcode" placeholder="请输入库位编号" clearable @keyup.enter="handleQuery" />
		</el-form-item>
		<el-form-item label="所属场景" prop="scene">
		  <el-select v-model="queryParams.scene" placeholder="请选择所属场景" style="width: 200px" clearable filterable>
			<el-option
				v-for="item in sceneOptions"
				:key="item.id"
				:label="item.sceneName"
				:value="item.sceneCode"
			/>
		  </el-select>
		</el-form-item>
		<el-form-item label="所属大库" prop="parentWarehouseCode">
			<el-select v-model="queryParams.parentWarehouseCode" placeholder="请选择所属大库" style="width: 200px" clearable filterable @change="selectBigWarehouse" @clear="clearFun">
				<el-option
					v-for="item in warehouseOptions"
					:key="item.warehouseCode"
					:label="item.warehouseName"
					:value="item.warehouseCode"
				/>
			</el-select>
		</el-form-item>
		<el-form-item label="所属小库" prop="warehouseCode">
			<el-select v-model="queryParams.warehouseCode" placeholder="请选择所属小库" style="width: 200px" clearable filterable>
			<el-option
				v-for="item in warehouseList"
				:key="item.warehouseCode"
				:label="item.warehouseName"
				:value="item.warehouseCode"
			/>
			</el-select>
		</el-form-item>
		<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-form>
  
	  <el-row :gutter="10" class="mb8">
		<el-col :span="1.5">
		  <el-button type="primary" plain icon="Plus" @click="handleBatchPrint" :disabled="multiple">批量打印</el-button>
		</el-col>
		<el-col :span="1.5">
		  <el-button type="success" plain icon="Edit"  @click="handleHandPrint" >手动打印</el-button>
		</el-col>
		<right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
	  </el-row>
  
	  <el-table v-loading="loading" :data="pcdedetailList" @selection-change="handleSelectionChange">
		<el-table-column type="selection" width="55" align="center" />
		<el-table-column label="库位编号" align="center" prop="pcode" />
		<el-table-column label="RFID" align="center" width="250" prop="encodedId" />
		<el-table-column label="所属场景" align="center" prop="sceneName" />
		<el-table-column label="所属大库" align="center" prop="parentWarehouseName" />
		<el-table-column label="所属小库" align="center" prop="warehouseName" />
		<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
		  <template #default="scope">
			<el-button link type="primary" icon="Edit" @click="handleOnePrint(scope.row)" >打印</el-button>
			
		  </template>
		</el-table-column>
	  </el-table>
  
	  <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
  
	  <!-- <el-dialog :title="title" v-model="open" width="500px" append-to-body>
		<el-form ref="pcdedetailRef" :model="form" :rules="rules" label-width="80px">
		  <el-form-item label="库位编号" prop="pcode">
			<el-input v-model="form.pcode" placeholder="请输入库位编号" />
		  </el-form-item>
		  <el-form-item label="所属仓库" prop="warehouse">
			<el-select v-model="form.warehouse" placeholder="请选择所属仓库" clearable filterable style="width: 100%">
			  <el-option
				v-for="item in warehouseOptions"
				:key="item.id"
				:label="item.warehouseName"
				:value="item.warehouseCode"
			  />
			</el-select>
		  </el-form-item>
		  <el-form-item label="所属场景" prop="scene">
			<el-select v-model="form.scene" placeholder="请选择所属场景" clearable filterable @change="handleQuery">
			  <el-option
				  v-for="item in sceneOptions"
				  :key="item.id"
				  :label="item.sceneName"
				  :value="item.sceneCode"
			  />
			</el-select>
		  </el-form-item>
		  <el-form-item label="标签" prop="tag">
			<el-input v-model="form.tag" placeholder="请输入标签" />
		  </el-form-item>
		  <el-form-item label="备注" prop="remark">
			<el-input v-model="form.remark" placeholder="请输入备注" />
		  </el-form-item>
		</el-form>
		<template #footer>
		  <div class="dialog-footer">
			<el-button type="primary" @click="submitForm">确 定</el-button>
			<el-button @click="cancel">取 消</el-button>
		  </div>
		</template>
	  </el-dialog> -->

	</div>
  </template>
  
  <script setup name="print">
  import {listPcdedetail,getAllScenes} from "@/api/information/pcdedetail";
  import {printFun} from "@/api/information/print";
  import { warehouseDict, warehouseAll} from "@/api/information/warehouseinfo";
  import { ElMessage, ElMessageBox } from 'element-plus'
import { isEmpty } from "element-plus/es/utils/types.mjs";

  
  const { proxy } = getCurrentInstance();
  
  const pcdedetailList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  const sceneOptions = ref([]);
  const warehouseOptions = ref([]);
  
  const data = reactive({
	form: {},
	queryParams: {
	  pageNum: 1,
	  pageSize: 10,
	  pcode: null,
	  scene: null,
	  warehouseName: null,
	  encodedId: null,
	  tag: null,
	  remark: null,
	  isDelete: null,
	  createdBy: null,
	  createdAt: null,
	  updatedBy: null,
	  updatedAt: null
	}
  });
  
  const { queryParams, form } = toRefs(data);
  

  

  
const warehouseList = ref([]);  

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

}

  
  function getList() {
	loading.value = true;
	//获取列表数据
	listPcdedetail(queryParams.value).then(response => {
	  pcdedetailList.value = response.rows;
	  total.value = response.total;
	  loading.value = false;
	});
	//获取所属场景下拉数据
	getAllScenes().then(res => {
	  sceneOptions.value = res.data || [];
	});
	//获取所属仓库下拉数据
	// warehouseDict().then(res => {
	//   warehouseOptions.value = res.data || [];
	// });
	warehouseAll().then(res => {
		warehouseOptions.value = res.data || [];
	});
  }
  
  function cancel() {
	open.value = false;
	reset();
  }
  
  function reset() {
	form.value = {
	  id: null,
	  pcode: null,
	  scene: null,
	  warehouse: null,
	  encodedId: null,
	  tag: null,
	  remark: null,
	  isDelete: null,
	  createdBy: null,
	  createdAt: null,
	  updatedBy: null,
	  updatedAt: null
	};
	proxy.resetForm("pcdedetailRef");
  }
  
  function handleQuery() {
	queryParams.value.pageNum = 1;
	getList();
  }
  
  function resetQuery() {
	proxy.resetForm("queryRef");
	handleQuery();
  }
  
  function handleSelectionChange(selection) {
	ids.value = selection.map(item => item.id);
	single.value = selection.length !== 1;
	multiple.value = !selection.length;
  }
  //批量打印
  function handleBatchPrint() {
	const dataInfo = {
		printType:1,
		ids:ids.value
	}
	// console.log(dataInfo)
	// return
	printFun(dataInfo).then(response=>{
		if(response == '打印成功'){
			proxy.$modal.msgSuccess("打印成功");
		}
		
	})
  }
  //单个打印
  function handleOnePrint(row) {
	const dataInfo = {
		printType:1,
		ids:[row.id]
	}
	// console.log(dataInfo)
	// return
	printFun(dataInfo).then(response=>{
		if(response == '打印成功'){
			proxy.$modal.msgSuccess("打印成功");
		}
		
	})
  }
  //输入内容 手动打印
  function handleHandPrint(){
	ElMessageBox.prompt('请输入打印内容', '', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    inputErrorMessage: '打印内容不能为空',
	inputValidator(value) {
          if (!value) {
            return '打印内容不能为空';
          } else {
            return true;
          }
        }
  })
    .then(({ value }) => {
		const dataInfo = {
			printType:2,
			printStr:value
		}
		printFun(dataInfo).then(response=>{
			if(response == '打印成功'){
				proxy.$modal.msgSuccess("打印成功");
			}
		})
    //   ElMessage({
    //     type: 'success',
    //     message: `Your email is:${value}`,
    //   })
    })
    .catch(() => {
    //   ElMessage({
    //     type: 'info',
    //     message: 'Input canceled',
    //   })
    })
  }
  
  function submitForm() {
	proxy.$refs["pcdedetailRef"].validate(valid => {
	  if (valid) {
		if (form.value.id != null) {
		  updatePcdedetail(form.value).then(() => {
			proxy.$modal.msgSuccess("修改成功");
			open.value = false;
			getList();
		  });
		} else {
		  addPcdedetail(form.value).then(() => {
			proxy.$modal.msgSuccess("新增成功");
			open.value = false;
			getList();
		  });
		}
	  }
	});
  }
  

  

  
  getList();
  
  </script>
  