<template>
    <!-- environment -->
    <div class="Environment">
        <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 { reqEnvironment, reqSetEnvironment } from "@/api/breed.js";
export default {
    name: "Environment",
    data() {
        return {
            list: [
                {
                    time: "",
                    temp: "",
                    humidity: ""
                }
            ]
        };
    },
    created() {
        this.getEnvironment();
    },
    methods: {
        onSubmit() {
            let time = [];
            let temp = [];
            let humidity = [];
            this.list.forEach(item => {
                time.push(item.time);
                temp.push(item.temp);
                humidity.push(item.humidity);
            });
            this.$confirm("确定更新信息？").then(() => {
                reqSetEnvironment({
                    time: time.toString(),
                    temp: temp.toString(),
                    humidity: humidity.toString()
                })
                    .then(res => {
                        if (res.errCode) {
                            this.$message.error(res.errMsg);
                        } else {
                            this.$message.success("更新成功！");
                            this.getEnvironment();
                        }
                    })
                    .catch(err => {
                        console.log(err);
                        this.$message.error("更新失败！");
                    });
            });
        },
        getEnvironment() {
            reqEnvironment()
                .then(res => {
                    let arr2 = res.conf["temp"];
                    let arr3 = res.conf["humidity"];
                    this.list = [];
                    res.conf["time"].forEach((item, i) => {
                        this.list.push({
                            time: item,
                            temp: arr2[i],
                            humidity: arr3[i]
                        });
                    });
                })
                .catch(err => {
                    console.error(err);
                });
        }
    }
};
</script>

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