<template>
    <div class="git-div-container disp-style-col sc5">
        <!--PLACE DESIGN HTML HERE-->
        <h3>Locators</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_mgmt_loc', { mode: 0 })" style="margin-left: auto; font-size: 32px"><i class="git-btn-fa git-btn-success fa fa-user-plus"></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: 160px" @click="reorderGroup(0)">Name</th>
                        <th style="width: 110px" @click="reorderGroup(1)">Category</th>
                        <th style="width: 180px" @click="reorderGroup(2)">Address</th>
                        <th style="width: 100px">Landline No.</th>
                        <th style="width: 100px">Mobile No.</th>
                        <th style="width: 80px" @click="reorderGroup(3)">Status</th>
                        <th style="width: 60px"></th>
                    </tr>
                </thead>
                <tbody>
                    <tr :id="'tr_expand_' + data.id" v-for="data in orderedLocators" :key="data.id" style="cursor: pointer">
                        <td><div style="margin-top: 3px; text-align: center" >{{ data.name }}</div></td>
                        <td><div style="margin-top: 3px">{{ data.category_desc }}</div></td>
                        <td><div style="margin-top: 3px">{{ data.address }}</div></td>
                        <td><p style="margin-top: 3px; text-align: center; white-space: pre-wrap;">{{ _jsonToStrList(data.landline_no) }}</p></td>
                        <td><p style="margin-top: 3px; text-align: center; white-space: pre-wrap;">{{ _jsonToStrList(data.mobile_no) }}</p></td>
                        <td><h6 v-show="data.date_inactive !== null" style="margin-top: 3px; text-align: center; color: #F9A825"><b>INACTIVE<br>SINCE: {{ data.date_inactive }}</b></h6><h6 v-show="data.date_inactive === null" style="margin-top: 3px; text-align: center; color: #2E7D32"><b>ACTIVE</b></h6></td>
                        <td style="max-width: 60px">
                            <div class="disp-style-col">
                                <div class="disp-style-row">
                                    <div @click="_addPopup('modal_loc_cred', { id: data.id, ent: data.name })" class="git-btn-fa git-btn-success disp-style-row div-act-btn" style="text-align: center; min-width: 44px; margin-right: 6px">
                                        <i class="fa-solid fa-eye" aria-hidden="true"></i>
                                    </div>
                                    <div @click="_addPopup('modal_loc_mgmt_loc', { mode: 1, id: data.id }, result => { runCallback(result, data.id); } )" class="git-btn-fa git-btn-info disp-style-row div-act-btn" style="text-align: center; min-width: 44px">
                                        <i class="fa-solid fa-pen-to-square"></i>
                                    </div>
                                </div>
                                <div class="disp-style-row">
                                    <div v-show="data.date_inactive === null" @click="setInactive(data.id)" class="git-btn-fa git-btn-warn disp-style-row div-act-btn" style="text-align: center; min-width: 44px; margin-right: 6px">
                                        <i class="fa-solid fa-ban"></i>
                                    </div>
                                    <div @click="deleteItem(data.id)" class="git-btn-fa git-btn-danger disp-style-row div-act-btn" style="text-align: center; min-width: 44px">
                                        <i class="fa-solid fa-trash-can"></i>
                                    </div>
                                </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_locators_asc: true,
                txt_search: '',
                txt_year: '',
                locators: [],
                loc_cat: [],
                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.loadData();
        },
        computed: {
            orderedLocators: function () { //FOR SORTING
                this.locators.forEach(data => {
                    data.showdetails = 0;
                });

                var tmpAppl = this.locators.filter((d) => {
                    if (this.txt_search !== '') {
                        var match = false;
                        if (d.name !== null && d.name !== '' && d.name !== undefined) {
                            if (match = d.name.toString().toLowerCase().includes(this.txt_search.toLowerCase().trim()))
                                return match;
                        }
                        if (match = d.name.toLowerCase().includes(this.txt_search.toLowerCase()))
                            return match;
                    }
                    else return true;
                });

                try {       
                    tmpAppl = tmpAppl.sort((t1, t2) => t1.name_sort < t2.name_sort ? -1 : 1);
                } catch { }

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

                tmpAppl = this.ceil ? tmpAppl.slice(this.floor, this.ceil) : tmpAppl;
                var sorting = 'name_sort';

                if (this.sortCol === 0)
                    sorting = 'name_sort';
                else if (this.sortCol === 1)
                    sorting = 'category_desc';
                else if (this.sortCol === 2)
                    sorting = 'address';
                else if (this.sortCol === 3)
                    sorting = 'status';
                    
                return _.orderBy(tmpAppl, sorting, (this.order_locators_asc ? 'asc' : 'desc'));
            }
        },
        methods: {
            customDTP(date) {
                return moment(date).format('YYYY');
            },
            reorderGroup(sortCol = 0) {
                if (this.sortCol != sortCol)
                    this.order_locators_asc = true;
                else
                    this.order_locators_asc = !this.order_locators_asc;
                this.sortCol = sortCol;
            },
            changeLimit() { //ENTRIES ALGORITHM
                this.limit = parseInt($('#cmb_entries').val());
                this.selectedIndex = this.limit;
                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;
            },
            setInactive(id) {
                this._addPopup('modal_loc_set_inactive', { id: id }, 
                    result => { this.runCallback(result, id); } 
                );
            },
            runCallback(result, id) {
                if (result.d_inactive !== null && result.d_inactive !== "" && typeof result.d_inactive !== 'undefined') 
                    this.locators.find(x => x.id === id).date_inactive = result.d_inactive;
                else 
                    this.locators.find(x => x.id === id).date_inactive = null;
                if (result.name !== null && result.name !== "" && typeof result.name !== 'undefined')
                    this.locators.find(x => x.id === id).name = result.name;
                if (result.address !== null && result.address !== "" && typeof result.address !== 'undefined')
                    this.locators.find(x => x.id === id).address = result.address;
                if (result.category_id !== null && result.category_id !== "" && typeof result.category_id !== 'undefined')
                    this.locators.find(x => x.id === id).category_desc = this.loc_cat.find(x => x.value === result.category_id).text;
                if (result.land !== null && result.land !== "" && typeof result.land !== 'undefined')
                    this.locators.find(x => x.id === id).landline_no = result.land;
                if (result.mobile !== null && result.mobile !== "" && typeof result.mobile !== 'undefined')
                    this.locators.find(x => x.id === id).mobile_no = result.mobile;
            },
            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(
                    'lcp_locator',
                    'fetch_locators',
                    { },
                    response1 => {
                        this.locators = response1;
                        this.totalitems = this.locators.length;
                        this.page = Math.ceil(this.totalitems / this.limit);
                        this.ceil = this.limit;
                        this.floor = 0;
                        this.currpage = 1;
                        
                        this._vueGETfromParam(
                            'lcp_locator',
                            'fetch_loc_categories',
                            { },
                            response2 => {
                                this.loc_cat = response2;
                                this.$loading(false);
                                $("body").css({ 'cursor': 'default' });
                            }
                        );
                    }
                );
            },
            deleteItem(id) {
                this.$confirm("Are you sure you want to delete `" + this.locators.find(data => data.id === id).name + "`?",
                    "Confirm",
                    "warning").then(r => {
                        this._vueObjDelete(
                            'lcp_locator',
                            {
                                func: 'locator', 
                                id: id 
                            },
                            response => {
                                if (response === 'success') {
                                    this.locators = this._softDelete(this.locators, id);
                                    this.showSuccessMsg({
                                        title: 'Success!',
                                        message: 'Locator deleted!'
                                    });
                                }
                            }
                        );
                    }
                );
            }
        }
    }
</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-completed td:nth-child(7) {
        color: #43A047;
    }
    .td-pending {
        background: radial-gradient(circle, rgba(129,199,132,1) 0%, rgba(156,204,101,1) 100%) !important;
        background-size: 500% 500% !important;
        animation: gradient 10s ease infinite !important;
    }
    .td-pending td:nth-child(7) {
        color: #1565C0;
    }
    .td-pending-jhmc td:nth-child(7) {
        color: #1565C0;
    }
</style>    