<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 class="compact-input"
        v-model="scanCode"
        placeholder="请扫描标签条码"
        prefix-icon="el-icon-search"
        @keyup.enter.native="handleScan"
        ref="scanInput"
      />
      <div class="mode-switch">
        <el-switch
          class="custom-switch"
          v-model="isRemoveMode"
          active-color="#ff4949"
          inactive-color="#13ce66">
        </el-switch>
        <span v-if="isRemoveMode" class="switch-text">{{ '移除' }}</span>
        <span v-else class="switch-text2">{{ '添加' }}</span>
      </div>
    </div>

    <!-- 出库单信息卡片 -->
    <div class="material-info-card" v-if="outboundInfo.outboundNo">
      <div class="card-header">
        <div class="card-title-group">
          <span class="title-label">出库单号</span>
          <span class="title-value">{{ outboundInfo.outboundNo }}</span>
        </div>
      </div>

      <div class="card-body">
        <div class="info-row">
          <div class="info-item">
            <span class="info-label">关联单号:</span>
            <span class="info-value">{{ outboundInfo.relatedNo || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="info-label">关联行号:</span>
            <span class="info-value highlight">{{ outboundInfo.relatedLineNo || '-' }}</span>
          </div>
        </div>

        <div class="stats-row">
          <div class="stat-item">
            <div class="stat-label">标签张数</div>
            <div class="stat-value">
              <span class="qualified">{{ outboundInfo.availableLabels }}</span>
              <span class="separator">/</span>
              <span class="total">{{ outboundInfo.totalLabels }}</span>
            </div>
          </div>
          <div class="stat-item">
            <div class="stat-label">物料总数</div>
            <div class="stat-value">
              <span class="qualified">{{ outboundInfo.availableQty }}</span>
              <span class="separator">/</span>
              <span class="total">{{ outboundInfo.totalQty }}</span>
            </div>
          </div>
          <div class="stat-item">
            <div class="stat-label">创建日期</div>
            <div class="stat-value single">{{ formatDate(outboundInfo.createDate) }}</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 出库信息确认标题 -->
    <div class="section-title">
      <div class="title-left">
        <i class="el-icon-circle-check"></i>
        <span>出库信息确认</span>
      </div>
      <div class="title-right">
        <span class="material-list-link" @click="showMaterialListDialog">物料清单</span>
      </div>
    </div>

    <!-- 标签列表 -->
    <div class="label-list">
      <div class="list-header">
        <div class="col-no">NO.</div>
        <div class="col-label">标签条码</div>
        <div class="col-part">物料编码</div>
        <div class="col-qty">标签数量</div>
      </div>

      <div class="list-body">
        <div
          v-for="(label, index) in labelList"
          :key="label.id"
          class="list-item"
        >
          <div class="col-no">{{ labelList.length - index }}</div>
          <div class="col-label">{{ label.labelCode }}</div>
          <div class="col-part">{{ label.partNo }}</div>
          <div class="col-qty">{{ label.quantity }}</div>
        </div>

        <!-- 空状态 -->
        <div v-if="labelList.length === 0" class="empty-labels">
          <p>暂无扫描标签</p>
        </div>
      </div>
    </div>

    <!-- 底部操作按钮 -->
    <div class="bottom-actions">
      <button class="action-btn secondary" @click="confirmOutbound">
        确定
      </button>
      <button class="action-btn secondary" style="margin-left: 10px;" @click="printLabels">
        打印
      </button>
      <button class="action-btn secondary" style="margin-left: 10px;" @click="cancelOutbound">
        取消
      </button>
    </div>

    <!-- 物料清单弹窗 -->
    <div v-if="showMaterialDialog" class="material-overlay">
      <div class="material-modal">
        <div class="modal-header">
          <span class="modal-title">物料清单</span>
          <i class="el-icon-close close-btn" @click="closeMaterialDialog"></i>
        </div>

        <div class="modal-body">
          <!-- 加载状态 -->
          <div v-if="materialListLoading" class="loading-container">
            <i class="el-icon-loading"></i>
            <span>加载中...</span>
          </div>

          <!-- 物料表格 -->
          <div v-else-if="materialList.length > 0" class="material-table">
            <div class="table-header">
              <div class="col-no">NO.</div>
              <div class="col-material-code">物料编码</div>
              <div class="col-part-name">物料名称</div>
              <div class="col-required-qty">需求数量</div>
              <div class="col-available-qty">出库数量</div>
              <div class="col-scans-qty">扫描数量</div>
            </div>

            <div class="table-body">
              <div
                v-for="(item, index) in materialList"
                :key="index"
                class="table-row"
              >
                <div class="col-no">{{ index + 1 }}</div>
                <div class="col-material-code clickable-part" @click="showStockDialogFn(item)">{{ item.materialCode || item.partNo }}</div>
                <div class="col-part-name">
                  <span class="part-name-text" @click.stop="showPartNameTip(item.materialName, $event)">{{ item.materialName || '-' }}</span>
                </div>
                <div class="col-required-qty">{{ item.requiredQty || 0 }}</div>
                <div class="col-available-qty">{{ item.pickedQty || 0 }}</div>
                <div class="col-scans-qty">{{ item.scansQty || 0 }}</div>
              </div>
            </div>
          </div>

          <!-- 空数据状态 -->
          <div v-else class="empty-material">
            <i class="el-icon-document"></i>
            <p>暂无物料数据</p>
          </div>
        </div>

        <div class="modal-footer">
          <button class="btn-close" @click="closeMaterialDialog">关闭</button>
        </div>
      </div>

      <!-- 物料名称提示框 -->
      <div v-if="showPartNameTooltip" class="part-name-tooltip" :style="tooltipStyle" @click.stop>
        <div class="tooltip-content">{{ currentPartName }}</div>
      </div>
    </div>

    <!-- 可用库存弹窗 -->
    <div v-if="showStockDialog" class="stock-overlay">
      <div class="stock-modal">
        <div class="modal-header">
          <span class="modal-title">可用库存 - {{ currentPartNo }}</span>
          <i class="el-icon-close close-btn" @click="closeStockDialog"></i>
        </div>
        <div class="modal-body">
          <div v-if="stockLoading" class="loading-container"><i class="el-icon-loading"></i><span>加载中...</span></div>
          <div v-else-if="stockList.length > 0" class="stock-table">
            <div class="table-header">
              <div class="col-roll-no">标签条码</div>
              <div class="col-qty">数量</div>
              <div class="col-location">库位</div>
              <div class="col-batch">合约号码</div>
              <div class="col-date">入库天数</div>
            </div>
            <div class="table-body">
              <div v-for="(item, index) in stockList" :key="index" class="table-row">
                <div class="col-roll-no">{{ item.rollNo }}</div>
                <div class="col-qty">{{ item.rollQty }} <span class="unit-text">{{ item.um }}</span></div>
                <div class="col-location">{{ item.location }}</div>
                <div class="col-batch">{{ item.batchNo }}</div>
                <div class="col-date">{{ item.daysInStock }}</div>
              </div>
            </div>
          </div>
          <div v-else class="empty-stock"><i class="el-icon-box"></i><p>暂无可用库存</p></div>
        </div>
        <div class="modal-footer"><button class="btn-close" @click="closeStockDialog">关闭</button></div>
      </div>
    </div>
  </div>
</template>

<script>
import { getOutboundDetails, validateLabelWithOutbound, confirmSalesOutbound, getMaterialList, getScannedLabelList } from "@/api/sales/sales-outbound.js";
import { getInventoryStock } from "@/api/inbound.js";
import { getCurrentWarehouse } from '@/utils'
import moment from 'moment';

export default {
  data() {
    return {
      scanCode: '',
      outboundInfo: {},
      labelList: [],
      outboundNo: '',
      relatedNo: '',
      relatedLineNo: '',
      showMaterialDialog: false,
      materialList: [],
      materialListLoading: false,
      isRemoveMode: false, // 默认为添加模式
      showStockDialog: false,
      stockList: [],
      stockLoading: false,
      currentPartNo: '',
      // 物料名称提示框相关
      showPartNameTooltip: false,
      currentPartName: '',
      tooltipStyle: { top: '0px', left: '0px' }
    };
  },
  methods: {
    formatDate(date) {
      return date ? moment(date).format('YYYY-MM-DD') : '';
    },

    // 处理扫描
    handleScan() {
      if (!this.scanCode.trim()) {
        return;
      }

      if (this.isRemoveMode) {
        this.removeLabelByCode(this.scanCode.trim());
      } else {
        this.validateAndAddLabel(this.scanCode.trim());
      }
      this.scanCode = '';
    },

    // 验证标签并添加到列表（调用存储过程）
    validateAndAddLabel(labelCode) {
      const params = {
        labelCode: labelCode,
        outboundNo: this.outboundNo,
        relatedNo: this.relatedNo,
        relatedLineNo: this.relatedLineNo,
        site: localStorage.getItem('site'),
        buNo: this.buNo,
        operationType: 'I', // 添加标签
        warehouseId: getCurrentWarehouse() // 当前仓库
      };
      validateLabelWithOutbound(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message.success('标签添加成功');
          // 重新加载已扫描标签列表
          this.loadScannedLabelList();
        } else {
          this.$message.error(data.msg || '该标签与出库单不符，请检查');
        }
      }).catch(error => {
        console.error('标签验证失败:', error);
        this.$message.error('操作失败');
      });
    },

    // 通过条码移除标签（调用存储过程）
    removeLabelByCode(labelCode) {
      const params = {
        labelCode: labelCode,
        outboundNo: this.outboundNo,
        relatedNo: this.relatedNo,
        relatedLineNo: this.relatedLineNo,
        site: localStorage.getItem('site'),
        buNo: this.buNo,
        operationType: 'D', // 移除标签
        warehouseId: getCurrentWarehouse() // 当前仓库
      };
      validateLabelWithOutbound(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message.success('标签移除成功');
          // 重新加载已扫描标签列表
          this.loadScannedLabelList();
        } else {
          this.$message.error(data.msg || '移除失败');
        }
      }).catch(error => {
        console.error('标签移除失败:', error);
        this.$message.error('移除失败');
      });
    },

    // 确认出库（调用存储过程）
    confirmOutbound() {
      if (this.labelList.length === 0) {
        this.$message.warning('请先扫描标签');
        return;
      }

      // 获取库位（使用第一个标签的库位，如果有的话）
      const locationCode = this.labelList.length > 0 && this.labelList[0].locationId
        ? this.labelList[0].locationId
        : '';

      const params = {
        site: this.outboundInfo.site,
        buNo: this.outboundInfo.buNo,
        outboundNo: this.outboundNo,
        relatedNo: this.relatedNo,
        relatedLineNo: this.relatedLineNo,
        locationCode: locationCode
      };
      confirmSalesOutbound(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message.success('出库成功');
          this.$router.back();
        } else {
          this.$message.error(data.msg || '出库失败');
        }
      }).catch(error => {
        console.error('出库失败:', error);
        this.$message.error('出库失败');
      });
    },

    // 打印标签
    printLabels() {
      if (this.labelList.length === 0) {
        this.$message.warning('暂无标签可打印');
        return;
      }

      this.$message.warning('打印功能开发中...');
    },

    // 取消出库
    cancelOutbound() {
      if (this.labelList.length > 0) {
        this.$confirm('取消后将清空已扫描的标签，确定取消吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '继续操作',
          type: 'warning'
        }).then(() => {
          this.$router.back();
        }).catch(() => {
          // 用户选择继续操作
        });
      } else {
        this.$router.back();
      }
    },

    // 显示物料清单弹窗
    showMaterialListDialog() {
      this.showMaterialDialog = true;
      this.loadMaterialList();
    },

    // 加载物料清单
    loadMaterialList() {
      if (!this.outboundInfo.site || !this.outboundInfo.buNo || !this.outboundNo) {
        this.$message.error('缺少必要参数，无法获取物料清单');
        return;
      }

      this.materialListLoading = true;
      const params = {
        site: this.outboundInfo.site,
        buNo: this.outboundInfo.buNo,
        outboundNo: this.outboundNo,
        warehouseId: getCurrentWarehouse() // 当前仓库
      };

      getMaterialList(params).then(({ data }) => {
        this.materialListLoading = false;
        if (data && data.code === 0) {
          this.materialList = data.data || [];
        } else {
          this.$message.error(data.msg || '获取物料清单失败');
          this.materialList = [];
        }
      }).catch(error => {
        this.materialListLoading = false;
        console.error('获取物料清单失败:', error);
        this.$message.error('获取物料清单失败');
        this.materialList = [];
      });
    },

    // 关闭物料清单弹窗
    closeMaterialDialog() {
      this.showMaterialDialog = false;
      this.hidePartNameTip();
    },

    // 显示物料名称提示框
    showPartNameTip(partName, event) {
      if (!partName || partName === '-') return;
      const rect = event.target.getBoundingClientRect();
      this.tooltipStyle = { top: (rect.top - 5) + 'px', left: rect.left + 'px' };
      this.currentPartName = partName;
      this.showPartNameTooltip = true;
      setTimeout(() => { document.addEventListener('click', this.hidePartNameTip); }, 0);
    },

    // 隐藏物料名称提示框
    hidePartNameTip() {
      this.showPartNameTooltip = false;
      this.currentPartName = '';
      document.removeEventListener('click', this.hidePartNameTip);
    },

    showStockDialogFn(item) {
      this.currentPartNo = item.materialCode || item.partNo;
      this.showStockDialog = true;
      this.loadInventoryStock(this.currentPartNo);
    },

    loadInventoryStock(partNo) {
      this.stockLoading = true;
      const params = {
        site: this.outboundInfo.site,
        notifyNo: this.outboundNo,
        notifyType: '销售出库',
        orderNo: '',
        orderLineNo: '',
        partNo: partNo,
        warehouseId: localStorage.getItem('warehouseId') || ''
      };
      getInventoryStock(params).then(({ data }) => {
        this.stockLoading = false;
        if (data && data.code === 0) { this.stockList = data.data || []; }
        else { this.$message.error(data.msg || '获取可用库存失败'); this.stockList = []; }
      }).catch(error => { this.stockLoading = false; this.$message.error('获取可用库存失败'); this.stockList = []; });
    },

    closeStockDialog() {
      this.showStockDialog = false;
      this.stockList = [];
      this.currentPartNo = '';
    },

    // 加载出库单详情
    loadOutboundDetails() {
      const params = {
        outboundNo: this.outboundNo,
        buNo: this.buNo,
        warehouseId: getCurrentWarehouse(),
        site: localStorage.getItem('site'),
      };

      getOutboundDetails(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.outboundInfo = data.data;
          // 从出库单详情中获取关联单号和关联单行号
          if (data.data.relatedNo) {
            this.relatedNo = data.data.relatedNo;
          }
          if (data.data.relatedLineNo) {
            this.relatedLineNo = data.data.relatedLineNo;
          }
          // 加载已扫描的标签列表
          this.loadScannedLabelList();
        } else {
          this.$message.error(data.msg || '获取出库单详情失败');
        }
      }).catch(error => {
        console.error('获取出库单详情失败:', error);
        this.$message.error('获取出库单详情失败');
      });
    },

    // 加载已扫描标签列表（从临时表）
    loadScannedLabelList() {
      const params = {
        site: localStorage.getItem('site'),
        buNo: this.buNo,
        outboundNo: this.outboundNo,
        relatedNo: this.relatedNo,
        relatedLineNo: this.relatedLineNo
      };

      getScannedLabelList(params).then(({ data }) => {
        if (data && data.code === 0) {
          this.labelList = (data.data || []).map(item => ({
            id: Date.now() + Math.random(),
            labelCode: item.labelCode || item.RollNo,
            partNo: item.partNo || item.part_no,
            quantity: item.quantity || item.RollQty,
            batchNo: item.batchNo || '',
            locationId: item.locationId || ''
          }));
        } else {
          console.error('获取已扫描标签列表失败:', data.msg);
          this.labelList = [];
        }
      }).catch(error => {
        console.error('获取已扫描标签列表失败:', error);
        this.labelList = [];
      });
    }
  },

  mounted() {
    // 获取路由参数
    this.outboundNo = this.$route.params.outboundNo;
    this.buNo = this.$route.params.buNo;
    if (!this.outboundNo || !this.buNo) {
      this.$message.error('参数错误');
      this.$router.back();
      return;
    }

    // 聚焦扫描框
    this.$nextTick(() => {
      if (this.$refs.scanInput) {
        this.$refs.scanInput.focus();
      }
    });

    // 加载出库单详情
    this.loadOutboundDetails();
  }
};
</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;
  display: flex;
  align-items: center;
  gap: 12px;
}

