<?php
$cate_id = "mnu_dashboard"; //현재 메뉴 ID 필수!!!
include_once "{$_SERVER['DOCUMENT_ROOT']}/common.inc.php";
include_once _ADM_INC_PATH_ ."/auth_check.php";

$admin_auth = array_keys($GLOBALS['_varsAuthAdmin']);
$sql = "select 
  sum(case when member_auth not in ('".implode("','",$admin_auth)."') then 1 else 0 end) as mem_num,
  sum(case when member_auth in     ('".implode("','",$admin_auth)."') then 1 else 0 end) as adm_num,
  sum(case when member_auth not in ('".implode("','",$admin_auth)."') and member_status = 1 then 1 else 0 end) as active_mem_num,
  sum(case when member_auth not in ('".implode("','",$admin_auth)."') and member_status = 2 then 1 else 0 end) as ready_mem_num,
  sum(case when member_auth not in ('".implode("','",$admin_auth)."') and member_status = 3 then 1 else 0 end) as fire_mem_num,
  sum(case when member_auth in     ('".implode("','",$admin_auth)."') and member_status = 1 then 1 else 0 end) as active_adm_num,
  sum(case when member_auth in     ('".implode("','",$admin_auth)."') and member_status = 2 then 1 else 0 end) as ready_adm_num,
  sum(case when member_auth in     ('".implode("','",$admin_auth)."') and member_status = 3 then 1 else 0 end) as fire_adm_num
from ".TB_member;
$mem_dash_info = $baseObj->DB->queryFirstRow($sql);

$sql = "select 
  sum(case when login_page = 'service' then 1 else 0 end) as svc_login,
  sum(case when login_page = 'admin'   then 1 else 0 end) as adm_login,
  sum(case when login_page = 'service' and login_success = 1 then 1 else 0 end) as success_svc_login,
  sum(case when login_page = 'service' and login_success = 0 then 1 else 0 end) as fail_svc_login,
  sum(case when login_page = 'admin' and login_success = 1 then 1 else 0 end) as success_adm_login,
  sum(case when login_page = 'admin' and login_success = 0 then 1 else 0 end) as fail_adm_login
from ".TB_member_log." where login_date >= DATEADD(DD,-1,GETDATE())";
$log_dash_info = $baseObj->DB->queryFirstRow($sql);

$vsobj = new Visit();

$today        = $_REQUEST['sdate'] ?: date("Y-m-d H:00:00");
$hour         = date("H", strtotime($today." +1 Hours"));
$today_bgin   = date("Y-m-d H:00:00",strtotime($today." -1 days"));
$prevday      = date("Y-m-d H:00:00",strtotime($today." -1 days"));
$prevday_bgin = date("Y-m-d H:00:00",strtotime($prevday." -1 days"));

$logdata['today']   = $vsobj->getVisitData("hour", "LOG", "service", $today_bgin,   $today);
$logdata['prevday'] = $vsobj->getVisitData("hour", "LOG", "service", $prevday_bgin, $prevday);

