<template>
    <div class="git-div-container disp-style-col sc5">
        <!--PLACE DESIGN HTML HERE-->
        <h3>Building Permit Requirements</h3>
        <div style="width: 100%; margin-top: 10px;" class="disp-style-row">
            <div style="width: 40%" class="git-div-border">
                <div style="width: 100%" class="disp-style-col">
                    <h5>Requirement Name</h5>
                    <textarea v-model="txt_req" style="height: 300px"></textarea>
                    <h5>Type</h5>
                    <select v-model="cmb_req">
                        <option value="1" selected>REQUIRED</option>
                        <option value="0">NOT REQUIRED</option>
                    </select>
                    <span @click="saveRequirement()" class="git-btn git-btn-info" style="padding: 0px 16px 32px 14px; width: fit-content; margin-top: 8px">
                        <i class="fa-solid fa-floppy-disk"></i>
                    </span>
                </div>
            </div>
            <div style="width: 100%" 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" @click="reorderGroup(0)">Requirements</th>
                        <th style="width: 120px" @click="reorderGroup(1)">Type</th>
                        <th style="width: 120px"></th>
                    </tr>
                    </thead>
                    <tbody>
                        <tr :class="styleStatus(data.bldg_status_id)" :id="'tr_expand_' + data.id" v-for="data in orderedRequirements" :key="data.id" style="cursor: pointer">
                            <td style="text-align: center">
                                {{ data.requirement }}
                            </td>
                            <td style="text-align-last: center; vertical-align: middle">
                                <a :style="(data.isRequired === '1' ? 'color: #4CAF50' : 'color: #E53935') + '; font-size: 16px; font-weight: bold'">
                                    {{ (data.isRequired === '1' ? 'REQUIRED' : 'NOT REQUIRED') }}
                                </a>
                            </td>
                            <td style="vertical-align: middle;">
                                <div class="disp-style-row">
                                    <span class="git-btn git-btn-info" style="padding: 0px 16px 32px 14px; margin-right: 6px">
                                        <i class="fa-solid fa-pen"></i>
                                    </span>
                                    <span @click="deleteItem(data.id)" class="git-btn git-btn-danger" style="padding: 0px 16px 32px 14px;">
                                        <i class="fa-solid fa-x"></i>
                                    </span>
                                </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>
    </div>
</template>
<script>
    import moment from 'moment';
    export default {
        data() {
            return {
                //DATA HERE
                order_req_asc: true,
                txt_search: '',
                txt_req: '',
                cmb_req: 1,
                bldg_req: [],
                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: {
            orderedRequirements: function () { //FOR SORTING
                this.bldg_req.forEach(data => {
                    data.showdetails = 0;
                });

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

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

                if (this.sortCol === 0)
                    sorting = 'requirement';
                else if (this.sortCol === 1)
                    sorting = 'isRequired';

                return _.orderBy(tmpAppl, sorting, (this.order_req_asc ? 'asc' : 'desc'));
            }
        },
        methods: {
            saveRequirement() {
                this.$loading(true);
                $("body").css({ 'cursor': 'wait' });

                if (this.txt_req.length < 3) {
                    this.showErrorMsg({
                        title: 'Error!',
                        message: 'Invalid Building requirement!'
                    });
                    this.$loading(false);
                    $("body").css({ 'cursor': 'default' });
                    return;
                }

                this._vuePOST(
                    'obo_settings', 
                    {
                        type: "bldg_req",
                        data: {
                            requirement: this.txt_req.trim(),
                            isRequired: this.cmb_req
                        }
                    }, 
                    response => {
                        this.showSuccessMsg({
                            title: 'Success!',
                            message: 'Added building permit settings!'
                        });
                        this._softInsert(this.bldg_req, response);
                        this.$loading(false);
                        $("body").css({ 'cursor': 'default' });
                    }
                );
            },
            customDTP(date) {
                return moment(date).format('YYYY');
            },
            reorderGroup(sortCol = 0, reset = false) {
                if (this.sortCol != sortCol)
                    this.order_req_asc = true;
                else
                    this.order_req_asc = !this.order_req_asc;
                if (reset)
                    if (this.sortCol != sortCol)
                        this.order_req_asc = true;
                    else
                        this.order_req_asc = !this.order_req_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;
                }
            },
            deleteItem(id) {
                this.$confirm("Are you sure you want to delete this requirement?",
                    "Confirm",
                    "warning").then(r => {
                        this._vueObjDelete(
                            'obo_settings',
                            { 
                                type: 'req',
                                id: id
                            },
                            response => {
                                if (response === 'success') {
                                    this.bldg_req = this._softDelete(this.bldg_req, id);
                                    this.showSuccessMsg({
                                        title: 'Success!',
                                        message: 'Requirement deleted!'
                                    });
                                }
                            }
                        );
                    }
                );
            },
            loadData() {
                this.$loading(true);
                $("body").css({ 'cursor': 'wait' });
                this.bldg_req = [];

                this._vueGETfromParam(
                    'obo_settings',
                    'fetch_bldg_req',
                    { },
                    response => {
                        this.bldg_req = response;
                        this.totalitems = this.bldg_req.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(rowid) {
                var id = parseInt(rowid);
                var output = '';
                if (id === 9)
                    output = 'tr-completed';
                else if (id === 1)
                    output = 'tr-pending';
                else if (id === 11 || id === 3)
                    output = 'tr-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;
    }
    .tr-completed h5 {
        color: #43A047;
    }
    .tr-pending {
        background: radial-gradient(circle, rgba(129,199,132,1) 0%, rgba(156,204,101,1) 100%);
        background-size: 500% 500%;
        animation: gradient 10s ease infinite;
    }
    .tr-pending h5 {
        color: #1565C0;
    }
    .tr-pending-jhmc h5 {
        color: #1565C0;
    }
</style>    