[%# 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 %]

[% title = "Test Run $run.id: $run.summary" FILTER html %]

[% PROCESS global/header.html.tmpl %]
<div id="mask"></div>
<script type="text/javascript">
var run = [% run.TO_JSON FILTER html_light %];
var user_login = '[% user.login FILTER html_light %]';
Ext.onReady(function(){
    Ext.QuickTips.init();
    
    var testopia_form = new Ext.form.BasicForm('testopia_helper_frm',{});
    
    function submitEdits(field, value){
        var params = {action: 'edit', run_id: run.run_id};
        switch(field){
            case 'run_product_version': 
                params.run_product_version = value;
                break;
            case 'build': 
                params.build = value;
                break;
            case 'status': 
                params.status = value;
                break;
            case 'summary': 
                params.summary = value;
                break;
            case 'environment': 
                params.environment = value;
                break;
            case 'run_notes': 
                params.run_notes = value;
                break;
            case 'manager': 
                params.manager = value;
                break;
            case 'target_pass': 
                params.target_pass = value;
                break;
            case 'target_completion': 
                params.target_completion = value;
                break;
        }
        testopia_form.submit({
            url: 'tr_process_run.cgi',
            params: params,
            success: function(f,a){
                Testopia.Util.notify.msg('Test Run updated', 'Test Run {0} was updated successfully', field);
            },
            failure: function(f,a){
                if (field == 'status'){
                    Ext.getCmp('run_status_cycle').setActiveItem(Ext.getCmp('run_status_cycle').getActiveItem().id == 'run_status_stopped' ? Ext.getCmp('run_status_running') : Ext.getCmp('run_status_stopped'), true);
                    Ext.getCmp('run_status_cycle').getEl().child(Ext.getCmp('run_status_cycle').buttonSelector).setStyle('background-image', 'url(' +Ext.getCmp('run_status_cycle').getActiveItem().icon +')');
                }
                Testopia.Util.error(f,a);
            }
        });
    }

    var runDelete = function(btn){
        if (btn == 'yes'){
            var form = new Ext.form.BasicForm("testopia_helper_frm",{});
            form.submit({
                url: 'tr_process_run.cgi',
                params: {run_id: run.run_id, action:'delete'},
                success: function(data){
                    Ext.Msg.show({
                        title:'Test Run Deleted',
                        msg: "Test Run " + run.run_id + " was deleted",
                        buttons: Ext.Msg.OK,
                        icon: Ext.MessageBox.INFO
                    });
                    window.location="tr_show_plan.cgi?plan_id=" + run.plan.id;
                },
                failure: Testopia.Util.error
            });
        }
    };
    var fileMenu = new Ext.menu.Menu({
        id: 'file-menu',
        items: [{
            text: "Clone",
            id:'run-clone-icon',
            icon: 'extensions/testopia/img/copy.png',
            iconCls: 'img_button_16x',
            handler: function(){
                Testopia.TestRun.ClonePopup(run.product_id, run.run_id);
            }
        },{
            text: "Delete",
            disabled: run.candelete ? false : true,
            id:'run-delete-icon',
            icon: 'extensions/testopia/img/delete.png',
            iconCls: 'img_button_16x',
            handler: function(){
                Ext.Msg.show({
                   title:'Confirm Delete?',
                   msg: 'You are about to delete run ' + run.id + ' with all of its history. Are you sure you want to continue?',
                   buttons: Ext.Msg.YESNO,
                   fn: runDelete,
                   animEl: 'run_delete_btn',
                   icon: Ext.MessageBox.QUESTION
                });
            }
        },{
            text: 'Export Results to CSV',
            icon: 'extensions/testopia/img/csv.png',
            iconCls: 'img_button_16x',
            handler: function(){
                window.location = 'tr_list_caseruns.cgi?ctype=csv&run_id=' + run.run_id;
            }

        }]
    });

    var runControls = new Ext.Toolbar();
    runControls.add({
        text: 'File',
        menu: fileMenu
    },new Ext.Toolbar.Fill()
    ,{
        xtype: 'button',
        id:'run-clone-ibtn',
        icon: 'extensions/testopia/img/copy.png',
        iconCls: 'img_button_16x',
        tooltip: 'Clone this Test Run',
        handler: function(){
            Testopia.TestRun.ClonePopup(run.product_id, run.run_id);
        }
    }
[% IF run.candelete %]
    ,{
        xtype: 'button',
        id: 'run_delete_btn',
        icon: 'extensions/testopia/img/delete.png',
        iconCls: 'img_button_16x',
        tooltip: 'Delete this Run',
        handler: function(){
            Ext.Msg.show({
               title:'Confirm Delete?',
               msg: 'You are about to delete run ' + run.id + ' with all of its history. Are you sure you want to continue?',
               buttons: Ext.Msg.YESNO,
               fn: runDelete,
               animEl: 'run_delete_btn',
               icon: Ext.MessageBox.QUESTION
            });
        }
    }
[% END %]    
    );
    var runpanel = new Ext.Panel({
        layout: 'border',
        applyTo: 'run-body-div',
        monitorResize: true,
        id: 'runpanel',
        height: Ext.state.Manager.get('run_height', 1200),
        footer: true,
        items:[{
            split: true,
            region: 'center',
            xtype: 'tabpanel',
            activeTab: 0,
            id: 'object_panel',
            items: [new Testopia.TestCaseRun.Panel(Testopia.Util.urlQueryToJSON(window.location.search), run),
            {
                title: 'Notes',
                layout: 'fit',
                items: [{
                    id: 'run_notes',
                    xtype:'textarea',
                    value: '[% run.notes FILTER js %]'
                }],
                buttons: [{
                    text: 'Submit',
                    disabled: run.canedit ? false : true,
                    handler: function(){
                        submitEdits('run_notes', Ext.getCmp('run_notes').getValue());
                    }
                }]
            },
            new Testopia.Util.HistoryList('run', [% run.id FILTER none %])
            ]
        },{
            split: true,
            id: 'run_overview_pane',
            title: '[% title FILTER js %]',
            headerAsText: true,
            region: 'north',
            collapsible: true,
            contentEl: 'run_overview',
            height: 'auto'
        },{
            collapsible: true,
            title: ' ',
            split: true,
            region:'east',
            layout: 'fit',
            width: 200,
            items: [{
                xtype: 'tabpanel',
                activeTab: 0,
                id: 'run_east_panel',
                items:[
                    new Testopia.Tags.ObjectTags('run', [% run.id FILTER none %]),
    //                {
    //                    title: 'CC List'
    //                },
                    new Testopia.TestRun.FiltersList(run)
                    
                ]
            }]
        }]
    });
[% IF run.canedit %]   
  [% IF run.canstatus %] 
// Manager Editing
    var manager_edit_btn = new Ext.Button({
        applyTo: 'manager_edit_btn_div',
        id:'manager_edit_btn',
        icon: 'extensions/testopia/img/edit.png',
        iconCls: 'img_button_16x',
        tooltip: 'Edit Manager',
        handler: function(){
            Ext.getCmp('manager_edt').startEdit(Ext.get('run_manager_div'), '[% run.manager.name FILTER js %]');
        }
    });
    var manager_edt = new Ext.Editor(
        new Testopia.User.Lookup({
            hiddenName:'manager',
            value: run.manager
        }),
    {
        alignment: 'tl-tl',
        id: 'manager_edt',
        updateEl: true,
        completeOnEnter: true,
        ignoreNoChange: true,
        cancelOnEsc: true
    });
    manager_edt.on('complete', function(e,v){
        submitEdits('manager', v);
    });
// Target Complete edit    
    var tcomp_edit_btn = new Ext.Button({
        applyTo: 'run_tcomp_edit_btn_div',
        id:'run_tcomp_edit_btn',
        icon: 'extensions/testopia/img/edit.png',
        iconCls: 'img_button_16x',
        tooltip: 'Edit Run Summary',
        handler: function(){
            Ext.getCmp('run_tcomp_edt').startEdit(Ext.get('run_tcomp_div'));
        }
    });
    var run_tcomp_edt = new Ext.Editor(
        new Ext.form.NumberField({
            width: 30,
            name: 'tcomp',
            allowBlank:true,
            minValue: 0,
            maxValue: 100
        }),{
            alignment: 'tl-tl',
            id: 'run_tcomp_edt',
            ignoreNoChange: true,
            updateEl: true,
            completeOnEnter: true,
            ignoreNoChange: true,
            cancelOnEsc: true
    });
    run_tcomp_edt.on('complete', function(e,v,sv){
        submitEdits('target_completion', v);
        Ext.getCmp('tpass_nf').maxValue = v;
    });
// Target Pass edit    
    var tpass_edit_btn = new Ext.Button({
        applyTo: 'run_tpass_edit_btn_div',
        id:'run_tpass_edit_btn',
        icon: 'extensions/testopia/img/edit.png',
        iconCls: 'img_button_16x',
        tooltip: 'Edit Run Summary',
        handler: function(){
            Ext.getCmp('run_tpass_edt').startEdit(Ext.get('run_tpass_div'));
        }
    });
    var run_tpass_edt = new Ext.Editor(
        new Ext.form.NumberField({
            width: 30,
            name: 'tpass',
            allowBlank: true,
            id: 'tpass_nf',
            minValue: 0,
            maxValue: [% run.target_completion || 100 FILTER js %]
        }),{
            alignment: 'tl-tl',
            id: 'run_tpass_edt',
            ignoreNoChange: true,
            updateEl: true,
            completeOnEnter: true,
            ignoreNoChange: true,
            cancelOnEsc: true
    });
    run_tpass_edt.on('complete', function(e,v,sv){
        submitEdits('target_pass', v);
    });

  [% END %]
    runControls.render('run-tb-div');
// Summary Editing    
    var summary_edit_btn = new Ext.Button({
        applyTo: 'run_summary_edit_btn_div',
        id:'run_summary_edit_btn',
        icon: 'extensions/testopia/img/edit.png',
        iconCls: 'img_button_16x',
        tooltip: 'Edit Run Summary',
        handler: function(){
            Ext.getCmp('run_summary_edt').startEdit(Ext.get('run_summary_div'));
        }
    });
    var run_summary_edt = new Ext.Editor(
        new Ext.form.TextField({
            width: Ext.get('run_summary_div').getWidth() < 200 ? 200 : Ext.get('run_summary_div').getWidth(),
            name: 'summary'
        }),{
            alignment: 'tl-tl',
            id: 'run_summary_edt',
            ignoreNoChange: true,
            updateEl: true,
            completeOnEnter: true,
            ignoreNoChange: true,
            cancelOnEsc: true
    });
    run_summary_edt.on('complete', function(e,v,sv){
        submitEdits('summary', v);
    });

// Combo Boxes
    var pbox = new Testopia.Product.VersionCombo({
        hiddenName: 'run_product_version',
        transform: 'product_version'
    });
    pbox.on('select', function(c,r,i){
        submitEdits('run_product_version', r.get('value'));
    });
    
    var bbox = new Testopia.Build.Combo({
        hiddenName: 'build',
        transform: 'run_default_build'
    });
    bbox.on('select', function(c,r,i){
        submitEdits('build', r.get('value'));
    });

    var ebox = new Testopia.Environment.Combo({
        hiddenName: 'environment',
        transform: 'run_default_environment'
    });
    ebox.on('select', function(c,r,i){
        submitEdits('environment', r.get('value'));
    });
[% END %]
// Status editor
    var statusBtn = new Ext.CycleButton({
        applyTo: 'run_status_btn',
        id: 'run_status_cycle',
        showText: true,
        [% 'disabled: true,' UNLESS run.canstatus %]
        icon: 'extensions/testopia/img/[% run.stop_date ? "stop" : "go" %].png',
        iconCls: 'img_button_16x',

        items: [{
            text: 'RUNNING',
            icon: 'extensions/testopia/img/go.png',
            id: 'run_status_running',
            iconCls: 'img_button_16x'
            [% ',checked: true' IF NOT run.stop_date %]
        },{
            text: 'STOPPED',
            id: 'run_status_stopped',
            icon: 'extensions/testopia/img/stop.png',
            iconCls: 'img_button_16x'
            [% ',checked: true' IF run.stop_date %]
        }],

        changeHandler: function (btn, item){
            Ext.Msg.show({
                msg: 'Are you sure you want to change the status to ' + item.text,
                title: 'Change Run Status?',
                buttons: Ext.Msg.YESNO,
                icon: Ext.MessageBox.QUESTION,
                fn: function(b){
                    if (b == 'yes'){
                        btn.getEl().child(btn.buttonSelector).setStyle('background-image', 'url(' +item.icon +')');
                        submitEdits('status', item.text);
                        if (item.text == 'STOPPED'){
                            Ext.getCmp('caserun_grid_tb').disable();
                            Ext.getCmp('summary_tb').disable();
                        }
                        else {
                            if (Ext.getCmp('caserun_grid').getSelectionModel().getCount() > 0){
                                Ext.getCmp('caserun_grid_tb').enable();
                                Ext.getCmp('summary_tb').enable();
                            }
                        }
                    }
                }
            });
        }
    });

// Resizer
    var resizer = new Ext.Resizable('run-body-div',{
        handles: 's',
        pinned: true
        
    });
    resizer.on('resize', function(r,w,h,e){
        runpanel.setHeight(h);
        runpanel.doLayout();
        Ext.state.Manager.set('run_height', h);
    });
    Ext.QuickTips.register({
        target: 'update_bugs',
        title: 'Update Attached Bugs Status',
        text: 'Update Status of Attached Bugs.<p><b>FAILED = REOPENED<br>PASSED = VERIFIED</b></p>'
    });
    Ext.getCmp('case_details_panel').on('resize', function(c,aw,ah,rw,rh){
        Ext.getCmp('cr_action_panel').setHeight(ah - 100);
        Ext.getCmp('cr_results_panel').setHeight(ah - 100);
        Ext.getCmp('cr_breakdown_panel').setHeight(ah - 100);
        Ext.getCmp('cr_setup_panel').setHeight(ah - 100);
        Ext.state.Manager.set('bigtext_height', ah - 100);
    });
  [% IF run.case_run_count == 0 %]
    Ext.getCmp('run_progress').updateProgress(0,0,0,'0%');
  [% ELSE %]
    Ext.getCmp('run_progress').updateProgress([% run.case_run_count(2) / run.case_run_count %],[% run.case_run_count(3) / run.case_run_count %],[% run.case_run_count(6) / run.case_run_count %], '[% run.percent_complete %]%');
  [% END %]
  [% IF run.stop_date %]
    Testopia.Util.DisableTools(Ext.getCmp('caserun_grid_tb'),['caserun_grid_tocsv','run_progress']);
  [% END %]
  Ext.get('run_overview').show();
  Ext.get('caserun-filter-div').show();
});
</script>

[% PROCESS testopia/run/navigate.html.tmpl %]
<div id="run-body-div"></div>
<div id="caserun"></div>
<form id="caserungrideditform"></form>
<form id="rungrideditform"></form>
<form id="attachmentgrideditform"></form>

[%##### Overview #####%]
<div id="run_overview" style="visibility:hidden">
<div id="run-tb-div"></div>
<div id="caserun-tb-div"></div>
<table border="0" cellpadding="1" width="100%">
  <tbody align="left">
    <tr class="bz_row_header">
      <th colspan="2">Run Summary</th>
      <th width="25%">Manager</th>
    </tr>
    <tr class="bz_row_data">
      <td colspan="2">
        <table>
          <tr style="width:100%;">
            <td><div id="run_summary_div">[% run.summary FILTER html %]</div></td>
            <td><div id="run_summary_edit_btn_div"></div></td>
          </tr>
        </table>
      </td>
      <td>
        <table>
          <tr><td><a href="mailto:[% run.manager.email FILTER html %]" id="run_manager_div">[% run.manager.identity FILTER html %]</a></td>
          <td><div id="manager_edit_btn_div"></div></td>
          </tr>
        </table>
      </td>
    </tr>
    <tr class="bz_row_header">
      <th>Plan</th>
      <th>Plan Text Version</th>
      <th>Start Date</th>
      </tr>
    <tr class="bz_row_data">
      <td><a href="tr_show_plan.cgi?plan_id=[% run.plan.id FILTER none %]">[% run.plan.name FILTER html %]</a></td>
      <td>[% run.plan_text_version FILTER html %]</td>
      <td>[% run.start_date FILTER time %]</td>
      </tr>
    <tr  class="bz_row_header">
      <th>Product</th>
      <th>Product Version</th>
      <th>Stop Date</th>
      </tr>
    <tr class="bz_row_data">
      <td><a href="tr_show_product.cgi?product_id=[% run.plan.product.id FILTER none %]">[% run.plan.product.name FILTER html %]</a></td>
      <td>[% IF run.canedit %]
          [% PROCESS select sel = { name  => 'product_version',
                                    list  => run.plan.product.versions,
                                    default => run.product_version  } %]
          [% ELSE %]
          [% run.product_version FILTER html %]
          [% END %]
      </td>
      <td>[% run.stop_date FILTER time %][% IF run.total_time %]&nbsp;([% run.total_time FILTER none %] D:H:M:S)[% END %]</td>
    </tr>
    <tr class="bz_row_header">
      <th>Default Build</th>
      <th>Default Environment</th>
      <td>
      <table>
        <tr><th width="120">Run&nbsp;Status</th><th width="120">Target&nbsp;Comp.&nbsp;%</th><th width="120">Target&nbsp;Pass&nbsp;%</th></tr>
      </table>
      </td>
    </tr>
    <tr class="bz_row_data">
      <td>[% IF run.canedit %]
          [% PROCESS select sel = { name  => 'run_default_build',
                                    list  => run.plan.product.builds('ACTIVE', run.build.id),
                                    default => run.build.name  } %]
          [% ELSE %]
          [% run.build.name FILTER html %]
          [% END %]
      </td>
      <td>[% IF run.canedit %]
          [% PROCESS select sel = { name  => 'run_default_environment',
                                    list  => run.plan.product.environments('ACTIVE', run.environment.id),
                                    default => run.environment.name  } %]
          [% ELSE %]
          [% run.environment.name FILTER html %]
          [% END %]
      </td>
      <td align="left">
      <table>
        <tr>
          <td width="120"><div id="run_status_btn"></div></td>
          <td width="120"><div id="run_tcomp_div" style="width:30px;">[% run.target_completion FILTER none %]</div></td>
          <td><div id="run_tcomp_edit_btn_div"></div></td>
          <td width="120"><div id="run_tpass_div" style="width:30px;">[% run.target_pass FILTER none %]</div></td>
          <td><div id="run_tpass_edit_btn_div"></div></td>
        </tr>
      </table>
      </td>      
    </tr>
  </tbody>
</table>
<div style="height: 10px"></div>
</div>

[% PROCESS testopia/caserun/filter.html.tmpl %]

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