<template>
  <!-- 
    成品库存管理主页面
    @Author: Antigravity
  -->
  <div class="app-container goods-stock-container">
    <!-- 头部高格调数据看板区 -->
    <el-row :gutter="20" class="mb20 summary-row">
      <el-col :span="6">
        <div class="summary-card total-stock">
          <div class="card-icon"><el-icon><Box /></el-icon></div>
          <div class="card-info">
            <span class="label">当前总库存支数</span>
            <h2 class="value">{{ summaryData.totalQty.toLocaleString() }} <small>支</small></h2>
          </div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="summary-card total-weight">
          <div class="card-icon"><el-icon><ScaleToOriginal /></el-icon></div>
          <div class="card-info">
            <span class="label">库存总重量</span>
            <h2 class="value">{{ summaryData.totalWt.toFixed(2) }} <small>kg</small></h2>
          </div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="summary-card aging-stock">
          <div class="card-icon"><el-icon><Timer /></el-icon></div>
          <div class="card-info">
            <span class="label">超 30 天超龄库存</span>
            <h2 class="value warning-text">{{ summaryData.agingCount }} <small>款</small></h2>
          </div>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="summary-card warehouse-stock">
          <div class="card-icon"><el-icon><HomeFilled /></el-icon></div>
          <div class="card-info">
            <span class="label">关联单据数</span>
            <h2 class="value info-text">{{ summaryData.docCount }} <small>个</small></h2>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 过滤搜索控制栏 -->
    <el-card class="search-card mb20 shadow-sm" border="false">
      <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="80px">
        <el-form-item label="型材型号" prop="modelNum">
          <el-input
            v-model="queryParams.modelNum"
            placeholder="请输入型材型号"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="表面名称" prop="colorName">
          <el-input
            v-model="queryParams.colorName"
            placeholder="请输入表面名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户名称" prop="clientName">
          <el-input
            v-model="queryParams.clientName"
            placeholder="请输入客户名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="单据编号" prop="docCode">
          <el-input
            v-model="queryParams.docCode"
            placeholder="请输入单据编号"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item class="btn-group">
          <el-button type="primary" icon="Search" class="glow-btn" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据呈现主表格 -->
    <el-card class="table-card shadow-sm" border="false">
      <el-table
        v-loading="loading"
        :data="stockList"
        stripe
        row-key="itemNo"
        border
        highlight-current-row
        class="premium-table"
      >
        <el-table-column type="index" label="序号" align="center" width="55" fixed />
        <el-table-column label="单据编号" align="center" prop="docCode" width="130" show-overflow-tooltip fixed />
        <el-table-column label="型材型号" align="left" prop="modelNum" width="130" show-overflow-tooltip fixed>
          <template #default="scope">
            <span class="bold-text">{{ scope.row.modelNum }}</span>
          </template>
        </el-table-column>
        <el-table-column label="型材名称" align="left" prop="modelName" width="150" show-overflow-tooltip />
        <el-table-column label="客户型号" align="left" prop="clientModel" width="130" show-overflow-tooltip />
        <el-table-column label="表面名称" align="center" prop="colorName" width="130" show-overflow-tooltip>
          <template #default="scope">
            <el-tag effect="plain" type="success" size="small">{{ scope.row.colorName }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="材质" align="center" prop="alloyName" width="90" />
        <el-table-column label="壁厚(mm)" align="right" prop="thick" width="95">
          <template #default="scope">
            <span>{{ scope.row.thick ? Number(scope.row.thick).toFixed(2) : '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="膜厚" align="right" prop="film" width="85">
          <template #default="scope">
            <span>{{ scope.row.film ? Number(scope.row.film).toFixed(2) : '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="长度(mm)" align="right" prop="flength" width="95">
          <template #default="scope">
            <span class="highlight-value">{{ scope.row.flength ? Math.round(scope.row.flength) : '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="现存支数" align="right" prop="qty" width="100">
          <template #default="scope">
            <span class="qty-text">{{ scope.row.qty ? scope.row.qty : 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="实际重量(kg)" align="right" prop="wt" width="115">
          <template #default="scope">
            <span class="wt-text">{{ scope.row.wt ? Number(scope.row.wt).toFixed(2) : '0.00' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="存放框名" align="center" prop="frameName" width="120" show-overflow-tooltip />
        <el-table-column label="仓库名称" align="center" prop="storeName" width="120" show-overflow-tooltip />
        <el-table-column label="库龄" align="center" prop="stockDays" width="95">
          <template #default="scope">
            <el-tooltip :content="'生产完成日期: ' + parseTime(scope.row.finishDate)" placement="top">
              <el-tag :type="getAgingTagType(scope.row.stockDays)" effect="dark" size="small">
                {{ scope.row.stockDays }} 天
              </el-tag>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="客户名称" align="left" prop="clientName" width="150" show-overflow-tooltip />
        <el-table-column label="完成日期" align="center" prop="finishDate" width="160">
          <template #default="scope">
            <span>{{ parseTime(scope.row.finishDate, '{y}-{m}-{d} {h}:{i}') }}</span>
          </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-card>
  </div>
</template>

<script setup name="GoodsStock">
/**
 * 成品库存组件逻辑
 * @Author: Antigravity
 */
import { ref, onMounted, reactive } from 'vue';
import { listGoodsStockPage } from '@/api/erp/goodsStock';
import { parseTime } from '@/utils/ruoyi';

const queryFormRef = ref();
const loading = ref(false);
const total = ref(0);
const stockList = ref([]);

// 汇总栏数据定义
const summaryData = reactive({
  totalQty: 0,
  totalWt: 0,
  agingCount: 0,
  docCount: 0
});

// 查询参数配置
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  modelNum: undefined,
  colorName: undefined,
  clientName: undefined,
  docCode: undefined
});

/** 查询库存列表 */
function getList() {
  loading.value = true;
  listGoodsStockPage(queryParams.value).then(response => {
    stockList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    calculateSummary(response.rows);
  }).catch(() => {
    loading.value = false;
  });
}

/** 动态统计当前页/当前汇总概览 */
function calculateSummary(rows) {
  if (!rows || rows.length === 0) {
    summaryData.totalQty = 0;
    summaryData.totalWt = 0;
    summaryData.agingCount = 0;
    summaryData.docCount = 0;
    return;
  }
  let qty = 0;
  let wt = 0;
  let aging = 0;
  const docs = new Set();
  rows.forEach(r => {
    qty += Number(r.qty || 0);
    wt += Number(r.wt || 0);
    if (Number(r.stockDays || 0) > 30) {
      aging++;
    }
    if (r.docCode) {
      docs.add(r.docCode);
    }
  });
  summaryData.totalQty = qty;
  summaryData.totalWt = wt;
  summaryData.agingCount = aging;
  summaryData.docCount = docs.size;
}

/** 库龄颜色渲染 */
function getAgingTagType(days) {
  const d = Number(days || 0);
  if (d <= 7) return 'success'; // 新鲜库存
  if (d <= 30) return 'info'; // 正常库存
  if (d <= 90) return 'warning'; // 超龄中等库存
  return 'danger'; // 呆滞库存
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value?.resetFields();
  handleQuery();
}

onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.goods-stock-container {
  padding: 20px;
  background-color: #f6f8fb;
  min-height: calc(100vh - 84px);

  // 1. 数据统计卡片区美化
  .summary-row {
    margin-bottom: 20px;

    .summary-card {
      display: flex;
      align-items: center;
      padding: 20px;
      background: #ffffff;
      border-radius: 12px;
      box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.03);
      transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
      border-left: 5px solid #409eff;

      &:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 30px 0 rgba(64, 158, 255, 0.12);
      }

      &.total-stock {
        border-left-color: #409eff;
        .card-icon { background: rgba(64, 158, 255, 0.1); color: #409eff; }
      }
      &.total-weight {
        border-left-color: #67c23a;
        .card-icon { background: rgba(103, 194, 58, 0.1); color: #67c23a; }
      }
      &.aging-stock {
        border-left-color: #f56c6c;
        .card-icon { background: rgba(245, 108, 108, 0.1); color: #f56c6c; }
      }
      &.warehouse-stock {
        border-left-color: #e6a23c;
        .card-icon { background: rgba(230, 162, 60, 0.1); color: #e6a23c; }
      }

      .card-icon {
        width: 54px;
        height: 54px;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 26px;
        margin-right: 15px;
      }

      .card-info {
        .label {
          font-size: 13px;
          color: #909399;
          display: block;
          margin-bottom: 5px;
        }
        .value {
          margin: 0;
          font-size: 22px;
          font-weight: 700;
          color: #303133;

          small {
            font-size: 12px;
            font-weight: normal;
            color: #909399;
            margin-left: 2px;
          }
        }
        .warning-text { color: #f56c6c; }
        .info-text { color: #e6a23c; }
      }
    }
  }

  // 2. 搜索控制栏美化
  .search-card {
    background: #ffffff;
    border-radius: 12px;
    border: none;

    :deep(.el-form-item) {
      margin-bottom: 0;
      margin-right: 18px;
    }

    .btn-group {
      margin-left: 10px;
    }

    .glow-btn {
      box-shadow: 0 4px 14px 0 rgba(64, 158, 255, 0.4);
      transition: all 0.3s;

      &:hover {
        transform: translateY(-1px);
        box-shadow: 0 6px 20px 0 rgba(64, 158, 255, 0.5);
      }
    }
  }

  // 3. 数据表格区美化
  .table-card {
    border-radius: 12px;
    border: none;
    background: #ffffff;

    .premium-table {
      font-size: 13px;

      :deep(.el-table__header-wrapper) th {
        background-color: #f8f9fc !important;
        color: #606266;
        font-weight: bold;
        height: 48px;
      }

      :deep(.el-table__row) {
        height: 44px;
      }

      .bold-text {
        font-weight: 600;
        color: #2c3e50;
      }

      .highlight-value {
        color: #2c3e50;
        font-weight: bold;
      }

      .qty-text {
        font-weight: bold;
        color: #2980b9;
      }

      .wt-text {
        font-weight: bold;
        color: #27ae60;
      }
    }
  }

  .mb20 { margin-bottom: 20px; }
}
</style>
