'use strict';

/**
 * 期计量 - 期列表页面 js
 *
 * @author Mai
 * @date 2018/12/7
 * @version
 */

// 获取审批流程
$('a[data-target="#sp-list" ]').on('click', function () {
   const data = {
       order: $(this).attr('s-order'),
   };
   postData('/tender/' + tenderId + '/measure/stage/auditors', data, function (result) {
       const stageAuditor = result.stageAuditor;
       const auditors = result.auditors;
       const auditHistory = result.auditHistory;
       // 生成左边列表流程
       const lefthtml = [];
       lefthtml.push('<li class="list-group-item"><i class="fa fa fa-play-circle fa-rotate-90"></i> '+ stageAuditor.name +'  <small class="text-muted">'+ stageAuditor.role +'</small></li>');
       for (const [index,a] of auditors.entries()) {
           if (index+1 === auditors.length) {
               lefthtml.push('<li class="list-group-item"><i class="fa fa-stop-circle"></i> '+ a.name +'  <small class="text-muted">'+ a.role +'</small></li>');
           } else {
               lefthtml.push('<li class="list-group-item"><i class="fa fa-chevron-circle-down"></i> '+ a.name +'  <small class="text-muted">'+ a.role +'</small></li>');
           }
       }
       $('#auditor-list').html(lefthtml.join(''));

       // 生成右边列表流程
       const righthtml = [];
       for(const ah of auditHistory) {
           righthtml.push('<div class="card mt-3"><ul class="list-group list-group-flush">');
           for (let iA = 0; iA < ah.length; iA++) {
               if (iA === 0) {
                   righthtml.push('<li class="list-group-item">');
                   righthtml.push('<span class="text-success pull-right">'+ (auditHistory.indexOf(ah) > 0 ? '重新' : '') +'上报</span>');
                   righthtml.push('<h5 class="card-title">');
                   righthtml.push('<i class="fa fa-play-circle fa-rotate-90 text-success"></i> '+ stageAuditor.name +' <small class="text-muted">'+ stageAuditor.role +'</small></h5>');
                   righthtml.push('<p class="card-text"><small class="text-muted">' + (ah[iA].begin_time ? moment(ah[iA].begin_time).format('YYYY-MM-DD') : '') + '</small></p></li>');
                   righthtml.push('<li class="list-group-item">');
                   if (ah[iA].status !== auditConst.status.uncheck) {
                       righthtml.push('<span class="'+ auditConst.statusClass[ah[iA].status] +' pull-right">' + auditConst.statusString[ah[iA].status] + (ah[iA].status === auditConst.status.checkNo ? ' ' + stageAuditor.name : '') + '</span>');
                   }
                   righthtml.push('<h5 class="card-title"><i class="fa '+ (iA === ah.length - 1 ? 'fa-stop-circle ' : 'fa-chevron-circle-down ') + auditConst.statusClass[ah[iA].status] +'"></i> '+ ah[iA].name +' <small class="text-muted">'+ ah[iA].role +'</small></h5>');
                   if (ah[iA].status === auditConst.status.checked || ah[iA].status === auditConst.status.checkNo || ah[iA].status === auditConst.status.checkNoPre) {
                       righthtml.push('<p class="card-text mb-1">'+ ah[iA].opinion +'</p>');
                       righthtml.push('<p class="card-text"><small class="text-muted">'+ (ah[iA].end_time ? moment(ah[iA].end_time).format('YYYY-MM-DD') : '') +'</small></p>');
                   }
                   righthtml.push('</li>');
               } else if (iA === ah.length - 1) {
                   righthtml.push('<li class="list-group-item">');
                   if (ah[iA].status !== auditConst.status.uncheck) {
                       righthtml.push('<span class="'+ auditConst.statusClass[ah[iA].status] +' pull-right">' + auditConst.statusString[ah[iA].status] + (ah[iA].status === auditConst.status.checkNo ? ' ' + stageAuditor.name : '') + '</span>');
                   }
                   righthtml.push('<h5 class="card-title"><i class="fa fa-stop-circle '+ auditConst.statusClass[ah[iA].status] +'"></i> '+ ah[iA].name +' <small class="text-muted">'+ ah[iA].role +'</small></h5>');
                   if (ah[iA].status === auditConst.status.checked || ah[iA].status === auditConst.status.checkNo || ah[iA].status === auditConst.status.checkNoPre) {
                       righthtml.push('<p class="card-text mb-1">'+ ah[iA].opinion +'</p>');
                       righthtml.push('<p class="card-text"><small class="text-muted">'+ (ah[iA].end_time ? moment(ah[iA].end_time).format('YYYY-MM-DD') : '') +'</small></p>');
                   }
                   righthtml.push('</li>');
               } else {
                   righthtml.push('<li class="list-group-item">');
                   if (ah[iA].status !== auditConst.status.uncheck) {
                       righthtml.push('<span class="'+ auditConst.statusClass[ah[iA].status] +' pull-right">' + auditConst.statusString[ah[iA].status] + (ah[iA].status === auditConst.status.checkNo ? ' ' + stageAuditor.name : '') + '</span>');
                   }
                   righthtml.push('<h5 class="card-title"><i class="fa fa-chevron-circle-down '+ auditConst.statusClass[ah[iA].status] +'"></i> '+ ah[iA].name +' <small class="text-muted">'+ ah[iA].role +'</small></h5>');
                   if (ah[iA].status === auditConst.status.checked || ah[iA].status === auditConst.status.checkNo || ah[iA].status === auditConst.status.checkNoPre) {
                       righthtml.push('<p class="card-text mb-1">'+ ah[iA].opinion +'</p>');
                       righthtml.push('<p class="card-text"><small class="text-muted">'+ (ah[iA].end_time ? moment(ah[iA].end_time).format('YYYY-MM-DD') : '') +'</small></p>');
                   }
                   righthtml.push('</li>');
               }
           }
           righthtml.push('</ul></div>');
       }
       $('#auditor-list2').html(righthtml.join(''));
   })
});
