<commit_before>/*

    TODO: Add fields and labels
    TODO: Add supporting methods to controller

    TODO: Experiment with number of columns listed in work item
    TODO: Experiment with panel configured as labels and fields instead of table
    TODO: Proceed with both so you can show both versions to team

    TODO: Layout options
    TODO: Demo to team

    TODO: Fix details
    TODO: Add descriptions
 */

import labelScheduleTitle from '@salesforce/label/c.RD2_ActiveSchedulesTitle';
import labelColumnDate from '@salesforce/label/c.RD2_ScheduleVisualizerColumnDate';

import { LightningElement, api, wire, track } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import { getRecord } from 'lightning/uiRecordApi';

import SOBJECT_RECURRING_DONATION from '@salesforce/schema/npe03__Recurring_Donation__c';

import FIELD_RD_AMOUNT from '@salesforce/schema/npe03__Recurring_Donation__c.npe03__Amount__c';
import FIELD_RD_PERIOD from '@salesforce/schema/npe03__Recurring_Donation__c.npe03__Installment_Period__c';
import FIELD_RD_FREQUENCY from '@salesforce/schema/npe03__Recurring_Donation__c.InstallmentFrequency__c';
import FIELD_RD_DAYOFMONTH from '@salesforce/schema/npe03__Recurring_Donation__c.Day_of_Month__c';
import FIELD_RD_STARTDATE from '@salesforce/schema/npe03__Recurring_Donation__c.StartDate__c';
import FIELD_RD_PAYMENT_METHOD from '@salesforce/schema/npe03__Recurring_Donation__c.PaymentMethod__c';
import FIELD_RD_CAMPAIGN from '@salesforce/schema/npe03__Recurring_Donation__c.npe03__Recurring_Donation_Campaign__c';

import getSchedules from '@salesforce/apex/RD2_ScheduleController.getSchedules';

const SCHEDULE_COLS = [
    { label: '$AMOUNT', fieldName: 'amount', type: 'currency', sortable: false,
        typeAttributes: { currencyCode: '$CURRENCYISOCODE' } },
    { label: '$PAYMENT_METHOD', fieldName: 'paymentMethod', type: 'text', sortable: false },
    { label: '$START_DATE', fieldName: 'startDate', type: 'date-local', sortable: false,
        typeAttributes:{
            month: "2-digit",
            day: "2-digit"
        } },
    { label: '$PERIOD', fieldName: 'period', type: 'text', sortable: false }
];

/*
    -- Work in progress on SCHEDULE_COLS --

    { label: '$AMOUNT', fieldName: 'amount', type: 'currency', sortable: false,
        typeAttributes: { currencyCode: '$CURRENCYISOCODE' } },
    { label: '$PAYMENT_METHOD', fieldName: 'paymentMethod', type: 'text', sortable: false },
    { label: '$CAMPAIGN', fieldName: 'campaign', type: 'text', sortable: false },
    { label: '$START_DATE', fieldName: 'startDate', type: 'date-local', sortable: false,
        typeAttributes:{
            month: "2-digit",
            day: "2-digit"
        } },
    { label: '$END_DATE', fieldName: 'endDate', type: 'date-local', sortable: false,
        typeAttributes:{
            month: "2-digit",
            day: "2-digit"
        } },
    { label: '$PERIOD', fieldName: 'period', type: 'text', sortable: false },
    { label: '$FREQUENCY', fieldName: 'frequency', type: 'number', sortable: false },
    { label: '$DAY_OF_MONTH', fieldName: 'dayOfMonth', type: 'text', sortable: false }

 */

export default class RdScheduleVisualizer extends LightningElement {

    @api recordId;
    @api displayNum;

    @track schedules;
    @track error;
    @track columns = SCHEDULE_COLS;
    @track currencyIsoCode;
    @track lblScheduleTitle = labelScheduleTitle;
    @track lblAmount;
    @track lblPmtMethod;
    @track lblCampaign;
    @track lblStartDate;
    @track lblEndDate;
    @track lblPeriod;
    @track lblFrequency;
    @track lblDayOfMonth;

    /*******************************************************************************
     * @description Whenever the related RD record is updated, this is called to force
     * a refresh of the table and component.
     */
    @wire(getRecord, { recordId: '$recordId',
        fields: [FIELD_RD_AMOUNT, FIELD_RD_DAYOFMONTH, FIELD_RD_FREQUENCY, FIELD_RD_PERIOD,
                 FIELD_RD_STARTDATE, FIELD_RD_PAYMENT_METHOD, FIELD_RD_CAMPAIGN] })
    wireRecordChange() {
        if (this.recordId) {
            getSchedules({ recordId: this.recordId, displayNum: this.displayNum })
                .then(data => {
                    this.handleCurrencyIsoCode(data);
                    this.handleColumns();
                    this.schedules = data;
                    this.error = null;

                })
                .catch(error => {
                    this.schedules = null;
                    this.error = this.handleError(error);
                });
        }
    }

