<template>
    <!-- productPrice -->
    <div class="ProductPrice">
        <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 { reqProductPrice, reqSetProductPrice } from "@/api/product.js";
export default {
    name: "ProductPrice",
    data() {
        return {
            id: '-1',
            list: [{ productName: "" , price: "", stock: ""}]
        };
    },
    created() {
        this.getProductPrice();
    },
    methods: {
        onSubmit() {
            console.log({id: this.id, jsonList: JSON.stringify(this.list)})
            this.$confirm("确定更新信息？").then(() => {
                reqSetProductPrice({id: this.id, jsonList: JSON.stringify(this.list)})
                    .then(res => {
                        console.log(res);
                        if (res.errCode) {
                            this.$message.error(res.errMsg);
                        } else {
                            this.getProductPrice();
                            this.$message.success("更新成功！");
                        }
                    })
                    .catch(err => {
                        console.log(err);
                        this.$message.error("更新失败！");
                    });
            });
        },
        getProductPrice() {
            reqProductPrice()
                .then(res => {
                    this.id = res.id
                    console.log(res)
                    this.list = JSON.parse(res.conf.jsonList)
                })
                .catch(err => {
                    console.error(err);
                });
        }
    }
};
</script>

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