.search-container .el-input {
  width: 240px;
  margin-right: 12px;
}

/* 紧凑型输入框样式 */
.compact-input ::v-deep .el-input__inner {
  height: 36px;
  padding: 0 12px 0 35px;
  font-size: 14px;
}

.compact-input ::v-deep .el-input__prefix {
  left: 10px;
}

.compact-input ::v-deep .el-input__suffix {
  right: 30px;
}

/* 模式切换开关 */
.mode-switch {
  position: relative;
  display: inline-block;
}

.custom-switch {
  transform: scale(1.3);
}
/* 中间文字 */
.switch-text {
  position: absolute;
  left: 25%;
  transform: translateX(-50%);
  top: 50%;
  transform: translateY(-50%) translateX(-50%);
  font-size: 12px;
  font-weight: 500;
  color: #606266;
  white-space: nowrap;
  pointer-events: none;
  z-index: 1;
  top: 53%;
  transform: translate(-50%, -50%);
  font-size: 12px;
  font-weight: bold;
  color: white;
  pointer-events: none;
  z-index: 2;
}

.switch-text2 {
  position: absolute;
  left: 75%;
  transform: translateX(-50%);
  top: 50%;
  transform: translateY(-50%) translateX(-50%);
  font-size: 12px;
  font-weight: 500;
  color: #606266;
  white-space: nowrap;
  pointer-events: none;
  z-index: 1;
  top: 53%;
  transform: translate(-50%, -50%);
  font-size: 12px;
  font-weight: bold;
  color: white;
  pointer-events: none;
  z-index: 2;
}

