<template>
  <div>
    <!-- 左侧树形结构 -->
    <el-container>
      <el-aside width="250px">
        <div class="JKWTree-tree">
          <h3>成品入仓管理</h3>
          <el-tree :data="treeData" :props="defaultProps" highlight-current="true"
            @node-click="handleNodeClick"></el-tree>
        </div>
      </el-aside>

      <el-container>
        <el-main>
          <!-- 按钮区域 -->
          <div class="gva-table-box">
            <div class="gva-btn-list">
              <el-row :span="6">
                <el-input v-model="positionvalue" placeholder="输入工单编号或产品名称" />
              </el-row>
              <el-button type="primary" :icon="Search" @click="onSearch">搜索</el-button>
              <el-button type="primary" @click="onDel">删除</el-button>
			  <el-button type="primary" @click="onSMB">入仓末板数据统计</el-button>
			  <el-button type="primary" @click="onDay">各日统计</el-button>
			  <el-button type="primary" @click="onAdd">新增</el-button>
			  
              <div style="margin-left: auto;">			
                <el-button type="primary" :icon="Download" @click="exportExcel">导出到Excel</el-button>
              </div>
            </div>
            <!-- 数据展示 -->
            <el-table ref="multipleTable" style="width: 100%" tooltip-effect="dark" :data="tableData" row-key="ID" @row-click="Click"
              highlight-current-row="true" border @selection-change="handleSelectionChange" :show-overflow-tooltip="true" @row-dblclick="doubleClick">
              <el-table-column type="selection" width="55" />
			  <el-table-column align="left" sortable  label="入仓类型" prop="入仓类型" width="120"/>
              <el-table-column align="left" sortable  label="仓库编号" prop="仓库编号" width="120"/>
              <el-table-column align="left"  sortable label="仓库名称" prop="仓库名称" width="120"/>
              <el-table-column align="left" sortable  label="入仓单号" prop="jjcp_num" width="120"/>
              <el-table-column align="left" sortable  label="工单编号" prop="jjcp_gdbh" width="160"/>
              <el-table-column align="left" sortable  label="印件号" prop="jjcp_yjno" width="160"/>
              <el-table-column align="left" sortable  label="入仓数量" prop="jjcp_sl" width="120"/>
			  <el-table-column align="left" sortable  label="累计入仓数量" prop="jjcp_sls" width="120"/>
			  <el-table-column align="left" sortable label="订单数量" prop="订单数量" width="120"/>
			  <el-table-column align="left" sortable label="完成率" prop="完成率" width="120"/>
			  <el-table-column align="left" sortable label="单位" prop="jjcp_dw" width="120"/>
			  <el-table-column align="left" sortable label="入仓日期" prop="jjcp_sj" width="160"/>
			  <el-table-column align="left" sortable label="首末版" prop="jjcp_smb" width="160"/>
			  <el-table-column align="left" sortable label="印件代号" prop="jjcp_cpdh" width="120"/>
			  <el-table-column align="left" sortable label="印件名称" prop="jjcp_cpmc" width="120"/>
			  <el-table-column align="left" sortable label="产品编号" prop="成品编码" width="120"/>
			  <el-table-column align="left" sortable label="产品名称" prop="成品名称" width="120"/>
			  <el-table-column align="left" sortable label="客户料号" prop="客户料号" width="120"/>
			  <el-table-column align="left" sortable label="备注" prop="jjcp_desc" width="160"/>
			  <el-table-column align="left" sortable label="客户编号" prop="客户编号" width="160"/>
			  <el-table-column align="left" sortable label="客户名称" prop="Gd_客户名称" width="120"/>
			  <el-table-column align="left" sortable label="最近入仓日期" prop="最近入仓日期" width="120"/>
			  <el-table-column align="left" sortable label="机型备注" prop="机型备注" width="120"/>
			  <el-table-column align="left" sortable label="创建用户" prop="Sys_id" width="120"/>
			  <el-table-column align="left" sortable label="创建时间" prop="sys_rq" width="160"/>
			  <el-table-column align="left" sortable label="修改时间" prop="mod_rq" width="160"/>
			  <el-table-column align="left" sortable label="UNIQID" prop="UniqId" width="120"/>
            </el-table>
            <!-- 分页 -->
            <div class="gva-pagination">
              <el-pagination layout="total, sizes, prev, pager, next, jumper" v-model:current-page="page"
                v-model:page-size="limit"
                :page-sizes="[10, 30, 50, 100]" :total="total" @current-change="handleCurrentChange"
                @size-change="handleSizeChange" />
            </div>
          </div>
          <!-- 弹出框 -->
          <el-dialog v-model="dialogFormVisible" :before-close="closeDialog" :title="type === 'create' ? '添加' : '修改'"
            destroy-on-close width="1200px">
            <!-- <el-scrollbar height="500px"> -->
            <el-form :model="formData" label-position="left" ref="elFormRef" :rules="rule">

              <el-row :gutter="20">
			 <el-col :span="4">
			   <el-form-item label="入仓类型" prop="id">
				 <el-input v-model="formData.入仓类型" id="入仓类型"  @keydown="ent($event, '入仓备注', '入仓类型', '仓库编号')" :clearable="true" placeholder="" />
			   </el-form-item>
			 </el-col>
			 <el-col :span="6">
			   <el-form-item label="仓库" prop="name">
				 <el-row :gutter="20">
				   <el-col :span="12">
					 <el-input v-model="formData.仓库编号" id="仓库编号"  @keydown="ent($event, '入仓类型', '仓库编号', '仓库名称')"  @keyup.enter.native="getCKsubmit" :clearable="true" placeholder="ZT01733" />
				   </el-col>   
				   <el-col :span="12">
					 <el-input v-model="formData.仓库名称" id="仓库名称"  @keydown="ent($event, '仓库编号', '仓库名称', '单据编号')" :clearable="true" placeholder="" />
				   </el-col>  
				 </el-row>
			   </el-form-item>
			 </el-col>
		   </el-row> 
		   
		   
		   <el-row :gutter="20">
			 <el-col :span="4">
			   <el-form-item label="单据编号" prop="id">
				 <el-input v-model="formData.jjcp_num"  id="单据编号"  @keydown="ent($event, '仓库名称', '单据编号', '工单编号')" :clearable="true" placeholder="" />
			   </el-form-item>
			 </el-col>
		   </el-row> 
		   
		   
		   
		   <el-row :gutter="20">
			 <el-col :span="4">
			   <el-form-item label="工单编号" prop="id">
				 <el-input v-model="formData.jjcp_gdbh" id="工单编号"  @keydown="ent($event, '单据编号', '工单编号', '印件号')" @keyup.enter.native="getGDsubmit" :clearable="true" placeholder="" />
			   </el-form-item>
			 </el-col>
			 <el-col :span="6">
			   <el-form-item label="印件号" prop="name">
				 <el-row :gutter="20">
				   <el-col :span="12">
					 <el-input v-model="formData.jjcp_yjno" id="印件号"  @keydown="ent($event, '工单编号', '印件号', '订单编号')" :clearable="true" placeholder="" />
				   </el-col>  				
				 </el-row>
			   </el-form-item>	   
			 </el-col>
			 <el-col :span="10">
			   <el-form-item label="订单编号" prop="name">
				 <el-row :gutter="20">
				   <el-col :span="20">
					 <el-input v-model="formData.订单编号" id="订单编号"  @keydown="ent($event, '印件号', '订单编号', '印件编号')" :clearable="true" placeholder="" />
				   </el-col>  				
				 </el-row>
			   </el-form-item>
			 </el-col>
		   </el-row> 
		   
           <el-row :gutter="20">
			 <el-col :span="4">
			   <el-form-item label="印件编号" prop="id">
				 <el-input v-model="formData.jjcp_cpdh" id="印件编号"  @keydown="ent($event, '订单编号', '印件编号', 'jjcp_cpmc')" :clearable="true" placeholder="" />
			   </el-form-item>
			 </el-col>
			 <el-col :span="10">
			 <el-input v-model="formData.jjcp_cpmc"  id="jjcp_cpmc"  @keydown="ent($event, '印件编号', 'jjcp_cpmc', '产品编号')" :clearable="true" placeholder="" />					   
			 </el-col>
           </el-row> 
            
			
			<el-row :gutter="20">
			 <el-col :span="4">
			   <el-form-item label="产品编号" prop="id">
				 <el-input v-model="formData.jjcp_cpdh" id="产品编号"  @keydown="ent($event, 'jjcp_cpmc', '产品编号', '成品名称')" @keyup.enter.native="getSLsubmit" :clearable="true" placeholder="" />
			   </el-form-item>
			 </el-col>
			 <el-col :span="10">
			 <el-input v-model="formData.成品名称" id="成品名称"  @keydown="ent($event, '产品编号', '成品名称', '客户料号')" :clearable="true" placeholder="" />					   
			 </el-col>
			</el-row> 

            <el-row :gutter="20">
			 <el-col :span="4">
			   <el-form-item label="客户料号" prop="id">
				 <el-input v-model="formData.客户料号" id="客户料号"  @keydown="ent($event, '成品名称', '客户料号', '首末版')" :clearable="true" placeholder="" />
			   </el-form-item>
			 </el-col>
            </el-row> 
			  
			  <el-row :gutter="20">
			   <el-col :span="4">
			     <el-form-item label="首末版" prop="id">
			  	 <el-input v-model="formData.jjcp_smb"  id="首末版"  @keydown="ent($event, '客户料号', '首末版', '入仓时间')" :clearable="true" placeholder="" />
			     </el-form-item>
			   </el-col>
			   <el-col :span="4">
				   <el-form-item label="入仓时间" prop="id">
			   <el-input v-model="formData.jjcp_sj"  id="入仓时间"  @keydown="ent($event, '首末版', '入仓时间', '入仓数量')" :clearable="true" placeholder="" />	
					</el-form-item>
			   </el-col>
			  </el-row> 
			  
			  
			  <el-row :gutter="20">
			   <el-col :span="4">
			     <el-form-item label="入仓数量" prop="id">
			  	 <el-input v-model="formData.jjcp_sl" id="入仓数量"  @keydown="ent($event, '入仓时间', '入仓数量', '入仓单位')" :clearable="true" placeholder="" />
			     </el-form-item>
			   </el-col>
			   <el-col :span="4">
			  	<el-form-item label="入仓单位" prop="id">
			   <el-input v-model="formData.jjcp_dw" id="入仓单位"  @keydown="ent($event, '入仓数量', '入仓单位', '订单数量')" :clearable="true" placeholder="" />	
			    </el-form-item>
			   </el-col>
			  </el-row> 
			  
			  
			  <el-row :gutter="20">
			   <el-col :span="4">
			     <el-form-item label="订单数量" prop="id">
			  	 <el-input v-model="formData.订单数量" id="订单数量"  @keydown="ent($event, '入仓单位', '订单数量', '已完工')" :clearable="true" placeholder="" />
			     </el-form-item>
			   </el-col>
			   <el-col :span="4">
			  	<el-form-item label="已完工" prop="id">
			   <el-input v-model="formData.jjcp_sls" id="已完工"  @keydown="ent($event, '订单数量', '已完工', '机型备注')" :clearable="true" placeholder="" />	
			    </el-form-item>
			   </el-col>
			   <el-col :span="4">
			   			  	<el-form-item label="机型备注" prop="id">
			   <el-input v-model="formData.机型备注" id="机型备注"  @keydown="ent($event, '已完工', '机型备注', '入仓备注')" :clearable="true" placeholder="" />	
			    </el-form-item>
			   </el-col>
			  </el-row> 
			  
			  
			  <el-row :gutter="20">
				 <el-col :span="4">
				   <el-form-item label="入仓备注" prop="id">
					 <el-input v-model="formData.jjcp_desc"  id="入仓备注"  @keydown="ent($event, '机型备注', '入仓备注', '入仓类型')":clearable="true" placeholder="" />
				   </el-form-item>
				 </el-col>
			  </el-row> 
			  
            </el-form>
            <!-- </el-scrollbar> -->
            <template #footer>
              <div class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="enterDialog">确 定</el-button>
              </div>
            </template>
          </el-dialog>



          <el-dialog
            v-model="CKVisible"
            title="选择"
            destroy-on-close
            width="600px"
          			>
            <el-table
              tooltip-effect="dark"
              :data="CKselectData"
              row-key="ID"
              highlight-current-row="true"
              border
              style="width:100%"
              @row-dblclick="CKSelectClick"
            >
              <el-table-column
                prop="编号"
                label="编号"
                width="200"
              />
			  <el-table-column
				prop="名称"
				label="名称"
				width="200"
			  />		      	 
            </el-table>
          </el-dialog>
		  
		  
		  
		  <el-dialog
		    v-model="GDVisible"
		    title="选择"
		    destroy-on-close
		    width="600px"
		  			>
		    <el-table
		      tooltip-effect="dark"
		      :data="GDselectData"
		      row-key="ID"
		      highlight-current-row="true"
		      border
		      style="width:100%"
		      @row-dblclick="GDSelectClick"
		    >
			<el-table-column
			  prop="gdyj"
			  label="工单印件"
			  width="600"
			/>
		      <el-table-column
		        prop="Gd_gdbh"
		        label="工单编号"
		        width="200"
		      />
			  <el-table-column
				prop="yj_yjno"
				label="印件号"
				width="200"
			  />
			  <el-table-column
			    prop="销售订单号"
			    label="销售订单号"
			    width="200"
			  />
			  <el-table-column
				prop="yj_Yjdh"
				label="印件编码"
				width="200"
			  />
			  <el-table-column
			    prop="yj_yjmc"
			    label="印件名称"
			    width="200"
			  />
			  <el-table-column
				prop="成品代号"
				label="成品代号"
				width="200"
			  />
			  <el-table-column
			    prop="成品名称"
			    label="成品名称"
			    width="200"
			  />
			  <el-table-column
				prop="客户料号"
				label="客户料号"
				width="200"
			  />
		    </el-table>
		  </el-dialog>
		  
		  
		  <el-dialog
		    v-model="TJVisible"
		    title="各日统计"
		    destroy-on-close
		    width="80%px;"
			style="height: 80%;"
			
		  			>
			<el-row :gutter="20">
			<el-col :span="41">
			  <el-form-item label="关键字" prop="id">
			  <el-input v-model="keywords" :clearable="true" placeholder="2023.12.14" />
			  </el-form-item>
			</el-col>
			 </el-row> 
		    <el-table
		      tooltip-effect="dark"
		      :data="TJselectData"
		      row-key="ID"
		      highlight-current-row="true"
		      border
		      style="width:100%"
			  height="300px"
		      @row-dblclick="TJSelectClick"
		    >
		      <el-table-column
		        prop="jjcp_sj"
		        label="完工日期"
		        width="200"
		      />
			  <el-table-column
				prop="jjcp_gdbh"
				label="工单编号"
				width="200"
			  />
			  <el-table-column
			    prop="jjcp_cpmc"
			    label="印件(产品名称)"
			    width="200"
			  />
			  <el-table-column
				prop="实际投料"
				label="实际投料"
				width="200"
			  />
			  <el-table-column
			    prop="入仓数量"
			    label="入仓数量"
			    width="200"
			  />
			  <el-table-column
				prop="投入产出率"
				label="投入产出率"
				width="200"
			  />
		    </el-table>
		  </el-dialog>
		  
		  
		  <el-dialog
		    v-model="SMVisible"
		    title="选择"
		    destroy-on-close
		    width="600px"
		  			>
		    <el-table
		      tooltip-effect="dark"
		      :data="SMselectData"
		      row-key="ID"
		      highlight-current-row="true"
		      border
		      style="width:100%"
		      @row-dblclick="SMSelectClick"
		    >
				<el-table-column
				  prop="gdyj"
				  label="工单编号"
				  width="180"
				/>
		      <el-table-column
		        prop="Gd_gdbh"
		        label="印件号"
		        width="200"
		      />
			  <el-table-column
				prop="yj_yjno"
				label="印件名称"
				width="200"
			  />
			  <el-table-column
				prop="销售订单号"
				label="计划交货数量"
				width="200"
			  />
			  <el-table-column
				prop="yj_Yjdh"
				label="印件投料"
				width="200"
			  />
			  <el-table-column
				prop="yj_yjmc"
				label="包装产量"
				width="200"
			  />
			  <el-table-column
				prop="成品代号"
				label="入仓数量"
				width="200"
			  />
			  <el-table-column
				prop="成品名称"
				label="缺数"
				width="200"
			  />
			  <el-table-column
				prop="客户料号"
				label="完工"
				width="200"
			  />
			  <el-table-column
				prop="客户料号"
				label="最近入仓时间"
				width="200"
			  />
		    </el-table>
		  </el-dialog>
		  
        </el-main>
      </el-container>
    </el-container>

  </div>
