Ext.define("App.view.MaterialItemWindow", {
    extend: "Ext.window.Window",
    alias: "widget.materialItemWindow",
    xtype: "materialItemWindow",
    title: App.localeRes("Item Details"),
    width: 600,
    modal: true,

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

    onSave: function(){
        var me = this,
        f = me.down('#detailForm');

        if (f.isValid()) {
            f.submit({
                url: App.url("/common/material/item/save"),
                method: 'POST',
                submitEmptyText: false,
                success: function (form, action) {
                    App.showMsg('Saved');
                    me.close();
                }
            });
        }
    },

    doLoad: function(){
        let me = this;
        me.down('#detailForm').load({
            url: App.url("/common/material/item/load"),
            method: "POST",
            params: {
                id: me.params.id
            },
            success: function(form, action) {
            }
        });
    },
    evil: function(fn) {
        return new Function('return ' + fn)();
    },
    initComponent: function () {
        var me = this;
        Ext.apply(me, {
            items: [
                {
                    xtype: "form",
                    itemId: 'detailForm',
                    padding: '16 16 0 16',
                    params: {},
                    callbackOnSave: function(){
                        me.isSave = true;
                        me.close();
                    },
                    bbar: {
                        defaultButtonUI: 'default',
                        layout: 'hbox',
                        padding: 0,
                        items: [
                            '->',
                            {
                                xtype: 'button',
                                text: App.localeRes('Save & Close'),
                                ui: "blue",
                                margin: '0 0 16 0',
                                formBind: true,
                                handler: function(button, event){
                                    me.onSave();
                                }
                            }
                        ]
                    },
                    defaults:{
                        width: '100%',
                    },
                    items:[
                        {
                            xtype: 'hidden',
                            itemId: 'itemId',
                            name: 'id',
                        },
                        {
                            xtype: 'hidden',
                            itemId: 'orderIdx',
                            name: 'orderIdx',
                        },
                        {
                            xtype: 'hidden',
                            itemId: 'bandId',
                            name: 'bandId',
                        },
                        {
                            xtype: 'textfield',
                            name: 'name',
                            fieldLabel: App.localeRes("Name"),
                            maxLength: 150,
                        },
                        {
                            xtype: 'textfield',
                            name: 'bunch',
                            fieldLabel: App.localeRes("Bunch"),
                            maxLength: 150,
                        },
                        {
                            xtype: 'textfield',
                            name: 'fabricNum',
                            fieldLabel: App.localeRes("Fabric No."),
                            maxLength: 150,
                        },
                        {
                            xtype: 'numberfield',
                            name: 'cost',
                            fieldLabel: App.localeRes("Cost"),
                            value: 0,
                            decimalPrecision:2,
                            listeners: {
                                change: function(field, newVal, oldVal) {
                                    if(!newVal) me.down("#expectedField").setValue("");
                                    if(me.params.formula){
                                        let formula = me.params.formula;
                                        formula = formula.replace("M", newVal);
                                        let v= me.evil(formula);
                                        me.down("#expectedField").setValue( Ext.util.Format.number(v, '0,000.00')); 
                                    }else me.down("#expectedField").setValue("");
                                }
                            }
                        },
                        {
                            xtype: 'numberfield',
                            name: 'suitPrice',
                            fieldLabel: App.localeRes("Suit"),
                            decimalPrecision:2
                        },
                        {
                            xtype: 'numberfield',
                            name: 'jacketPrice',
                            fieldLabel: App.localeRes("Jacket"),
                            decimalPrecision:2
                        },
                        {
                            xtype: 'numberfield',
                            name: 'pantsPrice',
                            fieldLabel: App.localeRes("Pants"),
                            decimalPrecision:2

                        },
                        {
                            xtype: 'numberfield',
                            name: 'overcoatPrice',
                            fieldLabel: App.localeRes("Overcoat"),
                            decimalPrecision:2

                        },
                        {
                            xtype: 'numberfield',
                            name: 'otherPrice',
                            fieldLabel: App.localeRes("Other"),
                            decimalPrecision:2
                        },
                        {
                            xtype: 'textfield',
                            itemId: 'expectedField',
                            readOnly: true,
                            fieldStyle: 'background-color: #ddd; background-image: none;',
                            fieldLabel: App.localeRes("Expected"),
                        },
                    ]
                },
               
            ],
            listeners: {
                show: function () {
                    if(me.params && me.params.id){
                        me.doLoad();
                    }else if(me.params && me.params.bandId){
                        me.down("#bandId").setValue(me.params.bandId);
                        me.down("#orderIdx").setValue(me.params.orderIdx);
                    }
                },
                close: function (window, opts) {
                    if (me.callbackOnClose) {
                        me.callbackOnClose(me.isSave);
                    }
                }
            }
        });
        me.callParent(arguments);
    }
});