    /*******************************************************************************
     * @description Call Apex to retrieve the Currency Code to use in the UI
     */
    handleCurrencyIsoCode(schedules) {
        if (schedules) {
            this.currencyIsoCode = schedules[0].currencyIsoCode;
        } else {
            this.currencyIsoCode = 'USD';
        }
    }

    /*******************************************************************************
     * @description Retrieve the object info for the Recurring Donation Object to set the field labels
     */
    @wire(getObjectInfo, { objectApiName: SOBJECT_RECURRING_DONATION })
    wireGetRDObjectInfo({error, data}) {
        if (data) {
            this.lblAmount = data.fields[FIELD_RD_AMOUNT.fieldApiName].label;
            this.lblPmtMethod = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
            this.lblStartDate = data.fields[FIELD_RD_STARTDATE.fieldApiName].label;
            this.lblPeriod = data.fields[FIELD_RD_PERIOD.fieldApiName].label;

/*
    -- Work in progress on RD wire

                    this.lblAmount = data.fields[FIELD_RD_AMOUNT.fieldApiName].label;
                    this.lblPmtMethod = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblCampaign = data.fields[FIELD_RD_CAMPAIGN.fieldApiName].label;
                    this.lblStartDate = data.fields[FIELD_RD_STARTDATE.fieldApiName].label;
                    this.lblPeriod = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblFrequency = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblDayOfMonth = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;

 */
        }
    }

    /*******************************************************************************
     * @description Called by the HTML to retrieve the schedule to render
     */
    get schedules() {
        return this.schedules;
    }

    /*******************************************************************************
     * @description Parse the error response
     * @param {object} error: Event holding error details
     * @return The error message to render.
     */
    handleError(error) {
        if (error && error.status && error.body) {
            return error.body.message;
        } else if (error && error.name && error.message) {
            return error.message;
        } else {
            return "";
        }
    }

