<script setup lang="ts">
import Panel from "@/components/common/Panel.vue";
import {nextTick, onMounted, ref} from "vue";
import {post} from "@/utils/request.ts";
import ExpireWarnDetail from "@/components/home/index/Dialog/ExpireWarnDetail.vue";

const expireWarnDetailRef = ref()
const openExpireWarnDetail = () => {
  nextTick(() => {
    expireWarnDetailRef.value.init()
  })

}
onMounted(() => {
  getExpiryDateWarning()
})
const expireDateWarningListSrc = ref<any>([]);
const expireDateWarningList = ref<any>([]);

const getExpiryDateWarning = () => {
  post("statistics/expiryDateWarning", {pageNum: 1, pageSize: 20}).then((res: any) => {
    expireDateWarningList.value = res.list.slice(0, 4)
  })
}
const isExpire = (item: any) => {
  return item.remaining_days < 0
}
</script>

<template>
  <Panel title="效期预警" class="xiaoQi">
    <template #tools>
      <span class="small-btn" @click="openExpireWarnDetail">查看详情</span>
    </template>
    <div style="padding: 0 24px 24px">
      <div class="box">
        <div class="item" style="margin-right: 8px" v-for="(item,index) in expireDateWarningList"
             :class="isExpire(item) ? 'item__danger' : 'item__warn'">
          <div class="image"/>
          <div class="item-content">
            <div class="item-name">{{ item.name }}</div>
            <div class="item-name-font">
              <span>{{ item.whole_number }}{{ item.packaging_unit }}</span>
              <span v-if="item.fragment_number>0">{{
                  item.fragment_number
                }}{{ item.min_packaging_unit }}</span>
            </div>
          </div>
          <div class="item-right" style="margin-right: 10px">
            <div v-if="item.remaining_days<0">
              已过期
              <span class="item-right-num">{{
                  Math.abs(item.remaining_days) > 99 ? '99+' : Math.abs(item.remaining_days)
                }}</span>
              天
            </div>
            <div v-else>剩余<span class="item-right-num">{{ Math.abs(item.remaining_days) }}</span>天
            </div>
          </div>
        </div>
      </div>
    </div>
  </Panel>
  <ExpireWarnDetail ref="expireWarnDetailRef"></ExpireWarnDetail>
</template>

<style scoped lang="scss">
.xiaoQi {
  height: 100%;
  display: flex;
  flex-direction: column;

  .box {
    .item__danger {
      .image {
        background-image: url("/static/images/home/danger.png");

      }

      .item-right {
        color: #FF282E;
      }
    }

    .item__warn {
      .image {
        background-image: url("/static/images/home/warn.png");

      }

      .item-right {
        color: #F69C51;

      }

    }

    .item {
      float: left;
      width: 48%;
      height: 58px;
      box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.1);
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      flex: 1 1 calc(50% - 8px); //每行显示2个 item，每个 item 占据 33.33% 减去间距
      .image {
        width: 34px;
        height: 34px;
        margin: 0 8px 0 16px;
        background-size: 100% 100%;
      }

      .item-content {
        flex: 1;

        .item-name {
          width: 80px;
          font-weight: 400;
          font-size: 14px;
          color: #333333;
          font-style: normal;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }

        .item-name-font {
          font-weight: 400;
          font-size: 12px;
          color: #999999;
          font-style: normal;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }

      .item-right {
        width: 120px;
        display: flex;

        .item-right-num {
          font-size: 28px;
          font-weight: bold;
          font-style: normal;
        }
      }

      &:nth-child(2n) {
        margin-right: 0 !important;
      }

      &:nth-child(3) {
        margin-top: 8px !important;
      }

      &:nth-child(4) {
        margin-top: 8px !important;
      }
    }


  }
}
</style>