/* 调整 switch 尺寸以便容纳文字 */
.custom-switch ::v-deep .el-switch__core {
  width: 60px;
  height: 28px;
}

/* 物料信息卡片 */
.material-info-card {
  background: white;
  margin: 4px 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border: 1px solid #f0f0f0;
  overflow: hidden;
}

/* 卡片头部 */
.card-header {
  background: white;
  padding: 10px 16px;
  border-bottom: 1px solid #e8e8e8;
}

.card-title-group {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.title-label {
  font-size: 11px;
  color: #999;
  font-weight: normal;
}

.title-value {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  line-height: 1.2;
  letter-spacing: 0.5px;
}

/* 卡片主体 */
.card-body {
  padding: 12px 16px;
}

/* 信息行（关联单号和行号） */
.info-row {
  display: flex;
  gap: 12px;
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px dashed #e8e8e8;
}

.info-item {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
}

.info-label {
  font-size: 11px;
  color: #666;
  font-weight: 500;
  white-space: nowrap;
}

.info-value {
  font-size: 12px;
  color: #333;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.info-value.highlight {
  color: #333;
  font-weight: 600;
}

/* 统计数据行 */
.stats-row {
  display: flex;
  justify-content: space-between;
  gap: 6px;
}

.stat-item {
  flex: 1;
  text-align: center;
  padding: 6px 4px;
  background: white;
  border-radius: 6px;
}

.stat-label {
  font-size: 10px;
  color: #666;
  margin-bottom: 4px;
  font-weight: normal;
}

.stat-value {
  font-size: 12px;
  color: #333;
  font-weight: 600;
  line-height: 1.3;
}

.stat-value.single {
  color: #666;
}

.stat-value .qualified {
  color: #333;
  font-weight: 700;
}

.stat-value .total {
  color: #666;
  font-weight: 500;
}

.stat-value .separator {
  color: #ccc;
  margin: 0 2px;
}

/* 区域标题 */
.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px;
  background: white;
  margin: 0 16px;
  margin-top: 4px;
  border-radius: 8px 8px 0 0;
  border-bottom: 2px solid #17B3A3;
}