</template>

<script setup>
import {
  finishedTab,
  finishedTabByGdbh,
  finishedList,
  finishedlocate,
  finishedInfo,
  finishedgetCk,
  finishedgetGd,
  finishedgetGdsl,
  finisheddel,
  getDayList,
  mbList,
  finishedadd
} from '@/api/jixiaoguanli/jitairibaobiao'

// 全量引入格式化工具 请按需保留
import { getDictFunc, formatDate, formatBoolean, filterDict, ReturnArrImg, onDownloadFile } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh, Download } from '@element-plus/icons-vue'
import { ref, reactive, onMounted, onBeforeMount } from 'vue'
import { getPackingSideTable, getPackingTable } from '@/api/mes_api_gty/myapi'
import { TRUE } from 'sass'

defineOptions({
  name: '06-packingDocuments'
})

// 侧边栏数据请求
const treeData = reactive([]);

const getTabdata = async () => {
    //接口调用函数
    const response  = await finishedTab();
	console.log(response)
	
	
	const transformedData = response.data.map(item => ({
	      label: `${item.date.replace(/-/g, '.')}【单据数: ${item.counts}张】`,
	      children: item.sys.map(sysItem => ({
	        label: `${sysItem.sys_id} 【记录数: ${sysItem.count}张】`,
	        params: {
	          date: item.date.replace(/\./g, '-'),
	          sys_id: sysItem.sys_id,
	          total: sysItem.count,
	        },
	      })),
	    }));
	    treeData.splice(0, treeData.length, ...transformedData);

	
	}
	

