/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/**
 * * ***************************************
 * 
 * User Event script for sourcing container number and HBL from Inbound Shipment to IR
 * 
 * JOINF-1675 : Source Container number and HBL from Inbound shipment to IR
 * 
 * Author : Jobin and Jismi IT Services
 * 
 * Date Created: 09 - My - 2023
 * 
 * Created by: Linu Paul, Jobin and Jismi IT Services
 * 
 * Description: Branded adding the container number and the HBL in the IR one by one after receiving an inbound.
  
               They need to automate the process to source the container number and HBL to the related item receipt memo field.
  
 * Revision History: 
 * *
 * *************************************
 */
define(['N/error', 'N/record', 'N/search','N/ui/serverWidget'],
    /**
 * @param{error} error
 * @param{record} record
 * @param{search} search
 */
    (error, record, search,serverWidget) => {
        function toFetchInboundDetails(internalId)
        {
            try
            {
                let inboundshipmentSearchObj = search.create({
                    type: "inboundshipment",
                    filters:
                    [
                       ["itemreceipt.internalid","anyof",internalId]
                    ],
                    columns:
                    [
                       search.createColumn({name: "billoflading", label: "Bill Of Lading"}),
                       search.createColumn({name: "custrecord_jj_container_number", label: "Container Number"})
                    ]
                });
                let searchResultCount = inboundshipmentSearchObj.runPaged().count;            
                let billOfLading,containerNumber;
                if(searchResultCount>0)
                {
                    inboundshipmentSearchObj.run().each(function(result){
                        billOfLading=result.getValue({name: "billoflading", label: "Bill Of Lading"});
                        containerNumber=result.getValue({name: "custrecord_jj_container_number", label: "Container Number"});
                    });
                }
                let memoValue = containerNumber+" "+billOfLading;
                return memoValue;
            }
            catch(e)
            {
                log.debug("error@search",e);
            }
        }
       
        /**
         * Defines the function definition that is executed before record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {
            try
            {
                let newRec = scriptContext.newRecord;
                if (scriptContext.type == scriptContext.UserEventType.CREATE) 
                {
                    let interId = scriptContext.newRecord.id;
                    let functionRes = toFetchInboundDetails(interId);
                    record.submitFields({
                        type: record.Type.ITEM_RECEIPT,
                        id: interId,
                        values: {
                            memo: functionRes
                        },
                        options: {
                            enableSourcing: false,
                            ignoreMandatoryFields : true
                        }
                    });
                }  
            }
            catch(err)
            {
                log.error("error@afterSubmitfunction",err);
                 return err.message;
            }
        }
         /**
         * Defines the function definition that is executed before record is loaded.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @param {Form} scriptContext.form - Current form
         * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
         * @since 2015.2
         */
        const beforeLoad = (scriptContext) => {
            try
            {
                let newRec = scriptContext.newRecord;
                if (scriptContext.type == scriptContext.UserEventType.EDIT) 
                {
                    let form = scriptContext.form;
                    let memoField = form.getField({
                        id:"memo"
                    });
                    
                    let memoValue = newRec.getValue({
                        fieldId:"memo"
                    });
                    if(memoValue)
                    {
                        memoField.updateDisplayType({
                            displayType: serverWidget.FieldDisplayType.DISABLED
                        });
                    }
                    else
                    {
                        memoField.updateDisplayType({
                            displayType: serverWidget.FieldDisplayType.ENABLED
                        });
                    }   
                }
            }
            catch(err)
            {
                log.error("error@beforeLoadfunction",err);
                 return err.message;
            }
        }
       return { afterSubmit,beforeLoad}
    });