.title-left {
  display: flex;
  align-items: center;
}

.title-left i {
  color: #17B3A3;
  font-size: 16px;
  margin-right: 8px;
}

.title-left span {
  color: #17B3A3;
  font-size: 14px;
  font-weight: 500;
}

.title-right {
  display: flex;
  align-items: center;
}

.material-list-link {
  color: #17B3A3;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: underline;
  transition: color 0.2s ease;
}

.material-list-link:hover {
  color: #0d8f7f;
}

/* 标签列表 */
.label-list {
  background: white;
  margin: 0 16px 12px;
  border-radius: 0 0 8px 8px;
  overflow: hidden;
  flex: 1;
  display: flex;
  flex-direction: column;
  max-height: 300px; /* 限制最大高度 */
}

.label-list .list-body {
  flex: 1;
  overflow-y: auto; /* 允许垂直滚动 */
  max-height: 250px; /* 内容区域最大高度 */
}

/* 滚动条样式优化 */
.label-list .list-body::-webkit-scrollbar {
  width: 4px;
}

.label-list .list-body::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 2px;
}

.label-list .list-body::-webkit-scrollbar-thumb {
  background: #17B3A3;
  border-radius: 2px;
}

.label-list .list-body::-webkit-scrollbar-thumb:hover {
  background: #0d8f7f;
}