getTabdata();

// 自动化生成的字典（可能为空）以及字段
const formData = ref({
})

// 验证规则
const rule = reactive({
})

const elFormRef = ref()
const elSearchFormRef = ref()

// =========== 表格控制部分 ===========
const tableData = reactive([])
const detailData = reactive([
])
const total = ref(0)
const page = ref(1)
const limit = ref(10)
const searchInfo = ref('')
const params = {
  type: '',
  date: '',
  sys_id: '',
  gdbh: '',
}

// 分页设置
const handleSizeChange = (val) => {
  switch (params.type) {
    case 'getTableData':
      getTableData()
      break
    case 'getLocateTable':
      GetmachineLocate()
      break
    default:
      break
  }
}

// 页面跳转
const handleCurrentChange = (val) => {
  switch (params.type) {
    case 'getTableData':
      getTableData()
      break
    case 'getLocateTable':
      GetmachineLocate()
      break
    default:
      break
  }
}

// 查询
const getTableData = async () => {
	
	
  const response = await finishedList(params);
  console.log(response)
  if (response.code === 0) {
    tableData.splice(0, tableData.length, ...response.data.rows);
  }
}

const handleNodeClick = (node, check) => {
  //存放当前节点的nodeId
  if (node.params) {
    params.date = node.params.date;
    params.sys_id = node.params.sys_id;
    total.value = node.params.total;
    getTableData();
  }
}
let positionvalue=ref()
let keywords=ref()
// 搜索
function onSearch() {
  params.gdbh = positionvalue.value
  params.type = 'getLocateTable'
  page.value = 1
  GetmachineLocate()
}
const ADD=ref('0')
//新增
function onAdd() {
	 GetInfo(lastCellValue)
	ADD.value='新增'
	console.log(ADD.value)
	dialogFormVisible.value = true
	// Productionadd()
}
//删除
function onDel() {
	Del()
}
//删除
const Del = async () => {
  const response = await finisheddel({UniqId:lastCellValue});
  if (response.code === 0) {
	ElMessage({
	  type: 'success',
	  message: '删除成功',
	})
  }
  getTableData()
}
const TJVisible = ref(false)
const TJselectData = ref([]);
//每日统计
function onDay() {
	GetDayList()
	TJVisible.value=true
}
const GetDayList = async (value) => {
  const response = await getDayList({key_word:'',page:page.value,limit:limit.value});
  console.log(response.data) 
  if (response.code === 0) {
	 TJselectData.value=response.data.rows
	 console.log(TJselectData)
  }
}
const SMVisible = ref(false)
const SMselectData = ref([]);
//首末版统计
function onSMB() {
	MbList()
	SMVisible.value=true
}
const MbList = async (value) => {
  const response = await mbList({page:page.value,limit:limit.value});
  console.log(response.data) 
  if (response.code === 0) {
	 SMselectData.value=response.data.rows
	 console.log(TJselectData)
  }
}
//定位
const GetmachineLocate = async () => {
  const response = await finishedlocate({
    gdbh: params.gdbh,
    page: page.value.toString(), limit: limit.value.toString(),
  })
  console.log(response)
  if (response.code === 0) {
    const processedData = response.data.rows.map(item => {
      return {
        ...item,
        combinedProp: item.sczl_gdbh + '--'+item.yj_yjmc,
        combinedProp2: item.sczl_yjgx + '-'+item.sczl_gxmc
      };
    });
	  total.value = response.data.total
    tableData.splice(0, tableData.length, ...processedData);
  }
}
//仓库编号回车事件
const getCKsubmit = (event) => {
	GetCK()
}
const CKVisible = ref(false)
const CKselectData = reactive([])
//获取仓库信息
const GetCK = async () => {
  const response = await finishedgetCk();
  console.log(response) 
  if (response.code === 0) {
  CKselectData.splice(0, CKselectData.length, ...response.data)
  CKVisible.value=true
  }
}
//选择
const CKSelectClick = (row, column, event) => {
  const { 编号,名称 } = row
  formData.value.仓库编号=编号
  formData.value.仓库名称=名称
  CKVisible.value = false
}
  
