  <!-- 
    成品库存管理主页面
    @Author: Antigravity
  -->
<template>

  <div class="app-container goods-stock-container">
    <!-- 过滤搜索控制栏 -->
    <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" 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 } 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 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;
  }).catch(() => {
    loading.value = false;
  });
}

/** 库龄颜色渲染 */
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);

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

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

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

      :deep(.el-button + .el-button) {
        margin-left: 8px;
      }
    }

    .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) {
        border: none !important;
      }

      :deep(.el-table::before) {
        display: none;
      }

      :deep(.el-table__inner-wrapper::before) {
        display: none;
      }

      :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>
