<template>
  <a-skeleton v-if="isskeleton" />
  <div v-else :class="$style.cardCarouselWrapper">
    <div
      :class="[$style.cardCarouselNavLeft, $style.cardCarouselNav]"
      :disabled="atHeadOfList"
      @click="moveCarousel(-1)"
    >
      <a-icon type="left" />
    </div>
    <div :class="$style.cardCarousel">
      <div :class="$style.cardCarouselOverflowContainer">
        <div
          :class="$style.cardCarouselCards"
          :style="{ transform: 'translateX' + '(' + currentOffset + 'px' + ')' }"
        >
          <div
            v-for="(item, index) in items"
            :key="index"
            ref="barparent"
            :class="$style.cardCarouselCard"
            :style="'height:' + cardheight"
            @click="golistUrl(item)"
          >
            <div
              :style="'width:' + cardwidth"
              :class="[
                $style.cardCarouselCardHeader,
                { [$style.exceedItem]: fnCheckItem(item, index) },
              ]"
            >
              <div :class="$style.cardTitle">
                <!-- <div :class="$style.pointOut">
                  <div :class="$style.pointIn"></div>
                </div> -->
                <div :class="$style.todoradio">
                  <a-icon type="sd-audit-todoradio" theme="filled"></a-icon>
                </div>
                <div v-if="typelist === 'processed'" :class="$style.express">
                  {{ item.flowName }}
                </div>
                <div v-else :class="$style.express">
                  {{ item.processName ? item.processName : item.appCat }}
                </div>
              </div>
              <div :class="$style.cardCont" :title="item.title">
                {{ formatTitle(item.title, 28) }}
              </div>
            </div>
            <div :style="'width:' + cardwidth" :class="$style.cardCarouselCardFooter">
              <div v-if="item.urgencyLevel === 1 && item.openMode === 1" :class="$style.urgent">
                {{ item.urgencyLevelName }}
              </div>
              <div
                v-if="item.urgencyLevel === 2 && item.openMode === 1"
                :class="$style.extraUrgent"
              >
                {{ item.urgencyLevelName }}
              </div>
              <div :class="$style.footerCont">
                <div :class="$style.dep" :title="item.senderName">
                  <div :class="$style.floatLeft">发送人员：</div>
                  <div :class="$style.floatRight">
                    {{ item.senderName }}
                  </div>
                </div>
                <div :class="$style.dep" :title="item.sentDate | sdDateFormat('YYYY-MM-DD')">
                  <div :class="$style.floatLeft">发送时间：</div>
                  <div :class="$style.floatRight">
                    {{ item.sentDate | sdDateFormat('YYYY.MM.DD HH:mm:ss') }}
                  </div>
                </div>
                <div v-if="item.stepColor" :class="$style.stepStatus">
                  <a-icon type="info-circle" theme="filled" /> {{ item.stepTimeToDeadline }}
                </div>
                <div v-if="!item.flowColor && !item.stepColor" :class="$style.stepStatus"></div>
              </div>
              <!-- <div :class="$style.rightHorn">
                <a-icon type="star" theme="filled" :class="$style.star" />
              </div> -->
            </div>
          </div>
          <a-empty v-if="items.length < 1" :class="$style.emptySize">
            <a-icon slot="image" type="sd-notebook3" />
          </a-empty>
        </div>
      </div>
    </div>
    <div
      :class="[$style.cardCarouselNavRight, $style.cardCarouselNav]"
      :disabled="atEndOfList"
      @click="moveCarousel(1)"
    >
      <a-icon type="right" />
    </div>
  </div>
</template>

<script>
import components from './_import-components/iam-todo-list-card-import'

