TraCorp.custom =
{
  // Identification
  lms_name: 'Apollo University', // also defined in global.config.php
  xtra_copyright: 'Apollo Associated Services, LLC',
  xtra_footer: '',
  lms_version: '7.4.9',

  // Custom buttons (all users)
  custom_buttons: [

  ],
  
  // Custom buttons (main and sub admins)
  custom_admin_buttons: [

  ],
  
  // Custom admin tools
  custom_admin_tools: [

    {
      text: 'Import Content License Keys',
      description: 'Import a list of license keys for a piece of content.',
      handler: function()
      {

        this.custom_import_keys = {step: 1};
        var new_win = new Ext.Window({
          title: 'Import Content License Keys',
          id: 'import_keys_win',
          width: 500,
          height: 300,
          modal: true,
          frame: true,
          layout: 'fit',
          items: [
            {
              id: 'import_keys_wizard',
              layout: 'card',
              activeItem: 0,
              border: false,
              bodyStyle: 'background: transparent;',
              defaults: {
                bodyStyle: 'background: transparent; padding: 5px;',
                border: false,
                defaults: {
                  bodyStyle: 'background: transparent;',
                  border: false
                }
              },
              bbar: [
                {
                  id: 'wiz-move-prev',
                  text: 'Back',
                  handler: TraCorp.custom.import_keys_nav.createDelegate(this, [-1]),
                  disabled: true
                },
                '->',
                {
                  id: 'wiz-move-next',
                  text: 'Next',
                  handler: TraCorp.custom.import_keys_nav.createDelegate(this, [1])
                }
              ],
              items: [
                {
                  id: 'import_keys_1',
                  layout: 'anchor',
                  items: [
                    {
                      bodyStyle: 'background: transparent; margin-bottom: 10px;',
                      html: 'Select a piece of content for which the license keys apply:'
                    },
                    {
                      // select content combo
                      xtype: 'multiselect',
                      id: 'import_keys_content',
                      width: 470,
                      height: 210,
                      editable: false,
                      forceSelection: true,
                      mode: 'remote',
                      triggerAction: 'all',

                      store: new Ext.data.JsonStore({
                        autoLoad: true,
                        url: 'io.php',
                        baseParams: {page_mode: 'get_cid_multiselect'},
                        root: 'data',
                        fields: ['id', 'title']
                      }),

                      displayField: 'title',
                      valueField: 'id'
                    }
                  ]
                },
                {
                  id: 'import_keys_2',
                  layout: 'anchor',
                  items: [
                    {
                      xtype: 'form',
                      fileUpload: true,
                      url: 'io.php',
                      baseParams: {page_mode: 'custom', op: 'import_license_keys'},
                      labelWidth: 100,
                      items: [
                        {
                          hideLabel: true,
                          border: false,
                          bodyStyle: 'background: transparent;',
                          height: 30,
                          html: 'Select the file to upload.  This should be a CSV or TXT file with one license key per line, and nothing else.'
                        },
                        {
                          hideLabel: true,
                          border: false,
                          bodyStyle: 'background: transparent; margin-bottom: 10px; font-weight: bold;'
                        },
                        {
                          fieldLabel: 'Import File',
                          xtype: 'textfield',
                          inputType: 'file',
                          name: 'import'
                        },
                        {
                          hideLabel: true,
                          border: false,
                          bodyStyle: 'background: transparent;'
                        },
                        {
                          id: 'import_keys_cid',
                          hideLabel: true,
                          xtype: 'textfield',
                          inputType: 'hidden',
                          name: 'cid'
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        });

        new_win.show();
      }
    }
  ],

  // Custom reports
  custom_reports: [

    {
      text: 'License Key Assignment',
      description: 'Determine which custom license keys have been assigned to users.',
      handler: function()
      {
        var new_win = new Ext.Window({
          height: 200,
          width: 350,
          id: "rpt_opts_win",
          resizable: true,
          frame: true,
          title: "Report Options - License Key Assignment Report",
          modal: true,
          layout: "fit",
          items: [
            {
              xtype: "form",
              border: false,
              bodyStyle: "background: transparent; padding: 3px;",
              autoScroll: true,
              labelWidth: 100,
              buttons: [
                {
                  text: "Create Report",
                  scope: this,
                  handler: function()
                  {
                    // generate report
                    
                    var start_date = Ext.getCmp("report_start_date").getValue();
                    var end_date = Ext.getCmp("report_end_date").getValue();
                    if (end_date != "" && start_date != "" && end_date.valueOf() < start_date.valueOf())
                    {
                      Ext.Msg.alert("Error", "The start date must be before the end date.");
                      return;
                    }
                    if (start_date != "")
                      start_date = start_date.format("n-j-Y");
                    if (end_date != "")
                      end_date = end_date.format("n-j-Y");
                      
                    var format = Ext.getCmp("report_format").getValue();
                    if (format == "csv" || format == "pdf")
                    {
                      var tmp = new Date();
                      Ext.get('downloadTarget').set({src:'io.php?' + Ext.urlEncode({
                        page_mode: "custom",
                        op: "license_key_report",
                        format: format,
                        start_date: start_date,
                        end_date: end_date,
                        cache: tmp.valueOf()
                      })});
                      return;
                    }

                    var dims = this.view.getSize();

                    // open report window, send XHR request
                    var report_win = new Ext.Window({
                      height: dims.height < 700 ? dims.height - 10 : 700,
                      width: dims.width < 1000 ? dims.width - 10 : 1000,
                      id: "rpt_win",
                      resizable: true,
                      frame: true,
                      title: "License Key Assignment Report",
                      modal: true,
                      layout: "fit",
                      stateful: false,
                      items: [
                        {
                          xtype: "grid",
                          stateful: false,
                          id: "report_grid",
                          stripeRows: true,
                          plugins: [new Ext.ux.plugins.MultiLineHeader()],
                          loadMask: {msg: 'Loading data...'},
                          columns: [
                            {header: "Content", width: 100, sortable: true, dataIndex: "content"},
                            {header: "User", width: 100, sortable: true, dataIndex: "user"},
                            {header: "License Key", width: 100, sortable: true, dataIndex: "lic_key"},
                            {header: "Assigned", width: 120, sortable: true, dataIndex: "assigned", fixed: true, renderer: Ext.util.Format.dateRenderer('n-j-Y g:ia')}
                          ],

                          viewConfig: {
                            autoFill: true,
                            forceFit: true,
                            emptyText: "No records to show"
                          },

                          store: new Ext.data.JsonStore({
                            url: "io.php",
                            baseParams: {
                              page_mode: "custom",
                              op: "license_key_report",
                              format: format,
                              start_date: start_date,
                              end_date: end_date
                            },
                            autoLoad: true,
                            root: 'data',
                            fields: [
                              'content',
                              'user',
                              'lic_key',
                              {name: 'assigned', sortType: Ext.data.SortTypes.asDate, type: 'date', dateFormat: 'n-j-Y g:ia'}
                            ]
                            /* reader: new Ext.data.JsonReader({
                              root: "data",
                              successProperty: "success"
                            }, Ext.data.Record.create([
                              'content',
                              'user',
                              'lic_key',
                              {name: 'assigned', sortType: Ext.data.SortTypes.asDate, type: 'date', dateFormat: 'n-j-Y g:ia'}
                            ])) */
                          })
                        }
                      ]
                    });
                
                    report_win.show();
                  }
                }
              ],
              items: [
                {
                  fieldLabel: "Format",
                  id: "report_format",
                  xtype: "combo",
                  emptyText: "Select a format",
                  allowBlank: true,
                  width: 200,
                  listWidth: 200,
                  editable: false,
                  triggerAction: "all",
                  forceSelection: true,
                  mode: "local",
                  store: new Ext.data.SimpleStore({
                    fields: ["val", "label"],
                    data: [["html", "Online"], ["csv", "Spreadsheet (CSV)"], ["pdf", "Document (PDF)"]]
                  }),
                  displayField: "label",
                  valueField: "val",
                  value: "html"
                },
                {
                  fieldLabel: "Start Date",
                  xtype: "datefield",
                  width: 100,
                  emptyText: "(Any)",
                  name: "start_date",
                  id: "report_start_date"
                },
                {
                  fieldLabel: "End Date",
                  xtype: "datefield",
                  width: 100,
                  emptyText: "(Any)",
                  name: "end_date",
                  id: "report_end_date"
                }
              ]
            }
          ]
        });
    
        new_win.show();
      }
    }
  ],



  import_keys_nav: function(dir)
  {
    var wiz = Ext.getCmp('import_keys_wizard');
    var pnl_id = 0;

    Ext.getCmp('wiz-move-next').setText('Next');

    switch (this.custom_import_keys.step)
    {
      case 1:
        if (dir == 1)
        {
          var val = Ext.getCmp('import_keys_content').getValue();
          if (val == '')
          {
            Ext.Msg.alert('Error', 'You must select a piece of content.');
            return;
          }

          if (val.substr(0, 3) == 'cat')
          {
            Ext.Msg.alert('Error', 'You have selected a content category.  You must select a single piece of content.');
            return;
          }
          var rec_idx = Ext.getCmp('import_keys_content').store.find('id', new RegExp('^' + val + '$'));
          if (rec_idx == -1)
          {
            return;
          }

          var rec = Ext.getCmp('import_keys_content').store.getAt(rec_idx);
          this.custom_import_keys.c_id = val.substr(4);
          this.custom_import_keys.c_title = rec.data.title;

          this.custom_import_keys.step = 2;
          pnl_id = 1;
          Ext.getCmp('wiz-move-prev').enable();

          wiz.items.get(1).items.get(0).items.get(1).body.update(this.custom_import_keys.c_title);
          wiz.items.get(1).items.get(0).items.get(3).body.update('URL to retrieve keys for this content:<br>http://apollo.server.tracorp.com/custom/get_license_key.php?id=' + this.custom_import_keys.c_id);
          Ext.getCmp('import_keys_cid').setValue(this.custom_import_keys.c_id);
          
          Ext.getCmp('wiz-move-next').setText('Finish');
        }
      break;

      case 2:
        if (dir == 1)
        {
          // submit form, handle response
          var form = wiz.items.get(1).items.get(0).getForm();
          
          form.submit({
            scope: this,
            failure: this.submit_failure,
            success: function()
            {
              Ext.getCmp('import_keys_win').close();
              Ext.Msg.alert('Info', 'The import has been completed.  Use the following URL to assign these license keys:<br>http://apollo.server.tracorp.com/custom/get_license_key.php?id=' + this.custom_import_keys.c_id);
            }
          });

          return;
        }
        else
        {
          this.custom_import_keys.step = 1;
          pnl_id = 0;
          Ext.getCmp('wiz-move-prev').disable();
        }
      break;
    }

    wiz.getLayout().setActiveItem(pnl_id);
  }

};