@use 'sass:math';

@mixin make-grid($class) {
  @for $i from 1 through $Grid-columns {
    .#{$ns}Grid-col--#{$class}#{$i} {
      flex: 0 0 percentage(math.div($i, $Grid-columns));
      max-width: percentage(math.div($i, $Grid-columns));
      min-height: 1px;
      border-right: 1px solid var(--saas-border-color);


      // gird最后一个再增加边距
      &:last-child {
        border-bottom: 1px solid var(--saas-border-color);
      }

      &.grid-item {
        margin-bottom: 16px;

        &.combination-grid-box {
          // margin-bottom: -1px !important;
          // margin-right: -1px !important;
          border-bottom: 1px solid var(--saas-border-color) !important;
          border-right: 1px solid var(--saas-border-color) !important;

          .combination-grid-box {
            // border-bottom: 0 !important;
            // margin-bottom: 0 !important;

            &:last-child {
              border-right: 0 !important;
              margin-right: 0 !important;
              border-bottom: 0 !important;
              margin-bottom: 0 !important;
            }
          }
        }
      }

      @if $i ==12 {
        &.grid-item {
          &.combination-grid-box {
            border-right: 0 !important;
            margin-right: 0;
          }
        }
      }

      @else {
        &.grid-item {
          &.combination-grid-box {
            width: 200px;
            border-right: 1px solid var(--saas-border-color) !important;
          }
        }
      }
    }

    .#{$ns}Form--quickEdit {
      .#{$ns}Grid-col--#{$class}#{$i} {
        border-right: none;
        border-bottom: none;
      }
    }

  }

  .mobile-grid-item {
    margin: 0 auto;

    .#{$ns}Grid--slider {
      opacity: 0
    }

    // 移动端下的无数据的图片的宽度配置为100
    .ant-empty-image {
      svg {
        width: 100%;
      }
    }

    // 2025-03-24 移动端的grid布局下又不要这个border
    .grid-relative-wrapper {
      // 移动端需要继承父容器高度，以展示正确图表高度
      height: inherit;

      .#{$ns}Quick-launch {
        border-right: none;

        &-body {
          border-top: none;
        }

        &-top {
          border-top: none;
        }
      }
    }

    &.#{$ns}Grid-col--md12 {
      .grid-relative-wrapper {
        height: 100%;
      }
    }
  }

  .grid-item-fixed {
    position: sticky;
    top: 0;
    z-index: 5;
  }

  .manual-fix-grid {
    position: sticky;
    top: 0;

    .affix-wrapper {
      position: absolute;
      top: 0;
      right: 0;
      z-index: 4;
      cursor: pointer;
      font-size: 16px;

      &.fixed {
        color: #3574EE;
      }

      &:hover {
        color: #777;
      }
    }
  }

  .#{$ns}Grid {
    &.grid-flex-zero {
      flex: 0 !important;
    }

    &-item-search {
      .mobile-grid-item {
        height: inherit;

        &.grid-item.combination-grid-box {
          height: unset;
        }

        // 这里也设置，为了防止容器没有配置Panel
        &.grid-item-fixed {
          height: auto !important;
        }
      }
    }

    // 用于解决移动端图表在分配grid-12 的时候高度异常
    &-col--md12 {
      &.mobile-grid-item {
        &.#{$ns}Panel-Combination-Filter {
          height: auto;
        }
      }
    }

    // 组合图表相关

    &.grid-chart-item {
      display: flex;
      flex-direction: column;
      flex-wrap: nowrap;

      .#{$ns}Panel-Combination-Filter {
        width: 100%;
      }

      .#{$ns}Panel-Combination-Filter,
      .grid-item-fixed {
        flex: unset;
      }

      .#{$ns}Panel-Combination-Chart {
        flex: 1;
        min-width: 100%;

        .#{$ns}Panel {
          height: inherit;
        }
      }
    }

  }

  .#{$ns}Grid-col--#{$class} {
    flex-basis: 0;
    flex-grow: 1;
    min-height: 1px;
    max-width: 100%;
    width: 100%;
    padding-left: math.div($Grid-gutterWidth, 2);
    padding-right: math.div($Grid-gutterWidth, 2);
  }

  .#{$ns}Grid-col--#{$class}Auto {
    flex: 0 0 auto;
    width: auto;
    max-width: 100%;
    padding-left: math.div($Grid-gutterWidth, 2);
    padding-right: math.div($Grid-gutterWidth, 2);
  }

  .#{$ns}Grid--slider {
    // visibility: hidden;
    position: absolute;
    right: 0;
    top: 25%;
    transform: translate(0, -50%);
    cursor: ew-resize;

    font-size: 10px;
    font-weight: 600;
    width: 20px;
    height: 55px;
    border-radius: 2px;
    border: 0px;
    line-height: 1.4;
    text-align: center;
    user-select: none;
    background-color: #fff;
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
    box-shadow: 0 2px 4px 0 rgb(0 0 0 / 15%);
    z-index: 3;

    .#{$ns}Grid--sliders {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
    }
  }

  .grid-relative-wrapper {
    position: relative;
    // PC端需要100%，保证图表正确展示高度
    height: 100%;

    // 2025-03 grid布局下的PC的快速发起容器需要在右侧加一个边框
    .#{$ns}Quick-launch {
      border-right: 1px solid var(--saas-border-color);

      &-header-title {
        // 2025-03 产品说这里的标题样式有问题，统一和底下组合页面的标题统一
        font-size: 18px;
        font-weight: 600;
      }

    }
  }
}

