<template>
    <!-- sale -->
    <div class="Sale">
        <h2 style="margin-bottom: 20px;padding-bottom:7px;border-bottom:2px solid #ddd">牧场销售</h2>
        <formList
            v-model="list"
            :title="['第一周时间','第一周金额','第二周时间','第二周金额','第三周时间','第三周金额']"
            inputWidth="120px"
            titleWidth="120px"
        ></formList>
        <el-button style="margin-left:810px" type="primary" @click="onSubmit">更新信息</el-button>
    </div>
</template>

<script>
import { reqSale, reqSetSale } from "@/api/breed.js";
export default {
    name: "Sale",
    data() {
        return {
            list: [
                {
                    firstTime: "",
                    firstMoney: "",
                    secondTime: "",
                    secondMoney: "",
                    thirdTime: "",
                    thirdMoney: ""
                }
            ]
        };
    },
    created() {
        this.getSale();
    },
    methods: {
        onSubmit() {
            let firstTime = [];
            let firstMoney = [];
            let secondTime = [];
            let secondMoney = [];
            let thirdTime = [];
            let thirdMoney = [];
            this.list.forEach(item => {
                firstTime.push(item.firstTime);
                firstMoney.push(item.firstMoney);
                secondTime.push(item.secondTime);
                secondMoney.push(item.secondMoney);
                thirdTime.push(item.thirdTime);
                thirdMoney.push(item.thirdMoney);
            });
            this.$confirm("确定更新信息？").then(() => {
                reqSetSale({
                    firstTime: firstTime.toString(),
                    firstMoney: firstMoney.toString(),
                    secondTime: secondTime.toString(),
                    secondMoney: secondMoney.toString(),
                    thirdTime: thirdTime.toString(),
                    thirdMoney: thirdMoney.toString()
                })
                    .then(res => {
                        if (res.errCode) {
                            this.$message.error(res.errMsg);
                        } else {
                            this.getSale();
                            this.$message.success("更新成功！");
                        }
                    })
                    .catch(err => {
                        console.log(err);
                        this.$message.error("更新失败！");
                    });
            });
        },
        getSale() {
            reqSale()
                .then(res => {
                    let arr2 = res.conf["firstMoney"];
                    let arr3 = res.conf["secondTime"];
                    let arr4 = res.conf["secondMoney"];
                    let arr5 = res.conf["thirdTime"];
                    let arr6 = res.conf["thirdMoney"];
                    this.list = [];
                    res.conf["firstTime"].forEach((item, i) => {
                        this.list.push({
                            firstTime: item,
                            firstMoney: arr2[i],
                            secondTime: arr2[i],
                            secondMoney: arr2[i],
                            thirdTime: arr2[i],
                            thirdMoney: arr2[i],
                        });
                    });
                })
                .catch(err => {
                    console.error(err);
                });
        }
    }
};
</script>

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