@use '../../styles/_ios-theme.scss' as *;

:host { 
  display: block; 
  height: 100%; 
}

// 全局 Material Tooltip 样式修复
::ng-deep {
  .mat-mdc-tooltip {
    font-size: 12px !important;
    background-color: rgba(97, 97, 97, 0.95) !important;
    color: white !important;
    padding: 6px 12px !important;
    border-radius: 4px !important;
    max-width: 250px !important;
    word-wrap: break-word !important;
    z-index: 9999 !important; // 确保 tooltip 在最上层
    pointer-events: none !important; // tooltip 不影响鼠标事件
  }

  .mat-mdc-tooltip-panel {
    pointer-events: none !important;
  }

  // 确保 Material 按钮内的内容正确显示
  .mat-mdc-button,
  .mat-mdc-raised-button,
  .mat-mdc-outlined-button,
  .mat-mdc-unelevated-button,
  .mat-mdc-icon-button {
    overflow: hidden !important; // 防止按钮内容溢出
    
    .mdc-button__label {
      display: inline-flex !important;
      align-items: center !important;
      gap: 4px !important;
      position: relative !important;
      z-index: 1 !important;
      overflow: hidden !important;
      text-overflow: ellipsis !important;
      white-space: nowrap !important;
    }
    
    // 确保按钮 ripple 效果不会导致内容显示问题
    .mat-mdc-button-persistent-ripple,
    .mat-ripple-element {
      z-index: 0 !important;
    }
  }
  
  // 修复 settlement-card 中的 Material 按钮样式
  .settlement-card {
    .mat-mdc-button,
    .mat-mdc-raised-button,
    .mat-mdc-outlined-button {
      min-width: 70px !important;
      height: 32px !important;
      line-height: 32px !important;
      
      span {
        line-height: normal !important;
      }
    }
  }
}

