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

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