@use 'sass:map';

@use 'mixins/mixins' as *;
@use 'mixins/utils' as *;
@use 'mixins/var' as *;
@use 'common/var' as *;
@use 'common/popup' as *;

@include b(modal) {
  @include set-component-css-var('modal', $modal);

  position: relative;
  width: var(--sg-modal-width);
  height: var(--sg-modal-height, auto);
  margin: var(--sg-modal-margin-top, 15vh) auto 50px var(--sg-modal-margin-left, auto);
  background: var(--sg-modal-bg-color);
  border-radius: var(--sg-border-radius-base);
  box-shadow: var(--sg-modal-box-shadow);
  box-sizing: border-box;
  outline: none;

  @include when(maximize) {
    --sg-modal-width: 100% !important;
    --sg-modal-margin-top: 0;
    --sg-modal-margin-left: 0;

    margin-bottom: 0;
    height: 100%;
    overflow: auto;
  }

  @include when(draggable) {
    @include e(header) {
      cursor: move;
      user-select: none;
    }
  }

  @include e(header) {
    $paddingTop: nth(map.get($modal, 'header-padding'), 1);
    $paddingRight: nth(map.get($modal, 'header-padding'), 2);

    border-top-left-radius: var(--sg-border-radius-base);
    border-top-right-radius: var(--sg-border-radius-base);
    overflow: hidden;

    @include e(title) {
      padding: $paddingTop $paddingRight;
      color: var(--sg-text-color-primary);
      font-size: var(--sg-modal-title-font-size);
      font-weight: 600;
      line-height: 24px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    @include e(actions) {
      position: absolute;
      top: $paddingTop;
      right: $paddingRight;
      display: flex;
      justify-content: flex-end;
      align-items: center;

      @include meb(button) {
        width: var(--sg-modal-tools-width);
        height: var(--sg-modal-tools-width);
        padding: 0;
        background: transparent;
        border: none;
        outline: none;
        color: var(--sg-modal-tools-color);
        cursor: pointer;

        &:hover {
          color: var(--sg-color-primary);
        }
      }
    }
  }

  @include e(body) {
    font-size: var(--sg-modal-body-font-size);
    padding: var(--sg-modal-body-padding);
    line-height: 1.5;
    overflow: hidden;

    @include m(title) {
      color: var(--sg-text-color-primary);
      font-weight: 600;
      white-space: nowrap;
      overflow: hidden;
    }

    & > iframe {
      width: 100%;
      height: 100%;
      border: 0;
      vertical-align: top;
    }
  }

  @include e(footer) {
    padding: var(--sg-modal-footer-padding);
    text-align: right;
  }

  // 内容居中布局
  @include m(center) {
    text-align: center;

    @include e(body) {
      text-align: initial;
    }

    @include e(footer) {
      text-align: inherit;
    }
  }

  // 确认对话框
  @include m(confirm) {
    @include when(prompt) {
      @include e(body) {
        padding: 32px 32px 24px;
      }

      @include e(footer) {
        padding: 0 32px 32px;
      }
    }

    @include e(body) {
      @include m(icon) {
        float: left;
        font-size: var(--sg-modal-confirm-icon-size);
        margin-right: 16px;

        & + .sg-modal__body--title + .sg-modal__body--content {
          margin: 8px 0 0 40px;
        }
      }

      @include m(title) {
        font-size: 18px;
        margin-left: 40px;
      }

      @include m(content) {
        color: var(--sg-text-color-regular);
      }
    }
  }

  // 提示对话框
  @include m(alert) {
    @include when(prompt) {
      @include e(body) {
        padding: 48px 0 16px;
      }

      @include e(footer) {
        padding: 0 0 48px;
      }
    }

    @include e(body) {
      text-align: center;

      @include m(icon) {
        font-size: var(--sg-modal-alert-icon-size);
      }

      @include m(title) {
        font-size: 18px;
        margin: 16px 0 8px;
      }

      @include m(content) {
        color: var(--sg-text-color-regular);
      }
    }

    @include e(footer) {
      text-align: center;
    }
  }

  // 表单对话框
  @include m(form) {
    @include e(header) {
      border-bottom: 1px solid var(--sg-border-color-extra-light);
    }

    @include e(body) {
      position: relative;

      @include m(content) {
        width: 100%;
        height: 100%;
      }
    }

    @include e(footer) {
      border-top: 1px solid var(--sg-border-color-extra-light);
    }
  }

  @at-root #{&}-minimize-box {
    $width: map.get($modal, 'tools-width');
    $height: map.get($modal, 'tools-width');

    position: relative;
    left: unset;
    height: $height;
    line-height: $height;
    background-color: map.get($modal, 'bg-color');
    margin: 10px;
    box-shadow: map.get($modal, 'box-shadow');
    display: inline-block;

    #{&}__title {
      max-width: 200px;
      margin: 0 10px;
      display: inline-block;
      padding-right: $width * 2;
      overflow-x: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;

      &.without-closable {
        padding-right: $width;
      }
    }

    #{&}__actions {
      @extend .sg-modal__actions;
      top: 0;
      right: 0;

      .sg-modal__actions--button {
        width: $width;
        height: $height;
      }
    }
  }
}

.#{$namespace}-modal-minimize-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 50px;
  z-index: 1000;
  overflow: auto;
}

.#{$namespace}-overlay-modal {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
}

.modal-fade-enter-active {
  animation: modal-fade-in var(--sg-transition-duration);
  .#{$namespace}-overlay-modal {
    animation: modal-fade-in var(--sg-transition-duration);
  }
}

.modal-fade-leave-active {
  animation: modal-fade-out var(--sg-transition-duration);
  .#{$namespace}-overlay-modal {
    animation: modal-fade-out var(--sg-transition-duration);
  }
}

@keyframes modal-fade-in {
  0% {
    transform: translate3d(0, -20px, 0);
    opacity: 0;
  }
  100% {
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
}

@keyframes modal-fade-out {
  0% {
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
  100% {
    transform: translate3d(0, -20px, 0);
    opacity: 0;
  }
}

@keyframes modal-fade-in {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@keyframes modal-fade-out {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
