/*!
 * Ext JS Library 3.1.1
 * Copyright(c) 2006-2010 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
Ext.ns('Pro.common.schedule');
Pro.common.schedule.task=0;
Pro.common.schedule.label='Work Breakdown Schedule';
Pro.common.schedule.Breakdown = function() {
    var tree = new Ext.ux.tree.TreeGrid({
    	id:'task-list-breakdown',
        width: 515,
        height: 275,
        renderTo: 'task-list',
        enableDD: true,
        columns:[{
            header: 'Task',
            dataIndex: 'task',
            width: 280
        },{
            header: 'Starts',
            width:80,
            dataIndex:'starts'
        },{
            header: 'Lead',
            width: 90,
            dataIndex: 'lead'
        },{
            header: 'Status',
            width: 55,
            dataIndex: 'status',
            style:'text-align:center;'
        }],
        tbar: new Ext.Toolbar({
            displayInfo: true,
            items:[
            {
                pressed: false,
                enableToggle:false,
                text: 'Delete Task',
                iconCls : 'icon-delete',
                listeners:{
                	click: function() 
                	{
                		var pNode = Ext.getCmp('task-list-breakdown').getNodeById(Pro.common.schedule.task).parentNode;
                		if (confirm('Are you sure you want to delete task "'+Pro.common.schedule.label+'"?')) {
                			var id = Ext.getCmp('task-list-breakdown').getNodeById(Pro.common.schedule.task);
                			pNode.removeChild(id);
                			sys.connect(null, 'common.js&runmethod=removetask&task='+Pro.common.schedule.task, 'nowrite');
                		}
                	}
                }
            },
            {
                pressed: false,
                enableToggle:true,
                text: 'Create Task',
                iconCls : 'icon-extjs-create',
                toggleHandler: function(btn, pressed){
                   //
                    if (pressed)
                  		Pro.common.schedule.TaskForm();
                	else {
                		Ext.getCmp('create-task-form').destroy();
                		sys.connect(null, 'common.schedule.edit&task='+Pro.common.schedule.task, 'task-add');
                	}
                }
            }]
        }),
        listeners: {
        	beforeclick: function(n,e) {
        		Pro.common.schedule.task = n.attributes.id;
        		Pro.common.schedule.label = n.attributes.task;
        		Pro.common.schedule.lead = n.attributes.lead;
        		Pro.common.schedule.leadid = n.attributes.leadid;
        		Pro.common.schedule.status = n.attributes.status.replace('%', '');
        		Pro.common.schedule.starts = n.attributes.starts;
        		Pro.common.schedule.ends = n.attributes.ends;
        		if ($('#ptask').length) {
        			Ext.getCmp('parent-task').setValue(n.attributes.task);
        			document.getElementById('ptask').value=n.id;
        		}
        		else {
        			// show details
        			sys.connect(null, 'common.schedule.edit&task='+n.id, 'task-add');
        		}
        	},
        	beforemovenode: function(t,node,oldparent,newparent,index) {
        		sys.connect(null, 'common.js&runmethod=movenode&node='+node.id+'&ptask='+newparent.id, 'nowrite');
        	}
        },
        loader: {
			clearOnLoad:true,
			dataUrl: 'data.php?class=common.schedule&feed=showTasks&pclass='+Pro.common.schedule.pclass+'&pid='+Pro.common.schedule.pid+'&ptask=0'
		}
    });
    
}

Pro.common.schedule.TaskForm = function() {
	document.getElementById('task-add').innerHTML='';
	var getPoc = new Ext.form.ComboBox({
		name: 'task-lead',
		id:'task-lead',
		hiddenName: 'tlead',
		fieldLabel: 'Task Lead',
		store: new Ext.data.JsonStore({
			url: 'data.php?class=json&feed=contacts',
			fields: ['id', 'label']
		}),
		valueField: 'id',
		displayField: 'label',
		queryParam: 'q',
		queryDelay: 50, 
		minChars:2,
		typeAhead: true,
		forceSelection:true,
		mode: 'remote',
		triggerAction: 'all',
		emptyText: 'Type contact name here...',
		selectOnFocus: true,
		anchor: '95%'
	});
	var getTasks = new Ext.form.ComboBox({
		name: 'parent-task',
		id:'parent-task',
		hiddenName: 'ptask',
		fieldLabel: 'Parent Task',
		store: new Ext.data.JsonStore({
			url: 'data.php?class=common.schedule&feed=getAllTasks&pclass='+Pro.common.schedule.pclass+'&pid='+Pro.common.schedule.pid+'&ptask=0',
			fields: ['task', 'label']
		}),
		hiddenValue:Pro.common.schedule.task,
		value:Pro.common.schedule.label,
		valueField: 'task',
		displayField: 'label',
		queryParam: 'q',
		queryDelay: 50, 
		minChars:2,
		typeAhead: false,
		forceSelection:true,
		mode: 'remote',
		triggerAction: 'all',
		emptyText: 'Type task name here...',
		selectOnFocus: true,
		anchor: '95%'
	});

	var simple = new Ext.FormPanel({
		width:515,
		id:'create-task-form',
		height:250,
		title:'Add New Task',
        labelWidth: 75,
        frame:true,
        bodyStyle:'padding:5px 5px 0',
        layout: 'form',
		items: [
		{
			xtype:'textfield',
			fieldLabel: 'Task Name',
			name: 'label',
			allowBlank:false,
			anchor:'95%'
        },getTasks,{
			xtype:'datefield',
			fieldLabel: 'Starts',
			name: 'starts',
			value:Pro.common.schedule.starts,
			allowBlank:false,
			anchor:'50%'
        },{
			xtype:'datefield',
			fieldLabel: 'Ends',
			name: 'ends',
			value:Pro.common.schedule.ends,
			allowBlank:false,
			anchor:'50%'
        },getPoc,{
			xtype: 'hidden',
			id: 'runmethod',
			name: 'runmethod',
			value:'addtask'
		},{
			xtype: 'hidden',
			id: 'pclass',
			name: 'pclass',
			value:Pro.common.schedule.pclass
		},{
			xtype: 'hidden',
			id: 'pid',
			name: 'pid',
			value:Pro.common.schedule.pid
		},{
			buttons: [{
				text: 'Save',
				handler: function(){
					if(simple.getForm().isValid()) {
						simple.getForm().submit({
							waitMsg:'Saving Data...',
							url: 'service.php',
							params: {request: 'common.js'},
							success: function(sObj,aObj) {
								// get the parent object
								var pNode = Ext.getCmp('task-list-breakdown').getNodeById(Pro.common.schedule.task);
								// create the child item
								var item = new Ext.tree.TreeNode({expanded:true, "leaf":false, "status":aObj.result.status, "id":aObj.result.id, "task":aObj.result.task, "lead":aObj.result.lead, "leadid":aObj.result.leadid, "starts":aObj.result.starts, "ends":aObj.result.ends});
								// append the child to the parent
								pNode.appendChild(item);
								// get the path
								var path = Ext.getCmp('task-list-breakdown').getNodeById(Pro.common.schedule.task).getPath();
								// expand it
				                Ext.getCmp('task-list-breakdown').expandPath(path);
							}
						})
					}
				}
			}]
		}]
	});
	simple.render('task-add');
}

Pro.common.schedule.TaskEdit = function() {
	var getPoc = new Ext.form.ComboBox({
		name: 'task-lead',
		id:'task-lead',
		hiddenName: 'tlead',
		hiddenValue:Pro.common.schedule.leadid,
		fieldLabel: 'Task Lead',
		store: new Ext.data.JsonStore({
			url: 'data.php?class=json&feed=contacts',
			fields: ['id', 'label']
		}),
		valueField: 'id',
		value:Pro.common.schedule.lead,
		displayField: 'label',
		queryParam: 'q',
		queryDelay: 50, 
		minChars:2,
		typeAhead: true,
		forceSelection:true,
		mode: 'remote',
		triggerAction: 'all',
		emptyText: 'Type contact name here...',
		selectOnFocus: true,
		anchor: '95%'
	});

	var simple = new Ext.FormPanel({
		width:515,
		id:'create-task-form',
		height:250,
		title:'Edit Task',
        labelWidth: 75,
        frame:true,
        bodyStyle:'padding:5px 5px 0',
        layout: 'form',
		items: [
		{
			xtype:'textfield',
			fieldLabel: 'Task Name',
			name: 'label',
			value:Pro.common.schedule.label,
			allowBlank:false,
			anchor:'95%'
        },{
			xtype:'datefield',
			fieldLabel: 'Starts',
			name: 'starts',
			value:Pro.common.schedule.starts,
			allowBlank:false,
			anchor:'50%'
        },{
			xtype:'datefield',
			fieldLabel: 'Ends',
			name: 'ends',
			value:Pro.common.schedule.ends,
			allowBlank:false,
			anchor:'50%'
        },{
            xtype: 'spinnerfield',
            fieldLabel: 'Status',
            name: 'status',
            value:Pro.common.schedule.status,
            minValue: 0,
            maxValue: 100,
            allowDecimals: true,
            decimalPrecision: 1,
            incrementValue: 10,
            alternateIncrementValue: 10,
            accelerate: true
        },getPoc,{
			xtype: 'hidden',
			id: 'runmethod',
			name: 'runmethod',
			value:'updatetask'
		},{
			xtype: 'hidden',
			id: 'task',
			name: 'task',
			value:Pro.common.schedule.task
		},{
			buttons: [{
				text: 'Update',
				handler: function(){
					if(simple.getForm().isValid()) {
						simple.getForm().submit({
							waitMsg:'Saving Data...',
							url: 'service.php',
							params: {request: 'common.js'},
							success: function(sObj,aObj) {
								var parent = Ext.getCmp('task-list-breakdown').getNodeById(Pro.common.schedule.task).parentNode;
								Ext.getCmp('task-list-breakdown').getNodeById(Pro.common.schedule.task).remove();
								var item = parent.appendChild(new Ext.tree.AsyncTreeNode({"id":aObj.result.id, "status":aObj.result.status, "ends":aObj.result.ends, "leadid": aObj.result.leadid, "task":aObj.result.task, "lead":aObj.result.lead, "starts":aObj.result.starts, "leaf":aObj.result.leaf}));
								// now select the item again
								parent.expand();
							}
						})
					}
				}
			}]
		}]
	});
	simple.render('updateform');
}

