Ext.define("App.view.InvoiceWindow", {
    extend: "Ext.window.Window",
    alias: "widget.invoiceWindow",
    xtype: "invoiceWindow",
    title: App.localeRes("Invoice"),
    width: 1000,
    height: '85%',
    modal: true,

    resizable: true,
    layout: "fit",
    constrainHeader: true,
    isReload: false,

    doLoad: function(){
        var me = this;
        Ext.Ajax.request({
            url: App.url('/order/invoice/get-cust-contact'),
            method: 'POST',
            params: { orderId: me.params.orderId},
            success: function(response, opts){
                var o = Ext.decode(response.responseText);
                if (o.data) {
                   me.down("#labelfield").setHtml(
                    App.localeRes('Customer')+" : "+o.data.custName+"<br/>"
                    + App.localeRes('Phone')+" : "+o.data.phone+"<br/>"
                    + App.localeRes('Email')+" : "+o.data.email+"<br/>"
                   );
                   me.down('[name=subject]').setValue(o.data.subject);
                   me.down('[name=body]').setValue(o.data.body);
                }
            },
            failure: function(response, opts){
                App.showError('Action failed!');
            }
        });
    },

    doLoadCust: function(){
        var me = this;
        Ext.Ajax.request({
            url: App.url('/order/invoice/get-cust-contact'),
            method: 'POST',
            params: { orderId: me.params.orderId},
            success: function(response, opts){
                var o = Ext.decode(response.responseText);
                if (o.data) {
                   me.down("#labelfield").setHtml(
                    App.localeRes('Customer')+" : "+o.data.custName+"<br/>"
                    + App.localeRes('Phone')+" : "+o.data.phone+"<br/>"
                    + App.localeRes('Email')+" : "+o.data.email+"<br/>"
                   );
                }
            },
            failure: function(response, opts){
                App.showError('Action failed!');
            }
        });
    },

    initComponent: function () {
        var me = this;
        Ext.apply(me, {
            items: [
                {
                    xtype: "form",
                    padding: '16 16 0 16',
                    layout: 'vbox',
                    params: {},
                    callbackOnSave: function(){
                        me.close();
                    },
                    tbar:[
                        {
                            xtype: 'label',
                            itemId: 'labelfield',
                            margin: 8,
                        },
                        '->',
                        {
                            xtype: 'button',
                            text: "<i class='far fa-file-pdf fa-2x'></i> "+App.localeRes('Preview')+" PDF",
                            ui: 'soft-purple',
                            margin: 8,
                            handler: function(button, event) {
                                var str = '?orderId='+me.params.orderId;
                                window.open( App.url("/order/invoice/invoice.pdf")+str);
                            }
                        },
                    ],
                    items:[
                        {
                            xtype: 'button',
                            text: "<i class='fas fa-user-edit'></i> "+App.localeRes('Customer Details')+"",
                            margin: '0 0 8 0',
                            handler: function(button, event) {
                                App.loadModuleWin('customerWindow', App.localeRes('Customer'), {
                                    params: { id: me.params.custId },
                                    callbackOnClose: function() {
                                        me.doLoadCust();
                                    }
                                });
                            }
                        },
                        {
                            xtype: 'textfield',
                            name: 'subject',
                            width: '100%',
                            maxLength: 500,
                            emptyText: App.localeRes('Subject'),
                            listeners: {
                                change: function(editor, newValue, oldValue) {
                                    if (newValue.length > editor.maxLength) {
                                        editor.setValue(oldValue);
                                        App.showWarning(App.localeRes('Invalid Input: Over %s character limit.', editor.maxLength));
                                    }
                                }
                            }
                        },
                        {
                            xtype: 'htmleditor',
                            name: 'body',
                            width: '100%',
                            flex: 1,
                        },
                        {
                            xtype: 'button',
                            text: "<i class='far fa-paper-plane fa-1x'></i> <b>"+App.localeRes('Send Email')+"</b>",
                            itemId: 'completeBtn',
                            width: "100%",
                            margin: 12,
                            handler: function(button, event) {
                                Ext.Ajax.request({
                                    url: App.url('/order/invoice/send'),
                                    method: 'POST',
                                    params: { 
                                        orderId: me.params.orderId,
                                        subject: me.down('[name=subject]').getValue(),
                                        body: me.down('[name=body]').getValue(),
                                    },
                                    success: function(response, opts){
                                        var o = Ext.decode(response.responseText);
                                        if (o.success) {
                                            me.close();
                                            App.showMsg('Success');
                                        }
                                    },
                                    failure: function(response, opts){
                                        App.showError('Action failed!');
                                    }
                                });
                            }
                        },
                    ]
                },
            ],
            listeners: {
                show: function () {
                   me.doLoad();
                },
                close: function (window, opts) {
                    if (me.isReload&&me.callbackOnClose) {
                        me.callbackOnClose();
                    }
                }
            }
        });
        me.callParent(arguments);
    }
});
