<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-4">
  <h1 class="h3 mb-0 text-gray-800">ประวัติการลาหยุด</h1>
  <ul class="navbar-nav ml-auto">
    <li class="nav-item dropdown no-arrow">
      <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        <span class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i class="fas fa-file fa-sm text-white-50"></i> ยื่นเรื่องขอลาหยุด</span>
      </a>
      <!-- Dropdown - User Information -->
      <div class="dropdown-menu dropdown-menu-right shadow animated--grow-in" aria-labelledby="userDropdown">

        <?php
        $sql = "SELECT company_leavetype.*, leavetype.leavetype_name, leavetype.leavetype_color, leavetype.leavetype_icon, leavetype.leavetype_description FROM company_leavetype 
  INNER JOIN leavetype ON leavetype.id = company_leavetype.leavetype_id 
  AND leavetype.company_id = '" . $this->session->userdata("user_profile")->company_id . "' 
  WHERE company_leavetype.company_id ='" . $this->session->userdata("user_profile")->company_id . "' 
  ORDER BY leavetype.sort";
        foreach ($this->db->query($sql)->result() as $item) {
        ?>
          <a class="dropdown-item" href="<?php echo base_url(); ?>leave/form/<?php echo $item->leavetype_id; ?>">
            <?php echo $item->leavetype_name; ?>
          </a>
        <?php
        }
        ?>

      </div>
    </li>
  </ul>
</div>

<!-- Content Row -->
<div class="row">
  <?php

  $sql = "SELECT company_leavetype.*, leavetype.leavetype_name, leavetype.leavetype_color, leavetype.leavetype_icon, leavetype.leavetype_description FROM company_leavetype 
  INNER JOIN leavetype ON leavetype.id = company_leavetype.leavetype_id 
  AND leavetype.company_id = '" . $this->session->userdata("user_profile")->company_id . "'
  WHERE company_leavetype.company_id ='" . $this->session->userdata("user_profile")->company_id . "' 
  ORDER BY leavetype.sort";
  $leavetype_result = $this->db->query($sql)->result();
  ?>
  <!-- Leave Summary -->
  <?php
  foreach ($leavetype_result as $leavetype) {
    /* ดูวันลาที่ใช้ นับจากที่มีการอนุมัติไปแล้ว (2) */
    $sql = "SELECT company_leavetype.id AS company_leavetype_id, company_leavetype.company_id, company_leavetype.leavetype_id, company_leavetype.num, company_leavetype.per_round,company_leavetype. collect, company_leavetype.advance_notice_day, company_leavetype.month ,
                (
                SELECT IFNULL(SUM(leave_qty),0) AS leave_qty FROM leavemaster 
                    WHERE leave_status IN ('2') 
                    AND company_leavetype_id = company_leavetype.id 
                    AND user_name = '" . $this->session->userdata("user_profile")->user_name . "' 
                    AND (created_at BETWEEN CONCAT(YEAR(now()),CONCAT(CONCAT('-',(company_leavetype.month+1)),'-01 00:00:00')) AND CONCAT(YEAR(DATE_ADD(now(), INTERVAL 1 YEAR)),CONCAT('-',CONCAT((company_leavetype.month)),'-31 23:59:59')))
                        
                ) AS leave_qty
                FROM company_leavetype 
                WHERE company_id = '" . $this->session->userdata("user_profile")->company_id . "'  AND leavetype_id ='" . $leavetype->leavetype_id . "' 
                ORDER BY company_leavetype.leavetype_id ASC";
    // echo $sql;
    $leave = $this->db->query($sql)->row();

  ?>
    <div class="col-xl-4 col-md-6 mb-4">
      <div style="border-left: 0.25rem solid <?php echo $leavetype->leavetype_color; ?> !important;" class="card shadow h-100 py-2">
        <div class="card-body">
          <div class="row no-gutters align-items-center">
            <div class="col mr-2">
              <div style="color: <?php echo $leavetype->leavetype_color; ?> !important;" class="text-xs font-weight-bold text-uppercase mb-1">
                <?php echo $leavetype->leavetype_name; ?> (/ ปี)</div>
              <div class="h5 mb-0 font-weight-bold text-gray-800">ใช้วันลา <?php echo $leave->leave_qty; ?><?php echo ($leavetype->num == 0 ? '' : '/' . ($this->session->userdata("user_profile")->leave_qty > 0 ? $this->session->userdata("user_profile")->leave_qty : $leavetype->num)); ?> วัน</div>
            </div>
            <div class="col-auto">
              <i class="<?php echo $leavetype->leavetype_icon; ?> fa-2x text-gray-300"></i>
            </div>
          </div>
        </div>
      </div>
    </div>
  <?php
  }
  ?>

  <!-- Pending Requests Card Example -->

</div>

<!-- Content Row -->

<div class="row">

  <!-- Area Chart -->
  <div class="col-xl-8 col-lg-7">
    <div class="card shadow mb-4">
      <!-- Card Header - Dropdown -->
      <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
        <h6 class="m-0 font-weight-bold text-primary">ปฏิทินการลา</h6>
      </div>
      <!-- Card Body -->
      <div class="card-body">
        <div class="chart-area">
          <canvas id="myAreaChart"></canvas>
        </div>
      </div>
    </div>
  </div>

  <!-- Pie Chart -->
  <div class="col-xl-4 col-lg-5">
    <div class="card shadow mb-4">
      <!-- Card Header - Dropdown -->
      <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
        <h6 class="m-0 font-weight-bold text-primary">สถิติการลาประจำปี</h6>
      </div>
      <!-- Card Body -->
      <div class="card-body">
        <div class="chart-pie pt-4 pb-2">
          <canvas id="myPieChart"></canvas>
        </div>
        <div class="mt-4 text-center small">
          <?php
          foreach ($leavetype_result as $leavetype) {
          ?>
            <span class="mr-2">
              <i style="color: <?php echo $leavetype->leavetype_color; ?> !important;" class="fas fa-circle"></i> <?php echo $leavetype->leavetype_name; ?>
            </span>
          <?php
          } ?>

        </div>
      </div>
    </div>
  </div>
</div>