<template>
    <div>
        <div class="box-header">
            <div class="box-title boldtext">
                体温 <span style="margin-left: 14px">{{ currentDate }}</span>
            </div>
            <div>
                <!-- <el-button @click="exportData" size="small" style="margin-right: 20px"
            >导出</el-button
          > -->
                <el-date-picker size="small" v-model="currentDatePicker" type="date" placeholder="选择日期"
                    value-format="yyyy-MM-dd" @change="datePickerchange">
                </el-date-picker>
            </div>
        </div>
        <div v-show="!detailsType">
            <div style="min-width: 841px; height: 320px; width: 100%;">
                <div ref="heartrateChart" style="width: 100%; height: 100%;min-width: 783px;"></div>
            </div>
        </div>
        <!-- 列表展示 -->
        <div v-show="detailsType">
            <el-table :data="dataList" style="width: 100%">
                <el-table-column prop="createTime" label="时间" />
                <el-table-column prop="estTemp" label="体温(℃)" />
                <el-table-column prop="shellTemp" label="体表温度(℃)" />
                
            </el-table>
            <pagination v-show="(total > 0) && detailsType" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getDataPage"/>
        </div>
        
    </div>
</template>

<script>
import * as echarts from "echarts";
import { queryTemperature ,queryTemperaturePageByDate} from "@/api/watch/deviceInfo";
export default {
    props: {
        detailsType: { 
            type: Boolean,
            default: false
        },
        deviceId: {
            type: String || Number,
            default: "",
        },
    },
    data() {
        return {
            total:0,
            dataList:[],
            queryParams:{
                pageNum: 1,
                pageSize: 10,
                deviceId:"",
                beginTime:"",
                endTime:""
            },
            currentDate: this.parseTime(new Date(), "{y}-{m}-{d}"),
            currentDatePicker: "",
            exportLoading: false,
            chart: null,
            chartOptions: {
                tooltip: {
                    trigger: 'axis'
                },
                legend: {},
                xAxis: {
                    boundaryGap: false,
                    type: "time",
                    splitNumber: 6,
                    axisLine: {
                        show: false,
                    },
                    axisTick: {
                        show: false,
                    },
                    splitLine: {
                        show: false,
                    },
                    axisLabel: {
                        color: "#999999",
                        margin: 20,
                        // fontSize: 14,
                        fontWeight: "bold",
                        formatter: (value) => {
                            return this.parseTime(value, "{h}:{i}");
                        },
                    }
                },
                yAxis: {
                    type: 'value',
                    axisLabel: {
                        formatter: '{value} °C'
                    }
                },
                series: [
                    {
                        name: '体温(℃)',
                        symbol: "none",
                        type: 'line',
                        data: [],
                        markPoint: {
                            data: [
                                { type: 'max', name: 'Max' },
                                { type: 'min', name: 'Min' }
                            ]
                        },

                    },
                    {
                        name: '体表温度(℃)',
                        type: 'line',
                        symbol: "none",
                        data: [],
                        markPoint: {
                            data: [
                                { type: 'max', name: 'Max' },
                                { type: 'min', name: 'Min' }
                            ]
                        },
                    }
                ]
            }

        }
    },
    methods: {
        getDataPage(){
            this.loading = true;
            this.queryParams.deviceId = this.deviceId;
            this.queryParams.beginTime = this.currentDate + " 00:00:00";
            this.queryParams.endTime = this.currentDate + " 23:59:59";
            queryTemperaturePageByDate(this.queryParams)
                .then((response) => {
                if (response) {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                } else {
                    console.warn("温度分页数据返回为空或格式不正确:", response);
                }
                })
                .catch((error) => {
                console.error("获取温度分页数据失败:", error);
                });
        },
        datePickerchange() {
            this.currentDate = this.currentDatePicker || this.parseTime(new Date(), "{y}-{m}-{d}");
            if(this.detailsType){
                this.getDataPage();
            } else{
                this.getDetailsData();
            }
        },
        initChart() {
            const chartElement = this.$refs.heartrateChart;
            const sportChartElement = this.$refs.sportHeartrate;
            if (chartElement) {
                this.chart = echarts.init(chartElement);
                this.seChartOptions()
            }
            if (sportChartElement) {
                this.sportChart = echarts.init(sportChartElement);
                this.sportChart.setOption(this.sportChartOption);
            }
        },
        seChartOptions() {
            this.chartOptions.xAxis.min = this.currentDate + ' 00:00:00'
            this.chartOptions.xAxis.max = this.currentDate + ' 23:59:59'
            this.chart.setOption(this.chartOptions);
        },
        // 获取体温折线图
        getDetailsData() {
            queryTemperature(this.currentDate, this.deviceId)
                .then((response) => {
                    if (response && response.data) {
                        // console.log("------------------" + JSON.stringify(response.data, null, 2))
                        const data1 = response.data.map((item) => (
                            {
                                value: [
                                    new Date(item.createTime).getTime(),
                                    item.estTemp,
                                ],
                            }));
                        const data2 = response.data.map((item) => (
                            {
                                value: [
                                    new Date(item.createTime).getTime(),
                                    item.shellTemp,
                                ],
                            }));

                        // 将数据分别赋值给不同的 series
                        this.chartOptions.series[0].data = data1;
                        this.chartOptions.series[1].data = data2;
                        // 更新图表
                        if (this.detailsType == false) {
                            this.initChart();
                        }
                    } else {
                        console.warn("体温数据返回为空或格式不正确:", response);
                    }
                })
                .catch((error) => {
                    console.error("获取体温数据失败:", error);
                });
        },
        //   // 导出数据逻辑
        //   exportData() {
        //     this.$confirm("是否确认导出所有用户数据项?", "警告", {
        //       confirmButtonText: "确定",
        //       cancelButtonText: "取消",
        //       type: "warning",
        //     })
        //       .then(() => {
        //         this.exportLoading = true;
        //         return exportHeartDate(this.currentDate, this.deviceId);
        //       })
        //       .then((response) => {
        //         this.download(response.msg);
        //         this.exportLoading = false;
        //       })
        //       .catch(() => {});
        //   },
    },
};
</script>