//工单编号回车事件
const getGDsubmit = (event) => {
	GetGD()
}
const GDVisible = ref(false)
const GDselectData = reactive([])
//获取工单信息
const GetGD = async () => {
	console.log(formData.value.jjcp_gdbh)
  const response = await finishedgetGd({gdbh:formData.value.jjcp_gdbh});
  console.log(response) 
  if (response.code === 0) {
  GDselectData.splice(0, GDselectData.length, ...response.data)
  GDVisible.value=true
  }
}
//选择
const GDSelectClick = (row, column, event) => {
  const { Gd_gdbh,yj_yjno,销售订单号,yj_Yjdh,yj_yjmc,成品代号,成品名称,客户料号,gdyj } = row
  formData.value.jjcp_gdbh=Gd_gdbh
  formData.value.jjcp_yjno=yj_yjno
  formData.value.订单编号=销售订单号
  formData.value.jjcp_cpdh=yj_Yjdh
  formData.value.jjcp_cpmc=yj_yjmc
  formData.value.成品编码=成品代号
  formData.value.成品名称=成品名称
  formData.value.客户料号=客户料号
  GDVisible.value = false
}

//产品编号回车事件
const getSLsubmit = (event) => {
	GetSL()
}
//获取工单数量信息
const GetSL = async () => {
	console.log(formData.value.jjcp_gdbh)
  const response = await finishedgetGdsl({gdbh:formData.value.jjcp_gdbh,cpdh:formData.value.jjcp_cpdh});
  console.log(response) 
  if (response.code === 0) {
 formData.value.订单数量=response.data.订单数量
 formData.value.jjcp_sls=response.data.jjcp_sls
  }
}
// ============== 表格控制部分结束 ===============

