//---------------------------------------------------------------------------------------------
// This file was AUTO-GENERATED by "TS Lookup Cache Loaders" Xomega.Net generator.
//
// Manual CHANGES to this file WILL BE LOST when the code is regenerated.
//---------------------------------------------------------------------------------------------

import { PersonCreditCard_ReadListOutput, IPersonCreditCardService } from 'ServiceContracts/Sales/IPersonCreditCardService';
import { LocalLookupCacheLoader, fromJSON, Header, LookupTable, ErrorList } from 'xomega';

export class PersonCreditCardReadListCacheLoader extends LocalLookupCacheLoader {

    constructor() {
        super(true, 'person credit card');
    }

    protected getLoadRequest(): JQueryAjaxSettings {
        var _businessEntityId = this.parameters['business entity id'];
        if (_businessEntityId == null) return null;
        return IPersonCreditCardService.getReadListAsyncRequest(_businessEntityId);
    }

    public loadCache(tableType: string, updateCache: (table: LookupTable) => void) {
        let req: JQueryAjaxSettings = this.getLoadRequest();
        if (req == null) return;
        let cl = this;
        req.success = function (data) {
            let lkpData: { [key: string]: Header } = {};
            let rows = data.result || data.Result || [];
            for (let r of rows) {
                let row = new PersonCreditCard_ReadListOutput();
                fromJSON(row, r);
                let h: Header = lkpData[row.CreditCardId] || new Header(tableType, row.CreditCardId, row.CreditCardName);
                h.addToAttribute('person name', row.PersonName);
                h.addToAttribute('card type', row.CardType);
                h.addToAttribute('card number', row.CardNumber);
                h.addToAttribute('exp month', row.ExpMonth);
                h.addToAttribute('exp year', row.ExpYear);
                lkpData[row.CreditCardId] = h;
            }
            var tblData = Object.keys(lkpData).map(k => lkpData[k]);
            var tbl: LookupTable = new LookupTable(tableType, tblData, cl.caseSensitive);
            updateCache(tbl);
        };
        req.error = (jqXHR, textStatus, errorThrow) => {
            updateCache(LookupTable.fromErrors(tableType, ErrorList.fromErrorResponse(jqXHR, errorThrow)));
        };
        $.ajax(req);
    }
}