// Generate Grid row
@mixin make-row($gutter: $Grid-gutterWidth) {
  // margin-left: math.div($gutter, -2);
  // margin-right: math.div($gutter, -2);
  display: flex;
  flex-wrap: wrap;
}

@include make-grid(xs);

@include media-breakpoint-up(sm) {
  .#{$ns}Grid {

    @include make-row;
    flex: 1;
    position: relative;

    &--hRight {
      justify-content: flex-end;
    }

    &--hCenter {
      justify-content: center;
    }

    &--hBetween {
      justify-content: space-between;
    }

    &--vMiddle>div {
      display: flex;
      flex-direction: column;
      justify-content: center;
    }

    &--vBottom>div {
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
    }

    &--vBetween>div {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }

    &>&-col--vTop {
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
    }

    &>&-col--vMiddle {
      display: flex;
      flex-direction: column;
      justify-content: center;
    }

    &>&-col--vBottom {
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
    }

    &>&-col--vBetween {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
    }

  }

  .#{$ns}Grid.scroll-grid-item {
    height: inherit !important;

    .scroll-grid-child-item {
      height: inherit;
    }
  }

  .#{$ns}Distance {
    height: 100%;
    overflow: auto;
    margin-right: 0;

    >.#{$ns}Grid-col--md12 {
      height: 50%;
      min-width: 100%;
      padding-right: 0rem;
    }

    .#{$ns}Grid-col--md12 {
      :first{
        border-bottom: 1px solid var(--saas-border-color);
      }
      .#{$ns}Panel {
        overflow: hidden;

        .#{$ns}Panel-body {
          height: 100%;

          .#{$ns}Combination {
            margin: unset;
          }
        }
      }

      .#{$ns}Crud {
        height: 100%;

        .#{$ns}Table {
          flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          height: 100%;

          .#{$ns}Table-contentWrap {
            flex: 1;
            // 2025-03 要求这里table背景为 透明
            // background-color: var(--saas-Background-Header);
            background-color: transparent;
            height: calc(100% - 88px);
            overflow-y: auto;

            .#{$ns}Table-content {
              height: 100%;

              .#{$ns}Table-table {
                thead>tr>th {
                  .#{$ns}TableCell--title {
                    .#{$ns}Button--link {
                      line-height: initial;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    .#{$ns}Grid--slider {
      bottom: 0;
      top: 100%;
      left: 50%;
      cursor: n-resize;
      width: 55px;
      height: 16px;
      line-height: 1;
      border-radius: 5px;

      svg {
        color: #000;
        font-size: 12px;
      }

      &.slider-closed {
        cursor: default;
      }
    }
  }

  .grid-collpase {
    position: absolute;
    top: 200px;
    right: 0;
    width: 20px;
    border-radius: 7px 0px 0px 7px;
    background-color: #eee;
    z-index: 5;
    font-size: 12px;
    color: #666;
    text-align: center;
    letter-spacing: 3px;

    .anticon {
      margin-bottom: 4px;
    }

    padding: 4px;
    cursor: pointer;
    box-shadow: -2px 3px 4px 0 #aaa;
    white-space: normal;

    &.closed {
      border-radius: 0 7px 7px 0;
      left: 0;
      box-shadow: 2px 3px 4px 0 #aaa;
    }
  }

  .#{$ns}Grid-line {
    width: 1px;
    position: relative;
    background-color: white;

    .collpase {
      position: absolute;
      z-index: 8;
      top: 0;
      bottom: 0;
      width: 10px;
      height: 100%;
      left: -5px;
      display: flex;
      justify-content: center;
      align-items: center;
      user-select: none;
      cursor: ew-resize;

      .left {
        cursor: pointer;
        position: absolute;
        top: 50%;
        right: 4px;
        padding: 1px;
        color: rgba(0, 0, 0, 0.4);
      }

      .right {
        cursor: pointer;
        position: absolute;
        top: 50%;
        left: 4px;
        padding: 1px;
        color: rgba(0, 0, 0, 0.4);
      }

      .left-hovered {
        border: 1px solid #3574EE;
        border-radius: 8px 0 0 8px;
        border-right: none;
        font-size: 13px;
      }

      .right-hovered {
        border: 1px solid #3574EE;
        border-radius: 0 8px 8px 0;
        border-left: none;
        font-size: 13px;
      }
    }

  }

  .#{$ns}Grid--combination {
    background-color: var(--saas-grid-combination-color);

    .#{$ns}Grid-item--combination {
      padding: 0;

      .#{$ns}Panel {
        // padding-right: 0.5rem;

        .#{$ns}Panel-heading {
          .#{$ns}Panel-title {
            font-size: 15px;
            font-weight: 600;
          }
        }
      }
    }

    .#{$ns}Grid-item--combination:nth-child(1) {

      .tree-layout {
        padding-right: 0rem;

        .#{$ns}Panel-body {
          .#{$ns}Form {
            .#{$ns}Form-item:nth-child(1) {
              margin-bottom: 0;

              .#{$ns}TextControl {
                .#{$ns}TextControl-input {
                  border-color: transparent;
                  background-color: var(--Form-input-bg);
                  border-radius: var(--Form-input-borderRadius);
                }
              }
            }

            >.#{$ns}Form-item {
              >.#{$ns}Form-label {
                display: none;
              }

              >.#{$ns}TreeControl {
                border: none
              }
            }
          }
        }
      }

      .#{$ns}Grid--slider {
        transform: none;
        line-height: normal;
        box-shadow: none;
        top: 0;
        right: 0px;
        width: 8px;
        border-radius: 0;
        background-color: transparent;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;

        svg {
          transform: rotateY(180deg);
          font-size: 19px;
          z-index: 1;
        }

        &.slider-closed {
          width: 0;
        }
      }
    }

    .#{$ns}Grid-item--combination:nth-child(2) {
      .#{$ns}Panel {
        padding: 0rem;
      }
    }

    .#{$ns}Panel {
      border: none;

      >.#{$ns}Panel-body {
        >.#{$ns}Tabs.#{$ns}Combination {
          margin: 0;
        }
      }
    }
  }

  .scroll-grid-item {
    height: inherit;
  }

  .#{$ns}Grid-item-search {
    height: inherit !important;

    >.#{$ns}Grid-item--combination:nth-child(1) {
      padding-right: 8px;
      width: 12px;
      right: 0px;

      .#{$ns}Panel {
        overflow: hidden;
        opacity: 0;

        .#{$ns}Panel-heading {
          .#{$ns}Panel-title {
            white-space: nowrap;
          }
        }
      }

      .#{$ns}Grid--slider {
        width: 28px;

        .#{$ns}Grid--sliders {
          z-index: 2;
        }
      }
    }

  }

  @include make-grid(sm);

}

