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

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