.list-header {
  display: flex;
  background: #f8f9fa;
  padding: 12px 8px;
  border-bottom: 1px solid #e0e0e0;
  font-size: 12px;
  color: #666;
  font-weight: 500;
}

.list-item {
  display: flex;
  padding: 12px 8px;
  border-bottom: 1px solid #f0f0f0;
  font-size: 12px;
  color: #333;
}

.list-item:last-child {
  border-bottom: none;
}

.col-no {
  width: 20px;
  text-align: center;
}

.col-label {
  flex: 2;
  text-align: center;
}

.col-part {
  flex: 2;
  text-align: center;
}

.col-qty {
  width: 60px;
  text-align: center;
}

.empty-labels {
  padding: 40px 20px;
  text-align: center;
  color: #999;
}

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

/* 底部操作按钮 */
.bottom-actions {
  display: flex;
  padding: 16px;
  gap: 20px;
  background: white;
  margin-top: auto;
}

.action-btn {
  flex: 1;
  padding: 12px;
  border: 1px solid #17B3A3;
  background: white;
  color: #17B3A3;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.action-btn:hover {
  background: #17B3A3;
  color: white;
}

.action-btn:active {
  transform: scale(0.98);
}

/* 物料清单弹窗样式 */
.material-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.material-modal {
  background: white;
  border-radius: 12px;
  width: 100%;
  max-width: 800px;
  max-height: 80vh;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.material-modal .modal-header {
  background: #17B3A3;
  color: white;
  padding: 5px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 28px;
}

.close-btn {
  font-size: 16px;
  cursor: pointer;
  color: white;
  transition: color 0.2s ease;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  color: #e0e0e0;
}

.material-modal .modal-title {
  font-size: 16px;
  font-weight: 500;
  margin: 0;
  line-height: 1.2;
}

.material-modal .modal-body {
  flex: 1;
  overflow: auto;
  padding: 0;
}

.material-table {
  min-width: 500px;
  width: max-content;
}

.table-header {
  display: flex;
  background: #f8f9fa;
  padding: 10px 6px;
  border-bottom: 2px solid #17B3A3;
  font-size: 12px;
  color: #333;
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 1;
}

.table-body {
  /* 垂直滚动由modal-body处理 */
}

.table-row {
  display: flex;
  padding: 10px 6px;
  border-bottom: 1px solid #f0f0f0;
  font-size: 12px;
  color: #333;
  transition: background-color 0.2s ease;
}

.table-row:hover {
  background-color: #f8f9fa;
}

.table-row:last-child {
  border-bottom: none;
}

.material-table .col-no {
  width: 25px;
  text-align: center;
  flex-shrink: 0;
  font-size: 12px;
}

.material-table .col-material-code {
  flex: 1.8;
  text-align: center;
  min-width: 100px;
  font-size: 12px;
  word-break: break-all;
}

.clickable-part { color: #17B3A3; font-weight: 500; cursor: pointer; text-decoration: underline; }
.clickable-part:hover { color: #0d8f7f; }
.stock-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.5); z-index: 10000; display: flex; align-items: center; justify-content: center; padding: 20px; }
.stock-modal { background: white; border-radius: 12px; width: 100%; max-width: 800px; max-height: 80vh; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3); overflow: hidden; display: flex; flex-direction: column; }
.stock-modal .modal-header { background: #17B3A3; color: white; padding: 5px 16px; display: flex; justify-content: space-between; align-items: center; min-height: 28px; }
.stock-modal .modal-body { flex: 1; overflow: auto; padding: 0; }
.stock-table { min-width: 500px; width: max-content; }
.stock-table .table-header { display: flex; background: #f8f9fa; padding: 10px 6px; border-bottom: 2px solid #17B3A3; font-size: 12px; color: #333; font-weight: 600; position: sticky; top: 0; }
.stock-table .table-body { /* 垂直滚动由modal-body处理 */ }
.stock-table .table-row { display: flex; padding: 10px 6px; border-bottom: 1px solid #f0f0f0; font-size: 12px; color: #333; }
.stock-table .table-row:hover { background-color: #f8f9fa; }
.stock-table .col-roll-no { flex: 1.5; text-align: center; min-width: 100px; }
.stock-table .col-qty { flex: 0.8; text-align: center; min-width: 60px; }
.unit-text { color: #999; font-size: 11px; margin-left: 2px; }
.stock-table .col-location { flex: 0.8; text-align: center; min-width: 60px; }
.stock-table .col-batch { flex: 1; text-align: center; min-width: 80px; }
.stock-table .col-date { flex: 1; text-align: center; min-width: 80px; }
.stock-modal .modal-footer { padding: 15px 20px; display: flex; justify-content: center; border-top: 1px solid #f0f0f0; }
.empty-stock { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 60px 20px; color: #999; }
.empty-stock i { font-size: 48px; margin-bottom: 16px; color: #ddd; }
.empty-stock p { margin: 0; font-size: 14px; }

.material-table .col-required-qty {
  flex: 0.8;
  text-align: center;
  min-width: 65px;
  font-size: 12px;
}

.material-table .col-available-qty {
  flex: 0.8;
  text-align: center;
  min-width: 65px;
  font-size: 12px;
}

.material-table .col-scans-qty {
  flex: 0.8;
  text-align: center;
  min-width: 65px;
  font-size: 12px;
}

/* 物料名称列样式 */
.material-table .col-part-name {
  flex: 1.2;
  text-align: left;
  min-width: 80px;
  font-size: 12px;
  overflow: hidden;
}

.material-table .col-part-name .part-name-text {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  color: #17B3A3;
  cursor: pointer;
}

.material-table .col-part-name .part-name-text:active {
  color: #0d8f7f;
}

/* 物料名称提示框样式 */
.part-name-tooltip {
  position: fixed;
  z-index: 10001;
  transform: translateY(-100%);
  max-width: 200px;
  animation: tooltipFadeIn 0.2s ease;
}

@keyframes tooltipFadeIn {
  from { opacity: 0; transform: translateY(-100%) translateY(5px); }
  to { opacity: 1; transform: translateY(-100%) translateY(0); }
}

.part-name-tooltip .tooltip-content {
  background: #303133;
  color: #fff;
  padding: 8px 12px;
  border-radius: 4px;
  font-size: 12px;
  line-height: 1.4;
  word-break: break-all;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
}

.part-name-tooltip::after {
  content: '';
  position: absolute;
  bottom: -6px;
  left: 15px;
  border-width: 6px 6px 0 6px;
  border-style: solid;
  border-color: #303133 transparent transparent transparent;
}

.material-modal .modal-footer {
  padding: 15px 20px;
  display: flex;
  justify-content: center;
  border-top: 1px solid #f0f0f0;
}

.btn-close {
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid #17B3A3;
  background: white;
  color: #17B3A3;
}

.btn-close:hover {
  background: #17B3A3;
  color: white;
}

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

.loading-container i {
  font-size: 24px;
  margin-bottom: 12px;
  color: #17B3A3;
}

.loading-container span {
  font-size: 14px;
}

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

.empty-material i {
  font-size: 48px;
  margin-bottom: 16px;
  color: #ddd;
}

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

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

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

  .material-info-card {
    margin: 4px 12px;
  }

  .card-header {
    padding: 10px 12px;
  }

  .card-body {
    padding: 10px 12px;
  }

  .info-row {
    flex-direction: column;
    gap: 8px;
  }

  .stats-row {
    flex-wrap: wrap;
    gap: 6px;
  }

  .stat-item {
    flex: 0 0 48%;
    min-width: 45%;
  }

  .section-title {
    margin: 0 12px;
    margin-top: 4px;
  }

  .label-list {
    margin: 0 12px 8px;
  }

  .list-header, .list-item {
    font-size: 11px;
  }

  .col-label, .col-part {
    flex: 1.5;
  }
}
</style>
