var GridQuestion;
var WindowQuestion;
var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';



function show_LogistMobile_message_Window()
{
	var idrow = null;
	
	if(!WindowQuestion){
		
Ext.define('ModelQuestion', {
  extend:'Ext.data.Model',
  fields: ['id','message','id_sender','id_recipient','time']
});
	
var StoreQuestion = new Ext.data.Store({
  model: 'ModelQuestion',
  groupField: 'zone',
  remoteSort: true,
  proxy: {
    type: 'ajax',
    url: 'app/LogistMobile_message.php?func=show',
    reader: {
      type: 'json',
      root: 'rows'
    },
	simpleSortMode: true
  },
  sorters: [{
                property: 'id',
                direction: 'ASC'
        }]
});

Ext.define('ModelTypeQuestion', {
    extend: 'Ext.data.Model',
    fields: [ 'id','name' ]
});

var StoreTypeQuestion = new Ext.data.Store({
        model: 'ModelTypeQuestion',
           proxy: {
              type: 'ajax',
              url: 'app/LogistMobile_message.php?func=showUser',
              reader: {
                  type: 'json',
                  root: 'rows'
              }
          }
});




StoreQuestion.load();
StoreTypeQuestion.load();

		
var GridQuestion = Ext.create('Ext.grid.Panel', {
  selType: 'rowmodel',
  store: StoreQuestion,
  id:'gridFormQuestionId',
  columnLines: true,
  frame: false,
  border: false,
  features: [{
	  	ftype: 'groupingsummary',
	  	groupHeaderTpl: '{name}',
	  	hideGroupedHeader: false,
		startCollapsed: false,
	  	enableGroupingMenu: false
  }],
  columns:[
    {xtype: 'checkcolumn', dataIndex: 'indoor', width: 35, hideable: false, sortable: false},
	{xtype: 'rownumberer', width: 20},
    {text: "Сообщение", flex: 3, sortable: false, hideable: false, dataIndex: 'message'},
    {text: "Отправитель", width: 100, sortable: false, hideable: false, dataIndex: 'id_sender'},
	{text: "Получатель", width: 100, sortable: false, hideable: false, dataIndex: 'id_recipient'},
	{text: "Время", width: 100, sortable: false, hideable: false, dataIndex: 'time'}
  ],
  listeners:
  {
    itemclick: function (View,record,item,index,e,eOpts)
    {
     
    }
  },
  tbar:[
		{
            text: 'Удалить',
			iconCls: 'delete',
            handler: function(){
							var arrayId=[];																	
							StoreQuestion.each(function(record){
								if(record.get('indoor') == true){
										arrayId.push(record.get('id'));
								}
							});
							
							if (arrayId.length==0){
								Ext.Msg.alert('Ошибка', 'Выберите строку(и) для удаления');
							} else {
								var myMask = new Ext.LoadMask(GridQuestion, {msg: 'Обработка...'});
								myMask.show();
								
								Ext.MessageBox.confirm('Удаление', 'Вы действительно хотите удалить строку(и)?',  function(btn){
								if(btn=='yes'){
								
											Ext.Ajax.request({
                                            url: 'app/LogistMobile_message.php',
                                            method: 'POST',
                                            params: { 'func':'delete', "id[]":arrayId },
                                            success: function(response) {
												var obj = Ext.decode(response.responseText);
												if(obj.success == true){
													myMask.hide();
													StoreQuestion.load();
												}else{
													Ext.MessageBox.alert('Ошибка', obj.message);
												}
                                            },
                                            failure: function(response) {
                                                Ext.MessageBox.alert('Ошибка', response.responseText);
                                            }
                                        });
										
								}else{
									//row.set('indoor') == false;
									myMask.hide();
								}
								});
								
								//$.post("app/LogistMobile_WarehouseToUsers.php",{'DeleteWarehouse':true, 'arrayWarehouseId[]':arrayWarehouseId}, function(data){
								//myMask.hide();
								//store_grid_warehouse_users.load();
								//});
								
							}
            }
        }
	]
});
		
         
		 WindowQuestion = new Ext.Window({
                        width: 800,
                        height: 550,
						title: LogistMobile.message,
                        closeAction : 'destroy',
                        //layout:'fit',
						layout:'border',
                        resizable: true,
                        modal:true,
                        //bodyStyle:{'background-color': '#FFFFFF'},
						listeners: {
							destroy : function(){
								WindowQuestion = null;						
							}
						},
                        items:[
				{
                 region:'center',
                 //minHeight: 200,
				 width: '100%',
                 autoScroll: true,
				 autoHeight: true,
				 layout:'anchor',
				 frame:false,
				 border:false,
				 dockedItems: [{
						xtype: 'toolbar',
						dock: 'bottom',
						items: [
						'->',{
						text: 'Отправить',
						iconCls: 'add',
						handler: function(){
							
							var form = Ext.getCmp('form_warehouse_users').getForm();
							if(form.isValid()){
								form.submit({
                                    waitMsg: 'Сохранение...',
                                    url: 'app/LogistMobile_message.php',
                                    wait: true,
                                    scope: this,
                                    method: 'POST',
                                    params: {'addedit':true,idrow:idrow},
                                    success: function(fp, o){
									if(o.result.success == true){
										StoreQuestion.load();
										GridQuestion.getView().refresh();
									}else{
										Ext.MessageBox.alert('Ошибка', o.result.message);
									}
                                    },
                                    failure: function(fp, o){
										Ext.MessageBox.alert('Ошибка', o.result.message);
									}
								});
							}else{
								Ext.MessageBox.alert('Ошибка', 'Заполните обязательные поля');
							}
							
						}
						}
						],
						}],
                 items:[
					{
					  xtype: 'form',
                      bodyStyle: {padding: '5px'},
					  id: 'form_warehouse_users',
					  border:false,
					  layout:'anchor',
					  defaults: {
						labelAlign: 'top'
						//labelWidth: 90,
						//msgTarget: 'side'
					  },
                      items:[
			{
						xtype: 'textareafield',
						grow: true,
						name: 'message',
						fieldLabel: 'Сообщение',
						allowBlank: false,
						afterLabelTextTpl: required,
						anchor: '100%'
			},		
			
			{
            xtype: 'container',
			anchor: '100%',
            layout:'column',
            items:[
			{
                xtype: 'container',
                columnWidth:.5,
                layout: 'anchor',
				defaults: {
						labelAlign: 'top'
						//labelWidth: 90,
						//msgTarget: 'side'
				},
                items: [
						
						{
						xtype: 'combobox',
						fieldLabel: 'Получатель',
						store: StoreTypeQuestion,
						allowBlank: false,
						//labelWidth: 100,
						editable: false,
						valueField: 'id',
						displayField: 'name',
						name: 'id_recipient',
						queryMode: 'local',
						selectOnFocus:true,
						anchor: '96%',
						afterLabelTextTpl: required,
						listeners: {
							select: function( field, value, eOpts ) {
							}
						}
						},
						
						
						
				]
            },{
                xtype: 'container',
                columnWidth:.5,
                layout: 'anchor',
				defaults: {
						labelAlign: 'top'
						//labelWidth: 90,
						//msgTarget: 'side'
				},
                items: [
						
						
				]
            }]
			}
						
						
						
						
		
					  ],
						
						/*buttons:[
						{
                                text: 'Сохранить',
                                handler: function() {
									
								}
						}
						]*/
					}
				 ]
				},
				{
				region: 'south',
				height: '50%',
				split: true,
				collapsible: false,
				floatable: false,
				closeAction: 'hide',
				minHeight: 300,
				autoScroll: true,
				frame:true,
				title: '',
				align: 'stretch',
				layout: {
					type: 'hbox',
					align: 'stretch'
				},
				defaults: { flex : 1 },
				items:[
					GridQuestion
				]
				}
						]
		 })      
     } 
    WindowQuestion.show();
	
}