<template>
    <div class="row">
        <div class="col">
            <loading-component style="height: 50px; top: 0;" key="1" color="success" v-show="$store.getters.isLoading(section)"></loading-component>
            <div class="row" v-show="!$store.getters.isLoading(section)">
                <div class="col">
                    <div class="row m-b-20">
                        <div class="col">
                            <h6 class="all-caps m-b-5 bold no-margin">{{ parameters.id ? 'Update Milestone' : 'Create Milestone' }}</h6>
                        </div>
                    </div>
                    <div class="row m-b-5 animate__animated animate__fadeInUpBig animate__fast" v-if="error">
                        <div class="col">
                            <small class="bold fs-10 text-danger">{{error}}</small>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <!-- Hidden Field -->
                            <input type="hidden" class="form-control" v-if="parameters.id" v-model="parameters.id">
                            <validation-wrapper-component selectable :validator="$v.parameters.name">
                                <label>Name</label>
                                <selectable-hard-coded-component section="milestoneNameOptions" :selectable-options="optionsMilestoneName" v-model="parameters.name"></selectable-hard-coded-component>
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="$v.parameters.description">
                                <label>Description</label>
                                <input type="text" class="form-control" v-model="parameters.description">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-15">
                        <div class="col">
                            <validation-wrapper-component selectable :validator="$v.parameters.reward_ids">
                                <label>Rewards</label>
                                <reward-checklist-component section="rewardListSection" v-model="parameters.reward_ids"></reward-checklist-component>
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col p-r-5">
                            <div class="btn btn-sm btn-default bg-master-lightest btn-block b-rad-none" data-dismiss="modal">Cancel</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm()">{{ parameters.id ? 'Update Milestone' : 'Create Milestone' }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    import ModalFormHandler from '../../../general/mixins/modalFormHandler';
    import { required } from "vuelidate/lib/validators";
    export default {
        props: {
            optionsMilestoneName: {
                type: Array,
                required: true
            }
        },
        data(){
            return {
                parameters: {
                    id: 0,
                    name: '',
                    description: '',
                    reward_ids: []
                }
            }
        },
        validations: {
            parameters: {
                id: {
                    required
                },
                name: {
                    required
                },
                description: {
                    required
                },
                reward_ids: {
                    required
                }
            }
        },
        methods: {
            successHandler(){
                this.closeModal();
                this.formHandler();
                this.reset();
                this.$store.dispatch('reloadList', {'name': "rewardListSection"});
            },
            submitForm(){
                this.submit(this.parameters.id ? this.route('api.milestone.update', this.parameters.id) : this.route('api.milestone.create'), this.data ? 'put' : 'post', this.section, true, true);
            },
            reset(){
                this.parameters.id = 0;
                this.parameters.name = '';
                this.parameters.description= '';
                this.parameters.reward_ids = [];
                this.$v.$reset();
            }
        },
        mixins: [ModalFormHandler]
    }
</script>