.settlement-card {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  border: 1px solid $ios-border;

  // 统计概览
  .stats-overview {
    margin-bottom: 24px;

    .stats-title {
      display: flex;
      align-items: center;
      gap: 8px;
      margin-bottom: 16px;
      font-size: 18px;
      font-weight: $ios-font-weight-semibold;
      color: $ios-text-primary;

      mat-icon {
        color: $ios-primary;
        font-size: 20px;
        width: 20px;
        height: 20px;
      }
    }

    .stats-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
      gap: 16px;

      .stat-item {
        background: #f8f9fa;
        border-radius: 8px;
        padding: 16px;
        text-align: center;
        border: 1px solid $ios-border;

        &.total {
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
          color: white;
          border: none;
        }

        &.pending {
          background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
          color: #8b4513;
          border: none;
        }

        &.overdue {
          background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
          color: #d63384;
          border: none;
        }

        &.completed {
          background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
          color: #198754;
          border: none;
        }

        .stat-value {
          font-size: 20px;
          font-weight: $ios-font-weight-bold;
          margin-bottom: 4px;
        }

        .stat-label {
          font-size: 12px;
          opacity: 0.8;
        }
      }
    }
  }

  // 筛选区域
  .filter-section {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    gap: 16px;

    .search-box {
      display: flex;
      align-items: center;
      background: #f8f9fa;
      border-radius: 20px;
      padding: 8px 16px;
      flex: 1;
      max-width: 300px;
      border: 1px solid $ios-border;

      mat-icon {
        color: $ios-text-secondary;
        margin-right: 8px;
        font-size: 18px;
        width: 18px;
        height: 18px;
      }

      input {
        border: none;
        background: transparent;
        outline: none;
        flex: 1;
        font-size: 14px;
        color: $ios-text-primary;

        &::placeholder {
          color: $ios-text-secondary;
        }
      }
    }

    .filter-buttons {
      display: flex;
      gap: 8px;

      .filter-btn {
        padding: 6px 16px;
        border: 1px solid $ios-border;
        background: white;
        border-radius: 16px;
        font-size: 13px;
        cursor: pointer;
        transition: all 0.2s ease;
        color: $ios-text-secondary;

        &:hover {
          background: #f8f9fa;
          border-color: $ios-primary;
        }

        &.active {
          background: $ios-primary;
          color: white;
          border-color: $ios-primary;
        }

        &.overdue.active {
          background: #dc3545;
          border-color: #dc3545;
        }
      }
    }
  }

  // 结算列表
  .settlement-list {
    .list-header {
      display: grid;
      grid-template-columns: 2fr 1fr 1fr 2fr 1fr;
      gap: 16px;
      padding: 12px 16px;
      background: #f8f9fa;
      border-radius: 8px;
      font-weight: $ios-font-weight-semibold;
      font-size: 13px;
      color: $ios-text-secondary;
      margin-bottom: 8px;
  
      span {
        text-align: left;
  
        &.col-amount,
        &.col-date {
          text-align: center;
        }
  
        &.col-actions {
          text-align: center;
        }
      }
    }
  
    .list-body {
      .settlement-item {
        display: grid;
        grid-template-columns: 2fr 1fr 1fr minmax(200px, 2fr) 1fr;
        gap: 16px;
        padding: 16px;
        border: 1px solid $ios-border;
        border-radius: 8px;
        margin-bottom: 8px;
        background: white;
        transition: all 0.2s ease;
        min-height: 80px;
        align-items: center;
  
        &:hover {
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
          border-color: $ios-primary;
        }
  
        // 项目信息列
        .col-project {
          display: flex;
          flex-direction: column;
          gap: 4px;
          min-width: 0; // 防止文本溢出
  
          .project-name {
            font-weight: $ios-font-weight-semibold;
            color: $ios-text-primary;
            font-size: 14px;
            line-height: 1.4;
            word-break: break-word;
            overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          line-clamp: 2; /* 标准属性，用于兼容性 */
          -webkit-box-orient: vertical;
          }
  
          .stage-name {
            font-size: 12px;
            color: $ios-text-secondary;
            background: rgba($ios-primary, 0.1);
            padding: 2px 6px;
            border-radius: 4px;
            display: inline-block;
            width: fit-content;
            max-width: 100%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
  
        // 金额列
        .col-amount {
          text-align: center;
          display: flex;
          flex-direction: column;
          gap: 2px;
  
          .amount {
            font-weight: $ios-font-weight-semibold;
            color: $ios-text-primary;
            font-size: 14px;
          }
  
          .percentage {
            font-size: 11px;
            color: $ios-text-secondary;
            background: rgba($ios-primary, 0.1);
            padding: 1px 4px;
            border-radius: 3px;
            display: inline-block;
          }
        }
  
        // 状态列
        .col-status {
          display: flex;
          flex-direction: column;
          align-items: center;
          gap: 4px;
  
          .status-badge {
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 11px;
            font-weight: $ios-font-weight-medium;
            text-align: center;
            white-space: nowrap;
            min-width: 60px;
  
            &.pending {
              background: rgba(255, 193, 7, 0.1);
              color: #f57c00;
              border: 1px solid rgba(255, 193, 7, 0.3);
            }
  
            &.completed {
              background: rgba(76, 175, 80, 0.1);
              color: #388e3c;
              border: 1px solid rgba(76, 175, 80, 0.3);
            }
  
            &.overdue {
              background: rgba(244, 67, 54, 0.1);
              color: #d32f2f;
              border: 1px solid rgba(244, 67, 54, 0.3);
            }
          }
  
          .overdue-days {
            font-size: 10px;
            color: #d32f2f;
            background: rgba(244, 67, 54, 0.1);
            padding: 2px 6px;
            border-radius: 8px;
            white-space: nowrap;
          }
  
          .auto-process-btn {
            margin-top: 4px;
            width: 32px;
            height: 32px;
            min-width: 32px;
  
            mat-icon {
              font-size: 16px;
              width: 16px;
              height: 16px;
            }
          }
        }
  
        // 操作列 - 重点修复区域
        .col-actions {
          display: flex;
          justify-content: center;
          align-items: center;
          min-width: 200px;
          max-width: 100%;
          position: relative; // 添加相对定位
          overflow: visible; // 确保 tooltip 可以显示
  
          .action-buttons {
            display: flex;
            flex-wrap: wrap;
            gap: 6px;
            justify-content: center;
            align-items: center;
            width: 100%;
            position: relative; // 添加相对定位
            z-index: 1; // 确保按钮在正确的层级
  
            button {
              font-size: 11px;
              padding: 6px 12px;
              min-width: 70px;
              height: 32px;
              border-radius: 6px;
              transition: all 0.2s ease;
              position: relative;
              overflow: hidden; // 保持 hidden 防止内容溢出
              white-space: nowrap;
              display: inline-flex;
              align-items: center;
              justify-content: center;
              line-height: 1;
              
              // 防止按钮内容溢出
              span {
                display: inline-flex;
                align-items: center;
                max-width: 100%;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
                position: relative;
                z-index: 1;
                line-height: 1.2;
              }

              mat-icon {
                font-size: 14px;
                width: 14px;
                height: 14px;
                margin-right: 4px;
                vertical-align: middle;
                flex-shrink: 0; // 防止图标被压缩
              }

              mat-spinner {
                margin-right: 4px;
                vertical-align: middle;
                flex-shrink: 0; // 防止 spinner 被压缩
              }

              // 悬浮效果优化
              &:hover:not(:disabled) {
                transform: translateY(-1px);
                box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
                z-index: 2; // 提升 z-index 确保悬浮时按钮在最上层
                
                // 确保悬浮时文字清晰可见
                span {
                  opacity: 1;
                  color: inherit;
                }
              }

              &:disabled {
                opacity: 0.6;
                cursor: not-allowed;
                transform: none;
                box-shadow: none;
              }

              // 特定按钮样式
              &.acceptance-btn {
                background: rgba(33, 150, 243, 0.1);
                border-color: rgba(33, 150, 243, 0.3);
                color: #1976d2;

                &:hover:not(:disabled) {
                  background: rgba(33, 150, 243, 0.2);
                  border-color: #1976d2;
                }
              }

              &.reminder-btn {
                background: rgba(255, 152, 0, 0.1);
                border-color: rgba(255, 152, 0, 0.3);
                color: #f57c00;

                &:hover:not(:disabled) {
                  background: rgba(255, 152, 0, 0.2);
                  border-color: #f57c00;
                }
              }

              &.wechat-send-btn {
                background: #1976d2;
                color: white;
                border: none;

                &:hover:not(:disabled) {
                  background: #1565c0;
                }
              }

              &.process-btn {
                background: #4caf50;
                color: white;
                border: none;

                &:hover:not(:disabled) {
                  background: #45a049;
                }
              }
            }
          }
        }
  
        // 日期列
        .col-date {
          text-align: center;
          display: flex;
          flex-direction: column;
          gap: 2px;
  
          .settled-date,
          .due-date {
            font-size: 12px;
            color: $ios-text-primary;
            font-weight: $ios-font-weight-medium;
          }
  
          .date-label {
            font-size: 10px;
            color: $ios-text-secondary;
          }
        }
  
        // 状态相关的行样式
        &.pending {
          border-left: 4px solid #ff9800;
        }
  
        &.completed {
          border-left: 4px solid #4caf50;
          opacity: 0.8;
        }
  
        &.overdue {
          border-left: 4px solid #f44336;
          background: rgba(244, 67, 54, 0.02);
        }
      }
    }
  
    // 空状态
    .empty-state {
      text-align: center;
      padding: 40px 20px;
      color: $ios-text-secondary;
  
      .empty-icon {
        font-size: 48px;
        margin-bottom: 16px;
      }
  
      .empty-title {
        font-size: 16px;
        font-weight: $ios-font-weight-semibold;
        color: $ios-text-primary;
        margin-bottom: 8px;
      }
  
      .empty-desc {
        font-size: 14px;
        line-height: 1.5;
      }
    }
  }
}

// 自动化控制头部样式
.automation-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  margin-bottom: 20px;
  color: white;
  
  .automation-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 16px;
    
    mat-icon {
      font-size: 20px;
      width: 20px;
      height: 20px;
    }
  }
  
  .automation-actions {
    display: flex;
    gap: 12px;
    align-items: center;
    
    button {
      border-radius: 20px;
      font-weight: 500;
      display: flex;
      align-items: center;
      gap: 8px;
      
      background: rgba(255, 255, 255, 0.2);
      backdrop-filter: blur(10px);
      border: 1px solid rgba(255, 255, 255, 0.3);
      color: white;
      
      &:hover:not(:disabled) {
        background: rgba(255, 255, 255, 0.3);
      }
      
      &:disabled {
        opacity: 0.6;
      }

      mat-spinner {
        margin-right: 4px;
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .settlement-card {
    .stats-overview .stats-grid {
      grid-template-columns: repeat(2, 1fr);
    }

    .filter-section {
      flex-direction: column;
      align-items: stretch;
      gap: 12px;

      .search-box {
        max-width: none;
      }

      .filter-buttons {
        justify-content: center;
        flex-wrap: wrap;
      }
    }

    .settlement-list {
      .list-header {
        display: none;
      }

      .list-body .settlement-item {
        grid-template-columns: 1fr;
        gap: 12px;

        .col-actions .action-buttons {
          flex-direction: row;
          justify-content: center;
          flex-wrap: wrap;

          button {
            width: auto;
            min-width: 100px;
          }
        }
      }
    }
  }
}