<script setup>
import { onMounted, reactive, ref } from "vue"
import border from "@/components/border.vue"
import mitt from "@/utils/mitt"
import { orderList } from "@/api/index.js"
import { useAppStore } from "@/store/app.js"

const appStore = useAppStore();

const initData = (data) => {
  let arr = [];
  for (let i = 0; i < data.length; i++) {
    let calss = 'div';
    if (i % 2 != 0) {
      calss = 'div div2'
    }
    arr.push(
      [
        `<div class="${calss}">${i + 1}</div>`,
        `<div class="${calss}">${data[i].real_name || '-'}</div>`,
        `<div class="${calss}">${data[i].user_phone || '-'}</div>`,
        `<div class="${calss}">${data[i].store_name || '-'}</div>`,
        `<div class="${calss}">${data[i].user_address || '-'}</div>`,
      ]
    )
  }
  list.data = arr;
  scrollBoardRef.value?.updateRows(list.data);
}

const scrollBoardRef = ref(null);
const list = reactive({
  header: [
    `<div style='width: 100%;text-align: center; height: 100%; color: aliceblue; background: linear-gradient(rgba(0, 168, 255, 0.76), rgba(0, 84, 128, 0.73));'>序号</div>`,
    `<div style='width: 100%;text-align: center; height: 100%; color: aliceblue; background: linear-gradient(rgba(0, 168, 255, 0.76), rgba(0, 84, 128, 0.73));'>收货人</div>`,
    `<div style='width: 100%;text-align: center; height: 100%; color: aliceblue; background: linear-gradient(rgba(0, 168, 255, 0.76), rgba(0, 84, 128, 0.73));'>收货人电话</div>`,
    `<div style='width: 100%;text-align: center; height: 100%; color: aliceblue; background: linear-gradient(rgba(0, 168, 255, 0.76), rgba(0, 84, 128, 0.73));'>商品名称</div>`,
    `<div style='width: 100%;text-align: center; height: 100%; color: aliceblue; background: linear-gradient(rgba(0, 168, 255, 0.76), rgba(0, 84, 128, 0.73));'>收货人地址</div>`,
  ],
  data: [],
  //   index: true,
  // headerBGC: 'rgba(0, 168, 255, 0.76)',
  // oddRowBGC: 'rgba(91, 219, 246, 0.20)',
  // evenRowBGC: 'rgba(0, 168, 255, 0.16)',
  oddRowBGC: '',
  evenRowBGC: "",
  columnWidth: [40, 80],
  align: ['center'],
  rowNum: 7
})
const cell = reactive([
  {
    value: 0,
    label: '0%',
    count: 0,
    borderWidth: 1,
    borderGap: 1,
    borderRadius: 3,
    colors: ['#66FFFF', '#FEDB65']
  },
  {
    value: 0,
    label: '0%',
    count: 0,
    borderWidth: 1,
    borderGap: 1,
    borderRadius: 3,
    colors: ['#66FFFF', '#FEDB65']
  },
  {
    value: 0,
    label: '0%',
    count: 0,
    borderWidth: 1,
    borderGap: 1,
    borderRadius: 3,
    colors: ['#66FFFF', '#FEDB65']
  }
])
const initCell = () => {
  let all = pendingOrderCount.value + undeliveredOrderCount.value + doneOrderCountQuery.value;
  cell[0].value = (pendingOrderCount.value / all * 100).toFixed(0);
  cell[0].label = (pendingOrderCount.value / all * 100).toFixed(2) + '%';
  cell[0].count = pendingOrderCount.value;
  cell[1].value = (undeliveredOrderCount.value / all * 100).toFixed(0);
  cell[1].label = (undeliveredOrderCount.value / all * 100).toFixed(2) + '%';
  cell[1].count = undeliveredOrderCount.value;
  cell[2].value = (doneOrderCountQuery.value / all * 100).toFixed(0);
  cell[2].label = (doneOrderCountQuery.value / all * 100).toFixed(2) + '%';
  cell[2].count = doneOrderCountQuery.value;
}


const openList = () => {
  mitt.emit('showBusinesses')
}

const orderCount = ref(0);
const pendingOrderCount = ref(0);
const undeliveredOrderCount = ref(0);
const doneOrderCountQuery = ref(0);
const cOrder = () => {
  orderCount.value = orderCount.value + '';
  let c = 6 - orderCount.value.length;
  for (let i = 0; i < c; i++) {
    orderCount.value = '0' + orderCount.value;
  }
  return orderCount.value.split('');
}

const pages = ref({
  areaCode: appStore.address.areaCode,
  streetCode: appStore.address.streetCode,
  page: 1,
  limit: 30
})