export default {
  name: 'IamTodoListCard',
  components: {
    ...components,
  },
  props: {
    items: {
      type: Array,
      default: function () {
        return []
      },
    },
    // 展示骨架
    isskeleton: {
      type: Boolean,
      default: false,
    },
    // 列表类型
    typelist: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      currentOffset: 0,
      windowSize: 3,
      cardwidth: null,
      cardheight: null,
    }
  },
  computed: {
    atEndOfList() {
      if (this.cardwidth) {
        return (
          this.currentOffset <=
          parseInt(this.cardwidth) * -1 * (this.items.length - this.windowSize)
        )
      } else {
        return true
      }
    },
    atHeadOfList() {
      return this.currentOffset >= 0
    },
  },
  mounted() {
    // 切换页签的时候计算卡片高度
    if (this.items) {
      this.cardRatio()
    }
    // 调整浏览器时候计算卡片高度
    window.onresize = () => {
      this.$nextTick(() => {
        this.cardRatio()
      })
    }
  },
  updated() {
    // 初始化卡片宽高比
    this.cardRatio()
  },
  destroyed() {
    window.onresize = null
  },
  methods: {
    // 格式化标题
    formatTitle(title, length) {
      if (!title) return '[无标题]'
      if (title.length > length) {
        return title.substr(0, length - 1) + '...'
      } else {
        return title
      }
    },
    //  计算待办超时
    fnCheckItem(item, index) {
      // 如果节点超时，才显示颜色
      if (item.stepColor) {
        return true
      }
      return false
    },
    // 计算卡片宽高比 设计图比例约为1.38
    cardRatio() {
      if (this.$refs.barparent) {
        // 分辨率大于1600，响应式适配比例，小于1600，给固定值
        if (document.body.clientWidth > 1600) {
          const h = this.$refs.barparent[0].clientHeight
          this.cardwidth = h * 1.37 + 'px'
        } else {
          this.cardwidth = '345px'
          // 4px为阴影
          this.cardheight = 345 / 1.47 - 4 + 'px'
        }
      }
    },
    moveCarousel(direction) {
      if (direction === 1 && !this.atEndOfList) {
        // +20为卡片边距
        this.currentOffset -= parseInt(this.cardwidth) + 20
      } else if (direction === -1 && !this.atHeadOfList) {
        this.currentOffset += parseInt(this.cardwidth) + 20
      }
    },
    golistUrl(item) {
      this.$emit('golistUrl', item, this.typelist)
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
$fonsicon-bg: 48px;
$nodata-icon: #d9d9d9;
.pull-left {
  float: left;
}
.pull-right {
  float: right;
}
.clearfix::after {
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
  content: '';
}
$title-head-height: 48px;
$title-height: 26px;
$horn-size: 40px;
$size-xs: 14px;
$horn-cover-percent: 0.5;
$color-font: #333;
$color-font-w: #fff;
$color-urgent: #108ee9;
$color-extra-urgent: #f50;
$size-urgent: 10px;
$size-hight: 1px;
.card-carousel-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 170px;

  .card-carousel {
    display: flex;
    justify-content: center;
    width: calc(100% - 90px);
    height: 100%;
    .card-carousel-overflow-container {
      overflow: hidden;
      .card-carousel-cards {
        display: flex;
        align-items: center;
        height: 100%;
        padding: 0 5px;
        transition: transform 300ms ease-out;
        transform: translateX(0);
        .card-carousel-card {
          z-index: 3;
          height: calc(100% - 4px);
          margin: 0 10px;
          margin-bottom: 5px;
          cursor: pointer;
          background: #f4f7fc;
          border-radius: 8px;
          box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
          .card-carousel-card-header {
            position: relative;
            min-width: 345px;
            height: calc(50% - -5px);
            color: $white;
            background: #2775e2;
            border-bottom: 0;
            border-radius: 8px 8px 0 0;
            .card-title {
              display: flex;
              align-items: center;
              justify-content: space-between;
              padding: 12px 0 0 0;
              .express {
                width: calc(100% - 33px);
                height: 30px;
                font-size: 16px;
                color: #fff;
                text-align: left;
              }
              .date {
                width: calc(100% - 60px);
                text-align: right;
                .title {
                  font-size: $size-xs;
                  font-weight: 300;
                }
                div {
                  width: 100%;
                  overflow: hidden;
                  text-overflow: ellipsis;
                  white-space: nowrap;
                }
              }
            }
            .card-cont {
              padding: 15px;
              font-size: 21px;
              font-weight: 600;
              word-break: break-all;
            }
          }
          .card-carousel-card-footer {
            position: relative;
            display: flex;
            align-items: center;
            width: 100%;
            min-width: 195px;
            height: 45%;
            padding: 15px 22px;
            overflow: hidden;
            color: $color-font;
            border-top: 0;
            border-radius: 0 0 8px 8px;
            .footer-cont {
              width: 100%;
              .dep {
                width: 100%;
                height: 30px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
              }
            }
            .urgent {
              position: absolute;
              top: 0;
              left: 0;
              display: flex;
              align-items: center;
              height: 2%;
              padding: 8px 10px;
              font-size: $size-urgent;
              line-height: $size-urgent;
              color: $text-color-inverse;
              background-color: $color-urgent;
              border: 1px solid rgb(248, 248, 248);
            }
            .extra-urgent {
              position: absolute;
              top: 0;
              left: 0;
              display: flex;
              align-items: center;
              height: 2%;
              padding: 8px 10px;
              font-size: $size-urgent;
              line-height: $size-urgent;
              color: $text-color-inverse;
              background-color: $color-extra-urgent;
              border: 1px solid rgb(248, 248, 248);
            }
            .right-horn {
              position: absolute;
              right: 0;
              bottom: 0;
              width: $horn-size;
              height: $horn-size;
              background: $primary-color;
              .star {
                position: absolute;
                right: 5px;
                bottom: 5px;
                color: $color-font-w;
              }
              &::after {
                position: absolute;
                top: $horn-size * (1 - 2 * $horn-cover-percent);
                right: $horn-size * (1 - 2 * $horn-cover-percent);
                bottom: 0;
                left: 0;
                content: '';
                border: $horn-size * $horn-cover-percent solid $body-background;
                border-right-color: transparent;
                border-bottom-color: transparent;
              }
            }
          }
        }
        .card-carousel-card:first-child {
          margin-left: 0;
        }
        .card-carousel-card:last-child {
          margin-right: 0;
        }
      }
    }
  }
  .card-carousel-nav {
    box-sizing: border-box;
    display: inline-block;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    color: $color-font-w;
    cursor: pointer;
    background: #000;
    opacity: 0.5;
    transition: transform 150ms linear;
  }
  .card-carousel-nav[disabled] {
    opacity: 0.2;
  }
  .card-carousel-nav-left {
    position: absolute;
    top: 40%;
    left: -26px;
    width: 58px;
    height: 58px;
    border-radius: 0 50% 50% 0;
    i {
      margin-left: 24px;
    }
  }
  .card-carousel-nav-left:active {
    transform: scale(0.9);
  }
  .card-carousel-nav-left[disabled]:active {
    transform: scale(1);
  }
  .card-carousel-nav-right {
    position: absolute;
    top: 40%;
    right: -26px;
    width: 58px;
    height: 58px;
    border-radius: 50% 0 0 50%;
    i {
      margin-right: 24px;
    }
  }
  .card-carousel-nav-right:active {
    transform: scale(0.9);
  }
  .card-carousel-nav-right[disabled]:active {
    transform: scale(1);
  }
}

@media screen and (max-width: 1599px) {
  .card-carousel-wrapper {
    height: calc(100% - 8px);
    margin: 0 0 16px;
    .card-carousel {
      .card-carousel-overflow-container {
        .card-carousel-cards {
          .card-carousel-card {
            .card-carousel-card-header {
              .card-cont {
                height: 92px;
                padding: 10px 15px;
                overflow: hidden;
                line-height: $title-height;
              }
            }
          }
        }
      }
    }
  }
}
@media screen and (min-width: 1600px) and (max-width: 1919px) {
  .card-carousel-wrapper {
    height: calc(100% - 8px);
    margin: 0 0 16px;
    .card-carousel {
      .card-carousel-overflow-container {
        .card-carousel-cards {
          .card-carousel-card {
            .card-carousel-card-header {
              .card-cont {
                height: 92px;
                padding: 10px 15px;
                overflow: hidden;
                line-height: $title-height;
              }
            }
          }
        }
      }
    }
  }
}
@media screen and (min-width: 1920px) {
  .card-carousel-wrapper {
    height: calc(100% - 10px);
    margin: 0 0 16px;
    .card-carousel {
      .card-carousel-overflow-container {
        .card-carousel-cards {
          .card-carousel-card {
            .card-carousel-card-header {
              .card-cont {
                height: 100px;
                padding: 5px 22px;
                overflow: hidden;
                line-height: 35px;
              }
            }
          }
        }
      }
    }
  }
}
.empty-size {
  margin: 10% 0;
  :global(.ant-empty-image) {
    height: 50px;
    i {
      height: 100%;
      font-size: $fonsicon-bg;
      color: $nodata-icon;
    }
  }
}
.float-left {
  float: left;
  color: #7c7d80;
}
.float-right {
  float: right;
}
.step-status {
  float: right;
  height: 20px;
  color: #e36550;
}
.point-out {
  width: 14px;
  height: 14px;
  margin-top: -5px;
  margin-left: 15px;
  background: #2775e2;
  border: 1px solid #fff;
  border-radius: 15px;
}
.point-in {
  width: 3px;
  height: 3px;
  margin: calc(50% - 1px) auto auto;
  vertical-align: middle;
  background: #fff;
  border-radius: 3px;
}
.todoradio {
  width: 30px;
  padding-right: 3px;
  margin-top: -5px;
  font-size: 12px;
  text-align: right;
}
.exceed-item {
  background: #cd5d38 !important;
}
</style>
