<template>
    <!-- sheepSale -->
    <div class="SheepSale">
        <h2 style="margin-bottom: 20px;padding-bottom:7px;border-bottom:2px solid #ddd">种羊销售</h2>
        <formList v-model="list" :title="['月份','销售数量']" inputWidth="300px"></formList>
        <el-button style="margin-left:710px" type="primary" @click="onSubmit">更新信息</el-button>
    </div>
</template>

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

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