#
# E-Mail Sync
# Table of all IMAP credentials, folder and sync status
#
# action: folderList, mailList, checkAccount, mailMove, seen, unseen, folderNew, folderDelete

form = {{form:SE}}

{
    sql = SELECT '{{action:SE:alnumx}}' AS _action
                 , '{{username:SE:alnumx}}' AS _username
                 , '{{folder:SE:alnumx}}' AS _folder
            FROM DUAL
            WHERE '{{action:SE:alnumx:w}}' != ''

    shead = <div class="alert alert-info">
               Don't forget to restart <b>email-sync.php</b> script after changing the configuration.
            </div>
    head = <div class="panel panel-info">
             <div class="panel-heading">
                 <h3 class="panel-title">{{infoType:SE:alnumx}}: {{username:SE:alnumx}}</h3>
             </div>
             <div class="panel-body">
    tail = </div></div>

    {
        sql = SELECT 'J:folderList::::{{username:RE}}' AS _link
                FROM DUAL
                WHERE '{{action:RE}}' = 'folderList'
    }

    {
        sql = SELECT CONCAT('<h4>', TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX('{{folder:RE}}', ',', seq), ',', -1)), '</h4>') AS currentFolder
                      , CONCAT('J:mailList:',
                         TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX('{{folder:RE}}', ',', seq), ',', -1)),
                         ':::',
                         '{{username:RE}}'
                      ) AS _link
              FROM seq_1_to_10
              WHERE seq <= (CHAR_LENGTH('{{folder:RE}}') - CHAR_LENGTH(REPLACE('{{folder:RE}}', ',', '')) + 1)
                  AND TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX('{{folder:RE}}', ',', seq), ',', -1)) != ''
                  AND '{{action:RE}}' = 'mailList'
    }
}

{
    sql = SELECT 'p:{{pageSlug:T}}?form=emailSync&r=0|s|b|N' AS '_link|newEmailBtn|_hide'
}

{
    sql = SELECT CONCAT('p:{{pageSlug:T}}?form=emailSync&r=', e.id, '|s|b|E') AS '_link|editEmailBtn|_hide'
                 , CONCAT('p:{{pageSlug:T}}?action=folderList&infoType=Folder List&username=', e.feUser, '|s|b|t:<i class="far fa-folder-open"></i>') AS '_link|showAllFolder|_hide'
                 , CONCAT('p:{{pageSlug:T}}?action=mailList&infoType=Mail List&username=', e.feUser, '&folder=', e.name2, '|s|b|t:<i class="fas fa-inbox"></i>') AS '_link|showInboxMails|_hide'
                 , e.name AS _host
                 , e.feUser AS _username
                 , e.name2 AS _folder
                 , e.id AS _settingId
                 , CONCAT('J:checkAccount::::', e.feUser) AS '_link|checkAccountStatus|_hide'
            FROM Setting AS e
            WHERE e.type = 'credentials'

    shead = <div class="panel panel-default">
              <div class="panel-heading">
                <h3 class="panel-title">E-Mail IMAP Configuration</h3>
              </div>
              <div class="panel-body">
                <table class="table tablesorter">
                  <thead>
                    <tr>
                      <th class="filter-false sorter-false">{{&newEmailBtn:RE::-}}</th>
                      <th>Host</th>
                      <th>Username</th>
                      <th>Folder</th>
                      <th>Status</th>
                      <th class="filter-false sorter-false"></th>
                    </tr>
                  </thead>
                  <tbody>

    stail =      </tbody>
                 </table>
               </div>
             </div>

    {
        sql = SELECT '{{&editEmailBtn:RE}} {{&showAllFolder:RE}} {{&showInboxMails:RE}}'
                     , '{{host:RE}}'
                     , '{{username:RE}}'
                     , '{{folder:RE}}'
                     , IF('{{&checkAccountStatus:R0}}' = 1, 'green|o:Active', 'red|o:Inactive') AS _bullet
                     , CONCAT('U:form=emailSync&r={{settingId:R0}}|q:Do you want delete this configuration?') AS _paged
        rbeg = <tr>
        rend = </tr>
        fbeg = <td>
        fend = </td>

    }
}