<template>
    <div class="git-div-container disp-style-col sc5">
        <!--PLACE DESIGN HTML HERE-->
        <h3>Occupancy Permit</h3>
        <div style="width: 100%; margin-top: 10px;" class="git-div-border">
            <div style="width: 100%" class="disp-style-col">
                <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 60px" @click="reorderGroup(0)">Locator Name</th>
                    <th style="width: 90px" @click="reorderGroup(1)">Status</th>
                    <th style="width: 130px" @click="reorderGroup(2)">Occupancy No.</th>
                    <th style="width: 100px">Valid From</th>
                    <th style="width: 150px">Building Permit No.</th>
                    <th style="width: 80px">Occupacy type</th>
                    <th style="width: 260px">Name of Project</th>
                    <th style="width: 160px">Date Created</th>
                    <th style="width: 90px"></th>
                </tr>
                </thead>
                <tbody>
                    <tr :class="styleStatus(parseInt(data.status_id))" v-for="data in orderedOccupants" :key="data.id" style="cursor: pointer">
                        <td><div style="margin-top: 3px">{{ data.name }}</div>
                        <td><div style="margin-top: 3px; text-align: center"><b>{{ data.status }}</b></div>
                        <td><div style="margin-top: 3px"><b>{{ data.occupancy_no }}</b></div>
                        <td><div style="margin-top: 3px">{{ data.valid_from }}</div>
                        <td><div style="margin-top: 3px">{{ data.bldg_no }}</div>
                        <td><div style="margin-top: 3px">{{ data.occup_type }}</div>
                        <td><div style="margin-top: 3px">{{ data.project_name }}</div>
                        <td><div style="margin-top: 3px">{{ data.created_at | formatMMMMddyyy }}</div>
                        <td><div @click="_addPopup('modal_occup_details', { id: data.id })" class="git-btn git-btn-success disp-style-row div-act-btn" style="text-align: center">View&nbsp;<i class="fa-solid fa-eye" aria-hidden="true"></i></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_occup_asc: false,
                txt_search: '',
                bldg_occup: [],
                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_occupancy',
                'fetch_occupancy',
                { },
                response => {
                    this.bldg_occup = response;
                    this.totalitems = this.bldg_occup.length;
                    this.page = Math.ceil(this.totalitems / this.limit);
                    this.$loading(false);
                    $("body").css({ 'cursor': 'default' });
                }
            );
        },
        computed: {
            orderedOccupants: function () { //FOR SORTING
                var tmpAppl = this.bldg_occup.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;
                });

                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';

                if (this.sortCol === 0)
                    sorting = 'name';
                else if (this.sortCol === 1)
                    sorting = 'status';
                else if (this.sortCol === 2)
                    sorting = 'valid_from';

                return _.orderBy(tmpAppl, sorting, (this.order_occup_asc ? 'asc' : 'desc'));
            }
        },
        methods: {
            customDTP(date) {
                return moment(date).format('YYYY');
            },
            reorderGroup(sortCol = 0) {
                if (this.sortCol != sortCol)
                    this.order_occup_asc = true;
                else
                    this.order_occup_asc = !this.order_occup_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_occupancy',
                    'fetch_occupancy',
                    { },
                    response => {
                        this.bldg_occup = response;
                        this.totalitems = this.bldg_occup.length;
                        this.page = Math.ceil(this.totalitems / this.limit);
                        this.$loading(false);
                        $("body").css({ 'cursor': 'default' });
                    }
                );
            },
            styleStatus(id) {
                var output = '';
                if (id === 9)
                    output = 'td-completed';
                else if (id === 1)
                    output = 'td-pending';
                else if (id === 11 || id === 3)
                    output = 'td-pending-jhmc';
                else
                    output = 'td-pending-jhmc';
                return output;
            }
        }
    }
</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(2) {
        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(2) {
        color: #1565C0;
    }
    .td-pending-jhmc td:nth-child(2) {
        color: #1565C0;
    }
</style>    