include _ADM_INC_PATH_ ."/header.html";
?>
  <div id="dashboard">
    <div id="dash_mem" class="dashbox">
      <div class="card">
        <div class="card-body">
          <h2 class="card_title"><i class="bi bi-info-square-fill"></i> 최근 24시간 회원 정보 </h2>
          <div class="card_a">
            <ul class="list-group">
              <li class="list-group-item"><i class="bi bi-app"></i> 총 회원수 : <span class="dash_val text-primary"><?=number_format($mem_dash_info['mem_num'])?></span> 명</li>
              <li class="list-group-item"><i class="bi bi-app"></i> 활동 회원 : <span class="dash_val text-info"><?=number_format($mem_dash_info['active_mem_num'])?></span> 명</li>
              <li class="list-group-item"><i class="bi bi-app"></i> 승인대기 회원 : <span class="dash_val text-success"><?=number_format($mem_dash_info['ready_mem_num'])?></span> 명</li>
              <li class="list-group-item"><i class="bi bi-app"></i> 비 활동 회원 : <span class="dash_val text-warning"><?=number_format($mem_dash_info['fire_mem_num'])?></span> 명</li>
            </ul>
          </div>
          <div class="card_b">
            <ul class="list-group">
              <li class="list-group-item"><i class="bi bi-app"></i> 총 로그인시도 : <span class="dash_val text-primary"><?=number_format($log_dash_info['svc_login'])?></span> 회</li>
              <li class="list-group-item"><i class="bi bi-app"></i> 로그인 성공 : <span class="dash_val text-success"><?=number_format($log_dash_info['success_svc_login'])?></span> 회</li>
              <li class="list-group-item"><i class="bi bi-app"></i> 로그인 실패 : <span class="dash_val text-danger"><?=number_format($log_dash_info['fail_svc_login'])?></span> 회</li>
            </ul>
          </div>
        </div>
      </div>
    </div>

    <div id="dash_log" class="dashbox">
      <div class="card">
        <div class="card-body">
          <h2 class="card_title"><i class="bi bi-info-square-fill"></i> 최근 24시간 관리자 정보 </h2>
          <div class="card_a">
            <ul class="list-group">
              <li class="list-group-item"><i class="bi bi-app"></i> 총 관리자수 : <span class="dash_val text-primary"><?=number_format($mem_dash_info['adm_num'])?></span> 명</li>
              <li class="list-group-item"><i class="bi bi-app"></i> 활동 관리자 : <span class="dash_val text-info"><?=number_format($mem_dash_info['active_adm_num'])?></span> 명</li>
              <li class="list-group-item"><i class="bi bi-app"></i> 승인대기 관리자 : <span class="dash_val text-success"><?=number_format($mem_dash_info['ready_adm_num'])?></span> 명</li>
              <li class="list-group-item"><i class="bi bi-app"></i> 비 활동 관리자 : <span class="dash_val text-warning"><?=number_format($mem_dash_info['fire_adm_num'])?></span> 명</li>
            </ul>
          </div>
          <div class="card_b">
            <ul class="list-group">
              <li class="list-group-item"><i class="bi bi-app"></i> 총 로그인시도 : <span class="dash_val text-primary"><?=number_format($log_dash_info['adm_login'])?></span> 회</li>
              <li class="list-group-item"><i class="bi bi-app"></i> 로그인 성공 : <span class="dash_val text-success"><?=number_format($log_dash_info['success_adm_login'])?></span> 회</li>
              <li class="list-group-item"><i class="bi bi-app"></i> 로그인 실패 : <span class="dash_val text-danger"><?=number_format($log_dash_info['fail_adm_login'])?></span> 회</li>
            </ul>
          </div>
        </div>
      </div>
    </div>

    <div id="dash_chart" class="dashbox">
      <div class="card">
        <div class="card-body">
          <h2 class="card_title"><i class="bi bi-info-square-fill"></i> 최근 24시간 페이지뷰 </h2>
          <div id="day_chart_pageview" style="width:99%; min-height:250px;"></div>
        </div>
      </div>
    </div>
  </div>

