#
# Form
#
# a) List of forms: {{form:S}}='', {{formIdHistory:S}}=''
# b) Edit Form: {{form:S}} - Open form {{form:S}} with record {{r:S}} - typically the FormEditor
# c) Use history of a given form: {{formIdHistory:S}}
#
# {{form:S}}
# {{formIdHistory:S0}} - usage history of form '{{formIdHistory:S}}'

form={{form:SE}}

dbIndex={{indexQfq:Y}}

# Show button only if file token is given over a variable
10 {
  sql = SELECT 'p:{{pageSlug:T}}&file=_searchRefactor|G:glyphicon-arrow-right|s|b|t:Search & Refactor' AS '_link|switchButton|_hide'
          FROM (SELECT '') AS fake
          WHERE '{{enableSwitch:XE}}' = 'yes'
}

# Set file user store variable
20 {
  sql = SELECT IF('{{file:SE}}' != '', '{{file:S::w}}', '') AS '_=file'
}

30 {
  # List of Forms: Do not show this list of forms if there is a form given by SIP.
  # Table header.
  sql = SELECT '<th data-sorter="false" class="filter-false" width="100px;"><div class="btn-group" style="display: inline-flex;">'
               , CONCAT('p:{{pageSlug:T}}&form=form') as _pagen
               , CONCAT('p:{{pageSlug:T}}&form=formJson|t:json|b|s') as _page
               , '</div></th><th>Name'
               , '</th><th>Title'
               , '</th><th>Table'
               , '</th><th>#'
               , '</th><th><em>First</em>'
               , '</th><th><em>Last</em>'
               , '</th><th><em>PageId</em></th>'
           FROM (SELECT '') AS fake
           WHERE {{formIdHistory:S0}}=0
  head = <div style="clear:both;"></div>
         <div class="panel panel-default" style="width: min-content;min-width: fit-content;">
           <div class="panel-heading">
             <h2 style="margin:0;">FormEditor
               <span class="pull-right" style="margin-top:-4px;">
               {{'p:{{pageSlug:T}}&stats=1|b|s|t:Statistic' AS _link}} {{&switchButton:RE::-}}
               </span>
             </h2>
           </div>
           <div style="clear:both;"></div>
           <div class="panel-body">
           <table class="table table-hover qfq-table-50 tablesorter tablesorter-filter {{SELECT IF({{clearMe:Y0}}, 'clear-filter', '')}}" id="{{pageSlug:T}}-form">
  rbeg = <thead class="qfq-sticky"><tr>
  rend = </tr></thead><tbody>
  tail = </tbody></table></div>

  40 {
    # All forms
    sql = SELECT '<td><div class="btn-group" style="display: inline-flex;">'
                   , CONCAT('p:{{pageSlug:T}}&form=form&r=', f.id) as _pagee
                   , CONCAT('p:{{pageSlug:T}}&form=formJson&r=', f.id, '|t:json|s|b') as _page
                 , '</div></td>'
                 , CONCAT(f.name, ' <span class="text-muted">(', f.id, ')</span>')
                 , QMORE(strip_tags(f.title),50)
                 , f.tableName
              FROM Form AS f
              WHERE '{{stats:S0}}'=0
              ORDER BY f.name
    rbeg = <tr>
    rend = </tr>
    fbeg = <td>
    fend = </td>
    fskipwrap = 1,2,3,4
  }

  50 {
    sql = SELECT '' FROM (SELECT '') AS fake WHERE '{{stats:S0}}'=1

    60 {
      # All forms
      sql = SELECT '<td><div class="btn-group" style="display: inline-flex;">'
                     , CONCAT('p:{{pageSlug:T}}&form=form&r=', f.id) as _pagee
                     , CONCAT('p:{{pageSlug:T}}&form=formJson&r=', f.id, '|t:json|s|b') as _page
                   , '</div></td>'
                   , CONCAT(f.name, ' <span class="text-muted">(', f.id, ')</span>')
                   , QMORE(strip_tags(f.title),50)
                   , f.tableName
                   , CONCAT('p:{{pageSlug:T}}&formIdHistory=', f.id, '|s|b|t:<span class="badge">', COUNT(fsl.id), '</span>'
                            , IF(COUNT(fsl.id)=0, '|r:3','')  ) as _link
                   , CONCAT( '<em><span title="',MIN(fsl.created), '">', DATE_FORMAT( MIN( fsl.created), '%d.%m.%Y'), '</span></em>')
                   , CONCAT( '<em><span title="',MAX(fsl.created), '">', DATE_FORMAT( MAX( fsl.created), '%d.%m.%Y'), '</span></em>')
                   , CONCAT('<em>', GROUP_CONCAT(DISTINCT fsl.pageId ORDER BY fsl.pageId), '<em>')
                FROM Form AS f
                LEFT JOIN FormSubmitLog AS fsl
                  ON fsl.formId=f.id
                WHERE '{{stats:S0}}'=1
                GROUP BY f.id
                ORDER BY f.name
      rbeg = <tr>
      rend = </tr>
      fbeg = <td>
      fend = </td>
      fskipwrap = 1,2,3,4
    }
  }

}

70 {
  # History of a Form {{formIdHistory:S0}}
  sql = SELECT f.name
               , fsl.feUser
               , fsl.recordId
               , fsl.pageId
               , fsl.created

           FROM FormSubmitLog AS fsl, Form AS f
           WHERE fsl.formId={{formIdHistory:S0}}
             AND fsl.formId=f.id
           ORDER BY fsl.created DESC

  head = <table class="table table-hover qfq-table-50 tablesorter tablesorter-filter" id="{{pageSlug:T}}-formHistory">
         <thead class="qfq-sticky"><tr><th>Form</th><th>feUser</th><th>recordId</th><th>pageId</th><th>Submit</th></tr></thead><tbody>
  tail = </tbody></table>
  rbeg = <tr>
  rend = </tr>
  fbeg = <td>
  fend = </td>
  altsql = SELECT IF('{{formIdHistory:S0}}'='0', '', '<div class="alert alert-warning">History is empty</div>')
}