<template>
    <!-- quarterlyTurnover -->
    <div class="QuarterlyTurnover">
        <h2 style="margin-bottom: 20px;padding-bottom:7px;border-bottom:2px solid #ddd">季度销售金额</h2>
        <formList
            v-model="list"
            :title="['时间','胴体','分割','直销']"
            inputWidth="150px"
            titleWidth="100px"
        ></formList>
        <el-button style="margin-left:610px" type="primary" @click="onSubmit">更新信息</el-button>
    </div>
</template>

<script>
import { reqQuarterlyTurnover, reqSetQuarterlyTurnover } from "@/api/meatTransport.js";
export default {
    name: "QuarterlyTurnover",
    data() {
        return {
            list: [
                {
                    time: "",
                    body: "",
                    part: "",
                    direct: ""
                }
            ]
        };
    },
    created() {
        this.getQuarterlyTurnover();
    },
    methods: {
        onSubmit() {
            let time = [];
            let body = [];
            let part = [];
            let direct = [];
            this.list.forEach(item => {
                time.push(item.time);
                body.push(item.body);
                part.push(item.part);
                direct.push(item.direct);
            });
            this.$confirm("确定更新信息？").then(() => {
                reqSetQuarterlyTurnover({
                    time: time.toString(),
                    body: body.toString(),
                    part: part.toString(),
                    direct: direct.toString()
                })
                    .then(res => {
                        if (res.errCode) {
                            this.$message.error(res.errMsg);
                        } else {
                            this.$message.success("更新成功！");
                            this.getQuarterlyTurnover();
                        }
                    })
                    .catch(err => {
                        console.log(err);
                        this.$message.error("更新失败！");
                    });
            });
        },
        getQuarterlyTurnover() {
            reqQuarterlyTurnover()
                .then(res => {
                    let arr2 = res.conf["body"];
                    let arr3 = res.conf["part"];
                    let arr4 = res.conf["direct"];
                    this.list = [];
                    res.conf["time"].forEach((item, i) => {
                        this.list.push({
                            time: item,
                            body: arr2[i],
                            part: arr3[i],
                            direct: arr4[i],
                        });
                    });
                })
                .catch(err => {
                    console.error(err);
                });
        }
    }
};
</script>

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