<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="88px"
    >
      <el-form-item label="选择巷道" prop="streetId">
        <el-select
          v-model="queryParams.streetId"
          placeholder="请选择巷道"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        >
        <el-option
            v-for="dict in streetList"
            :key="dict.plcId"
            :label="dict.name"
            :value="dict.plcId"
        />
      </el-select>
      </el-form-item>
      <el-form-item label="盘点批次号" prop="batchNumber">
        <el-input
          v-model="queryParams.batchNumber"
          placeholder="请输入盘点批次号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      
      <el-form-item label="任务工单号" prop="taskId">
        <el-input
          v-model="queryParams.taskId"
          placeholder="请输入任务工单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="层" prop="row">
        <el-input
          v-model="queryParams.row"
          placeholder="请输入层"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="列" prop="column">
        <el-input
          v-model="queryParams.column"
          placeholder="请输入列"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!-- <el-form-item label="巷道id" prop="streetId">
        <el-select
          v-model="queryParams.streetId"
          placeholder="请选择巷道id"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="taskid" prop="taskId">
        <el-input
          v-model="queryParams.taskId"
          placeholder="请输入taskid"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="货架方向" prop="direction">
        <el-select
          v-model="queryParams.direction"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        >
        <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.DIRECTION)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="盘点状态" prop="status">
        <el-select
          v-model="queryParams.status"
          clearable
          class="!w-240px"  
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CHECK_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="货位号" prop="storage_Code">
        <el-input
          v-model="queryParams.storage_Code"
          placeholder="请输入货位号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['logistics:check-log:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['logistics:check-log:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="盘点批次号" align="center" prop="batchNumber" />
      <el-table-column label="工单号" align="center" prop="taskId" />
      <el-table-column label="位置" align="center" prop="row" >
        <template #default="scope">
          <space>{{ scope.row.streetName+"-"+ getDirectionName(scope.row.direction)+ scope.row.row+'层-'+scope.row.column+'列' }}</space>
        </template>
      </el-table-column>
      <el-table-column

        v-for="(column, index) in columns"
        :key="index"
        :label="column.remark"
        :align="column.align || 'center'"
        :prop="column.label"
      >
      <!--原先可以点击，查看信息
       @click="getDialogVisible(scope.row.streetName,scope.row.direction,scope.row.row, scope.row.column,scope.row.id)" -->
        <template #default="scope">
          <el-button
            link
            
            :type='scope.row.status==0?"warning":(scope.row["wms"+capitalize(column.label)] != scope.row[(column.label)]?"danger":"success")'>
            {{ scope.row["wms"+capitalize(column.label)] }}
          </el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="货架方向" align="center" prop="direction" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.DIRECTION" :value="scope.row.direction" />
        </template>
      </el-table-column> -->
      <el-table-column label="盘点状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CHECK_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      
      <el-table-column label="时间" align="center" 
        :formatter="dateFormatter" prop="createTime"/>
       
      <!-- <el-table-column label="盘点图片" align="center" prop="pic" min-width="150">
        <template #default="scope">
          
  <div v-if="scope.row.urlResources && scope.row.urlResources.length > 0" class="image-cell-container">
    <el-image
      v-for="(pic, index) in scope.row.urlResources.filter(p => p.little === '缺件拍照').slice(0, 1)"
      :key="index"
      :src="pic.url"
      style="width: 100px; height: 100px; margin-right: 10px;"
      :preview-src-list="scope.row.urlResources.filter(p => p.little === '缺件拍照').slice(0, 1).map(p => p.url)"
      :initial-index="0"
      :preview-teleported="true"
      fit="cover"
    />
  </div>
  <div v-else>
    无图片
  </div>
      </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center">
        <template #default="scope">
          
          
          <el-button
            link
            type="success"
            @click="getDialogVisible(scope.row.streetName,scope.row.direction,scope.row.row, scope.row.column,scope.row.id)"
            v-hasPermi="['logistics:check-log:delete']"
          >
          详情
          </el-button>
          
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['logistics:check-log:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CheckLogForm ref="formRef" @success="getList" />
  
  <el-dialog
    v-model="dialogVisible"
    :title="dialogTitle"
    width="40%"
    @close="handleClose"
  >
   
  <stock :stockId="''" :checkLogId="getcheckLogId()"  :key="getcheckLogId()"/>
  </el-dialog>
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { StreetApi,StreetVO } from '@/api/logistics/street'
import download from '@/utils/download'
import { CheckLogApi, CheckLogVO } from '@/api/logistics/checklog'
import CheckLogForm from './CheckLogForm.vue'
import stock from '@/views/logistics/stock/stock.vue'

/** 盘点 列表 */
defineOptions({ name: 'CheckLog' })

const dialogVisible = ref(false)
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const streetList = ref<StreetVO[]>([]) // 巷道队列
const loading = ref(true) // 列表的加载中
const list = ref<CheckLogVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  batchNumber: undefined,
  row: undefined,
  column: undefined,
  streetId: undefined,
  exportTime: [],
  taskId: undefined,
  direction: undefined,
  side: undefined,
  status: undefined,
  storage_Code: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const dialogTitle = ref('')
const checkLogId = ref<number>()

const getDialogVisible = (streetName,directionName,row,colum,id) => {
  console.log(id);
  
  dialogVisible.value = true
  checkLogId.value = id
  dialogTitle.value = streetName+getDirectionName(directionName)+'侧'+row+'层'+colum+'列'
}
const getcheckLogId = ()=>{
  return checkLogId.value
}

const getDirectionName = (direction)=>{
  return getIntDictOptions(DICT_TYPE.DIRECTION).find(item => item.value === direction)?.label
}
const getPicList = (pic: any) => {
  console.log(pic);
  
  return pic.map.url
}
const handleClose = async () => {
  dialogVisible.value = false
   // 重新获取列表数据
    await getList()
}
// 首字母大写方法
const capitalize = (str: string): string => {
  if (!str) return '';
  
  return str.charAt(0).toUpperCase() + str.slice(1);
};
// 定义列配置数组
const columns = ref();

/** 查询列表 */
const getColumns = async () => {
  loading.value = true
  try {
    const data = await CheckLogApi.getColumns()
    console.log(data);
    
    columns.value = data
  } finally {
    loading.value = false
  }
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CheckLogApi.getCheckLogPage(queryParams)
    
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CheckLogApi.deleteCheckLog(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await CheckLogApi.exportCheckLog(queryParams)
    download.excel(data, '盘点.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
/** 查询巷道列表 */
const getStringList =  () => {
 
 try {
   const data = StreetApi.getStreetList()
   data.then((res)=>{
     streetList.value = res
   })
   console.log(data);
   
   console.log(streetList);
   
 } finally {
   
 }
}
/** 初始化 **/
onMounted(() => {
  getList()
  getColumns()
  getStringList()

})
</script>
<style scoped>
.image-cell-container {

  overflow-x: auto;
  white-space: nowrap;
}

.image-cell-container::-webkit-scrollbar {
  height: 6px;
}

.image-cell-container::-webkit-scrollbar-thumb {
  background-color: #c1c1c1; /* 灰色滚动条 */
  border-radius: 3px;
}

.image-cell-container::-webkit-scrollbar-track {
  background-color: #f1f1f1; /* 滚动条轨道背景色 */
  border-radius: 3px;
}
.image__error .image-slot {
  font-size: 30px;
}
.image__error .image-slot .el-icon {
  font-size: 30px;
}
.image__error .el-image {
  width: 100%;
  height: 200px;
}
</style>