<template>
    <!-- analyse -->
    <div class="Analyse">
        <h2 style="margin-bottom: 20px;padding-bottom:7px;border-bottom:2px solid #ddd">温湿度曲线</h2>
        <formList
            v-model="list"
            :title="['时间','上周','本周']"
            inputWidth="150px"
            titleWidth="100px"
        ></formList>
        <el-button style="margin-left:610px" type="primary" @click="onSubmit">更新信息</el-button>
    </div>
</template>

<script>
import { reqAnalyse, reqSetAnalyse } from "@/api/meatTransport.js";
export default {
    name: "Analyse",
    data() {
        return {
            list: [
                {
                    time: "",
                    lastWeek: "",
                    thisWeek: ""
                }
            ]
        };
    },
    created() {
        this.getAnalyse();
    },
    methods: {
        onSubmit() {
            let time = [];
            let lastWeek = [];
            let thisWeek = [];
            this.list.forEach(item => {
                time.push(item.time);
                lastWeek.push(item.lastWeek);
                thisWeek.push(item.thisWeek);
            });
            this.$confirm("确定更新信息？").then(() => {
                reqSetAnalyse({
                    time: time.toString(),
                    lastWeek: lastWeek.toString(),
                    thisWeek: thisWeek.toString()
                })
                    .then(res => {
                        if (res.errCode) {
                            this.$message.error(res.errMsg);
                        } else {
                            this.$message.success("更新成功！");
                            this.getAnalyse();
                        }
                    })
                    .catch(err => {
                        console.log(err);
                        this.$message.error("更新失败！");
                    });
            });
        },
        getAnalyse() {
            reqAnalyse()
                .then(res => {
                    let arr2 = res.conf["lastWeek"];
                    let arr3 = res.conf["thisWeek"];
                    this.list = [];
                    res.conf["time"].forEach((item, i) => {
                        this.list.push({
                            time: item,
                            lastWeek: arr2[i],
                            thisWeek: arr3[i]
                        });
                    });
                })
                .catch(err => {
                    console.error(err);
                });
        }
    }
};
</script>

<style lang="scss" scoped>
.Analyse {
    .left {
        width: 400px;
        margin-left: 50px;
    }
}
</style>