<template>
    <!-- historyStockIn -->
    <div class="HistoryStockIn">
        <h2 style="margin-bottom: 20px;padding-bottom:7px;border-bottom:2px solid #ddd">历史入库量</h2>
        <formList v-model="list" :title="['时间','数量']" inputWidth="200px"></formList>
        <el-button style="margin-left:510px" type="primary" @click="onSubmit">更新信息</el-button>
    </div>
</template>

<script>
import { reqHistoryStockIn, reqSetHistoryStockIn } from "@/api/product.js";
export default {
    name: "HistoryStockIn",
    data() {
        return {
            id: '-1',
            list: [{ keyList: "" , valueList: ""}]
        };
    },
    created() {
        this.getHistoryStockIn();
    },
    methods: {
        onSubmit() {
            let keyList = [];
            let valueList = [];
            this.list.forEach(item => {
                keyList.push(item.keyList);
                valueList.push(item.valueList);
            });
            this.$confirm("确定更新信息？").then(() => {
                reqSetHistoryStockIn({id: this.id, keyList: keyList.toString(), valueList: valueList.toString()})
                    .then(res => {
                        console.log(res);
                        if (res.errCode) {
                            this.$message.error(res.errMsg);
                        } else {
                            this.getHistoryStockIn();
                            this.$message.success("更新成功！");
                        }
                    })
                    .catch(err => {
                        console.log(err);
                        this.$message.error("更新失败！");
                    });
            });
        },
        getHistoryStockIn() {
            reqHistoryStockIn()
                .then(res => {
                    this.id = res.id
                    let arr2 = res.conf['valueList']
                    this.list = [];
                    res.conf['keyList'].forEach((item, i) => {
                        this.list.push({ 'keyList': item , 'valueList': arr2[i]});
                    });
                })
                .catch(err => {
                    console.error(err);
                });
        }
    }
};
</script>

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