import Pager from '/Components/Common/Pager.js?v=3.9'
import NoRecords from '/Components/Common/NoRecords.js?v=3.9'
import OperationsPanel from '/Components/Common/OperationsPanelWrapper.js?v=3.9'
import { getLabelText } from '/Components/Common/utils.js?v=3.9'
import Loading from "/Components/Common/Loading.js?v=3.9"

export default {
    props: {
        readOnly: { type: Boolean, required: true },
        sortPreference: { type: Object, required: true }
    },
    components: {
        OperationsPanel,
        NoRecords,
        Pager,
        Loading
    },
    data() {
        return {
            isLoading: true,
            searchContainerDivClass: "col-md-3 col-sm-6 col-xs-12",
            totalRecords: 0,
            records: [],
            search: {

                pageIndex: 1,
                pageSize: gridPageSize,
                sortExpression: 'FirstName',
                sortDir: 'Desc',
                updateSorting: false
            },
            showClearButton: false,
        }
    },
    methods: {
        getLabelText,
        sortBy: function (exp) {

            if (this.search.sortExpression != exp) {
                this.search.sortExpression = exp;
                this.search.sortDir = "Asc";
            }
            else
                this.search.sortDir = this.search.sortDir == "Asc" ? "Desc" : "Asc";

            this.search.updateSorting = true;
            this.doSearch();
        },
        listRecords: async function () {

            var vm = this;
            vm.isLoading = true;
            let postRequest = { ...fetchPost };
            postRequest.body = JSON.stringify(vm.search);

            await fetch("/RCC/HelpDesk/AdminUserListing.aspx/GetHelpDeskUsers", postRequest)
                .then(response => response.json()).then((data) => {

                    vm.search.updateSorting = false;
                    vm.records = JSON.parse(data.d);
                    if (vm.records.length > 0)
                        this.totalRecords = this.records[0].Counter;
                });
            vm.isLoading = false;
        },
        confrimRemove: function (e) {
            // Get the value of the disabled attribute
            const disabledValue = e.target.getAttribute('disabled');

            if (disabledValue === 'true' || disabledValue === true)
                return false;

            
            //CustomerId, OwnerId, Name, SubsCount, UserCount
            let Id = e.target.dataset.id;
           
            if (confirm(this.getLabelText('GN000200')))
                this.removeRecord(Id);
        },
        removeRecord: function (Id) {
            var vm = this;
           
            let postRequest = { ...fetchPost };
            let params = {
                'Id': Id
            };
            postRequest.body = JSON.stringify(params);

            fetch("/RCC/HelpDesk/AdminUserListing.aspx/RemoveUser", postRequest)
                .then(response => response.json()).then((data) => {
                    vm.doSearch();
                    displaySuccessMsg(data.d);
                }).catch(function (e) {
                    displayErrorMsg(e);
                });
        },
        pagerChangeHandler: function (o) {
            this.search.pageIndex = o.pageIndex;
            this.search.pageSize = o.pageSize;
            this.doSearch();
        },
        doClearSearch: async function () {
            this.search.pageIndex = 1;

            updateSelectInterface();    //Required to update SOL control in case of html select.
            this.doSearch();
        },
        doSearch: function () {
            clearAllMsgs();
            var vm = this;
            this.listRecords().catch(function (e) {
                vm.records = [];
                vm.totalRecords = 0;
                vm.isLoading = false;
                displayErrorMsg(e);
            });
        },
    },
    created: async function () {

        this.search.sortDir = this.sortPreference.SortDirection;
        this.search.sortExpression = this.sortPreference.SortExpression;

        var vm = this;

        await this.listRecords().catch(function (e) {
            vm.records = [];
            vm.totalRecords = 0;
            vm.isLoading = false;
            displayErrorMsg(e);
        });

    },
    template: `

        <div class="col-xs-12">
            <div class="card">
                <div class="header"><h3>{{ getLabelText('M0023') }}</h3></div>
                <div class="body">
                    <div class="row">

                        <OperationsPanel v-if="!readOnly">
                            <a v-bind:href="'/RCC/HelpDesk/AddAdminUser.aspx'" class="btn btn-primary waves-effect btnAction">{{ getLabelText('GN00001321') }}</a>
                        </OperationsPanel>

                        <div class="col-xs-12" v-if="isLoading">
                            <Loading/>
                        </div>
                        <div class="col-xs-12" v-else>
                            <table class="table table-bordered table-striped table-hover" v-if="records.length > 0">
                                <tbody>
                                    <tr>
                                        <th class="col-xs-3"><a href="#" @click.prevent="sortBy('FirstName')">{{getLabelText('GN0000298')}}</a></th>
                                        <th class="col-xs-3"><a href="#" @click.prevent="sortBy('Email')">{{ getLabelText('GN0000365') }}</a></th>
                                        <th class="col-xs-2"><a href="#" @click.prevent="sortBy('NickName')">{{ getLabelText('GN0000280') }}</a></th>
                                        <th class="col-xs-2"><a href="#" @click.prevent="sortBy('Designation')">{{ getLabelText('GN0000657') }}</a></th>
                                        <th class="col-xs-2 align-center">{{ getLabelText('GN0000360') }}</th>
                                    </tr>
                                    <tr v-for="record in records">

                                        <td>{{ record.FirstName + ' ' + record.LastName }}</td>
                                        <td>{{ record.Email }}</td>
                                        <td>{{ record.NickName }}</td>
                                        <td>{{ record.Designation }}</td>

                                        <td>
                                            <a v-bind:href="'/RCC/HelpDesk/EditAdminUser.aspx?EmpId=' + record.EmployeeID" class="btn btn-primary waves-effect" target='_self'>{{ getLabelText('GN0000276') }}</a>
                                            <a v-if="!readOnly" @click="confrimRemove" :data-id="record.EmployeeID" class="btn btn-primary waves-effect btnAction">{{ getLabelText('GN0000278') }}</a>
                                        </td>
                                        
                                    </tr>

                                    <Pager :totalRecords="totalRecords" :pageSize="this.search.pageSize" :currentPage="this.search.pageIndex" @pagerChange="pagerChangeHandler" />
                                </tbody>
                            </table>
                            <NoRecords v-else />
                        </div>
                    </div>
                </div>
            </div>
        </div>
`
}
