[%# 1.0@bugzilla.org %]
[%# The contents of this file are subject to the Mozilla Public
  # License Version 1.1 (the "License"); you may not use this file
  # except in compliance with the License. You may obtain a copy of
  # the License at http://www.mozilla.org/MPL/
  #
  # Software distributed under the License is distributed on an "AS
  # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  # implied. See the License for the specific language governing
  # rights and limitations under the License.
  #
  # The Original Code is the Bugzilla Test Runner System.
  #
  # The Initial Developer of the Original Code is Maciej Maczynski.
  # Portions created by Maciej Maczynski are Copyright (C) 2001
  # Maciej Maczynski. All Rights Reserved.
  # 
  # Contributor(s): Ed Fuentetaja <efuentetaja@acm.org> 
  #                 Greg Hendricks <ghendricks@novell.com>
  #%]

[%# INTERFACE:
  # ...
  #%]
  
[% PROCESS global/variables.none.tmpl %]
[% PROCESS testopia/blocks.html.tmpl %]
[% PROCESS testopia/style.none.tmpl %]

[% javascript_urls.push("extensions/testopia/extjs/examples/ux/Portal.js") %]
[% javascript_urls.push("extensions/testopia/extjs/examples/ux/PortalColumn.js") %]
[% javascript_urls.push("extensions/testopia/extjs/examples/ux/Portlet.js") %]

[% IF NOT plan.isactive %]
    [% title = "Test Plan $plan.id: $plan.name (ARCHIVED)" FILTER html %]
[% ELSE %]
    [% title = "Test Plan $plan.id: $plan.name" FILTER html %]
[% END %]

[% PROCESS global/header.html.tmpl %]

<script type="text/javascript">
    var product_id;
    var plan = [% plan.TO_JSON FILTER html_light %];
    
    Ext.onReady(function(){
        Ext.QuickTips.init();
        var testopia_form = new Ext.form.BasicForm('testopia_helper_frm',{});
        
        var confirmDelete = function(){
            Ext.Msg.show({
               title:'Confirm Delete?',
               msg: 'You are about to delete plan ' + plan.plan_id + ' with all of its children and history. Are you sure you want to continue?',
               buttons: Ext.Msg.YESNO,
               fn: planDelete,
               animEl: 'plan-delete-btn',
               icon: Ext.MessageBox.QUESTION
            });
        };
        var planDelete = function(btn){
            if (btn == 'yes'){
                testopia_form.submit({
                    url: 'tr_process_plan.cgi',
                    params: {plan_id: plan.plan_id, action:'delete'},
                    success: function(data){
                        Ext.Msg.show({
                            title:'Test Plan Deleted',
                            msg: "Test Plan " + plan.plan_id + " was deleted",
                            buttons: Ext.Msg.OK,
                            icon: Ext.MessageBox.INFO
                        });
                        window.location = 'tr_show_product.cgi?product_id=' + plan.product_id;
                    },
                    failure: Testopia.Util.error
                });
            }
        };
        var cgrid = new Testopia.TestCase.Grid({plan_id: plan.plan_id },{id: 'plan_case_grid'});
        var rgrid = new Testopia.TestRun.Grid({plan_id: plan.plan_id },{id: 'plan_run_grid'});
        var tgrid = new Testopia.Tags.ObjectTags('plan', plan.plan_id);
        var attachGrid = new Testopia.Attachment.Grid(plan);
        
        var fileMenu = new Ext.menu.Menu({
            id: 'plan-file-menu',
            items: [{
                text: "Archived",
                id: 'plan_archive_mnu',
                ignoreParentClicks: true,
                checked: [% plan.isactive ? 'false' : 'true' %],
                checkHandler: function(i,c){
                    testopia_form.submit({
                        url: 'tr_process_plan.cgi',
                        params: {action: c ? 'archive':'unarchive' , plan_id: plan.plan_id},
                        success: function(){
                            Testopia.Util.notify.msg('Plan updated', 'Plan {0} was updated successfully', 'archival');
                            if (c){
                                Ext.get('archive_flag').addClass('plan_archived');
                            }
                            else {
                                Ext.get('archive_flag').removeClass('plan_archived');
                            }
                        },
                        failure: Testopia.Util.error
                    });
                }
            }
            [% IF user.in_group('Testers') %]
            ,{
                text: 'Clone',
                icon: 'extensions/testopia/img/copy.png',
                iconCls: 'img_button_16x',
                handler: function(){
                    Testopia.TestPlan.ClonePopup(plan);
                }
            }
            [% END %]
            ,{
                text: "Print",
                handler: function(){
                    window.location='tr_show_plan.cgi?&plan_id=[% plan.id FILTER none %]&ctype=print';
                }
            },{
                text: 'Export',
                menu: [{
                    text: 'Test Results as CSV',
                    handler: function(){
                        window.location = 'tr_list_caseruns.cgi?ctype=csv&viewall=1&plan_id=[% plan.id FILTER none %]';
                    }
                },{
                    text: 'Cases as CSV',
                    handler: function(){
                        window.location='tr_show_plan.cgi?&plan_id=[% plan.id FILTER none %]&export=testcases&ctype=csv&viewall=1';
                    }
                },{
                    text: 'Plan as XML',
                    handler: function(){
                        window.location='tr_show_plan.cgi?&plan_id=[% plan.id FILTER none %]&export=testcases&ctype=xml&viewall=1';             
                    }
                }]
            },{
                text: 'Import',
                menu:[{
                    text: 'Cases',
                    handler: function(){
                        Testopia.TestPlan.Import({
                            plan_id: plan.plan_id
                        });
                    }
                }]
            },{
                text: 'New',
                menu: [{
                    text: 'Test Run',
                    handler: function(){
                        Testopia.TestRun.NewRunPopup(plan);
                    }
                },{
                    text: 'Test Case',
                    handler: function(){
                        Testopia.TestCase.NewCasePopup(plan.plan_id, plan.product_id);
                    }
                }]
            },{
                text: "Delete",
                id: 'btn-delete',
                icon: 'extensions/testopia/img/delete.png',
                iconCls: 'img_button_16x',
                disabled: plan.candelete ? false : true,
                handler: confirmDelete
            }]
        });
        var editMenu = new Ext.menu.Menu({
            id: 'plan-edit-menu',
            items: [{
                text: "Access Controls",
                handler: function(){
                    Ext.getCmp('object_panel').activate('plan-permissions');
                }
            }]
        });
        var viewMenu = new Ext.menu.Menu({
            id: 'view-menu',
            items: [{
                text: "Reports",
                menu: [{
                    text: 'New Status Report',
                    handler: function(){
                        Ext.getCmp('object_panel').setActiveTab('dashboardpanel');
                        
                        var newPortlet = new Ext.ux.Portlet({
                            title: 'Status Report',
                            closable: true,
                            autoScroll: true,
                            tools: PortalTools
                        });
                        newPortlet.url = 'tr_run_reports.cgi?type=status&plan_ids=' + plan.plan_id;
                        Testopia.Search.dashboard_urls.push(newPortlet.url);
                        Ext.getCmp('dashboard_leftcol').add(newPortlet);
                        Ext.getCmp('dashboard_leftcol').doLayout();
                        newPortlet.load({
                            url: newPortlet.url
                        });
                    }
                }, {
                    text: 'New Completion Report',
                    handler: function(){
                        Ext.getCmp('object_panel').setActiveTab('dashboardpanel');
                        
                        var newPortlet = new Ext.ux.Portlet({
                            title: 'Completion Report',
                            closable: true,
                            autoScroll: true,
                            tools: PortalTools
                        });
                        newPortlet.url = 'tr_run_reports.cgi?type=completion&plan_ids=' + plan.plan_id;
                        Testopia.Search.dashboard_urls.push(newPortlet.url);
                        Ext.getCmp('dashboard_leftcol').add(newPortlet);
                        Ext.getCmp('dashboard_leftcol').doLayout();
                        newPortlet.load({
                            url: newPortlet.url
                        });
                    }
                }, {
                    text: 'New Run Execution Report',
                    handler: function(){
                        var win = new Ext.Window({
                            title: 'Select a date range',
                            id: 'plan_execution_win',
                            layout: 'fit',
                            split: true,
                            plain: true,
                            shadow: false,
                            width: 350,
                            height: 150,
                            items: [new Ext.FormPanel({
                                labelWidth: '40',
                                bodyStyle: 'padding: 5px',
                                items: [{
                                    xtype: 'datefield',
                                    id: 'execution_start_date',
                                    fieldLabel: 'Start Date',
                                    name: 'chfieldfrom'
                                }, {
                                    xtype: 'datefield',
                                    fieldLabel: 'Stop Date',
                                    id: 'execution_stop_date',
                                    emptyText: 'Now',
                                    name: 'chfieldto'
                                }]
                            })],
                            buttons: [{
                                text: 'Submit',
                                handler: function(){
                                    Ext.getCmp('object_panel').setActiveTab('dashboardpanel');
                                    
                                    var newPortlet = new Ext.ux.Portlet({
                                        title: 'Execution Report',
                                        closable: true,
                                        autoScroll: true,
                                        tools: PortalTools
                                    });
                                    newPortlet.url = 'tr_run_reports.cgi?type=execution&plan_ids=' + plan.plan_id + '&chfieldfrom=' + Ext.getCmp('execution_start_date').getValue() + '&chfieldto=' + Ext.getCmp('execution_stop_date').getValue();
                                    Testopia.Search.dashboard_urls.push(newPortlet.url);
                                    Ext.getCmp('dashboard_leftcol').add(newPortlet);
                                    Ext.getCmp('dashboard_leftcol').doLayout();
                                    newPortlet.load({
                                        url: newPortlet.url
                                    });
                                    win.close();
                                }
                            }, {
                                text: 'Cancel',
                                handler: function(){
                                    win.close();
                                }
                            }]
                        });
                        win.show();
                    }
                }, {
                    text: 'New Priority Breakdown Report',
                    handler: function(){
                        Ext.getCmp('object_panel').setActiveTab('dashboardpanel');
                        
                        var newPortlet = new Ext.ux.Portlet({
                            title: 'Status Report',
                            closable: true,
                            autoScroll: true,
                            tools: PortalTools
                        });
                        newPortlet.url = 'tr_run_reports.cgi?type=priority&plan_ids=' + plan.plan_id;
                        Testopia.Search.dashboard_urls.push(newPortlet.url);
                        Ext.getCmp('dashboard_leftcol').add(newPortlet);
                        Ext.getCmp('dashboard_leftcol').doLayout();
                        newPortlet.load({
                            url: newPortlet.url
                        });
                    }
                }, {
                    text: 'New Bug Report',
                    handler: function(){
                        Ext.getCmp('object_panel').setActiveTab('dashboardpanel');
                        var newPortlet = new Ext.ux.Portlet({
                            title: 'Bug Report',
                            closable: true,
                            autoScroll: true,
                            tools: PortalTools
                        });
                        newPortlet.url = 'tr_run_reports.cgi?type=bug_grid&plan_ids=' + plan.plan_id + '&noheader=1';
                        Testopia.Search.dashboard_urls.push(newPortlet.url);
                        Ext.getCmp('dashboard_leftcol').add(newPortlet);
                        Ext.getCmp('dashboard_leftcol').doLayout();
                        newPortlet.load({
                            scripts: true,
                            url: newPortlet.url
                        });
                    }
                }, {
                    text: 'Worst Offender Report',
                    handler: function(){
                        Ext.getCmp('object_panel').setActiveTab('dashboardpanel');
                        
                        var newPortlet = new Ext.ux.Portlet({
                            title: 'Worst Offender Report',
                            closable: true,
                            autoScroll: true,
                            tools: PortalTools
                        });
                        newPortlet.url = 'tr_run_reports.cgi?type=worst&plan_ids=' + plan.plan_id + '&noheader=1';
                        Testopia.Search.dashboard_urls.push(newPortlet.url);
                        Ext.getCmp('dashboard_leftcol').add(newPortlet);
                        Ext.getCmp('dashboard_leftcol').doLayout();
                        newPortlet.load({
                            scripts: true,
                            url: newPortlet.url
                        });
                        
                    }
                },{
                    text: 'Missing Cases Report',
                    handler: function(){
                        window.open('tr_list_cases.cgi?report_type=missing&plan_ids=' + plan.plan_id);
                    }
                }, {
                    text: 'Case Roll-up Report',
                    handler: function(){
                        window.open('tr_list_caseruns.cgi?report_type=rollup&plan_ids=' + plan.plan_id);
                    }
                }]
            }]
        });
    
        var planpanel = new Ext.Panel({
            layout: 'border',
            title: '[% title FILTER js %]',
            monitorResize: true,
            height: 800,
            footer: true,
            autoShow: true,
            tbar: [{
                    text: 'File',
                    menu: fileMenu
                },{
                    text: 'Edit',
                    menu: editMenu
                },{
                    text: 'View',
                    menu: viewMenu
                },
                new Ext.Toolbar.Fill()
            [% IF user.in_group('Testers') %]
                ,{
                    xtype: 'button',
                    id: 'plan-copy-button',
                    icon: 'extensions/testopia/img/copy.png',
                    iconCls: 'img_button_16x',
                    tooltip: 'Clone this Test Plan',
                    handler: function(){
                        Testopia.TestPlan.ClonePopup(plan);        
                    }
                }
            [% END %]
            [% IF plan.candelete %]
                ,{
                    xtype: 'button',
                    id: 'plan-delete-btn',
                    icon: 'extensions/testopia/img/delete.png',
                    iconCls: 'img_button_16x',
                    tooltip: 'Delete this Plan',
                    handler: confirmDelete
                }
            [% END %]
            ],
            applyTo: 'plan-body-div',
            items:[{
                xtype: 'tabpanel',
                stateEvents: ['tabchange'],
                split: true,
                region: 'center',
                id: 'object_panel',
                activeTab: Ext.state.Manager.get('plan_active_tab', 0),
                listeners: { 
                  'tabchange': function(){
                        Ext.state.Manager.set('plan_active_tab', Ext.getCmp('object_panel').getActiveTab().getId());
                  }
                },
                items: [
                    new Testopia.Search.DashboardPanel({}),
                    rgrid,
                    cgrid,
                    attachGrid,
                    {
                        layout: 'fit',
                        title: 'Plan Document',
                        tbar: new DocCompareToolbar('plan',plan.plan_id),
                        items: [{
                            id: 'plan_doc',
                            xtype:'htmleditor',
                            value: '[% plan.text.plan_text FILTER js %]'
                        }],
                        buttons: [{
                            text: 'Submit',
                            disabled: plan.canedit ? false : true,
                            handler: function(){
                                testopia_form.submit({
                                    url: 'tr_process_plan.cgi',
                                    params: {action: 'edit', plandoc: Ext.getCmp('plan_doc').getValue(), plan_id: plan.plan_id},
                                    success: function(){
                                        Testopia.Util.notify.msg('Plan updated', 'Plan {0} was updated successfully', 'Document');
                                    },
                                    failure: Testopia.Util.error
                                });
                            }
                        }]
                    },{
                        layout: 'fit',
                        id:'plan-permissions',
                        title: 'Permissions',
                        autoShow: true,
                        autoScroll: true,
                        contentEl: 'plan-permissions-div',
                        bodyStyle: 'padding: 10px;'
                    },new Testopia.Util.HistoryList('plan',plan.plan_id)]
                
            },{
                id: 'test-plan-id',
                split: true,
                region: 'north',
                contentEl: 'plan_overview',
                height: Ext.get('plan_overview_table').getHeight() + 30
            },{
                id: 'plan_east_panel',
                xtype: 'tabpanel',
                region: 'east',
                width: '200',
                activeTab: 0,
                split: true,
                items: [tgrid, new Testopia.Search.SavedReportsList({
                        title: 'Reports',
                        id: 'reports_grid',
                        type: 1
                    })
                ]
            }]
        });
    [% IF plan.canedit %]    
        var vbox = new Testopia.Product.VersionCombo({
            hiddenName: 'product_version',
            transform: 'product_version'
        });
        vbox.on('select', function(c,r,i){
            testopia_form.submit({
                url: 'tr_process_plan.cgi',
                params: {action: 'edit', prod_version: r.get('value'), plan_id: plan.plan_id},
                success: function(){
                    Testopia.Util.notify.msg('Plan updated', 'Plan {0} was updated successfully', 'Product Version');
                },
                failure: Testopia.Util.error
            });
        });
    
        if (Ext.get('adduser')){
            var adduser = new Testopia.User.Lookup({
                id: 'plan_permission_add_user',
                hiddenName: 'adduser'
            });
            adduser.render('adduser');
        }
        
        var types = new Testopia.TestPlan.TypesCombo({
            hiddenName: 'plan_type',
            transform: 'plan_type'
        });
        types.on('select', function(c,r,i){
            testopia_form.submit({
                url: 'tr_process_plan.cgi',
                params: {action: 'edit', type: r.get('value'), plan_id: plan.plan_id},
                success: function(){
                    Testopia.Util.notify.msg('Plan updated', 'Plan {0} was updated successfully', 'type');
                },
                failure: Testopia.Util.error
            });
        });
    
        var edit_summary_btn = new Ext.Button({
            applyTo: 'plan_name_edit_btn',
            id:'edit_plan_summary_btn',
            icon: 'extensions/testopia/img/edit.png',
            iconCls: 'img_button_16x',
            tooltip: 'Edit Plan Summary',
            handler: function(){
                Ext.getCmp('plan_name_edt').startEdit(Ext.get('plan_name'));
            }
        });
        var plan_name_edt = new Ext.Editor(
            new Ext.form.TextField({
                width: Ext.get('plan_name').getWidth() < 200 ? 200 : Ext.get('plan_name').getWidth(),
                name: 'plan_name'
            }),{
                alignment: 'tl-tl',
                id: 'plan_name_edt',
                ignoreNoChange: true,
                updateEl: true,
                completeOnEnter: true,
                cancelOnEsc: true
        });
        plan_name_edt.on('complete', function(e,v,sv){
            testopia_form.submit({
                url: 'tr_process_plan.cgi',
                params: {action: 'edit', name: v, plan_id: plan.plan_id},
                success: function(){
                    Testopia.Util.notify.msg('Plan updated', 'Plan {0} was updated successfully', 'name');
                },
                failure: Testopia.Util.error
            });
        });
    [% END %]
        Ext.getCmp('dashboardpanel').on('render', function(){
            Ext.getCmp('dashboardpanel').getTopToolbar().add({
                xtype: 'button',
                id: 'save_dashboard_btn',
                icon: 'extensions/testopia/img/save.png',
                iconCls: 'img_button_16x',
                tooltip: 'Save this dashboard',
                handler: function(b,e){
                    Testopia.Search.save('dashboard', Testopia.Search.dashboard_urls);
                }
            });
        });
        Ext.get('plan_overview').show();
    });
</script>

[% PROCESS testopia/plan/navigate.html.tmpl %]

<div id="plan-body-div" class="x-display-hidden"></div>

<div id="plan_overview" style="visibility: hidden">

<table border="0" cellpadding="3" cellspacing="3" width="100%" id="plan_overview_table">
  <tbody align="left">
    <tr class="bz_row_header">
      <th colspan="3">Plan Name</th>
    </tr>
    <tr class="bz_row_data">
      <td colspan="3">
        <table>
          <tr>
            <td><div id="plan_name">[% plan.name FILTER html %]</div></td>
            <td><div id="plan_name_edit_btn"></div></td>
            <td><div id="archive_flag" style="position: absolute; right:50px; top: 0px;" [% 'class="plan_archived"' IF NOT plan.isactive %]></div></td>
          </tr>
        </table>
      </td>
    </tr>
    <tr class="bz_row_header">
      <th>Product</th>
      <th>Product Version </th>
      <th>Created</th>
    </tr>
    <tr class="bz_row_data">
      <td><a href="tr_show_product.cgi?product_id=[% plan.product_id FILTER none %]">[% plan.product.name FILTER html %]</a></td>
      <td>[% IF plan.canedit %]
          [% PROCESS select sel = { name  => 'product_version',
                                    list  => plan.product.versions,
                                    default => plan.product_version  } %]
          [% ELSE %]
            [% plan.product_version FILTER html %]
          [% END %]
      </td>
      <td>[% plan.creation_date FILTER time %]</td>
      </tr>
    <tr class="bz_row_header">
      <th>Author</th>
      <th>Plan Version</th>
      <th>Type</th>
      </tr>
    <tr class="bz_row_data">
      <td><a href="mailto:[% plan.author.email FILTER html %]">[% plan.author.identity FILTER html %]</a></td>
      <td>[% plan.version FILTER html %]</td>
      <td>[% IF plan.canedit %]
          [% PROCESS select sel = { name  => 'plan_type',
                                    list  => plan.get_plan_types,
                                    default => plan.plan_type  } %]
          [% ELSE %]
          [% plan.plan_type FILTER html %]
          [% END %]
      </td>
    </tr>
  </tbody>
</table>
</div>

<div id="plan-permissions-div" class="x-hidden">
    [% PROCESS "testopia/plan/access-list.html.tmpl" %]
</div>

[% PROCESS global/footer.html.tmpl %]