// 获取需要的字典 可能为空 按需保留
const setOptions = async () => {}


// 获取需要的字典 可能为空 按需保留
setOptions()

// 多选数据
const multipleSelection = ref([])
// 多选
const handleSelectionChange = (val) => {
  if(val.length>0){
  	// multipleSelection.value = val
  	const lenth=val.length
  	// ScrapFactor.gdbh=val[lenth-1].combinedProp.split('-')[0];
  	// const parts = val[lenth-1].combinedProp2.split('-')
  	// ScrapFactor.yjno=parts[0];
  	// ScrapFactor.gxh=parts[1];
  	// ScrapFactor.type=val[lenth-1].sczl_检验类别
  	// console.log(ScrapFactor)
  	lastCellValue=val[lenth-1].UniqId
  	console.log(val)
  	val.splice(0, val.length);  // 清空 val 数组	
  	console.log(lastCellValue)
  }
}

// 批量删除控制标记
const deleteVisible = ref(false)

// 多选删除
const onDelete = async () => {
  const ids = []
  if (multipleSelection.value.length === 0) {
    ElMessage({
      type: 'warning',
      message: '请选择要删除的数据'
    })
    return
  }
  multipleSelection.value &&
    multipleSelection.value.map(item => {
      ids.push(item.ID)
    })
  const res = await deleteCompanyByIds({ ids })
  if (res.code === 0) {
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    if (tableData.value.length === ids.length && page.value > 1) {
      page.value--
    }
    deleteVisible.value = false
    getTableData()
  }
}

