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

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