define(['jquery', 'underscore', 'backbone', 'text!modules/addingr/addingrViewTemplate.html', 'modules/base/baseView',
    'jqxbuttons', 'jqxdata', 'jqxdropdownlist',
    'jqxinput', 'jqxrating', 'jqxnumberinput'],
        function($, _, Backbone, addingrViewTemplate, BaseView) {

            var AddIngrView = BaseView.extend({
                dataAdapter: undefined,
                //initialize template
                template: _.template(addingrViewTemplate),
                initialize: function() {
                    AddIngrView.__super__.initialize.call(this);
                    //this.header.title = "pippo";
                },
                openDlg: function()
                {
                    $("#ingrediente").jqxDropDownList('selectedIndex', -1);
                    $("#unitMis").jqxDropDownList('selectedIndex', -1);
                    $("#qty").jqxNumberInput('val', '');
                    $("#nota").val('');
                    $("#addIngrWindow").jqxWindow('open');
                },
                validate: function() {
                    if ($("#ingrediente").val() === "22" && $("#nota").val() !== "")
                        return true;
                    if ($("#ingrediente").val() > 0 &&
                            $("#ingrediente").val() !== "22" &&
                            $("#qty").val() > 0 &&
                            $("#unitMis").val() > 0)
                        return true;
                    return false;
                },
                addBtnFn: undefined,
                //render the content into div of view
                render: function() {
                    //this.header.title = "pippo";
                    AddIngrView.__super__.render.call(this);
                    var self = this;

                    this.$el.append(this.template());

                    this.$el.find("#addIngrWindow").jqxWindow({
                        width: 350,
                        height: 230,
                        resizable: false,
                        isModal: true,
                        autoOpen: false,
                        cancelButton: this.$el.find("#cancelIngrBtn"),
                        modalOpacity: 0.01,
                        initContent: function() {
                            var source =
                                    {
                                        datatype: "jsonp",
                                        datafields: [
                                            {name: 'tipo_ingredienti_id'},
                                            {name: 'name'}
                                        ],
                                        url: myManifest.Settings.DefaultURL + "/backend/typeingredients",
                                        data: {
                                        }
                                    };
                            var dataAdapter = new $.jqx.dataAdapter(source);
                            $("#ingrediente").jqxDropDownList(
                                    {
                                        width: 200,
                                        height: 25,
                                        source: dataAdapter,
                                        selectedIndex: -1,
                                        displayMember: "name",
                                        valueMember: "tipo_ingredienti_id"
                                    });
                            var sourceunitMis =
                                    {
                                        datatype: "jsonp",
                                        datafields: [
                                            {name: 'tipo_quantita_id'},
                                            {name: 'name'}
                                        ],
                                        url: myManifest.Settings.DefaultURL + "/backend/typeqtys",
                                        data: {
                                        }
                                    };
                            var dataAdapterunitMis = new $.jqx.dataAdapter(sourceunitMis);
                            $("#unitMis").jqxDropDownList(
                                    {
                                        width: 200,
                                        height: 25,
                                        source: dataAdapterunitMis,
                                        selectedIndex: -1,
                                        displayMember: "name",
                                        valueMember: "tipo_quantita_id"
                                    });

                            $("#ingrediente").on('select', {scope: self}, function(event) {
                                if (event.args) {
                                    var item = event.args.item;
                                    if (item) {
                                        if (item.value === "22")
                                        {
                                            $("#qty").jqxNumberInput({disabled: true});
                                            $("#unitMis").jqxDropDownList({disabled: true});
                                        }
                                        else
                                        {
                                            $("#qty").jqxNumberInput({disabled: false});
                                            $("#unitMis").jqxDropDownList({disabled: false});
                                        }
                                    }
                                }
                            });
                            $("#qty").jqxNumberInput({width: '250px', height: 25, decimal: 0.0, min: 0, decimalDigits: 0, digits: 4, groupSeparator: ''});
                            $("#nota").jqxInput({placeHolder: "Nota:", width: '250px', height: 45});

                            $("#addIngrBtn").jqxButton({width: '48%', height: 40});
                            $("#cancelIngrBtn").jqxButton({width: '48%', height: 40});
                            $("#addIngrBtn").on('click', function() {
                                if (self.validate())
                                {
                                    var newItem = {
                                        "id_tipo_ingredienti": $("#ingrediente").jqxDropDownList('getSelectedItem').value,
                                        "nome_ingrediente": $("#ingrediente").jqxDropDownList('getSelectedItem').label,
                                        "quantita": $("#qty").jqxNumberInput('val'),
                                        "id_tipo_quantita": null,
                                        "unita": null,
                                        "note": $("#nota").val()
                                    };
                                    
                                    if($("#unitMis").jqxDropDownList('getSelectedItem') !== null)
                                        newItem.unita = $("#unitMis").jqxDropDownList('getSelectedItem').label;
                                    
                                    if($("#unitMis").jqxDropDownList('getSelectedItem') !== null)
                                        newItem.id_tipo_quantita = $("#unitMis").jqxDropDownList('getSelectedItem').value;

                                    if (self.addBtnFn)
                                        self.addBtnFn(newItem);

                                    $('#addIngrWindow').jqxWindow('close');
                                }
                                else
                                    alert("Inserire correttamente i dati");
                            });
                        }
                    });
                    return this;
                }
            });
            return AddIngrView;
        });