<template>
    <!-- poopSale -->
    <div class="PoopSale">
        <h2 style="margin-bottom: 20px;padding-bottom:7px;border-bottom:2px solid #ddd">羊粪销售</h2>
        <formList v-model="list" :title="['时间','价格(万元)']" inputWidth="300px" titleWidth="150px"></formList>
        <el-button style="margin-left:810px" type="primary" @click="onSubmit">更新信息</el-button>
    </div>
</template>

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

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