<template>
    <div class="row">
        <div class="col">
            <loading-component style="height: 50px; top: 0;" key="1" color="success" v-show="isLoading"></loading-component>
            <div class="row" v-show="!isLoading">
                <div class="col">
                    <div class="row m-b-10">
                        <div class="col">
                            <h3 class="all-caps m-b-5 bold no-margin">Assign Segment</h3>
                            <div class="fs-11">Select a segment to {{ parameters.name }}</div>
                        </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-15">
                        <div class="col">
                            <validation-wrapper-component selectable :validator="v$.parameters.segment_id">
                                <label>Segment</label>
                                <selectable-component :endpoint="route('api.segment.list') + '?filters=' + JSON.stringify({ id_not_in: currentSegmentIds })" :section="multiple ? 'segmentsListSection_' + data.id : 'segmentsListSection'" valueColumn="id" :labelColumn="['name']" v-model="parameters.segment_id"></selectable-component>
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row m-b-15">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.ending_on">
                                <label>End Date</label>
                                <date-picker-component :parameters="parameters" v-model.lazy="parameters.ending_on"></date-picker-component>
                            </validation-wrapper-component>
                            <p class="fs-13 d-block text-center m-t-10 m-b-0">Please leave End Date <b class="text-uppercase">empty</b> if the end date is not confirmed or there is no end date.</p>
                        </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">Not Now</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm">Assign Segment</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { computed, watch } from 'vue';
import { useFormHandler } from '@/composables/useFormHandler';
import { useVuelidate } from '@vuelidate/core';
import { required } from '@vuelidate/validators';
import route from '@/general/functions/router';

interface Props {
    section: string;
    data: any;
    multiple?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
    multiple: false
});

const { parameters, submit, error, isLoading, closeModal } = useFormHandler({
    section: props.section,
    data: props.data
});

watch(
    () => props.data,
    () => {
        parameters.value.segment_id = '';
        parameters.value.ending_on = '';
    },
    { immediate: true }
);

const rules = {
    parameters: {
        segment_id: { required },
        ending_on: {}
    }
};

const v$ = useVuelidate(rules, { parameters });

const currentSegmentIds = computed(() =>
    (parameters.value.segments || []).map((value: any) => value.id)
);

const submitForm = async () => {
    const isValid = await v$.value.$validate();
    if (!isValid) return;

    submit(route('api.company.segment.assign', props.data.id), 'post', props.section, true, false, parameters.value);
};
</script>
