<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">{{this.data?"Update":"Create"}} Announcement</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">
                            <validation-wrapper-component :validator="$v.parameters.title">
                                <label>Title</label>
                                <input type="text" class="form-control" v-model="parameters.title">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col">
                            <validation-wrapper-component :validator="$v.parameters.description">
                                <label>Message</label>
                                <textarea class="form-control h-75" v-model="parameters.description"  rows="20"></textarea>
                            </validation-wrapper-component>
                        </div>
                    </div>

                    <div class="row m-b-20">
                        <div class="col-6 p-r-0">
                            <validation-wrapper-component :validator="$v.parameters.starting_on">
                                <label>Start Date</label>
                                <date-picker-component v-model="parameters.starting_on"></date-picker-component>
                            </validation-wrapper-component>
                        </div>
                        <div class="col-6 p-l-0">
                            <validation-wrapper-component :validator="$v.parameters.ending_on">
                                <label>End Date</label>
                                <date-picker-component v-model="parameters.ending_on"></date-picker-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="closable ? 'modal' : ''" @click="$emit('close')">Cancel</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm()">{{data ? "Update" : "Create"}}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    import ModalFormHandler from '../../../general/mixins/modalFormHandler';
    import { required, requiredIf } from "vuelidate/lib/validators";
    export default {
        props : {            
            closable: {
                type: Boolean,
                default: true
            },
            country_id: {
                type: Number,
                default: 1
            }
        },
        data(){
            return {
                parameters: {
                    starting_on:'',
                    ending_on:'',                    
                    // segment_id: '',
                    title: '',
                    description: '',                    
                }
            }
        },
        validations: {
            parameters: {
                title: {
                    required
                },
                description: {
                    required
                },
                starting_on:{
                    required
                },
                ending_on:{
                    required
                },                
            }
        },
        created(){
            if (this.data) {
                this.parameters.starting_on = this.data.starting_on;
                this.parameters.ending_on = this.data.ending_on;
                this.parameters.title = this.data.title;
                this.parameters.description = this.data.description;
            }
        },
        methods: {
            submitForm(){
                this.submit((this.data ? this.route('api.announcement.update', this.data.id) : this.route('api.announcement.create')), (this.data ? 'put' : 'post'), this.section, true, false)
            },
        },
        mixins: [ModalFormHandler],

    }
</script>