<?php include 'header.php'; ?>

<div class="content-wrapper">

  <section class="content-header">
    <h1>
      Setting Profil
      <small>Profil Perusahaan / Aplikasi</small>
    </h1>
    <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
      <li class="active">Setting Profil</li>
    </ol>
  </section>

  <section class="content">
    <div class="row">
      <section class="col-lg-12">
        <div class="box box-info">

          <div class="box-header">
            <h3 class="box-title">Profil Perusahaan / Aplikasi</h3>
          </div>
          <div class="box-body">

            <?php 
            include '../koneksi.php';
            
            // Cek apakah tabel sudah ada
            $cek_tabel = mysqli_query($koneksi, "SHOW TABLES LIKE 'profil_perusahaan'");
            if(mysqli_num_rows($cek_tabel) == 0){
              // Buat tabel jika belum ada
              mysqli_query($koneksi, "CREATE TABLE IF NOT EXISTS `profil_perusahaan` (
                `profil_id` int(11) NOT NULL AUTO_INCREMENT,
                `profil_nama` varchar(255) NOT NULL,
                `profil_alamat` text,
                `profil_telepon` varchar(50) DEFAULT NULL,
                `profil_email` varchar(100) DEFAULT NULL,
                `profil_website` varchar(255) DEFAULT NULL,
                `profil_logo` varchar(255) DEFAULT NULL,
                `profil_deskripsi` text,
                PRIMARY KEY (`profil_id`)
              ) ENGINE=InnoDB DEFAULT CHARSET=latin1") or die(mysqli_error($koneksi));
            }
            
            // Cek apakah data sudah ada, jika belum buat data default
            $cek = mysqli_query($koneksi, "SELECT * FROM profil_perusahaan LIMIT 1");
            if(mysqli_num_rows($cek) == 0){
              // Buat data default
              mysqli_query($koneksi, "INSERT INTO profil_perusahaan (profil_nama, profil_alamat, profil_telepon, profil_email, profil_website, profil_logo, profil_deskripsi) VALUES ('Nama Perusahaan', 'Alamat Perusahaan', '081234567890', 'email@perusahaan.com', 'www.perusahaan.com', '', '')") or die(mysqli_error($koneksi));
            }
            
            $data = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT * FROM profil_perusahaan LIMIT 1"));
            ?>

            <form action="setting_profil_update.php" method="post" enctype="multipart/form-data">
              <div class="row">
                <div class="col-md-6">
                  <div class="form-group">
                    <label>Nama Perusahaan / Aplikasi</label>
                    <input type="text" name="nama_perusahaan" class="form-control" placeholder="Nama Perusahaan" value="<?php echo $data['profil_nama'] ?? ''; ?>" required>
                  </div>
                </div>
                <div class="col-md-6">
                  <div class="form-group">
                    <label>Alamat</label>
                    <textarea name="alamat" class="form-control" rows="3" placeholder="Alamat Perusahaan" required><?php echo $data['profil_alamat'] ?? ''; ?></textarea>
                  </div>
                </div>
              </div>

              <div class="row">
                <div class="col-md-4">
                  <div class="form-group">
                    <label>Telepon</label>
                    <input type="text" name="telepon" class="form-control" placeholder="Nomor Telepon" value="<?php echo $data['profil_telepon'] ?? ''; ?>">
                  </div>
                </div>
                <div class="col-md-4">
                  <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" placeholder="Email Perusahaan" value="<?php echo $data['profil_email'] ?? ''; ?>">
                  </div>
                </div>
                <div class="col-md-4">
                  <div class="form-group">
                    <label>Website</label>
                    <input type="text" name="website" class="form-control" placeholder="Website (contoh: www.perusahaan.com)" value="<?php echo $data['profil_website'] ?? ''; ?>">
                  </div>
                </div>
              </div>

              <div class="form-group">
                <label>Logo Perusahaan</label>
                <?php if(!empty($data['profil_logo']) && file_exists('../gambar/logo/'.$data['profil_logo'])){ ?>
                  <div style="margin-bottom: 10px;">
                    <img src="../gambar/logo/<?php echo $data['profil_logo']; ?>" alt="Logo" style="max-width: 200px; max-height: 100px; border: 1px solid #ddd; padding: 5px; background: #f9f9f9;">
                    <br>
                    <small class="text-muted">Logo saat ini: <?php echo $data['profil_logo']; ?></small>
                  </div>
                <?php } else { ?>
                  <div style="margin-bottom: 10px; padding: 10px; background: #f9f9f9; border: 1px dashed #ddd; color: #999;">
                    <i class="fa fa-image"></i> Belum ada logo
                  </div>
                <?php } ?>
                <input type="file" name="logo" class="form-control" accept="image/jpeg,image/jpg,image/png,image/gif">
                <small class="text-info"><i class="fa fa-info-circle"></i> Format: JPG, PNG, GIF. Maksimal 2MB. Kosongkan jika tidak ingin mengubah logo.</small>
              </div>

              <div class="form-group">
                <label>Deskripsi / Keterangan</label>
                <textarea name="deskripsi" class="form-control" rows="4" placeholder="Deskripsi perusahaan atau aplikasi"><?php echo $data['profil_deskripsi'] ?? ''; ?></textarea>
              </div>

              <div class="form-group">
                <button type="submit" class="btn btn-primary">
                  <i class="fa fa-save"></i> Simpan Perubahan
                </button>
                <button type="reset" class="btn btn-default">
                  <i class="fa fa-refresh"></i> Reset
                </button>
              </div>
            </form>

          </div>

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

</div>

<?php include 'footer.php'; ?>

<script>
$(document).ready(function() {
  // SweetAlert Notifications
  <?php 
  if(isset($_SESSION['alert']) && isset($_SESSION['message'])):
    $alertType = $_SESSION['alert'];
    $message = $_SESSION['message'];
    unset($_SESSION['alert']);
    unset($_SESSION['message']);
    
    if($alertType == 'success'):
    ?>
      Swal.fire({
        icon: 'success',
        title: 'Berhasil!',
        text: <?php echo json_encode($message); ?>,
        confirmButtonColor: '#3085d6',
        confirmButtonText: 'OK',
        timer: 3000,
        timerProgressBar: true
      });
    <?php elseif($alertType == 'error'): ?>
      Swal.fire({
        icon: 'error',
        title: 'Gagal!',
        text: <?php echo json_encode($message); ?>,
        confirmButtonColor: '#d33',
        confirmButtonText: 'OK'
      });
    <?php elseif($alertType == 'warning'): ?>
      Swal.fire({
        icon: 'warning',
        title: 'Peringatan!',
        text: <?php echo json_encode($message); ?>,
        confirmButtonColor: '#ffc107',
        confirmButtonText: 'OK'
      });
    <?php endif; ?>
  <?php endif; ?>
});
</script>