// 行为控制标记（弹窗内部需要增还是改）
const type = ref('')

// 更新行
const updateCompanyFunc = async (row) => {
  const res = await findCompany({ ID: row.ID })
  type.value = 'update'
  if (res.code === 0) {
    formData.value = res.data.recompany
    dialogFormVisible.value = true
  }
}

// 弹窗控制标记
const dialogFormVisible = ref(false)

// 打开弹窗
const openDialog = () => {
  type.value = 'create'
  dialogFormVisible.value = true
}

// 关闭弹窗
const closeDialog = () => {
  dialogFormVisible.value = false
  formData.value = {
    address: '',
    image: '',
    name: '',
  }
}
// 弹窗确定
const enterDialog = async () => {
  // elFormRef.value?.validate(async (valid) => {
  //   if (!valid) return
  //   let res
  //   switch (type.value) {
  //     case 'create':
  //       res = await createCompany(formData.value)
  //       break
  //     case 'update':
  //       res = await updateCompany(formData.value)
  //       break
  //     default:
  //       res = await createCompany(formData.value)
  //       break
  //   }
  //   if (res.code === 0) {
  //     ElMessage({
  //       type: 'success',
  //       message: '创建/更改成功'
  //     })
  //     closeDialog()
  //     getTableData()
  //   }
  // })
  // dialogFormVisible.value=false
  updateDetailData()
}
// 更新数据
const updateDetailData = async() => {
  const restoredData = {
   UniqId: lastCellValue, 
   Sys_id:'[1035/刘晓庆]',
   入仓类型:formData.value.入仓类型,
   仓库编号:formData.value.入仓类型,
   仓库名称:formData.value.仓库名称,
   jjcp_num:formData.value.jjcp_num,
   jjcp_gdbh:formData.value.jjcp_gdbh,
   jjcp_yjno:formData.value.jjcp_yjno,
   订单编号:formData.value.订单编号,
   jjcp_cpdh:formData.value.jjcp_cpdh,
   jjcp_cpmc:formData.value.jjcp_cpmc,
   成品编码:formData.value.成品编码,
   成品名称:formData.value.成品名称,
   客户料号:formData.value.客户料号,
   jjcp_smb:formData.value.jjcp_smb,
   jjcp_sj:formData.value.jjcp_sj,
   jjcp_sl:formData.value.jjcp_sl,
   jjcp_dw:formData.value.jjcp_dw,
   机型备注:formData.value.机型备注,
   jjcp_desc:formData.value.jjcp_desc,
  }
  if(ADD.value=='新增'){
  	 // console.log('新增')
  	 console.log('新增')
  	 console.log(ADD.value)
  	 console.log(restoredData)
  	 const response = await finishedadd(restoredData);
  	 // ADD.value='1'
  	 // console.log(ADD.value)
  	 // ADD.value='0'
  	 console.log(response)
	 if(response.code==0){
	 		 ElMessage({
	 		   type: 'success',
	 		   message: '新增成功',
	 		 })
	 }
	 dialogFormVisible.value = false
  	 
  }else{
  	  // const response = await productionedit(restoredData);
  	  console.log('修改')
  	  console.log(ADD.value)
  	  // console.log(response)
  	 // ADD.value='0'
  	 // console.log(ADD.value)
  }
 //   console.log(restoredData)
 //    console.log(lastCellValue)
 //    formData.value.UniqId=lastCellValue
 //    console.log(formData)
	// restoredData.UniqId='5'
  
  // console.log(FormData)
  // console.log(response)
}
//键盘 input框跳转
const ent = (event,id1,id2,id3) => {

  if (event.keyCode === 13 || event.keyCode === 40) { // Enter 或向下箭头
    if(id2=='仓库编号'&&event.keyCode === 13){
		getCKsubmit()
	}if(id2=='工单编号'&&event.keyCode === 13){
		getGDsubmit()
	}if(id2=='产品编号'&&event.keyCode === 13){
		getSLsubmit()
	}else if(id3!=''){
      document.getElementById(id3).focus()
    }
  } else if (event.keyCode === 38) { // 向上箭头
    if(id1!=''){
      document.getElementById(id1).focus()
    }
  }else if (event.keyCode === 8) { // 删除箭头
    if(id1!='' && document.getElementById(id2).value==''){
      document.getElementById(id1).focus()
    }
    console.log(document.getElementById(id2).value)
  }else if (event.keyCode === 37) { // 向左箭头
    if(id1!='' && document.getElementById(id2).selectionStart==0){
      document.getElementById(id1).focus()
    }
  }else if (event.keyCode === 39) { // 向右箭头
    if(id3!='' && document.getElementById(id2).selectionStart == document.getElementById(id2).value.length){
      document.getElementById(id3).focus()
    }
  }
}
let table=ref(5);
let lastCellValue=ref()
// 双击表格操作
function doubleClick(row, column, event) {
  type.value = 'update';
  		  lastCellValue= row['UniqId'];
  		 console.log(lastCellValue)
  	    table.value=lastCellValue
  	 	 dialogFormVisible.value = true
  		// dialogSelectVisible.value = true
  GetInfo(table.value)
  // dialogFormVisible.value = true
}
// 单击表格操作
function Click(row, column, event) {
    lastCellValue= row['UniqId'];
   console.log(lastCellValue)
  
}
//获取详细信息
const GetInfo = async (value) => {
  const response = await finishedInfo({UniqId:value});
  console.log(response) 
  if (response.code === 0) {
	formData.value=response.data;
  }
}
// 导出excel
function exportExcel() {
  console.log('导出到excel');
}


function openFolderDialog() {
  console.log('导出到excel');
}



// 生命周期钩子
onMounted(async () => {
});
</script>

<style>
.JKWTree-container {
  display: flex;
}

.JKWTree-tree {
  width: 300px;
  background-color: #fff;
  padding: 10px;
  margin-right: 20px;
}

.JKWTree-tree h3 {
  font-size: 15px;
  font-weight: 700;
  margin: 10px 0;
}

.JKWTree-content {
  flex: 1;
}

/* 选中某行时的背景色*/
.el-table__body tr.current-row>td {
  background: #ff80ff !important;
  /* 背景颜色 */
}
</style>