<template>
    <div class="git-div-container disp-style-col sc5">
        <!--PLACE DESIGN HTML HERE-->
        <h3>OBO Building Permit Application</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 style="margin-top: 2px !important"><b>Year&nbsp;</b></p>
                    <datepicker :format="customDTP" minimum-view="year" v-model="txt_year" style="text-align: center !important"></datepicker>
                    &nbsp;
                    <button class="git-btn" @click="loadData()">Show</button>
                </div>
                <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: 80px">Permit No.</th>
                    <th style="width: 280px" @click="reorderGroup(1)">Type</th>
                    <th style="width: 180px" @click="reorderGroup(0)">Status</th>
                    <th style="width: 120px">Date Created</th>
                    <th style="width: 120px">Valid From</th>
                    <th style="width: 120px">Valid To</th>
                    <th style="width: 100px"></th>
                </tr>
                </thead>
                <tbody>
                    <tr :class="styleStatus(parseInt(data.stat_id))" :id="'tr_expand_' + data.id" v-for="data in orderedApplications" :key="data.id" style="cursor: pointer">
                        <td><div style="margin-top: 3px; text-align: center">{{ data.permit_no }}</div>
                        <td><div style="margin-top: 3px">{{ data.bldg_type }}</div>
                        <td><div style="margin-top: 3px; text-align: center">{{ data.stat_desc }}</div>
                        <td><div style="margin-top: 3px; text-align: center">{{ data.date_created }}</div>
                        <td><div style="margin-top: 3px; text-align: center">{{ data.valid_from }}</div>
                        <td><div style="margin-top: 3px; text-align: center">{{ data.valid_to }}</div>
                        <td><div @click="_addPopup('modal_loc_bldg_permit', { 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_bldgapp_asc: false,
                txt_search: '',
                txt_year: '',
                bldg_apps: [],
                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.txt_year = moment().year() + "-01-01";
            this.loadData();
        },
        computed: {
            orderedApplications: function () { //FOR SORTING
                this.bldg_apps.forEach(data => {
                    data.showdetails = 0;
                });

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

                try {       
                    tmpAppl = tmpAppl.sort((t1,t2) => t1.bldg_type < t2.bldg_type ? -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 = 'bldg_type';

                if (this.sortCol === 0)
                    sorting = 'bldg_type';
                else if (this.sortCol === 1)
                    sorting = 'stat_desc';

                return _.orderBy(tmpAppl, sorting, (this.order_bldgapp_asc ? 'asc' : 'desc'));
            }
        },
        methods: {
            customDTP(date) {
                return moment(date).format('YYYY');
            },
            reorderGroup(sortCol = 0) {
                if (this.sortCol != sortCol)
                    this.order_bldgapp_asc = true;
                else
                    this.order_bldgapp_asc = !this.order_bldgapp_asc;
                this.sortCol = sortCol;
            },
            changeLimit() { //ENTRIES ALGORITHM
                this.$loading(true);
                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;
                this.$loading(false);
            },
            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;
                }
            },
            deleteItem(id) {
                this.$confirm("Are you sure you want to delete `" + this.bldg_apps.find(data => data.id === id).name + "`?",
                    "Confirm",
                    "warning").then(r => {
                        this._vueDelete(
                            'user_credentials',
                            id,
                            response => {
                                if (response === 'success') {
                                    this.bldg_apps = this._softDelete(this.bldg_apps, id);
                                    this.showSuccessMsg({
                                        title: 'Success!',
                                        message: 'Item deleted!'
                                    });
                                }
                            }
                        );
                    }
                );
            },
            loadData() {
                this.$loading(true);
                $("body").css({ 'cursor': 'wait' });
                
                this._vueGETfromParam(
                    'locator',
                    'fetch_bldg_permits',
                    { 
                        year: this.txt_year,
                        id: parseInt(this.$cookies.get('uid'))
                    },
                    response => {
                        this.bldg_apps = response;
                        this.totalitems = this.bldg_apps.length;
                        this.page = Math.ceil(this.totalitems / this.limit);
                        this.ceil = this.limit;
                        this.floor = 0;
                        this.currpage = 1;
                        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;
            },
            cancelApplication(id) {
                var name = this.bldg_apps.find(d => d.id === id).name;
                this.$loading(true);
                $("body").css({ 'cursor': 'wait' });
                this._vueGETfromParam(
                    'obo_building',
                    'cancel_bldg_application',
                    { id: id },
                    response => {
                        if (response === 'success') {
                            this.bldg_apps = this._softDelete(this.bldg_apps, id);
                            this.totalitems = this.bldg_apps.length;
                            this.page = Math.ceil(this.totalitems / this.limit);
                            this.showSuccessMsg({
                                title: 'Success!',
                                message: 'Canceled ' + name + '\'s application!'
                            });
                            this.$loading(false);
                            $("body").css({ 'cursor': 'default' });
                        }
                    }
                );
            } 
        }
    }
</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>    