@include media-breakpoint-up(md) {
  @include make-grid(md);
}

@include media-breakpoint-up(lg) {
  @include make-grid(lg);
}

.#{$ns}Grid--none {
  margin-left: 0;
  margin-right: 0;

  >div {
    padding-left: 0;
    padding-right: 0;
  }
}

.#{$ns}Grid--xs {
  margin-left: calc(var(--gap-xs) * -0.5);
  margin-right: calc(var(--gap-xs) * -0.5);

  >div {
    padding-left: calc(var(--gap-xs) * 0.5);
    padding-right: calc(var(--gap-xs) * 0.5);
  }
}

.#{$ns}Grid--sm {
  margin-left: calc(var(--gap-sm) * -0.5);
  margin-right: calc(var(--gap-sm) * -0.5);

  >div {
    padding-left: calc(var(--gap-sm) * 0.5);
    padding-right: calc(var(--gap-sm) * 0.5);
  }
}

.#{$ns}Grid--base {
  margin-left: calc(var(--gap-base) * -0.5);
  margin-right: calc(var(--gap-base) * -0.5);

  >div {
    padding-left: calc(var(--gap-base) * 0.5);
    padding-right: calc(var(--gap-base) * 0.5);
  }
}

.#{$ns}Grid--md {
  margin-left: calc(var(--gap-md) * -0.5);
  margin-right: calc(var(--gap-md) * -0.5);

  >div {
    padding-left: calc(var(--gap-md) * 0.5);
    padding-right: calc(var(--gap-md) * 0.5);
  }
}

