<template>
  <div class="voter-nominees-page row col-md-10 col-md-offset-1">
    <div class="col-xs-12 page-column">
      <div class="page-section-heading">Maklumat Calon</div>
      <div class="nominee-page-intro">
        Tekan gambar atau baris calon untuk melihat maklumat penuh.
      </div>

      <div class="panel panel-primary nominee-panel" v-for="position in data.positions" :key="position.id">
        <div class="panel-heading nominee-panel-heading">
          <span>{{ position.name }}</span>
          <span class="nominee-panel-badge">Senarai Calon</span>
        </div>
        <div class="panel-body table-responsive nominee-panel-body">
          <table class="table table-hover nominee-table" id="table-nominee">
            <thead>
              <tr>
                <th></th>
                <th>Bil.</th>
                <th>Nama</th>
                <th>No. Anggota</th>
                <th>Unit</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(nominee, i) in data.nominees" :key="nominee.id" v-if="nominee.position_id == position.id"
                class="nominee-row" @click="viewNomineeDetails(nominee)">
                <td>
                  <img :src="createBase64ImageUrl(nominee.photo)" class="thumbnail nominee-thumb">
                </td>
                <td>{{ nominee.no_calon }}</td>
                <td>{{ nominee.name }}</td>
                <td>{{ nominee.no_anggota }}</td>
                <td>{{ nominee.unit }}</td>
              </tr>
              <tr v-if="!data.nominees.some(nominee => nominee.position_id == position.id)">
                <td colspan="5" class="nominee-empty-state">Tiada calon untuk jawatan ini.</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    <div v-if="showNomineeModal" class="modal-backdrop nominee-modal-backdrop" @click.self="closeNomineeModal">
      <div class="modal-dialog" id="view-nominee-modal">
        <div class="modal-content nominee-modal-content">
          <div class="modal-header">
            <div>
              <h5 class="modal-title">Maklumat Calon</h5>
            </div>
          </div>
          <div class="modal-body nominee-modal-body">
            <div class="photo-section">
              <div class="nominee-photo-card">
                <img :src="nomineeViewed.photo" class="thumbnail nominee-viewed-photo" />
                <div class="nominee-photo-meta">
                  <div class="nominee-name">{{ nomineeViewed.name }}</div>
                </div>
              </div>
            </div>
            <div class="detail-section">
              <div class="nominee-summary-grid">
                <div class="summary-item">
                  <span class="summary-label">Nama</span>
                  <span class="summary-value">{{ nomineeViewed.name || '-' }}</span>
                </div>
                <div class="summary-item">
                  <span class="summary-label">Unit</span>
                  <span class="summary-value">{{ nomineeViewed.unit || '-' }}</span>
                </div>
                <div class="summary-item">
                  <span class="summary-label">Umur</span>
                  <span class="summary-value">{{ nomineeViewed.umur || '-' }}</span>
                </div>
                <div class="summary-item">
                  <span class="summary-label">Jawatan Sekarang</span>
                  <span class="summary-value">{{ nomineeViewed.jawatan_sekarang || '-' }}</span>
                </div>
              </div>

              <div class="nominee-section-card">
                <div class="section-title">Taraf Pendidikan</div>
                <ol v-if="nomineeViewed.education && nomineeViewed.education.length" class="detail-list">
                  <li v-for="(item, index) in nomineeViewed.education" :key="index">{{ item }}</li>
                </ol>
                <p v-else class="detail-empty">Tiada maklumat pendidikan.</p>
              </div>

              <div class="nominee-section-card">
                <div class="section-title">Pengalaman Kerja</div>
                <ol v-if="nomineeViewed.experience && nomineeViewed.experience.length" class="detail-list">
                  <li v-for="(item, index) in nomineeViewed.experience" :key="index">{{ item }}</li>
                </ol>
                <p v-else class="detail-empty">Tiada maklumat pengalaman kerja.</p>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <button class="btn btn-success" @click="closeNomineeModal">Tutup</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      nomineeViewed: {},
      showNomineeModal: false
    };
  },

  methods: {
    viewNomineeDetails(nominee) {
      this.nomineeViewed = { ...nominee };
      this.nomineeViewed.photo = this.createBase64ImageUrl(nominee.photo);
      this.showNomineeModal = true;
    },

    closeNomineeModal() {
      this.showNomineeModal = false;
    },

    createBase64ImageUrl(photo) {
      return `data:image/jpeg;base64,${photo}`;
    }
  }
};
</script>

<style>
.voter-nominees-page {
  margin-top: 24px;
  margin-bottom: 40px;
}

.page-column {
  margin-bottom: 24px;
}

.page-section-heading {
  margin-bottom: 14px;
  padding: 14px 18px;
  border-radius: 14px;
  background: linear-gradient(135deg, #0d6efd, #2f80ed);
  color: #fff;
  font-size: 1.8rem;
  font-weight: 700;
  text-align: center;
  box-shadow: 0 12px 28px rgba(13, 110, 253, 0.2);
}

.nominee-page-intro {
  margin-bottom: 18px;
  padding: 12px 16px;
  border-radius: 12px;
  background: #eef5ff;
  border: 1px solid #dbe8ff;
  color: #36506b;
  font-size: 1.4rem;
}

.nominee-panel {
  margin-bottom: 20px;
  border: 0;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 14px 28px rgba(35, 64, 97, 0.1);
}

.nominee-panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: linear-gradient(135deg, #1d4e89, #2563eb);
  color: #fff;
  font-size: 1.7rem;
  font-weight: 700;
}

.nominee-panel-badge {
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.18);
  font-size: 1.2rem;
  font-weight: 600;
}

