<template>
    <div class="git-div-container disp-style-col sc5">
        <!--PLACE DESIGN HTML HERE-->
        <h3>Locator Excemptions</h3>
        <div style="width: 100%; margin-top: 10px;" class="git-div-border">
            <div style="width: 100%" class="disp-style-col">
                <span @click="_addPopup('modal_loc_excemp', { mode: 0 }, loadData);" style="margin-left: auto; font-size: 32px"><i class="git-btn-fa git-btn-success fa-solid fa-list-check"></i></span>
                <div class="disp-style-row" style="margin-top: 10px">
                    <p>
                        Show 
                        <select id="cmb_entries" @change="changeLimit()">
                            <option value="10">10</option>
                            <option value="25">25</option>
                            <option value="50">50</option>
                            <option value="100">100</option>
                        </select>
                        Entries
                    </p>
                    <label style="margin-left: auto">Search:&nbsp;<input v-model="txt_search" type="search" @change="changeLimit()"></label>
                </div>
            </div>
            <table class="table table-frozen-th table-hover table-fa-icon">
                <thead>
                <tr>
                    <th style="width: fill-content" @click="reorderGroup(0)">Locator Name</th>
                    <th style="width: 400px" @click="reorderGroup(1)">Excempted To</th>
                    <th style="width: 420px"></th>
                </tr>
                </thead>
                <tbody>
                    <tr v-for="data in orderedExcemptions" :key="data.id" style="cursor: pointer">
                        <td><div style="margin-top: 3px">{{ data.name }}</div>
                        <td><div :class="styleStatus(parseInt(data.excemp_id))" style="margin-top: 3px; text-align: center"><b>{{ data.excemption }}</b></div>
                        <td>
                            <div class="disp-style-row">
                                <div @click="_addPopup('modal_loc_excemp', { id: data.id, mode: 1, locator_id: data.locator_id, name: data.name }, loadData);" class="git-btn git-btn-info disp-style-row div-act-btn" style="text-align: center">Edit&nbsp;<i class="fa-solid fa-pen-to-square" aria-hidden="true"></i></div>
                                &nbsp;
                                <div @click="deleteExcemp(data.id)" class="git-btn git-btn-danger disp-style-row div-act-btn" style="text-align: center">Delete&nbsp;<i class="fa-solid fa-trash-can" aria-hidden="true"></i></div>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="disp-style-row" style="margin-top: 10px">
                <label>Showing {{ (this.totalitems > 0 ? this.floor + 1 : 0) }} to {{ this.totaldisp }} out of {{ this.totalitems }} entries</label>
                <div style="disp-style-row; margin-left: auto" class="div-pages">
                    <button class="git-btn" @click="movePage(false, true)">Prev</button>
                    <select @change="movePage(false, false, $event.target.value)">
                        <option v-for="index in this.page" :key="index" :id="'btn_page' + index" :selected="index === currpage">{{index}}</option>
                    </select>
                    <button class="git-btn" @click="movePage(true)">Next</button>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    import moment from 'moment';
    export default {
        data() {
            return {
                //DATA HERE
                order_loc_asc: false,
                txt_search: '',
                loc_excemp: [],
                floor: 0, //FOR PAGE ALGORITHM
                ceil: 10, //FOR PAGE ALGORITHM
                limit: 10, //FOR PAGE ALGORITHM
                totalitems: 0, //FOR PAGE ALGORITHM
                totaldisp: 10, //FOR PAGE ALGORITHM
                currpage: 1, //FOR PAGE ALGORITHM
                page: 0, //FOR PAGE ALGORITHM
                sortCol: 0 //FOR SORTING ALGORITHM
            }
        },
        mounted() {
            this.$loading(true);
            $("body").css({ 'cursor': 'wait' });
            
            this._vueGETfromParam(
                'obo_excemption',
                'fetch_excemptions',
                { },
                response => {
                    this.loc_excemp = response;
                    this.totalitems = this.loc_excemp.length;
                    this.page = Math.ceil(this.totalitems / this.limit);
                    this.$loading(false);
                    $("body").css({ 'cursor': 'default' });
                }
            );
        },
        computed: {
            orderedExcemptions: function () { //FOR SORTING
                var searchFields = ['name', 'excemption'];

                var tmpData = this.loc_excemp.filter((d) => {
                    if (this.txt_search !== '') {
                        var match = false;
                        for (var x = 0; x < searchFields.length; x++) {
                            if (d[searchFields[x]] !== null && d[searchFields[x]] !== '' && d[searchFields[x]] !== undefined) {
                                if (match = d[searchFields[x]].toString().toLowerCase().includes(this.txt_search.toLowerCase().trim()))
                                    return match;
                            }
                        }
                    }
                    else return true;
                });

                this.limit = (typeof $('#cmb_entries').val() === 'undefined' ? 10 : parseInt($('#cmb_entries').val()));
                this.totalitems = tmpData.length;
                this.totaldisp = (this.ceil > this.totalitems ? this.totalitems : this.ceil);

                tmpData = this.ceil ? tmpData.slice(this.floor, this.ceil) : tmpData;
                var sorting = 'name';

                if (this.sortCol === 0)
                    sorting = 'orderid';
                if (this.sortCol === 1)
                    sorting = 'status_id';

                return _.orderBy(tmpData, sorting, (this.order_loc_asc ? 'asc' : 'desc'));
            }
        },
        methods: {
            customDTP(date) {
                return moment(date).format('YYYY');
            },
            reorderGroup(sortCol = 0) {
                if (this.sortCol != sortCol)
                    this.order_loc_asc = true;
                else
                    this.order_loc_asc = !this.order_loc_asc;
                this.sortCol = sortCol;
            },
            changeLimit() { //ENTRIES ALGORITHM
                this.limit = parseInt($('#cmb_entries').val());
                this.totaldisp = (this.ceil > this.totalitems ? this.totalitems : this.ceil);
                this.page = Math.ceil(this.totalitems / this.limit);
                this.ceil = this.limit;
                this.floor = 0;
                this.currpage = 1;
            },
            movePage(next = false, prev = false, to = -1) { //MOVE PAGE ALGORITHM
                if (next && this.ceil < this.totalitems) {
                    this.floor += this.limit;
                    this.ceil += this.limit;
                    this.currpage++;
                }
                else if (prev && this.floor > 0) {
                    this.floor -= this.limit;
                    this.ceil -= this.limit;
                    this.currpage--;
                }
                else if (to > 0) {
                    this.floor = (to * this.limit) - this.limit;
                    this.ceil = (to * this.limit);
                    this.currpage = to;
                }
            },
            loadData() {
                this.$loading(true);
                $("body").css({ 'cursor': 'wait' });
                
                this._vueGETfromParam(
                    'obo_excemption',
                    'fetch_excemptions',
                    { },
                    response => {
                        this.loc_excemp = response;
                        this.totalitems = this.loc_excemp.length;
                        this.page = Math.ceil(this.totalitems / this.limit);
                        this.$loading(false);
                        $("body").css({ 'cursor': 'default' });
                    }
                );
            },
            styleStatus(id) {
                var output = '';
                if (id === 1)
                    output = 'td-annual';
                else if (id === 2)
                    output = 'td-bldg';
                else if (id === 3)
                    output = 'td-occu';
                else
                    output = '';
                return output;
            },
            deleteExcemp(id) {
                this.$confirm("Are you sure you want to delete " + this.loc_excemp.find(data => data.id === id).name + "'s `" + this.loc_excemp.find(data => data.id === id).excemption + "` Excemption?",
                    "Confirm",
                    "warning").then(r => {
                        this._vueDelete(
                            'obo_excemption',
                            id,
                            response => {
                                if (response === 'success') {
                                    this.loc_excemp = this._softDelete(this.loc_excemp, id);
                                    this.showSuccessMsg({
                                        title: 'Success!',
                                        message: 'Item deleted!'
                                    });
                                    this.loadData();;
                                }
                            }
                        );
                    }
                );
            }
        }
    }
</script>
<style lang="css" scoped>
    .div-act-btn {
        max-width: 190px; 
        width: fit-content;
        margin-bottom: 4px;
    }
    .div-act-btn i {
        height: 20px;
        padding: 4px 0 0 4px;
    }
    .td-annual {
        color: #C62828;
    }
    .td-occu {
        color: #1565C0;
    }
    .td-bldg {
        color: #43A047;
    }
</style>    