<style>
  #dashboard {
    display: grid;
    /*grid-gap: 30px;*/
    column-gap: 20px;
    row-gap: 20px;
    grid-template-columns: auto auto;

    margin-top: 20px;
  }

  #dashboard #dash_mem {
    grid-column: 1;
    grid-row: 1;
  }

  #dashboard #dash_log {
    grid-column: 2;
    grid-row: 1;
  }

  #dashboard #dash_chart {
    grid-column: 1 / 3;
    grid-row: 2;
  }

  #dashboard .dashbox {
    height: calc( (100vh - 200px)/2 );
  }
  #dashboard .dashbox .card {
    height: 100%;
  }
  #dashboard .dashbox .card .card-body {
    display: flex;
    flex-wrap: wrap;
    align-content: stretch;
  }
  #dashboard .dashbox .card .card-body .card_title {
    flex-basis: 100%;
    height: 60px;
    background-color: #EAEAEA;
    vertical-align: middle;
    line-height: 60px;
    padding-left: 20px;
    border-radius: 10px;
    font-size: 1.3rem;
  }
  #dashboard .dashbox .card .card-body .card_a,
  #dashboard .dashbox .card .card-body .card_b {
    flex: 1;
    height: calc(100% - 60px);
    font-size: 1.2em;
  }
  #dashboard .dashbox .card .card-body .card_b {
    margin-left: 20px;
  }
  #dashboard .dashbox .card .card-body .list-group-item {
    padding-left: 20px;
    border-top: none;
    border-left: none;
    border-right: none;
  }
</style>

<?php
include _ADM_INC_PATH_ ."/footer.html";
?>
<script src="https://www.gstatic.com/charts/loader.js" type="text/javascript"></script>
<script>
const today = "<?=$today?>";
const chart_log_data = <?=json_encode($logdata)?>;

let divnum = 2;

let gcdata = {
  log: [['시간','지정일','이전일','평균']],
};

Array.from(new Array(24)).forEach( (v, i) => {
  let hour = Math.abs(<?=$hour?:0?> + i) % 24;
  let st = ('0'+hour).slice(-2);
  let et = ('0'+(hour+1)).slice(-2);
  gcdata.log.push([
    { v:Number(i), f:st+'시 ~ ' + et+'시'},
    chart_log_data.today.data['h'+st]||0,
    chart_log_data.prevday.data['h'+st]||0,
    Math.round((Number(chart_log_data.today.data['h'+st])+Number(chart_log_data.prevday.data['h'+st]))*10/divnum)/10
  ]);
});

let options_log = {
  chartArea: {
    top: 50,
    left:50,
    right:10,
    bottom: 100,
  },
  title: '최근 24시간 시간별 페이지뷰',
  titleTextStyle: {
    align: 'center',
  },
  seriesType: 'bars',
  bar: { groupWidth: '75%' },
  colors: ['#D2695C','#52AFAE','#ffd045','#ABDCF7'],

  legend: {
    position: 'bottom',
    textStyle: {
      fontSize: 12,
    }
  }, //개별
  //titlePosition: 'in',//타이틀
  //axisTitlesPosition: 'in',//가로축

  tooltip: {
    textStyle: {
      fontSize: 12,
      color: 'darkgreen',
    },
  },
  vAxis: {
    //textPosition: 'in'
    textStyle: {
      fontSize: 12,
    },
    // viewWindow:{
    //   min:0
    // },
  },

  hAxis: {
    textStyle: {
     fontSize: 12,
    },
    maxTextLines:1,
    maxAlternation: 1,
    slantedTextAngle: 30,
    ticks: Array.from(new Array(24)).map( (v, i) => {
      let hour = Math.abs(<?=$hour?:0?> + i) % 24;
      let data = {v:i, f:('0'+hour).slice(-2)+'시 ~ ' + ('0'+(hour+1)).slice(-2)+'시'};
      return data;
    } ),
    //title: '시간대',
    //textPosition: 'in',
    format: 'decimal',
    viewWindowMode: 'maximized',
    gridlines: {
      color: '#ccc',
      interval:1,
      units: {
        hours: {format:'Date(5)'}
      },
      minSpacing: 10
  }},
  series: {
    2: {type: 'line', a_curveType: 'function',},
  }
};

function chartLogDraw() {
  let data = google.visualization.arrayToDataTable(gcdata.log);
  let options = options_log;
  let chart = new google.visualization.ComboChart(document.getElementById('day_chart_pageview'));
  google.visualization.events.addListener(chart, 'select', () => {
    let selection = chart.getSelection();
  });
  chart.draw(data, options);
};

google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(chartLogDraw);
</script>
