import VueSelect from '/Components/Common/VueSelect.js?v=3.6'
import InputSearch from '/Components/Common/InputSearch.js?v=3.6'
import SearchPanel from '/Components/Common/SearchPanelWrapper.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 Loading from "/Components/Common/Loading.js?v=3.6"
import { getLabelText } from '/Components/Common/utils.js?v=3.6'

export default {
    props: {
        readOnly: { type: Boolean, required: true },
        sdnServersFilter: { type: Object, required: true },
        isLoggedInAsProvider: { type: Boolean, required: true },
        isLoggedInAsCustomer: { type: Boolean, required: true },
        customerId: { type: String, required: true },
    },
    components: {
        SearchPanel,
        VueSelect,
        OperationsPanel,
        InputSearch,
        NoRecords,
        Pager,
        Loading,
    },
    data() {
        return {
            isLoading: true,
            searchContainerDivClass: "col-md-3 col-sm-6 col-xs-12",
            records: [],
            owners: {},
            sdnServers: this.sdnServersFilter,
            showClearButton: false,

            totalRecords: 0,

            search: {
                selectedOwner: -1,
                customerId: (this.customerId != '0' ? this.customerId : ''),
                customerName: '',
                customerCompanyName: '',
                vlanId: '',
                selectedSDNServerId: -1,
                interfaceName: '',
                description: '',
                pageIndex: 1,
                pageSize: gridPageSize,
                sortExpression: 'Description',
                sortDir: 'Asc'
            }
        }
    },
    methods: {
        getLabelText,

        inputSearchChangeHandler: function (obj) {
            this.search[obj.fieldName] = obj.searchValue;
            this.showClearButton = obj.submit;

            if (obj.submit === true)
                this.doSearch();
        },
        listOwners: async function () {

            var vm = this;

            await fetch("/webservices/machpanelservice.asmx/GetOwners", fetchGet)
                .then(response => response.json()).then((data) => {
                    //alert(data.d);
                    vm.owners = JSON.parse(data.d);
                    this.search.selectedOwner = vm.owners.initialValue;
                    // alert(vm.owners.show);
                })
                .catch(function (e) {
                    displayErrorMsg(e);
                });

        },
        ownerChangeHandler: async function (obj) {

            this.search.selectedOwner = obj.selectedValue;
            setOwnerCookie(obj.selectedValue);

        },
        sdnServersChangeHandler: function (obj) {

            this.search.selectedSDNServerId = obj.selectedValue;

        },
        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.doSearch();
        },
        pageChanged: function (newPage) {
            this.pageIndex = newPage;
            this.listRecords();
        },
        listRecords: async function () {
            // alert("listRecords");
            var vm = this;
            vm.isLoading = true;
            let postRequest = { ...fetchPost };


            postRequest.body = JSON.stringify(vm.search);
            // alert(postRequest.body);
            await fetch("/PCC/VPS/SDN/SDN.aspx/GetVNets", postRequest)
                .then(response => response.json()).then((data) => {
                    vm.records = JSON.parse(data.d);
                    if (vm.records.length > 0)
                        this.totalRecords = this.records[0].Counter;
                });

            vm.isLoading = false;
        },
        pagerChangeHandler: function (o) {
            this.search.pageIndex = o.pageIndex;
            this.search.pageSize = o.pageSize;
            this.doSearch();
        },
        doClearSearch: function () {
            this.search.customerId = (this.customerId != '0' ? this.customerId : '');
            this.search.customerName = '';
            this.search.customerCompanyName = '';
            this.search.vlanId = '';
            this.search.selectedSDNServerId = -1;
            this.search.interfaceName = '';
            this.search.description = '';
            this.pageIndex = 1;
            this.pageSize = gridPageSize;
            this.sortExpression = 'Description';
            this.sortDir = 'Asc';
            this.doSearch();
        },
        doSearch: function () {
            clearAllMsgs();
            this.pageIndex = 1;
            var vm = this;
            this.listRecords().catch(function (e) {
                vm.records = [];
                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 vlanId = e.target.dataset.id;
            let description = e.target.dataset.name;

            if (confirm(this.getLabelText('VNETRemoveConfirmMsg').replace('%operand%', description))) {
                this.removeRecord(vlanId);
            }
        },
        removeRecord: function (vlanId) {
            this.isLoading = true;
            var vm = this;
            //alert(vlanId);
            let postRequest = { ...fetchPost };
            let params = {
                'VLANId': vlanId,
            };
            postRequest.body = JSON.stringify(params);
            //alert(params);
            fetch("/PCC/VPS/SDN/SDN.aspx/RemoveVNET", postRequest)
                .then(response => response.json()).then((data) => {

                    vm.doSearch();
                    displaySuccessMsg(data.d);
                }).catch(function (e) {
                    displayErrorMsg(e);
                });

            this.isLoading = false;
        },

    },
    created: async function () {
        var vm = this;
        await this.listOwners().then(vm.listRecords).catch(function (e) {
            vm.records = [];
            vm.isLoading = false;
            displayErrorMsg(e);
        });
    },
    template: '#vpssdnvnet-template',
}
