define(['jquery', 'underscore', 'backbone', 'text!modules/mulini/muliniViewTemplate.html', 'modules/base/baseView',
    'controls/window/window', 'modules/editmulino/editmulino', 'jqx'],
        function($, _, Backbone, muliniViewTemplate, BaseView, Window, EditMulino) {

            var MuliniView = BaseView.extend({
                dataAdapter: undefined,
                //initialize template
                template: _.template(muliniViewTemplate),
                window:null,
                editorMulino: null,
                
                initialize: function() {
                    MuliniView.__super__.initialize.call(this);
                    //this.header.title = "pippo";
                },
                openMulino: function(row) {
                    var editrow = row;
                    var dataRecord = $("#jqxgrid").jqxGrid('getrowdata', editrow);
                    this.editorMulino.openMulino(dataRecord.mulino_id);
                },
                //render the content into div of view
                render: function() {
                    //this.header.title = "pippo";
                    MuliniView.__super__.render.call(this);
                    var self = this;

                    if(self.window === null)
                    {
                        self.window = new Window({
                            id: "mulini",
                            title: "Mulini",
                            width: "850px",
                            resizable: true,
                            height: "510px",
                            maxHeight: 1000,
                            maxWidth: 1000,
                            content: "<p><div id=\"jqxgrid\"></div></p>",
                            initContentFn: function() {
                                // prepare the data
                                self.dataAdapter = new $.jqx.dataAdapter({
                                    datatype: "jsonp",
                                    datafields: [
                                        {name: 'mulino_id', type: 'int'},
                                        {name: 'nome', type: 'string'},
                                        {name: 'regione', type: 'string'}
                                    ],
                                    id: 'mulino_id',
                                    url: myManifest.Settings.DefaultURL + "/backend/mulini"
                                });
                                $("#jqxgrid").jqxGrid({
                                    width: "100%",
                                    height: "95%",
                                    source: self.dataAdapter,
                                    filterable: true,
                                    autoshowfiltericon: true,
                                    columns: [
                                        {text: 'Titolo', dataField: 'titolo', width: "50%",
                                            cellsrenderer: function(row, columnfield, value, defaulthtml, columnproperties) {
                                                return "<div style='overflow: hidden; text-overflow: ellipsis; padding-bottom: 2px; text-align: left; margin-right: 2px; margin-left: 4px; margin-top: 4px;'>" + unescape(value ) + "</div>";
                                            }
                                        },
                                        {text: 'Autore', dataField: 'autore', width: "30%",
                                            cellsrenderer: function(row, columnfield, value, defaulthtml, columnproperties) {
                                                return "<div style='overflow: hidden; text-overflow: ellipsis; padding-bottom: 2px; text-align: left; margin-right: 2px; margin-left: 4px; margin-top: 4px;'>" + unescape(value ) + "</div>";
                                            }
                                        },
                                        {text: '', datafield: 'Edit', columntype: 'button', width: '10%', filterable: false, sortable: false, menu: false, 
                                            cellsrenderer: function() {
                                                return "Modifica";
                                            },
                                            buttonclick: function(row) {
                                                self.openMulino(row);
                                            }
                                        },
                                        {text: '', datafield: 'Del', columntype: 'button', width: '10%', filterable: false, sortable: false, menu: false, 
                                            cellsrenderer: function() {
                                                return "Elimina";
                                            },
                                            buttonclick: function(row) {
                                                var items = $("#jqxgrid").jqxGrid('source');
                                                var item = items.records[row];
                                                $.ajax({
                                                    url: myManifest.Settings.DefaultURL + "/backend/mulino/" + item.mulino_id,
                                                    //dataType: "json",
                                                    type: 'DELETE'
                                                }).then(function (response) {
                                                    var msg = response;
                                                    alert(msg.message);
                                                    $("#jqxgrid").jqxGrid('updatebounddata');
                                                });
                                            }
                                        }
                                    ]
                                });

                                $('#jqxgrid').on('rowdoubleclick', function(event)
                                {
                                    var args = event.args;
                                    var row = args.rowindex;
                                    self.openMulino(row);
                                });
                            }
                        });

                        this.$el.append(this.template());
                        this.$el.find("#content").append(self.window.$el);
                        self.window.render();
                        
                        this.editorMulino = new EditMulino();
                        this.$el.find("#content").append(this.editorMulino.$el);
                        
                        this.editorMulino.render();
                    }
                    else
                        self.window.open();
                    return this;
                }
            });
            return MuliniView;
        });