<style lang="scss" scoped>
@mixin u-flex($flexD, $alignI, $justifyC) {
    display: flex;
    flex-direction: $flexD;
    align-items: $alignI;
    justify-content: $justifyC;
}

.fz15 {
    font-size: 15px !important;
}

.boldtext {
    font-size: 16px;
    color: #292929;
    line-height: 30px;
    font-weight: 700;
}

.box-header {
    padding: 0 15px;
    @include u-flex(row, center, space-between);
    height: 50px;
}

.box-title {
    color: #292929;
    font-size: 16px;
    font-family: PingFang SC-Medium;
    padding-left: 10px;
    border-left: 4px solid #2284ff;
    line-height: 13px;
}

.heartrate-data {
    @include u-flex(row, center, space-around);
    margin-top: 30px;

    &-item {
        height: 60px;
        width: 196px;
        border-radius: 4px;
        box-shadow: 0 0 10px rgba(1, 24, 54, 0.1);
        overflow: hidden;
        @include u-flex(row, center, flex-start);

        img {
            height: 41px;
            width: 41px;
        }
    }

    &-imgbox {
        flex-shrink: 0;
        height: 60px;
        width: 60px;
        @include u-flex(row, center, center);
    }

    &-right {
        flex: 1;
        text-align: center;
        font-size: 13px;
        color: #606165;
        font-family: PingFang SC-Medium;
        background: #fff;
        height: 60px;
    }

    &-num {
        font-size: 22px;
        color: #2c2c3b;
        line-height: 38px;
    }
}

.heartrate-sport {
    @include u-flex(row, flex-start, flex-start);
    margin-top: 20px;

    &-l {
        width: 490px;
        flex-shrink: 0;
    }

    &-r {
        flex: 1;
        max-width: 720px;
    }

    &-lechart {
        width: 490px;
        height: 300px;
    }

    &-rechart {
        width: 100%;
        @include u-flex(row, center, center);
    }
}
</style>