Ext.define("App.view.InvoiceTemplateContainer", {
    extend: "Ext.form.Panel",
    alias: "widget.invoiceTemplateContainer",
    xtype: "invoiceTemplateContainer",
    width: "100%",
    minWidth: 800,
    height: '100%',
    minHeight: 650,
    layout: 'hbox',
    padding: 8,

    loadForm: function(){
        var me = this;

        var f = me.getForm();
        f.load({
            url: App.url("/setting/mail/load"),
            method: 'POST',
            params:{},
            success: function(form, action) {}
        });        
    },

    initComponent: function () {
        var me = this;
        Ext.apply(me, {
            items: [
                {
                    xtype: 'form',
                    layout: 'vbox',
                    height: '100%',
                    flex: 1,
                    items:[
                        {
                            xtype: 'fieldset',
                            title: App.localeRes('Mail Template'),
                            width: '100%',
                            flex:  1,
                            layout: 'vbox',
                            margin: '0 8 8 8',
                            items:[
                                {
                                    xtype: 'textfield',
                                    name: 'subject',
                                    width: '100%',
                                    margin: 8,
                                    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%',
                                    margin: 8,
                                    flex: 1,
                                },
                            ]
                        },
                    ],
                    tbar: [
                        {
                            xtype: 'button',
                            text: App.localeRes("SMTP Setting"),
                            ui: 'soft-blue',
                            margin: '0 0 16 8',
                            handler: function(){
                                App.loadModuleWin('settingWindow_SMTP', App.localeRes('SMTP Setting'), {
                                    params: {},
                                    callbackOnClose: function() {}
                                });
                            }
                        },
                        '->',
                        {
                            xtype: 'button',
                            text: App.localeRes("Save"),
                            margin: '0 0 16 0',
                            ui: 'soft-blue',
                            handler: function(button, event){
                                me.getForm().submit({
                                    method: "POST",
                                    waitMsg: "Uploading...",
                                    url: App.url("/setting/mail/save"),
                                    success: function(f, action) {
                                        App.showMsg(App.localeRes("Saved"));
                                        me.loadForm();
                                    },
                                    failure: function(f, action) {
                                        alert("Action Fail");
                                    }
                                });
                            }
                        },
                    ]
                },
                {
                    xtype: 'label',
                    height: '100%',
                    width: 200,
                    padding: 8,
                    html:"<dl><br/><br/><br/>"
                        +me.labelDt("Order")
                        +me.labelDd("Order No.", "orderNo")
                        +me.labelDd("Order Date", "orderDate")
                        +me.labelDd("Amount", "amount")
                        +me.labelDt("Customer")
                        +me.labelDd("First Name", "firstName")
                        +me.labelDd("Surname", "surname")
                        +me.labelDd("Chinese Name", "nameCh")
                        +me.labelDt("Other")
                        +me.labelDd("Today", "today")
                        +"</dl>",
                    
                }
            ]
        });
        me.callParent(arguments);
        me.loadForm();
    },
    labelDt: function(text){
        return "<br/><b><dt>"+ App.localeRes(text) +":</dt></b>";
    },
    labelDd: function(title, value){
        return "<dd><b>"+App.localeRes(title)+":</b></dd><dd>{$"+value+"}</dd>";
    }
});
