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

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