/**
 * window.c.UserBalance component
 * Render the current user total balance and request fund action
 *
 * Example:
 * m.component(c.UserBalance, {
 *     user_id: 123,
 * })
 */
import m from 'mithril';
import _ from 'underscore';
import models from '../models';
import h from '../h';
import modalBox from './modal-box';
import userBalanceRequestModalContent from './user-balance-request-modal-content';

const I18nScope = _.partial(h.i18nScope, 'users.balance');

const userBalance = {
    controller(args) {
        args.balanceManager.load();

        return {
            userBalances: args.balanceManager.collection,
            displayModal: h.toggleProp(false, true)
        };
    },
    view(ctrl, args) {
        const balance = _.first(ctrl.userBalances()) || { user_id: args.user_id, amount: 0},
            balanceRequestModalC = [
                userBalanceRequestModalContent,
                _.extend({}, { balance }, args)
            ];

        return m('.w-section.section.user-balance-section', [
            (ctrl.displayModal() ? m.component(modalBox, {
                displayModal: ctrl.displayModal,
                content: balanceRequestModalC
            }) : ''),
            m('.w-container', [
                m('.w-row', [
                    m('.w-col.w-col-8.u-text-center-small-only.u-marginbottom-20', [
                        m('.fontsize-larger', [
                            I18n.t('totals', I18nScope()),
                            m('span.text-success', `R$ ${h.formatNumber(balance.amount, 2, 3)}`)
                        ])
                    ]),
                    m('.w-col.w-col-4', [
                        m(`a[class="r-fund-btn w-button btn btn-medium u-marginbottom-10 ${(balance.amount <= 0 ? 'btn-inactive' : '')}"][href="javascript:void(0);"]`,
                          { onclick: (balance.amount > 0 ? ctrl.displayModal.toggle : 'javascript:void(0);') },
                          I18n.t('withdraw_cta', I18nScope()))
                    ])
                ])
            ])
        ]);
    }
};

export default userBalance;

@@ -227,16 +227,45 @@
 score';%0A
+import moment from 'moment';%0A
 import m
@@ -288,16 +288,16 @@
 odels';%0A
-
 import h
@@ -1638,32 +1638,60 @@
             m('.
+card.card-terciary.u-radius.
 w-col.w-col-4', 
@@ -1784,16 +1784,17 @@
 m-10 $%7B(
+(
 balance.
@@ -1804,16 +1804,42 @@
 unt %3C= 0
+ %7C%7C balance.in_period_yet)
  ? 'btn-
@@ -1913,17 +1913,52 @@
         
-%7B
+  %7B %0A                               
  onclick
@@ -1959,16 +1959,17 @@
 nclick: 
+(
 (balance
@@ -1979,16 +1979,89 @@
 ount %3E 0
+ && (_.isNull(balance.in_period_yet) %7C%7C balance.in_period_yet === false))
  ? ctrl.
@@ -2105,19 +2105,50 @@
 d(0);') 
+%0A                            
 %7D,%0A
+  
         
@@ -2200,17 +2200,856 @@
 Scope())
-)
+%0A                        ),%0A                        m('.fontsize-smaller.fontweight-semibold', %0A                            (balance.last_transfer_amount && balance.in_period_yet ? %0A                                I18n.t('last_withdraw_msg', I18nScope(%7B%0A                                    amount: %60R$ $%7Bh.formatNumber(balance.last_transfer_amount, 2, 3)%7D%60,%0A                                    date: moment(balance.last_transfer_created_at).format('MMMM')%0A                                %7D))%0A                                : I18n.t('no_withdraws_this_month', I18nScope(%7Bmonth_name: moment().format('MMMM')%7D)) ),%0A                        ),%0A                        m('.fontcolor-secondary.fontsize-smallest.lineheight-tight.u-marginbottom-10',%0A                            I18n.t('withdraw_limits_msg', I18nScope())%0A                        )%0A
 %0A       

Disable withdraw button when last transfer in period