.#{$ns}Grid--lg {
  margin-left: calc(var(--gap-lg) * -0.5);
  margin-right: calc(var(--gap-lg) * -0.5);

  >div {
    padding-left: calc(var(--gap-lg) * 0.5);
    padding-right: calc(var(--gap-lg) * 0.5);
  }
}

.#{$ns}Grid {
  .grid-flex-auto {
    // display: block !important;
    flex: 0 1;

    // height: auto !important;
    .#{$ns}Panel-body {
      // flex: 0 1 auto;
    }

    .align-self-start {
      align-self: flex-start;
    }
  }

  &-col--md,
  .grid-item {
    background: var(--Page-main-bg);

    .#{$ns}Grid {

      .#{$ns}Grid-col--md12,
      .#{$ns}Grid-col--md,
      .grid-item {
        border-right: 0;
        box-shadow: none;
        padding: 0;
        box-shadow: none;
      }
    }
  }

  &--block {
    // 如果有固定高度配置。那容器需要切换为block
    display: block !important;
  }
}

@include media-breakpoint-down(sm) {
  .#{$ns}Grid {
    padding: 0 8px;
    background-color: var(--saas-Background-Header);

    .#{$ns}Grid {
      padding: 0 auto;
    }

    &>.inline-grid-item {
      display: inline-block;

      &.#{$ns}Grid-col--md1 {
        width: 8.33333333%;
      }

      &.#{$ns}Grid-col--md2 {
        width: 16.66666666%;
      }

      &.#{$ns}Grid-col--md3 {
        width: 25%;
      }

      &.#{$ns}Grid-col--md4 {
        width: 33.33333333%;
      }

      &.#{$ns}Grid-col--md6 {
        width: 50%;
      }

      &.#{$ns}Grid-col--md9 {
        width: 75%;
      }

      &.#{$ns}Grid-col--md12 {
        width: 100%;
      }
    }
  }
}

// 扫码内的grid项高度自适应
.#{$ns}Scan-code-container {
  >.#{$ns}Grid {
    >.mobile-grid-item {
      height: auto;
    }
  }
}