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


export default {
    props: {
        readOnly: { type: Boolean, required: true },
        serverId: { type: String, required: true },
        serverName: { type: String, required: true },
        serverGroupId: { type: String, required: true },
        serverGroupName: { type: String, required: true }
    },
    components: {
        OperationsPanel,
        VueSelect,
        InputSearch,
        NoRecords,
        Pager,
        Loading
    },
    data() {
        return {
            isLoading: true,
            searchContainerDivClass: "col-md-3 col-sm-6 col-xs-12",
            totalRecords: 0,
            records: [],
            search: {
                serverId: this.serverId,

                pageIndex: 1,
                pageSize: gridPageSize,
                sortExpression: 'SwitchId',
                sortDir: 'Asc',
                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("/PCC/VPS/ServerGroups.aspx/GetVirtualPrivateNetwork", 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.length;
                });
            vm.isLoading = false;
        },
        pagerChangeHandler: function (o) {
            this.search.pageIndex = o.pageIndex;
            this.search.pageSize = o.pageSize;
            this.doSearch();
        },
        doClearSearch: async function () {
            this.search.selectedStatus = '-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);
            });
        },
        confirmRemove: function (e) {
            // Get the value of the disabled attribute
            const disabledValue = e.target.getAttribute('disabled');

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

            let sId = this.serverId;
            let switchId = e.target.dataset.switchid;
            let Name = e.target.dataset.name;
            
            if (confirm('Are you sure you want to remove ' + Name + ' Virtual Network?'))
                this.removeRecord(sId, switchId, Name);
        },
        removeRecord: function (sId, switchId, name) {

            var vm = this;

            let postRequest = { ...fetchPost };
            let params = {
                'ServerId': sId,
                'SwitchId': switchId,
                'Name': name
            };
            postRequest.body = JSON.stringify(params);

            fetch("/PCC/VPS/ServerGroups.aspx/RemoveVirtualPrivateNetwork", postRequest)
                .then(response => response.json()).then((data) => {

                    vm.doSearch();
                    displaySuccessMsg(data.d);
                }).catch(function (e) {
                    displayErrorMsg(e);
                });
        },
        hideFromPanelCheckedChanged: function (sId, name, hideFromPanel) {
            var vm = this;

            let postRequest = { ...fetchPost };
            let params = {
                'SwitchId': sId,
                'Name': name,
                'HideFromPanel': hideFromPanel,
                'ServerGroupId': this.serverGroupId
            
            };

            //alert(this.serverGroupId);
            postRequest.body = JSON.stringify(params);

            fetch("/PCC/VPS/ServerGroups.aspx/HideFromPanelCheckedChanged", postRequest)
                .then(response => response.json()).then((data) => {

                    vm.doSearch();
                    displaySuccessMsg(data.d);
                }).catch(function (e) {
                    displayErrorMsg(e);
                });
        }
    },
    created: async function () {

        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="body">
                    <div class="row">

                        <OperationsPanel v-if="!readOnly">
                            <a v-bind:href="'/PCC/VPS/AddVirtualNetwork.aspx?VPSServerID=' + this.serverId" class="btn btn-primary waves-effect btnAction">{{ getLabelText('L00202') }}</a>
                        </OperationsPanel>
                        
                        <div class="col-xs-12">
                            <table class="table table-bordered table-striped table-hover">
                                <tbody>
                                <tr>
                                    <td width="20%" class="row-label">{{ getLabelText('GN0000102') }}</td>
                                    <td width="30%" v-html="serverGroupName"></td>
                                    <td width="20%" class="row-label">{{ getLabelText('GN0000980') }}</td>
                                    <td width="30%" v-html="serverName"></td>
                                </tr>
                                </tbody>
                            </table>
                        </div>                        

                       <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-1 align-center">{{getLabelText('String385')}}</th>
                                        <th class="col-xs-5">{{ getLabelText('HVServer21') }}</th>
                                        <th class="col-xs-1">{{ getLabelText('strHideFromPanel') }}</th>
                                        <th class="col-xs-5 align-center hide">{{ getLabelText('GN0000360') }}</th>
                                    </tr>
                                    <tr v-for="record in records">

                                        <td class="align-center">{{ getLabelText('GN0000222') }}</td>
                                        <td>{{ record.Name }}</td>
                                        <td>
                                            <input type="checkbox" v-bind:id="'record.HideFromPanel' + 'record.SwitchId'" v-model="record.HideFromPanel" @click.prevent="hideFromPanelCheckedChanged(record.SwitchId, record.Name, !record.HideFromPanel)" />
                                            <label v-bind:for="'record.HideFromPanel' + 'record.SwitchId'"></label>
                                        </td>
                                        <td class="hide">
                                            <a class="btn btn-primary waves-effect btnAction" @click.prevent="confirmRemove" :data-switchid="record.SwitchId" :data-name="record.Name">{{ 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>
`
}