    /*******************************************************************************
     * @description Configure column labels and the currency code to use for the amount field
     * by replacing constants in the SCHEDULE_COL string with the actual (translated) field labels
     * as well as the $CurrencyIsoCode with the RD or Org CurrencyIsoCode as retrieved from Apex
     */
    handleColumns() {
        const labelConversions = [
            { label: '$AMOUNT', value: this.lblAmount },
            { label: '$PAYMENT_METHOD', value: this.lblPmtMethod },
            { label: '$START_DATE', value: this.lblStartDate },
            { label: '$PERIOD', value: this.lblPeriod }
        ];

        /*
            -- Work in progress on labelConversions

                    this.lblAmount = data.fields[FIELD_RD_AMOUNT.fieldApiName].label;
                    this.lblPmtMethod = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblCampaign = data.fields[FIELD_RD_CAMPAIGN.fieldApiName].label;
                    this.lblStartDate = data.fields[FIELD_RD_STARTDATE.fieldApiName].label;
                    this.lblEndDate = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblPeriod = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblFrequency = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblDayOfMonth = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;

         */

        const currencyIsoCode = this.currencyIsoCode;

        this.columns = SCHEDULE_COLS;
        this.columns.forEach(function(col){
            if (col.label === '$AMOUNT') {
                col.typeAttributes.currencyCode = currencyIsoCode;
            }
            labelConversions.forEach(function(lbl){
                if (col.label === lbl.label) {
                    col.label = lbl.value;<commit_msg>add remaining fields, so QE can begin testing while look and feel is modified<commit_after>/*

    TODO: Add fields and labels
    TODO: Add supporting methods to controller

    TODO: Experiment with number of columns listed in work item
    TODO: Experiment with panel configured as labels and fields instead of table
    TODO: Proceed with both so you can show both versions to team

    TODO: Layout options
    TODO: Demo to team

    TODO: Fix details
    TODO: Add descriptions
 */

import labelScheduleTitle from '@salesforce/label/c.RD2_ActiveSchedulesTitle';
import labelColumnDate from '@salesforce/label/c.RD2_ScheduleVisualizerColumnDate';

import { LightningElement, api, wire, track } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import { getRecord } from 'lightning/uiRecordApi';

import SOBJECT_RECURRING_DONATION from '@salesforce/schema/npe03__Recurring_Donation__c';

import FIELD_RD_AMOUNT from '@salesforce/schema/npe03__Recurring_Donation__c.npe03__Amount__c';
import FIELD_RD_PERIOD from '@salesforce/schema/npe03__Recurring_Donation__c.npe03__Installment_Period__c';
import FIELD_RD_FREQUENCY from '@salesforce/schema/npe03__Recurring_Donation__c.InstallmentFrequency__c';
import FIELD_RD_DAYOFMONTH from '@salesforce/schema/npe03__Recurring_Donation__c.Day_of_Month__c';
import FIELD_RD_STARTDATE from '@salesforce/schema/npe03__Recurring_Donation__c.StartDate__c';
import FIELD_RD_PAYMENT_METHOD from '@salesforce/schema/npe03__Recurring_Donation__c.PaymentMethod__c';
import FIELD_RD_CAMPAIGN from '@salesforce/schema/npe03__Recurring_Donation__c.npe03__Recurring_Donation_Campaign__c';

import getSchedules from '@salesforce/apex/RD2_ScheduleController.getSchedules';

const SCHEDULE_COLS = [
    { label: '$AMOUNT', fieldName: 'amount', type: 'currency', sortable: false,
        typeAttributes: { currencyCode: '$CURRENCYISOCODE' } },
    { label: '$PAYMENT_METHOD', fieldName: 'paymentMethod', type: 'text', sortable: false },
    { label: '$CAMPAIGN', fieldName: 'campaign', type: 'text', sortable: false },
    { label: '$START_DATE', fieldName: 'startDate', type: 'date-local', sortable: false,
        typeAttributes:{
            month: "2-digit",
            day: "2-digit"
        } },
    { label: '$END_DATE', fieldName: 'endDate', type: 'date-local', sortable: false,
        typeAttributes:{
            month: "2-digit",
            day: "2-digit"
        } },
    { label: '$PERIOD', fieldName: 'period', type: 'text', sortable: false },
    { label: '$FREQUENCY', fieldName: 'frequency', type: 'number', sortable: false },
    { label: '$DAY_OF_MONTH', fieldName: 'dayOfMonth', type: 'text', sortable: false }
];

/*
    -- Work in progress on SCHEDULE_COLS --

    { label: '$AMOUNT', fieldName: 'amount', type: 'currency', sortable: false,
        typeAttributes: { currencyCode: '$CURRENCYISOCODE' } },
    { label: '$PAYMENT_METHOD', fieldName: 'paymentMethod', type: 'text', sortable: false },
    { label: '$CAMPAIGN', fieldName: 'campaign', type: 'text', sortable: false },
    { label: '$START_DATE', fieldName: 'startDate', type: 'date-local', sortable: false,
        typeAttributes:{
            month: "2-digit",
            day: "2-digit"
        } },
    { label: '$END_DATE', fieldName: 'endDate', type: 'date-local', sortable: false,
        typeAttributes:{
            month: "2-digit",
            day: "2-digit"
        } },
    { label: '$PERIOD', fieldName: 'period', type: 'text', sortable: false },
    { label: '$FREQUENCY', fieldName: 'frequency', type: 'number', sortable: false },
    { label: '$DAY_OF_MONTH', fieldName: 'dayOfMonth', type: 'text', sortable: false }

 */

export default class RdScheduleVisualizer extends LightningElement {

    @api recordId;
    @api displayNum;

    @track schedules;
    @track error;
    @track columns = SCHEDULE_COLS;
    @track currencyIsoCode;
    @track lblScheduleTitle = labelScheduleTitle;
    @track lblAmount;
    @track lblPmtMethod;
    @track lblCampaign;
    @track lblStartDate;
    @track lblEndDate;
    @track lblPeriod;
    @track lblFrequency;
    @track lblDayOfMonth;

    /*******************************************************************************
     * @description Whenever the related RD record is updated, this is called to force
     * a refresh of the table and component.
     */
    @wire(getRecord, { recordId: '$recordId',
        fields: [FIELD_RD_AMOUNT, FIELD_RD_DAYOFMONTH, FIELD_RD_FREQUENCY, FIELD_RD_PERIOD,
                 FIELD_RD_STARTDATE, FIELD_RD_PAYMENT_METHOD, FIELD_RD_CAMPAIGN] })
    wireRecordChange() {
        if (this.recordId) {
            getSchedules({ recordId: this.recordId, displayNum: this.displayNum })
                .then(data => {
                    this.handleCurrencyIsoCode(data);
                    this.handleColumns();
                    this.schedules = data;
                    this.error = null;

                })
                .catch(error => {
                    this.schedules = null;
                    this.error = this.handleError(error);
                });
        }
    }

    /*******************************************************************************
     * @description Call Apex to retrieve the Currency Code to use in the UI
     */
    handleCurrencyIsoCode(schedules) {
        if (schedules) {
            this.currencyIsoCode = schedules[0].currencyIsoCode;
        } else {
            this.currencyIsoCode = 'USD';
        }
    }

    /*******************************************************************************
     * @description Retrieve the object info for the Recurring Donation Object to set the field labels
     */
    @wire(getObjectInfo, { objectApiName: SOBJECT_RECURRING_DONATION })
    wireGetRDObjectInfo({error, data}) {
        if (data) {
            this.lblAmount = data.fields[FIELD_RD_AMOUNT.fieldApiName].label;
            this.lblPmtMethod = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
            this.lblCampaign = data.fields[FIELD_RD_CAMPAIGN.fieldApiName].label;
            this.lblStartDate = data.fields[FIELD_RD_STARTDATE.fieldApiName].label;
            this.lblEndDate = 'End Date';
            // this.lblEndDate = data.fields[FIELD_RD_ENDDATE.fieldApiName].label;
            this.lblPeriod = data.fields[FIELD_RD_PERIOD.fieldApiName].label;
            this.lblFrequency = data.fields[FIELD_RD_FREQUENCY.fieldApiName].label;
            this.lblDayOfMonth = data.fields[FIELD_RD_DAYOFMONTH.fieldApiName].label;

/*
    -- Work in progress on RD wire

                    this.lblAmount = data.fields[FIELD_RD_AMOUNT.fieldApiName].label;
                    this.lblPmtMethod = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblCampaign = data.fields[FIELD_RD_CAMPAIGN.fieldApiName].label;
                    this.lblStartDate = data.fields[FIELD_RD_STARTDATE.fieldApiName].label;
                    this.lblPeriod = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblFrequency = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblDayOfMonth = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;

 */
        }
    }

    /*******************************************************************************
     * @description Called by the HTML to retrieve the schedule to render
     */
    get schedules() {
        return this.schedules;
    }

    /*******************************************************************************
     * @description Parse the error response
     * @param {object} error: Event holding error details
     * @return The error message to render.
     */
    handleError(error) {
        if (error && error.status && error.body) {
            return error.body.message;
        } else if (error && error.name && error.message) {
            return error.message;
        } else {
            return "";
        }
    }

    /*******************************************************************************
     * @description Configure column labels and the currency code to use for the amount field
     * by replacing constants in the SCHEDULE_COL string with the actual (translated) field labels
     * as well as the $CurrencyIsoCode with the RD or Org CurrencyIsoCode as retrieved from Apex
     */
    handleColumns() {
        const labelConversions = [
            { label: '$AMOUNT', value: this.lblAmount },
            { label: '$PAYMENT_METHOD', value: this.lblPmtMethod },
            { label: '$CAMPAIGN', value: this.lblCampaign },
            { label: '$START_DATE', value: this.lblStartDate },
            { label: '$END_DATE', value: this.lblEndDate },
            { label: '$PERIOD', value: this.lblPeriod },
            { label: '$FREQUENCY', value: this.lblFrequency },
            { label: '$DAY_OF_MONTH', value: this.lblDayOfMonth }
        ];

        /*
            -- Work in progress on labelConversions

                    this.lblAmount = data.fields[FIELD_RD_AMOUNT.fieldApiName].label;
                    this.lblPmtMethod = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblCampaign = data.fields[FIELD_RD_CAMPAIGN.fieldApiName].label;
                    this.lblStartDate = data.fields[FIELD_RD_STARTDATE.fieldApiName].label;
                    this.lblEndDate = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblPeriod = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblFrequency = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;
                    this.lblDayOfMonth = data.fields[FIELD_RD_PAYMENT_METHOD.fieldApiName].label;

         */

        const currencyIsoCode = this.currencyIsoCode;

        this.columns = SCHEDULE_COLS;
        this.columns.forEach(function(col){
            if (col.label === '$AMOUNT') {
                col.typeAttributes.currencyCode = currencyIsoCode;
            }
            labelConversions.forEach(function(lbl){
                if (col.label === lbl.label) {
                    col.label = lbl.value;
{'fraction_non_alphanumeric': 0.1488163520700803, 'fraction_numerical': 0.0034414433204713736, 'mean_word_length': 2.9005491153142158, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2264028', 'n_tokens_mistral': 5723, 'n_tokens_neox': 5522, 'n_words': 1176}