<template>
  <div class="pda-container">
    <!-- 头部栏 -->
    <div class="header-bar">
      <div class="header-left" @click="$router.back()">
        <i class="el-icon-arrow-left"></i>
        <span>仓库盘点</span>
      </div>
      <div class="header-right" @click="$router.push({ path: '/' })">
        首页
      </div>
    </div>

    <!-- 搜索框 -->
    <div class="search-container">
      <el-input
        clearable
        v-model="searchValue"
        placeholder="请扫描盘点单号"
        prefix-icon="el-icon-search"
        @keyup.enter.native="onSearch"
        ref="searchInput"
      />
    </div>

    <!-- 盘点单列表 -->
    <div class="content-area">
      <div
        v-for="item in list"
        :key="item.reportId"
        class="inbound-card"
        @click="goToDetail(item)">
        <div class="card-title">
          <span class="title-label">盘点单号</span>
          <span class="title-value">{{ item.reportId }}</span>
        </div>

        <div class="card-details">
          <div class="detail-item">
            <div class="detail-label">标签张数</div>
            <div class="detail-value">
              <span class="qualified">{{ item.checkedCount }}</span><span class="total">{{ item.totalCount }}</span>
            </div>
          </div>
          <div class="detail-item">
            <div class="detail-label">物料总数</div>
            <div class="detail-value">
              <span class="qualified">{{ item.checkedQty }}</span><span class="total">{{ item.totalQty }}</span>
            </div>
          </div>
          <div class="detail-item">
            <div class="detail-label">盘点状态</div>
            <div class="detail-value status-value">
              <span class="status-tag" :class="'status-' + item.status">{{ item.statusDesc }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-if="list.length === 0 && !loading" class="empty-state">
        <i class="el-icon-box"></i>
        <p>暂无盘点单</p>
      </div>

      <!-- 加载状态 -->
      <div v-if="loading" class="loading-state">
        <i class="el-icon-loading"></i>
        <p>加载中...</p>
      </div>
    </div>
  </div>
</template>

<script>
import { countingWIPTodayList } from '@/api/warehouse/countingWIP.js'

export default {
  name: 'CountingList',
  data() {
    return {
      searchValue: '',
      list: [],
      loading: false
    }
  },
  mounted() {
    // 聚焦搜索框
    this.$nextTick(() => {
      if (this.$refs.searchInput) {
        this.$refs.searchInput.focus()
      }
    })
    // 加载数据
    this.getList()
  },
  methods: {
    // 搜索
    onSearch() {
      this.getList()
    },

    // 获取列表
    getList() {
      this.loading = true
      const params = {
        reportId: this.searchValue
      }
      countingWIPTodayList(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.list = data.list || []
        } else {
          this.$message.error(data.msg || '查询失败')
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
        this.$message.error('查询失败')
      })
    },

    // 跳转到明细页
    goToDetail(item) {
      this.$router.push({
        name: 'countingDetail',
        query: {
          site: item.site,
          buNo: item.buNo,
          reportId: item.reportId,
          totalCount: item.totalCount,
          totalQty: item.totalQty,
          checkedCount: item.checkedCount,
          checkedQty: item.checkedQty
        }
      })
    }
  }
}
</script>

<style scoped>
.pda-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f5f5f5;
}

/* 头部栏 */
.header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  background: #17B3A3;
  color: white;
  height: 40px;
  min-height: 40px;
}

.header-left {
  display: flex;
  align-items: center;
  cursor: pointer;
  font-size: 16px;
  font-weight: 500;
}

.header-left i {
  margin-right: 8px;
  font-size: 18px;
}

.header-right {
  cursor: pointer;
  font-size: 16px;
  font-weight: 500;
}

/* 搜索容器 */
.search-container {
  padding: 12px 16px;
  background: white;
}

/* 内容区域 */
.content-area {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
}

/* 入库卡片 */
.inbound-card {
  background: white;
  border-radius: 8px;
  margin-bottom: 12px;
  padding: 16px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.2s ease;
}

.inbound-card:hover {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
  transform: translateY(-1px);
}

.inbound-card:active {
  transform: translateY(0);
}

/* 卡片标题 */
.card-title {
  margin-bottom: 12px;
}

.title-label {
  font-size: 12px;
  color: #666;
  display: block;
  margin-bottom: 4px;
}

.title-value {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-left: 20px;
}

/* 卡片详情 */
.card-details {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 4px;
}

.detail-item {
  flex: 1;
  text-align: center;
  min-width: 60px;
  max-width: 90px;
}

.detail-label {
  font-size: 11px;
  color: #666;
  margin-bottom: 4px;
  line-height: 1.2;
  margin-left: -12px;
}

.detail-value {
  font-size: 13px;
  color: #333;
  line-height: 1.2;
  margin-left: -12px;
}

.detail-value .qualified {
  color: #17B3A3;
  font-weight: 500;
}

.detail-value .total {
  color: #333;
  font-weight: 500;
}

.detail-value .total::before {
  content: '/';
  color: #333;
}

.detail-value.status-value {
  margin-left: 0;
}

.status-tag {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 11px;
  color: #fff;
  font-weight: 500;
}

.status-0 {
  background-color: #909399;
}

.status-1 {
  background-color: #409EFF;
}

.status-2 {
  background-color: #E6A23C;
}

.status-3 {
  background-color: #67C23A;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #999;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-state p {
  font-size: 14px;
  margin: 0;
}

/* 加载状态 */
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #17B3A3;
}

.loading-state i {
  font-size: 24px;
  margin-bottom: 12px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.loading-state p {
  font-size: 14px;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 360px) {
  .header-bar {
    padding: 8px 12px;
  }

  .search-container {
    padding: 8px 12px;
  }

  .content-area {
    padding: 8px 12px;
  }

  .inbound-card {
    padding: 12px;
  }

  .card-details {
    flex-wrap: wrap;
    gap: 6px;
  }

  .detail-item {
    flex: 0 0 48%;
    margin-bottom: 6px;
    min-width: 60px;
  }
}
</style>

