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

onMounted(() => {
  getInventoryWarning()
})
const inventoryWarnDetailRef = ref()
const inventoryWarnList = ref<any>([]);
const openInventoryWarnDetail = () => {
  nextTick(() => {
    inventoryWarnDetailRef.value.init()
  })

}
const getInventoryWarning = () => {
  post("statistics/numberEarlyWarning", {pageNum: 1, pageSize: 20}).then((res: any) => {
    inventoryWarnList.value = res.list.slice(0, 4)
  })
}
const isZeroInventory = (item: any) => {
  return item.inventoryWholeNumber === 0 && item.inventoryFragmentNumber === 0
}

</script>

<template>
  <Panel title="库存预警" class="kuCun">
    <template #tools>
      <span class="small-btn" @click="openInventoryWarnDetail">查看详情</span>
    </template>
    <div style="padding: 0 24px 24px">
      <div class="box">
        <div class="item" style="margin-right: 8px" v-for="item in inventoryWarnList"
             :class="isZeroInventory(item) ? 'item__danger' : 'item__warn'">
          <div class="image"/>
          <div class="item-content">
            <div class="item-name">{{ item.name }}</div>
          </div>
          <div class="item-right">
            <div class="wholeNumber">{{ item.inventoryWholeNumber }}
              <div class="item-right-num">{{ item.packagingUnit }}</div>
            </div>
            <div class="wholeNumber" v-if="item.inventoryFragmentNumber>0">
              {{ item.inventoryFragmentNumber }}
              <div class="item-right-num" v-if="item.inventoryFragmentNumber>0">
                {{ item.minPackagingUnit }}
              </div>
            </div>
          </div>
        </div>

      </div>
    </div>
  </Panel>
  <InventoryWarnDetail ref="inventoryWarnDetailRef"></InventoryWarnDetail>
</template>

<style scoped lang="scss">
.kuCun {
  height: 100%;
  display: flex;
  flex-direction: column;
  margin-right: 24px;

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

      }

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

    .item__warn {
      .image {
        background-image: url("/static/images/home/1-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 {
          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: 70px;
        display: flex;
        margin-right: 10px;
        font-weight: bold;
        font-style: normal;
        font-size: 28px;
        align-items: flex-end;

        .wholeNumber {
          display: flex;
          align-items: flex-end;

          .item-right-num {
            font-weight: 400;
            font-size: 10px;
            color: #333333;
            font-style: normal;
            padding-bottom: 3px;
          }
        }
      }


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

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

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

  }

}
</style>