<template>
  <div class="deviceStatistics card">
    <div class="card-head">
      <div class="title">内容数据</div>
      <div class="condition">
        <el-radio-group v-model="radioType">
          <el-radio-button label="时" value="hour" />
          <el-radio-button label="天" value="day" />
          <el-radio-button label="周" value="week" />
        </el-radio-group>
        <div class="condition-time">
          <el-date-picker v-if="radioType == 'week'" v-model="value" type="week" value-format="YYYY-MM-DD" placeholder="请选择周" @change="handleWeekChange" />
          <el-date-picker
            v-else
            style="width: 350px"
            v-model="value1"
            type="datetimerange"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            @change="handleDateChange"
          />
        </div>
      </div>
    </div>
    <slot name="chart"></slot>
  </div>
</template>
<script setup>
import { format } from "@/utils";
import { ref } from "vue";
const value = ref("");
const value1 = ref("");
const radioType = ref("hour");
const { api } = defineProps({
  api: {
    type: Function,
    required: true,
  },
});

const handleWeekChange = (val) => {
  let time = new Date(val).getTime() + 6 * 24 * 60 * 60 * 1000;
  api({ type: radioType.value, startDate: `${val} 00:00:00`, endDate: `${format(time, "YYYY-MM-DD")} 00:00:00` });
};
const handleDateChange = (val) => {
  api({ type: radioType.value, startDate: val[0], endDate: val[1] });
};
</script>
<style scoped lang="less">
.card {
  height: 380px;
  height: 100%;
  background: #ffffff;
  padding: 16px 10px;
  box-sizing: border-box;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
  box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
  margin-bottom: 20px;
  .card-head {
    color: #061451;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    transform: translateX(-10px);
    .title {
      &::before {
        display: inline-block;
        margin-right: 10px;
        content: "";
        width: 2px;
        height: 14px;
        border-radius: 20px;
        background: #061451;
      }
    }
    .condition {
      display: flex;
      align-items: center;
      margin-left: 20px;
      .condition-time {
        overflow: hidden;
        margin-left: 20px;
      }
    }
  }
}
</style>