const loadOrderList = () => {
  orderList(pages.value).then((res) => {
    initData(res.data.currOrderList);
    orderCount.value = res.data.currOrderCount;
    pendingOrderCount.value = res.data.pendingOrderCount;
    undeliveredOrderCount.value = res.data.undeliveredOrderCount;
    doneOrderCountQuery.value = res.data.doneOrderCountQuery;
    initCell();
  }).catch(err => {
    console.error(err);
  })
}

onMounted(() => {
  loadOrderList();
})

</script>

<template>
  <border>
    <div class="box">
      <div class="title">今日订单数</div>
      <div class="my-day-num">
        <div class="my-num-item" v-for="(item, index) in cOrder()" :key="index">
          {{ item }}
        </div>
      </div>
      <div class="order-list">
        <div class="top">
          <div class="t-right">
            <img src="/src/assets/img/item.png" />
            <div>今日订单</div>
          </div>
          <div class="flex" @click="openList">
            <div>查看更多</div>
            <div>{{ "〉" }}</div>
          </div>
        </div>
        <dv-scroll-board
          ref="scrollBoardRef"
          :config="list"
          style="width: 100%; height: 90%"
        />
      </div>

      <div class="cylinder">
        <div class="cy">
          <div class="text">
            <div>
              占比<span class="sapn">{{ cell[0].label }}</span>
            </div>
            <div class="num">{{ cell[0].count }}</div>
          </div>
          <dv-percent-pond :config="cell[0]" class="cell" />
          <div class="type">待取货订单数</div>
        </div>
        <div class="cy">
          <div class="text">
            <div>
              占比<span class="sapn">{{ cell[1].label }}</span>
            </div>
            <div class="num">{{ cell[1].count }}</div>
          </div>
          <dv-percent-pond :config="cell[1]" class="cell" />
          <div class="type">配送中订单数</div>
        </div>
        <div class="cy">
          <div class="text">
            <div>
              占比<span class="sapn">{{ cell[2].label }}</span>
            </div>
            <div class="num">{{ cell[2].count }}</div>
          </div>
          <dv-percent-pond :config="cell[2]" class="cell" />
          <div class="type">已完成订单数</div>
        </div>
      </div>
    </div>
  </border>
</template>

<style scoped lang="scss">
.box {
  height: 100%;
  width: 100%;
  box-sizing: border-box;
  padding: 1.25rem 1.25rem;
  font-size: 0.8rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  .title {
    width: 100%;
    background-image: url(../../../assets/img/title.png);
    background-size: 100% 100%;
    height: 2.5rem;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: "ifonts";
    font-size: 1.2rem;
  }
  .my-day-num {
    display: flex;
    justify-content: center;
    width: 100%;
    height: 10%;
    margin-top: 0.63rem;
    .my-num-item {
      margin-right: 0.63rem;
      width: 4rem;
      height: 4rem;
      background-image: url(../../../assets/img/day_num.png);
      background-size: 100% 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 2.2rem;
      font-family: "ifonts";
    }
  }
  .order-list {
    height: 55%;
    width: 100%;
    margin-top: 0.63rem;
    .top {
      height: 10%;
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .t-right {
        display: flex;
        align-items: center;
        img {
          width: 1rem;
          height: 1rem;
          margin-right: 0.5rem;
        }
      }
      .flex {
        display: flex;
        cursor: pointer;
        div {
          margin-left: 0.5rem;
        }
      }
    }

    :deep(.div) {
      width: 100%;
      text-align: center;
      height: 100%;
      color: aliceblue;
      background: rgba(91, 219, 246, 0.2);
      white-space: nowrap; /* 防止文本换行 */
      overflow: hidden; /* 溢出部分隐藏 */
      text-overflow: ellipsis; /* 显示省略号 */
    }

    :deep(.div2) {
      background: rgba(0, 168, 255, 0.16);
    }

    :deep(.ceil) {
      padding: 0 !important;
      margin-bottom: 0.3rem;
      font-size: 0.8rem;
    }

    :deep(.header-item) {
      padding: 0 !important;
      font-size: 0.9rem;
    }
  }
  .cylinder {
    width: 100%;
    display: flex;
    justify-content: space-between;
    height: 20%;
    .cy {
      width: 30%;
      height: 100%;
      background-image: url(../../../assets/img/cylinder.png);
      background-size: 100% 100%;
      position: relative;
      .text {
        height: 70%;
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .top {
          margin-top: 1rem;
        }
        .num {
          font-size: 1.5rem;
          font-family: "ifonts";
        }
        .sapn {
          color: #fee165;
        }
      }
      .type {
        height: 30%;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 1rem;
      }
      .cell {
        transform: rotate(-90deg);
        width: 3rem;
        height: 1rem;
        position: absolute;
        right: -0.5rem;
        top: 2rem;
      }
      ::v-deep .dv-percent-pond text {
        display: none;
      }
    }
  }
}
</style>
