const PaymentAgentTransferUI = require('./payment_agent_transfer.ui');
const Client                 = require('../../../../base/client');
const BinarySocket           = require('../../../../base/socket');
const getDecimalPlaces       = require('../../../../common/currency').getDecimalPlaces;
const FormManager            = require('../../../../common/form_manager');
const localize               = require('../../../../../_common/localize').localize;
const State                  = require('../../../../../_common/storage').State;

const PaymentAgentTransfer = (() => {
    let is_authenticated_payment_agent,
        common_request_fields,
        $form_error;

    const onLoad = () => {
        PaymentAgentTransferUI.initValues();
        BinarySocket.wait('get_settings', 'balance').then(() => {
            is_authenticated_payment_agent = State.getResponse('get_settings.is_authenticated_payment_agent');
            if (is_authenticated_payment_agent) {
                BinarySocket.send({
                    paymentagent_list: Client.get('residence'),
                    currency         : Client.get('currency'),
                }).then((response) => {
                    const pa_values = response.paymentagent_list.list.filter(
                        (a) => a.paymentagent_loginid === Client.get('loginid')
                    )[0];
                    init(pa_values);
                });
            } else {
                setFormVisibility(false);
            }
        });
    };

    const init = (pa) => {
        const form_id     = '#frm_paymentagent_transfer';
        const $no_bal_err = $('#no_balance_error');
        const currency    = Client.get('currency');

        $form_error = $('#form_error');

        if (!currency || +Client.get('balance') === 0) {
            $('#pa_transfer_loading').remove();
            $no_bal_err.setVisibility(1);
            return;
        }

        $no_bal_err.setVisibility(0);
        setFormVisibility(true);
        PaymentAgentTransferUI.updateFormView(currency);

        common_request_fields = [
            { request_field: 'paymentagent_transfer', value: 1 },
            { request_field: 'currency',              value: currency },
        ];

        FormManager.init(form_id, [
            { selector: '#client_id', validations: ['req', ['regular', { regex: /^\w+\d+$/, message: 'Please enter a valid Login ID.' }]], request_field: 'transfer_to' },
            { selector: '#amount',    validations: ['req', ['number', { type: 'float', decimals: getDecimalPlaces(currency), min: pa.min_withdrawal, max: pa.max_withdrawal }], ['custom', { func: () => +Client.get('balance') >= +$('#amount').val(), message: localize('Insufficient balance.') }]] },

            { request_field: 'dry_run', value: 1 },
        ].concat(common_request_fields));

        FormManager.handleSubmit({
            form_selector       : form_id,
            fnc_response_handler: responseHandler,
            enable_button       : 1,
        });
    };

    const setFormVisibility = (is_visible) => {
        if (is_visible) {
            $('#pa_transfer_loading').remove();
            PaymentAgentTransferUI.showForm();
            PaymentAgentTransferUI.showNotes();
        } else {
            PaymentAgentTransferUI.hideForm();
            PaymentAgentTransferUI.hideNotes();
            if (!is_authenticated_payment_agent) {
                $('#pa_transfer_loading').remove();
                $('#not_pa_error').setVisibility(1);
            }
        }
    };

    const responseHandler = (response) => {
        const req   = response.echo_req;
        const error = response.error;

        if (error) {
            if (req.dry_run === 1) {
                $form_error.text(error.message).setVisibility(1);
                return;
            }
            PaymentAgentTransferUI.showTransferError(error.message);
            return;
        }

        if (response.paymentagent_transfer === 2) {
            PaymentAgentTransferUI.hideFirstForm();
            PaymentAgentTransferUI.showConfirmation();
            PaymentAgentTransferUI.updateConfirmView(response.client_to_full_name, req.transfer_to.toUpperCase(),
                req.amount, req.currency);
            initConfirm(req);
            return;
        }

        if (response.paymentagent_transfer === 1) {
            PaymentAgentTransferUI.hideFirstForm();
            PaymentAgentTransferUI.showDone();
            PaymentAgentTransferUI.updateDoneView(Client.get('loginid'), req.transfer_to.toUpperCase(), req.amount, req.currency);
        }
    };

    const initConfirm = (req) => {
        const confirm_form_id = '#frm_confirm_transfer';

        FormManager.init(confirm_form_id, [
            { request_field: 'transfer_to', value: req.transfer_to },
            { request_field: 'amount',      value: req.amount },
        ].concat(common_request_fields));

        FormManager.handleSubmit({
            form_selector       : confirm_form_id,
            fnc_response_handler: responseHandler,
        });

        $('#back_transfer').off('click').click(() => {
            PaymentAgentTransferUI.showForm();
            PaymentAgentTransferUI.showNotes();
            PaymentAgentTransferUI.hideConfirmation();
            PaymentAgentTransferUI.hideDone();
            $form_error.setVisibility(0);
        });
    };

    return {
        onLoad,
    };
})();

module.exports = PaymentAgentTransfer;

@@ -2556,16 +2556,21 @@
 y), min:
+ pa ?
  pa.min_
@@ -2583,14 +2583,24 @@
 awal
+ : 10
 , max:
+ pa ?
  pa.
@@ -2614,16 +2614,23 @@
 hdrawal 
+: 2000 
 %7D%5D, %5B'cu

add default of 10, 2000