<?php 
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");

	
include_once(PATH_TO_ROOT."_class/class.Extjs_grid.php");

$tableName = $_POST['project'];
$projectId = $_POST['project_id'];
$export_select = $_POST['exportSelect'];
$tabId = $_POST['tabId'];
$dataTable = $export_select == 8 ? '_DatamapAscii' : '_DatamapBinary';
$id_language = $_POST['id_language'];
$id_lngDef = $_POST['id_lngDef'];

$grid = new MakeGrid($tableName.$dataTable);

	//lets see if there are records _DatamapAscii
	$sql = "SELECT * FROM $dataTable WHERE id_project = ".$projectId." LIMIT 1";
	$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
	if(!$res = $db->SQLexecute($sql))  {echo "alert('db erro!1111');".$db->GetError(); exit;}
	if($db->NumRows($res) == 0 )
	{
		include (PATH_TO_ROOT.'webcati/_ajax/ExImportData/ajax.dataMapSetup.php');
	}
$grid->setMainTable($dataTable,DB_WARPIT_WEBCATI_BASE);

$grid->addColumn('id');
$grid->addColumn('id_project');
$grid->addColumn('var_name','Variable')->width(150);
if($export_select==8)
{
$grid->addColumn('start_col','Start')->width(50)->editor('new Ext.form.NumberField()');
$grid->addColumn('length_col','Length')->width(50)->editor('new Ext.form.NumberField()');
}
else
{
    $grid->addColumn('var_value','Value')->width(50)->editor('new Ext.form.NumberField()');
	$grid->addColumn('bin_length','Length')->width(50)->editor('new Ext.form.NumberField()');
    $grid->addColumn('bin_card','Bin card')->width(50)->editor('new Ext.form.NumberField()');
	$grid->addColumn('bin_column','Start column')->width(50)->editor('new Ext.form.NumberField()');
    $grid->addColumn('var_rec_value','Rec value')->width(50)->editor('new Ext.form.NumberField()');
	//var_name;var_value;bin_length;bin_card;bin_column;var_rec_value
}

$grid->addColumn('sys_name','Name')->hidden(true);
$grid->setGrouping('sys_name');
$grid->setRemoteSort();
$grid->setSort('id');
$grid->reqUrl = 'webcati/_ajax/ExImportData/ajax.dataMap.php';
$grid->baseParams = "SQLwhere:'id_project = $projectId'";

$grid->addtBarItem("{text:'Redefine',handler:function(){
	new RMajax().request({
		url:'webcati/_ajax/ExImportData/ajax.dataMapSetup.php',
		params: {
			redefine:true,
			project:'$tableName',
			project_id:$projectId,
			exportSelect:$export_select,
			id_language: $id_language,id_lngDef:$id_lngDef
		},
		success:function(){
			$grid.getStore().reload();
		}
	});
}}");
$grid->addtBarItem("'->'");
$grid->addtBarItem("{text:'Import',iconCls: 'icon_import_database',handler:function(){
   
    var fp = new Ext.FormPanel({
        enctype:'multipart/form-data',
        fileUpload: true,
        width: 285,
        frame: false,
        autoHeight: true,
        title:'Import data map',
        bodyStyle: 'padding: 10px 10px 0 10px;',
        labelWidth: 50,
        defaults: {
            anchor: '90%',
            allowBlank: false,
            msgTarget: 'side'
        },
        uploadSuccess: function(fp, o){
                          var respStatus = Ext.decode(o.response.responseText);
                          $grid.getStore().reload();
                          win1.close();
                        },
        uploadFailure: function(fp,o){
                                if(o.response)
                                    Ext.Msg.alert('Error!', Ext.decode(o.response.responseText).error);
                                //win1.close();
                            },
        items: [{
             xtype: 'fileuploadfield',
            id: 'form-file',
            emptyText: 'Select file',
            fieldLabel: 'File',
            name: 'csvfilepath',
            buttonText: '',
            buttonCfg: {
                iconCls: 'upload-icon'
            }
        }],
        buttons: [{
            text: 'Import',
            handler: function(){
              
                    fp.getForm().submit({
                        url: 'webcati/_ajax/ExImportData/ajax.dataMapImport.php',
                        method : 'post',
                        params:{
                            action: 'import',
                            type: 'binary',
                            project:'$tableName',
                            project_id:$projectId,
                            exportSelect: '" . $export_select . "'
                        },
                        waitMsg: 'Importing data map...',
                        success: fp.uploadSuccess,
                            failure:fp.uploadFailure
                    });
              
            }
        },{
            text: 'Reset',
            handler: function(){
                fp.getForm().reset();
            }
        }]
    });
    
    var win1 = new Ext.Window({
        modal:true,
        //layout:'tabpanel',
        title: 'Import',
        width:450,
        closeAction:'close',
        plain: true,
        items: { xtype:'tabpanel', height:130,activeTab:0,
        items: [fp]}

    }).show();
}}");
$grid->addExportButton();

echo $grid->createGrid();


echo "var p = Ext.getCmp('$tabId');
		p.layout.east.panel.add($grid );
		p.layout.east.panel.setWidth(330);
		p.layout.east.panel.layout.setActiveItem($grid.id);
		p.doLayout();";

?>