<?php
if (!defined('ABSPATH')) exit;

$user = wp_get_current_user();
$avatar_id = (int) get_user_meta((int) $user->ID, 'blr_ba_avatar_id', true);
$avatar_url = '';

if ($avatar_id > 0) {
    $avatar_url = (string) wp_get_attachment_image_url($avatar_id, 'thumbnail');
}


if ($avatar_url === '') {
    $avatar_url = BLR_BA_URL . 'assets/blr-ba/img/placeholder-image.png';
}

// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only query args used for post-redirect notices.
$updated = isset($_GET['updated']) ? absint((string) wp_unslash($_GET['updated'])) === 1 : false;
$err = \BLR\Backline\Core\ProfileHandler::consume_profile_error((int) $user->ID);
if ($err === '' && isset($_GET['err'])) {
    $err = sanitize_text_field((string) wp_unslash($_GET['err']));
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
?>
<div class="container-fluid p-0 blr-ba-profile-page">
  <div class="d-flex flex-column flex-lg-row align-items-lg-center justify-content-between gap-3 mb-4">
    <div>
      <h1 class="h3 mb-1"><?php echo esc_html__('Profile', 'blr-backline'); ?></h1>
    </div>
  </div>

  <?php if ($updated): ?>
    <div class="alert alert-success alert-dismissible fade show" role="alert">
      <?php echo esc_html__('Profile updated.', 'blr-backline'); ?>
      <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="<?php echo esc_attr__('Close', 'blr-backline'); ?>"></button>
    </div>
  <?php endif; ?>

  <?php if ($err): ?>
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
      <?php echo esc_html($err); ?>
      <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="<?php echo esc_attr__('Close', 'blr-backline'); ?>"></button>
    </div>
  <?php endif; ?>

  <div class="row">
    <div class="col-md-5 col-xl-3">
      <div class="card mb-3">
        <div class="card-body text-center">
          <img
            id="blr-ba-avatar-summary"
            src="<?php echo esc_url($avatar_url); ?>"
            alt="<?php echo esc_attr($user->display_name); ?>"
            class="img-fluid rounded-circle mb-2 blr-ba-avatar-image"
          >
          <h5 class="card-title mb-0"><?php echo esc_html($user->display_name); ?></h5>
          <div class="text-muted mb-2">
            <?php echo esc_html($user->user_email); ?>
          </div>

          <?php if (!empty($user->user_login)): ?>
            <div class="small text-muted">
              <?php
              printf(
                /* translators: %s: username */
                esc_html__('Username: %s', 'blr-backline'),
                esc_html($user->user_login)
              );
              ?>
            </div>
          <?php endif; ?>

        </div>
      </div>

      <div class="card">
        <div class="card-header">
          <h5 class="card-title mb-0"><?php echo esc_html__('Settings', 'blr-backline'); ?></h5>
        </div>
        <div class="list-group list-group-flush" role="tablist">
          <a
            class="list-group-item list-group-item-action active"
            data-bs-toggle="list"
            href="#blr-ba-profile-account"
            role="tab"
            aria-selected="true"
          >
            <?php echo esc_html__('Account', 'blr-backline'); ?>
          </a>
          <a
            class="list-group-item list-group-item-action"
            data-bs-toggle="list"
            href="#blr-ba-profile-password"
            role="tab"
            aria-selected="false"
          >
            <?php echo esc_html__('Password', 'blr-backline'); ?>
          </a>
        </div>
      </div>
    </div>

    <div class="col-md-7 col-xl-9">
      <div class="tab-content">

        <div class="tab-pane fade show active" id="blr-ba-profile-account" role="tabpanel">
          <div class="card">
            <div class="card-header">
              <h5 class="card-title mb-0"><?php echo esc_html__('Public info', 'blr-backline'); ?></h5>
            </div>
            <div class="card-body">
              <form method="post" enctype="multipart/form-data" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
                <input type="hidden" name="action" value="blr_ba_update_profile_info">
                <?php wp_nonce_field('blr_ba_update_profile'); ?>

                <div class="row">
                  <div class="col-md-8">
                    <div class="mb-3">
                      <label class="form-label" for="display_name"><?php echo esc_html__('Display Name', 'blr-backline'); ?></label>
                      <input
                        class="form-control"
                        id="display_name"
                        name="display_name"
                        type="text"
                        value="<?php echo esc_attr($user->display_name); ?>"
                      >
                    </div>

                    <div class="mb-3">
                      <label class="form-label" for="user_email"><?php echo esc_html__('Email', 'blr-backline'); ?></label>
                      <input
                        class="form-control"
                        id="user_email"
                        name="user_email"
                        type="email"
                        value="<?php echo esc_attr($user->user_email); ?>"
                      >
                    </div>

                    <div class="mb-3">
                      <label class="form-label" for="user_url"><?php echo esc_html__('Website', 'blr-backline'); ?></label>
                      <input
                        class="form-control"
                        id="user_url"
                        name="user_url"
                        type="url"
                        value="<?php echo esc_attr($user->user_url); ?>"
                      >
                    </div>
                  </div>

                  <div class="col-md-4">
                    <div class="text-center">
                      <img
                        id="blr-ba-avatar-preview"
                        class="img-fluid rounded-circle mb-2 blr-ba-image-preview blr-ba-avatar-image"
                        src="<?php echo esc_url($avatar_url); ?>"
                        data-original="<?php echo esc_url($avatar_url); ?>"
                        alt="<?php echo esc_attr($user->display_name); ?>"
                      >

                      <div class="mb-3">
                        <label class="form-label d-block" for="avatar_image"><?php echo esc_html__('Avatar', 'blr-backline'); ?></label>
                        <input
                          id="avatar_image"
                          class="form-control blr-ba-image-input"
                          type="file"
                          name="avatar_image"
                          accept="image/*"
                          data-preview="#blr-ba-avatar-preview"
                        >
                      </div>

                      <?php if ($avatar_id > 0): ?>
                        <div class="form-check text-start">
                          <input class="form-check-input" type="checkbox" value="1" id="remove_avatar" name="remove_avatar">
                          <label class="form-check-label" for="remove_avatar">
                            <?php echo esc_html__('Remove custom avatar', 'blr-backline'); ?>
                          </label>
                        </div>
                      <?php endif; ?>
                    </div>
                  </div>
                </div>

                <button class="btn btn-primary" type="submit">
                  <?php echo esc_html__('Save changes', 'blr-backline'); ?>
                </button>
              </form>
            </div>
          </div>
        </div>

        <div class="tab-pane fade" id="blr-ba-profile-password" role="tabpanel">
          <div class="card">
            <div class="card-header">
              <h5 class="card-title mb-0"><?php echo esc_html__('Password', 'blr-backline'); ?></h5>
            </div>
            <div class="card-body">
              <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
                <input type="hidden" name="action" value="blr_ba_update_password">
                <?php wp_nonce_field('blr_ba_update_profile'); ?>

                <div class="mb-3">
                  <label class="form-label" for="current_password"><?php echo esc_html__('Current password', 'blr-backline'); ?></label>
                  <input
                    class="form-control"
                    id="current_password"
                    type="password"
                    name="current_password"
                    autocomplete="current-password"
                  >
                </div>

                <div class="mb-3">
                  <label class="form-label" for="new_password"><?php echo esc_html__('New password', 'blr-backline'); ?></label>
                  <input
                    class="form-control"
                    id="new_password"
                    type="password"
                    name="new_password"
                    autocomplete="new-password"
                  >
                </div>

                <div class="mb-3">
                  <label class="form-label" for="confirm_password"><?php echo esc_html__('Confirm new password', 'blr-backline'); ?></label>
                  <input
                    class="form-control"
                    id="confirm_password"
                    type="password"
                    name="confirm_password"
                    autocomplete="new-password"
                  >
                </div>

                <button class="btn btn-primary" type="submit">
                  <?php echo esc_html__('Save changes', 'blr-backline'); ?>
                </button>

                <p class="text-muted small mt-2 mb-0">
                  <?php echo esc_html__('If you update your password, you’ll stay logged in.', 'blr-backline'); ?>
                </p>
              </form>
            </div>
          </div>
        </div>

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