<template>
  <div class="demo device-charts" style="height: 90%">
    <div class="list-item-header item-warp" :style="{ flex: listKeys.length }">
      <template v-for="(h, indexh) in listKeys" :key="indexh">
        <div class="item-td" :style="{ width: 'calc(100% / ' + listKeys.length + ')' }">{{ listKeysHeader[h] }}</div>
      </template>
    </div>
    <vue3ScrollSeamless class="scroll-wrap" :class-options="classOptions" :data-list="datalist">
      <div v-for="(item, index) in datalist" :key="index" class="list-item">
        <div class="list-item-content">
          <div class="list-item-boday item-warp" :style="{ flex: listKeys.length }">
            <template v-for="(b, indexb) in listKeys" :key="indexb">
              <div class="item-td" :class="item.status == 1 ? 'td-title' : 'td-warn'" :style="{ width: 'calc(100% / ' + listKeys.length + ')' }">
                <span v-if="b == 'num'">
                  {{ item[b] }}
                </span>
                <span v-else-if="b == 'duration'" class="duration">
                  <span class="val">{{ item[b] + 'h' }}</span>
                  <div class="pro">
                    <customProgress height="5px" :percent="item.percent" inactive-bg="#081931"></customProgress>
                  </div>
                </span>
                <span v-else>
                  {{ item[b] == 0 ? '待机' : '运行' }}
                </span>
              </div>
            </template>
          </div>
        </div>
      </div>
    </vue3ScrollSeamless>
  </div>
  <!-- </div> -->
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed, reactive } from 'vue';
import { vue3ScrollSeamless } from 'vue3-scroll-seamless';
import customProgress from '@/components/customProgress.vue';
const props = defineProps({
  //   items: {
  //     type: Array,
  //     default: () => [],
  //   },
});

let list = reactive([
  { num: '投喂机', duration: '3.7', status: 1 },
  { num: '喂水机', duration: '10.0', status: 1 },
  { num: '投喂机', duration: '6.4', status: 1 },
  { num: '喂水机', duration: '3.9', status: 1 },
  { num: '投喂机', duration: '3.6', status: 0 },
  { num: '喂水机', duration: '4.5', status: 1 },
  { num: '投喂机', duration: '5.6', status: 1 },
]);

const listKeys = reactive(['num', 'status', 'duration']);
const listKeysHeader = reactive({
  num: '设备编号',
  status: '设备状态',
  duration: '设备今日运行时长',
});

let datalist = computed(() => {
  return list.map((m) => {
    return {
      ...m,
      percent: Number((Number(parseInt(m.duration) / max.value) * 100).toFixed(0)),
    };
  });
});

let max = computed(() => {
  let valueList = new Set(list.map((item) => item.duration));
  let sortValue = [...valueList].sort((a, b) => b - a) || [];
  // console.info('valueList', sortValue);
  return sortValue.length ? sortValue[0] : 0;
});

const classOptions = {
  singleHeight: 48,
};
</script>

<style scoped lang="scss">
.device-charts {
  margin-top: 8px;
  .scroll-wrap {
    height: 80%;
    width: 100%;
    margin: 4px auto;
    overflow: hidden;
  }
  .list-item-header {
    background: #144482;
    font-size: 10px;
    width: 100%;
    .item-td {
      padding: 8px 6px;
    }
  }
  .list-item-boday {
    background: transparent;
    width: 100%;
    .item-td {
      padding: 4px 6px;
      &.td-title {
        color: #6beff9 !important;
      }

      &.td-warn {
        color: red !important;
      }
    }
  }
  .item-warp {
    display: inline-flex;
    justify-content: space-around;
    .item-td {
      display: inline-block;
      vertical-align: middle;

      text-align: center;
      color: #fff;
    }
    .duration {
      width: 100%;
      .val,
      .pro {
        display: inline-block;
        vertical-align: middle;
        box-sizing: border-box;
      }
      .val {
        width: 50px;
        padding-right: 10px;
      }
      .pro {
        width: calc(100% - 50px);
      }
    }
  }
  .list-item {
    // border-bottom: 1px dashed rgba(255, 255, 255, 0.2);
    line-height: 18px;

    .list-item-content {
      display: inline-flex;
      width: 100%;
      justify-content: space-around;
      position: relative;
    }
  }
}
.demo {
  //   display: flex;
  //   align-items: center;
  //   justify-content: center;
  //   margin-top: 10px;
}
</style>
