[%# 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("testopia/js/attachments.js") %]
[% javascript_urls.push("testopia/js/tags.js") %]
[% javascript_urls.push("testopia/js/plan.js") %]
[% javascript_urls.push("testopia/js/search.js") %]
[% javascript_urls.push("testopia/js/case.js") %]
[% javascript_urls.push("testopia/js/run.js") %]
[% javascript_urls.push("testopia/extjs/examples/portal/Portal.js") %]
[% javascript_urls.push("testopia/extjs/examples/portal/PortalColumn.js") %]
[% javascript_urls.push("testopia/extjs/examples/portal/Portlet.js") %]


[% IF NOT plan.isactive %]
  [% title = "Test Plan $plan.id: $plan.name (ARCHIVED)" %]
[% ELSE %]
  [% title = "Test Plan $plan.id: $plan.name" %]
[% 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: testopiaError
            });
        }
    };
    var cgrid = new CaseGrid({plan_id: plan.plan_id },{id: 'plan_case_grid'});
    var rgrid = new RunGrid({plan_id: plan.plan_id },{id: 'plan_run_grid'});
    var tgrid = new TestopiaObjectTags('plan', plan.plan_id);
    var attachGrid = new AttachGrid(plan);
    var t = new TestopiaUtil();
    var clone = new PlanClonePopup(plan);
    
    var fileMenu = new Ext.menu.Menu({
        id: 'plan-file-menu',
        items: [{
            text: "Archived",
            id: 'plan_archive_mnu',
            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(){
                        TestopiaUtil.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: testopiaError
                });
            }
        },{
            text: 'Clone',
            icon: 'testopia/img/copy.png',
            iconCls: 'img_button_16x',
            handler: function(){
                clone.window.show();
            }
        },{
            text: "Print",
            handler: function(){
                window.location='tr_show_plan.cgi?&plan_id=[% plan.id FILTER none %]&ctype=print';
            }
        },{
            text: 'Export',
            menu: [{
                text: 'CSV',
                handler: function(){
                    window.location='tr_show_plan.cgi?&plan_id=[% plan.id FILTER none %]&export=testcases&ctype=csv&viewall=1';
                }
            },{
                text: 'XML',
                handler: function(){
                    window.location='tr_show_plan.cgi?&plan_id=[% plan.id FILTER none %]&export=testcases&ctype=xml&viewall=1';             
                }
            }]
        },{
            text: 'New',
            menu: [{
                text: 'Test Run',
                handler: function(){
                    t.newRunPopup(plan);
                }
            },{
                text: 'Test Case',
                handler: function(){
                    t.newCaseForm(plan.plan_id, plan.product_id);
                }
            }]
        },{
            text: "Delete",
            id: 'btn-delete',
            icon: '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"
        }]
    });

    var planControls = new Ext.Toolbar();
    planControls.render('plan-tb-div');
    planControls.add({
        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',
        template: button_16x_tmpl,
        icon: 'testopia/img/copy.png',
        iconCls: 'img_button_16x',
        tooltip: 'Clone this Test Plan',
        handler: function(){
                clone.window.show();
        }
    }
[% END %]
[% IF plan.candelete %]
    ,{
        xtype: 'button',
        template: button_16x_tmpl,
        id: 'plan-delete-btn',
        icon: 'testopia/img/delete.png',
        iconCls: 'img_button_16x',
        tooltip: 'Delete this Plan',
        handler: confirmDelete
    }
[% END %]    
    );
    
    var planpanel = new Ext.Panel({
        layout: 'border',
        applyTo: 'plan-body-div',
        monitorResize: true,
        height: 800,
        footer: true,
        autoShow: true,
        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 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(){
                                TestopiaUtil.notify.msg('Plan updated', 'Plan {0} was updated successfully', 'Document');
                            },
                            failure: testopiaError
                        });
                    }
                }]
            },{
                layout: 'fit',
                id:'plan-permissions',
                title: 'Permissions',
                autoShow: true,
                autoScroll: true,
                contentEl: 'plan-permissions-div',
                bodyStyle: 'padding: 10px;'
            },new HistoryGrid('plan',plan.plan_id)]
            
        },{
            xtype: 'panel',
            id: 'test-plan-id',
            split: true,
            title: '[% title FILTER js %]',
            region: 'north',
            contentEl: 'plan_overview',
            height: Ext.get('plan_overview_table').getHeight() + 65
        },tgrid
        ]
    });
[% IF plan.canedit %]    
    var vbox = new ProductVersionCombo({
        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(){
                TestopiaUtil.notify.msg('Plan updated', 'Plan {0} was updated successfully', 'Product Version');
            },
            failure: testopiaError
        });
    });

    if (Ext.get('adduser')){
        var adduser = new UserLookup({
            id: 'plan_permission_add_user',
            hiddenName: 'adduser'
        });
        adduser.render('adduser');
    }
    
    var types = new PlanTypesCombo({
        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(){
                TestopiaUtil.notify.msg('Plan updated', 'Plan {0} was updated successfully', 'type');
            },
            failure: testopiaError
        });
    });

    var edit_summary_btn = new Ext.Button({
        applyTo: 'plan_name_edit_btn',
        template: button_16x_tmpl,
        id:'edit_plan_summary_btn',
        icon: '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(){
                TestopiaUtil.notify.msg('Plan updated', 'Plan {0} was updated successfully', 'name');
            },
            failure: testopiaError
        });
    });
[% END %]
    tgrid.store.load();
});
</script>




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

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

<div id="plan_overview">
<div id="plan-tb-div"></div>

<table border="0" cellpadding="1" 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 style="position: relative">
          <tr style="width:100%;">
            <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:0px; 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-clone-div" class="x-hidden">
    [% PROCESS testopia/plan/clone.html.tmpl %]
</div>

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

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