.nominee-panel-body {
  padding: 0;
  background: #fff;
}

.nominee-table {
  margin-bottom: 0;
}

.nominee-table thead {
  background: #f4f8fd;
}

.nominee-table thead th {
  border-bottom: 1px solid #dce7f5;
  color: #48627e;
  font-size: 1.25rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.nominee-table tbody td {
  vertical-align: middle;
  border-top: 1px solid #edf2f7;
  color: #22384f;
}

.nominee-row {
  cursor: pointer;
  transition: transform 0.18s ease, box-shadow 0.18s ease, background-color 0.18s ease;
}

.nominee-row:hover {
  background: #f8fbff;
  box-shadow: inset 4px 0 0 #2f80ed;
}

.nominee-thumb {
  width: 64px;
  height: 64px;
  margin-bottom: 0;
  border: 2px solid #dbe8ff;
  border-radius: 16px;
  object-fit: cover;
  box-shadow: 0 8px 18px rgba(47, 128, 237, 0.12);
}

.nominee-empty-state {
  padding: 18px !important;
  text-align: center;
  color: #6f8193;
}

.nominee-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1050;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background: rgba(7, 25, 48, 0.72);
  backdrop-filter: blur(3px);
}

#view-nominee-modal.modal-dialog {
  width: 100%;
  max-width: 960px;
  margin: 0;
}

.nominee-modal-content {
  border: 0;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 22px 60px rgba(0, 0, 0, 0.28);
}

.nominee-modal-content .modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 20px 24px 16px;
  border-bottom: 1px solid #e7edf5;
  background: linear-gradient(135deg, #0d6efd, #2f80ed);
  color: #fff;
}

.nominee-modal-content .modal-title {
  margin: 4px 0 0;
  font-size: 2.2rem;
  font-weight: 700;
}

.nominee-modal-content .modal-header .close {
  margin-top: -4px;
  color: #fff;
  opacity: 0.9;
  text-shadow: none;
}

.nominee-modal-kicker {
  font-size: 1.2rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  opacity: 0.85;
}

.nominee-modal-body {
  display: flex;
  gap: 24px;
  padding: 24px;
  background: #f5f8fc;
}

.photo-section {
  flex: 0 0 300px;
}

.nominee-photo-card {
  padding: 18px;
  border-radius: 18px;
  background: #fff;
  border: 1px solid #e5ebf3;
  box-shadow: 0 10px 24px rgba(35, 64, 97, 0.08);
}

.nominee-viewed-photo {
  width: 100%;
  height: 320px;
  object-fit: contain;
  object-position: center;
  border: 0;
  border-radius: 14px;
  margin: 0 0 16px;
  background: #eef3f8;
}

.nominee-photo-meta {
  text-align: center;
}

.nominee-name {
  font-size: 2rem;
  font-weight: 700;
  color: #17324d;
}

.nominee-subtitle {
  margin-top: 4px;
  font-size: 1.4rem;
  color: #5f7388;
}

.detail-section {
  flex: 1;
  min-width: 0;
}

.nominee-summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.summary-item {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px 16px;
  border-radius: 14px;
  background: #fff;
  border: 1px solid #e5ebf3;
  box-shadow: 0 8px 22px rgba(35, 64, 97, 0.06);
}

.summary-label {
  font-size: 1.2rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #6f8193;
}

.summary-value {
  font-size: 1.6rem;
  font-weight: 600;
  line-height: 1.4;
  color: #17324d;
}

.nominee-section-card {
  margin-bottom: 16px;
  padding: 18px 20px;
  border-radius: 16px;
  background: #fff;
  border: 1px solid #e5ebf3;
  box-shadow: 0 8px 22px rgba(35, 64, 97, 0.06);
}

.section-title {
  margin-bottom: 12px;
  font-size: 1.5rem;
  font-weight: 700;
  color: #17324d;
}

.detail-list {
  margin: 0;
  padding-left: 20px;
  color: #30475f;
}

.detail-list li {
  margin-bottom: 8px;
  line-height: 1.6;
}

.detail-empty {
  margin: 0;
  color: #6f8193;
}

.nominee-modal-content .modal-footer {
  padding: 16px 24px 22px;
  border-top: 1px solid #e7edf5;
  background: #fff;
}

@media (max-width: 767px) {
  .nominee-modal-backdrop {
    align-items: flex-start;
    overflow-y: auto;
    padding: 16px 10px;
  }

  .nominee-modal-body {
    flex-direction: column;
    padding: 18px;
  }

  .photo-section {
    flex: 1 1 auto;
  }

  .nominee-viewed-photo {
    height: 260px;
  }

  .nominee-summary-grid {
    grid-template-columns: 1fr;
  }

  .page-section-heading {
    font-size: 1.6rem;
  }

  .nominee-panel-heading {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  /* Show all columns on mobile; allow horizontal scroll via table-responsive */
  .nominee-thumb {
    width: 52px;
    height: 52px;
    border-radius: 14px;
  }

  .nominee-table thead th {
    font-size: 1.15rem;
  }

  .nominee-table tbody td {
    padding: 10px 8px;
  }
}

@media (max-width: 991px) {
  .voter-nominees-page {
    margin-top: 16px;
  }
}
</style>
