Ext.define("App.view.OrderChangeStatusWindow", {
    extend: "Ext.window.Window",
    alias: "widget.orderItemWindow",
    xtype: "orderChangeStatusWindow",
    title: App.localeRes("Change Status"),
    width: 250,
    modal: true,

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

    initComponent: function () {
        var me = this;
        var ids = me.params.ids;
        var havePayment = me.params.havePayment;
        var havePaymentOrderStr = me.params.havePaymentOrderStr;
        Ext.apply(me, {
            items: [
                {
                    xtype: "form",
                    padding: '16 16 0 16',
                    layout: 'vbox',
                    params: {},
                    callbackOnSave: function(){
                        me.close();
                    },
                    items:[
                        {
                            xtype: 'button',
                            text: "<b>"+App.localeRes('On Hand')+"</b>",
                            width: "100%",
                            margin: 12,
                            handler: function(button, event) {
                                if (ids.length == 0) return;
                                Ext.Ajax.request({
                                    url: App.url('/order/list/on-hand'),
                                    params: { ids: ids },
                                    success: function(response, opts) {
                                        App.showMsg('Status changed to On Hand');
                                        me.isReload = true;
                                        me.close();
                                    },
                                    failure: function(response, opts) {
                                        App.showError('Cannot Change Status to On Hand : ' + response.status);
                                        me.close();
                                    },
                                    scope: this
                                });
                            }
                        },
                        {
                            xtype: 'button',
                            text: "<b>"+App.localeRes('Complete')+"</b>",
                            ui: 'soft-green',
                            itemId: 'completeBtn',
                            width: "100%",
                            margin: 12,
                            handler: function(button, event) {
                                if (ids.length == 0) return;
                                Ext.Ajax.request({
                                    url: App.url('/order/list/complete'),
                                    params: { ids: ids },
                                    success: function(response, opts) {
                                        App.showMsg('Record Completed');
                                        me.isReload = true;
                                        me.close();
                                    },
                                    failure: function(response, opts) {
                                        App.showError('Cannot Complete : ' + response.status);
                                        me.close();
                                    },
                                    scope: this
                                });
                            }
                        },
                        {
                            xtype: 'button',
                            text: "<b>"+App.localeRes('Cancel')+"</b>",
                            ui: 'soft-red',
                            itemId: 'cancelBtn',
                            width: "100%",
                            margin: 12,
                            doCancel: function(ids) {
                                if (App.isGranted(['ORDER_CANCEL']) == true) {
                                    Ext.Ajax.request({
                                        url: App.url('/order/list/cancel'),
                                        params: { ids: ids },
                                        success: function(response, opts) {
                                            App.showMsg('Record Cancelled');
                                            me.isReload = true;
                                            me.close();
                                        },
                                        failure: function(response, opts) {
                                            App.showError('Cannot Cancel : ' + response.status);
                                            me.close();
                                        },
                                        scope: this
                                    });
                                } else {
                                    App.showError('You Have No Authority To Process This Action!', 'Authority Issue');
                                    me.close();
                                }
                            },
                            handler: function(button, event) {
                                if (ids.length == 0) return;
                                if (havePayment) {
                                    App.confirm(havePaymentOrderStr + ' have payment.', 'Do you want to Cancel record?', function(
                                        buttonId,
                                        text,
                                        opt
                                    ) {
                                        if (buttonId === 'yes') {
                                            button.doCancel(ids);
                                        }else{
                                            me.close();
                                        }
                                    });
                                } else {
                                    button.doCancel(ids);
                                }
                            }
                        },
                        {
                            xtype: 'button',
                            text: "<b>"+App.localeRes('Ignore')+"</b>",
                            ui: 'default-toolbar',
                            itemId: 'ignoreBtn',
                            width: "100%",
                            margin: 12,
                            doCancel: function(ids) {
                                if (App.isGranted(['ORDER_CANCEL']) == true) {
                                    Ext.Ajax.request({
                                        url: App.url('/order/list/ignore'),
                                        params: { ids: ids },
                                        success: function(response, opts) {
                                            App.showMsg('Record Ignored');
                                            me.isReload = true;
                                            me.close();
                                        },
                                        failure: function(response, opts) {
                                            App.showError('Cannot mark Ignore : ' + response.status);
                                            me.close();
                                        },
                                        scope: this
                                    });
                                } else {
                                    App.showError('You Have No Authority To Process This Action!', 'Authority Issue');
                                    me.close();
                                }
                            },
                            handler: function(button, event) {
                                if (ids.length == 0) return;
                                if (havePayment) {
                                    App.confirm(havePaymentOrderStr + ' have payment.', 'Do you want to Ignore record?', function(
                                        buttonId,
                                        text,
                                        opt
                                    ) {
                                        if (buttonId === 'yes') {
                                            button.doCancel(ids);
                                        }else{
                                            me.close();
                                        }
                                    });
                                } else {
                                    button.doCancel(ids);
                                }
                            }
                        }
                    ]
                },
            ],
            listeners: {
                show: function () {
                },
                close: function (window, opts) {
                    if (me.isReload&&me.callbackOnClose) {
                        me.callbackOnClose();
                    }
                }
            }
        });
        me.callParent(arguments);
    }
});
