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

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