<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 inventoryWarnListSrc = ref<any>([]);
const inventoryWarnList = ref<any>([]);
const openInventoryWarnDetail = () => {
  nextTick(() => {
    inventoryWarnDetailRef.value.init(inventoryWarnListSrc.value)
  })

}

const getInventoryWarning = () => {
  post("statistics/numberEarlyWarning").then((res: any) => {
    inventoryWarnListSrc.value = res
    inventoryWarnList.value = res.slice(0, 4)
  })
}
</script>

<template>
  <Panel title="库存预警" class="kuCun">
    <template #tools>
      <el-button type="primary" size="small" @click="openInventoryWarnDetail">查看详情</el-button>
    </template>
    <div style="padding: 0 24px 24px">
      <div class="box">
        <div class="item" style="margin-right: 8px" v-if="inventoryWarnList[0]">
          <img class="image" src="/static/images/home/1-danger.png" alt=""/>
          <div class="item-content">
            <el-tooltip
                effect="dark"
                :content="inventoryWarnList[0].name"
                placement="bottom-start"
            >
              <div class="item-name">{{ inventoryWarnList[0].name }}</div>
            </el-tooltip>
            <div class="item-name-font">剩余库存值</div>
          </div>
          <div class="item-right">
            <div class="wholeNumber">{{ inventoryWarnList[0].inventoryWholeNumber }}
              <div class="item-right-num">{{ inventoryWarnList[0].packagingUnit }}</div>
            </div>
            <div class="wholeNumber" v-if="inventoryWarnList[0].inventoryFragmentNumber>0">
              {{ inventoryWarnList[0].inventoryFragmentNumber }}
              <div class="item-right-num" v-if="inventoryWarnList[0].inventoryFragmentNumber>0">
                {{ inventoryWarnList[0].minPackagingUnit }}
              </div>
            </div>
          </div>
        </div>
        <div class="item1" style="margin-right: 8px" v-if="inventoryWarnList[1]">
          <img class="image" src="/static/images/home/1-danger.png" alt=""/>
          <div class="item-content">
            <el-tooltip
                effect="dark"
                :content="inventoryWarnList[1].name"
                placement="bottom-start"
            >
              <div class="item-name">{{ inventoryWarnList[1].name }}</div>
            </el-tooltip>
            <div class="item-name-font">剩余库存值</div>
          </div>
          <div class="item-right">
            <div class="wholeNumber">{{ inventoryWarnList[1].inventoryWholeNumber }}
              <div class="item-right-num">{{ inventoryWarnList[1].packagingUnit }}</div>
            </div>
            <div class="wholeNumber" v-if="inventoryWarnList[1].inventoryFragmentNumber>0">
              {{ inventoryWarnList[1].inventoryFragmentNumber }}
              <div class="item-right-num" v-if="inventoryWarnList[1].inventoryFragmentNumber>0">
                {{ inventoryWarnList[1].minPackagingUnit }}
              </div>
            </div>
          </div>
        </div>
        <div class="item" style="margin-right: 8px" v-if="inventoryWarnList[2]">
          <img class="image" src="/static/images/home/1-danger.png" alt=""/>
          <div class="item-content">
            <el-tooltip
                effect="dark"
                :content="inventoryWarnList[2].name"
                placement="bottom-start"
            >
              <div class="item-name">{{ inventoryWarnList[2].name }}</div>
            </el-tooltip>
            <div class="item-name-font">剩余库存值</div>
          </div>
          <div class="item-right">
            <div class="wholeNumber">{{ inventoryWarnList[2].inventoryWholeNumber }}
              <div class="item-right-num">{{ inventoryWarnList[2].packagingUnit }}</div>
            </div>
            <div class="wholeNumber" v-if="inventoryWarnList[2].inventoryFragmentNumber>0">
              {{ inventoryWarnList[2].inventoryFragmentNumber }}
              <div class="item-right-num" v-if="inventoryWarnList[2].inventoryFragmentNumber>0">
                {{ inventoryWarnList[2].minPackagingUnit }}
              </div>
            </div>
          </div>
        </div>
        <div class="item1" style="margin-right: 8px" v-if="inventoryWarnList[3]">
          <img class="image" src="/static/images/home/1-danger.png" alt=""/>
          <div class="item-content">
            <el-tooltip
                effect="dark"
                :content="inventoryWarnList[3].name"
                placement="bottom-start"
            >
              <div class="item-name">{{ inventoryWarnList[3].name }}</div>
            </el-tooltip>
            <div class="item-name-font">剩余库存值</div>
          </div>
          <div class="item-right">
            <div class="wholeNumber">{{ inventoryWarnList[3].inventoryWholeNumber }}
              <div class="item-right-num">{{ inventoryWarnList[3].packagingUnit }}</div>
            </div>
            <div class="wholeNumber" v-if="inventoryWarnList[3].inventoryFragmentNumber>0">
              {{ inventoryWarnList[3].inventoryFragmentNumber }}
              <div class="item-right-num" v-if="inventoryWarnList[3].inventoryFragmentNumber>0">
                {{ inventoryWarnList[3].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 {
      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;
      }

      .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;
        color: #FF282E;
        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;
      }
    }
    .item1 {
      float: right;
      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;
      }

      .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: 16px;
        font-weight: bold;
        font-style: normal;
        font-size: 28px;
        color: #FF282E;
        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>