<div class="container-fluid py-4">
  <div class="card pb-0 p-3">
    <div class="row">
      <div class="col-6 d-flex align-items-center">
        <h6 class="mb-0">รายการกระเป๋าเงินของคุณ</h6>
      </div>
      <div class="col-6 text-end ">
        <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@mdo"><i class="fas fa-plus" aria-hidden="true"></i>&nbsp;&nbsp;เพิ่มกระเป๋าเงิน</a></button>
      </div>
    </div>
  </div>
  <br>

  <?php if (isset($validation)) : ?>
    <div class="col-12">
      <div class="alert alert-danger" role="alert">
        <?= $validation->listErrors() ?>
      </div>
    </div>
  <?php endif;

  if (@$count <= 0) {
    echo '<div class="text-center" style="color:red;"><p>No Category Found!</p></div>';
  } else {
    echo '<div class="row row-cols-1 row-cols-md-5 g-4 col-13 ">';
    foreach ($category as $value) { ?>
      <div class="col">
        <div class="card h-100"><br>
          <center><img src="https://cdn.niendsitekungz.com/img/wallet2.png" class="card-img-top" style="width: 220px;" alt="..."></center>
          <div class="card-body">
            <h5 class="card-title"><?php echo $value['topic']; ?></h5>
            <p class="card-text text-success">ยอดเงินทั้งหมด: 0.00 บาท</p>
            <p class="card-text">ID : CATEGORY-5937-005<?php echo $value['category_id']; ?></p>
          </div>
          <div class="vstack border-top gap-2 pb-0 p-3">
            <a type="button" href="<?= base_url('account/category/edit/' . $value['category_id']); ?>" class="btn btn-warning">แก้ไขกระเป๋าตังค์</a>
            <button type="delete" name="delete" value="<?php echo $value['category_id']; ?>" id="<?php echo $value['category_id']; ?>" type="submit" class="btn btn-outline-danger" onclick="archiveFunction(this.id)">ลบกระเป๋าตังค์</button>
          </div>
          <div class="card-footer">
            <small class="text-muted">สร้างเมื่อ: <?php echo $value['create_at']; ?></small>
          </div>
        </div>
      </div>
  <?php
    }
  }
  ?>
  <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h1 class="modal-title fs-5" id="exampleModalLabel">เพิ่มกระเป๋าเงิน</h1>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <form class="" action="<?= base_url('/account/category/add') ?>" method="post">
            <div class="form-group">
              <label for="email">รายการ</label>
              <input type="text" class="form-control" value="wallet" disabled>
            </div>
            <div class="form-group">
              <label for="email">ชื่อกระเป๋าเงิน</label>
              <input type="text" class="form-control" name="topic" id="topic" placeholder="ตั่งชื่อกระเป๋าตังค์ของคุณ...">
            </div><br>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
              <button type="submit" class="btn btn-success">เพิ่มกระเป๋าเงิน</button>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
</div>
</div>
</div>


<script type="text/javascript">
  $(document).ready(function() {
    $('[data-toggle="tooltip"]').tooltip();
  });

  $('#reloadpage').click(function() {

    location.reload(true);

  });

  function archiveFunction(id) {
    event.preventDefault(); // prevent form submit
    var form = event.target.form; // storing the form
    swal({
        title: "คุณแน่ใจใช่ไหม?",
        text: "เมื่อคุณลบกระเป๋าใบนี้ไปแล้วคุณจะไม่สามารถกู้คืนได้อีก",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#F83D3D",
        confirmButtonText: "ใช่, ต้องการลบ!",
        cancelButtonText: "ไม่, ไม่ต้องการลบ!",
        closeOnConfirm: false,
        closeOnCancel: false
      },
      function(isConfirm) {
        if (isConfirm) {
          window.location = "/account/category/delete/" + id;

        } else {

          setTimeout(function() {
            swal({
              title: "Cancelled!",
              text: "ลบข้อมูลไม่สำเร็จ!",
              type: "error",
              timer: 1000,
            })
          });
        }
      });
  }
</script>