<template>
    <div>
        <Button v-if="$auth.loggedIn" type="primary" @click="active = true">Dodaj produkt</Button>
        <Modal v-model="active" @on-cancel="cancel">
            <p slot="header" style="text-align:center">
                <Icon type="ios-pizza"/>
                <span>Dodawanie produktu</span>
            </p>
            <div>
                <Form ref="form" :model="form" :rules="rules">
                    <FormItem label="Nazwa" prop="name">
                        <Input v-model="form.name" placeholder="Podaj nazwę produktu"/>
                    </FormItem>
                    <FormItem label="Kilokalorie" prop="kcal">
                        <InputNumber class="input" v-model="form.kcal" :min="0" :precision="0"/>
                    </FormItem>
                    <FormItem label="Węglowodany" prop="carbohydrates">
                        <InputNumber class="input" v-model="form.carbohydrates" :min="0"/>
                    </FormItem>
                    <FormItem label="Białko" prop="protein">
                        <InputNumber class="input" v-model="form.protein" :min="0"/>
                    </FormItem>
                    <FormItem label="Tłuszcz" prop="fat">
                        <InputNumber class="input" v-model="form.fat" :min="0"/>
                    </FormItem>
                </Form>
            </div>
            <div slot="footer">
                <Button type="error" @click="cancel">Anuluj</Button>
                <Button type="success" :loading="modal_loading" @click="ok">Zatwierdź</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    export default {
        name: 'AddProductModal',
        data() {
            return {
                active: false,
                modal_loading: false,
                form: {
                    name: null,
                    kcal: null,
                    carbohydrates: null,
                    protein: null,
                    fat: null
                },
                rules: {
                    name: [
                        {
                            required: true,
                            message: 'Proszę podać nazwę produktu',
                            trigger: 'blur'
                        }
                    ],
                    kcal: [
                        {
                            required: false
                        }
                    ],
                    carbohydrates: [
                        {
                            required: false
                        }
                    ],
                    protein: [
                        {
                            required: false
                        }
                    ],
                    fat: [
                        {
                            required: false
                        }
                    ]
                },
            };
        },
        methods: {
            cancel() {
                this.active = false;
                this.$Message.error('Anulowano');
            },
            ok() {
                this.$refs.form.validate((valid) => {
                    if (!valid) {
                        this.$Message.error('Błąd');
                        return;
                    }

                    this.modal_loading = true;

                    this.$axios.post('/product', this.form)
                        .then(() => {
                            this.$Message.success('Sukces!');
                            this.active = false;
                            this.resetForm();
                        })
                        .catch(() => {
                            this.$Message.error('Błąd!');
                            this.modal_loading = false;
                        });

                });
            },
            resetForm() {
                this.modal_loading = false;
                this.$refs.form.resetFields();
            }
        }
    };
</script>

<style scoped>
    .input {
        width: 15%;
    }
</style>
