{% extends "base.html" %}
{% block title %}My Profile{% endblock %}

{% block content %}
<style>
  :root {
    --primary-color: #667eea;
    --success-color: #10b981;
    --bg-card: #ffffff;
    --border-color: #e2e8f0;
    --text-dark: #1e293b;
    --text-light: #64748b;
    --shadow-md: 0 4px 6px rgba(0,0,0,0.1);
  }

  .page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-radius: 12px;
    padding: 2rem;
    margin-bottom: 2rem;
    box-shadow: var(--shadow-md);
  }

  .page-title {
    font-size: 2rem;
    font-weight: 800;
    margin-bottom: 0.5rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
  }

  .page-subtitle {
    font-size: 1rem;
    opacity: 0.9;
  }

  .profile-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
  }

  .section-card {
    background: var(--bg-card);
    border-radius: 12px;
    padding: 2rem;
    box-shadow: var(--shadow-md);
    border: 2px solid var(--border-color);
  }

  .section-title {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-dark);
    margin-bottom: 1.5rem;
    padding-bottom: 0.75rem;
    border-bottom: 2px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .info-group {
    margin-bottom: 1.25rem;
  }

  .info-label {
    font-weight: 700;
    color: var(--text-light);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.5rem;
  }

  .info-value {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-dark);
  }

  .badge {
    padding: 0.4rem 0.8rem;
    border-radius: 6px;
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
  }

  .badge-admin {
    background: linear-gradient(135deg, #8b5cf6 0%, #6d28d9 100%);
    color: white;
  }

  .badge-viewer {
    background: linear-gradient(135deg, #3b82f6 0%, #1e40af 100%);
    color: white;
  }

  .form-label {
    font-weight: 700;
    color: var(--text-dark);
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    margin-bottom: 0.5rem;
  }

  .form-control {
    border: 2px solid var(--border-color);
    border-radius: 8px;
    padding: 0.65rem 0.9rem;
    transition: all 0.3s ease;
  }

  .form-control:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    outline: none;
  }

  .btn {
    padding: 0.75rem 1.75rem;
    border-radius: 8px;
    font-weight: 600;
    transition: all 0.3s ease;
    text-transform: uppercase;
    font-size: 0.9rem;
    letter-spacing: 0.03em;
  }

  .btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
  }

  .btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
  }

  .btn-success {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    border: none;
  }

  .btn-success:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
  }

  .avatar {
    width: 100px;
    height: 100px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 3rem;
    color: white;
    margin-bottom: 1rem;
    box-shadow: 0 10px 25px rgba(102, 126, 234, 0.3);
  }

  .profile-info {
    display: flex;
    align-items: center;
    gap: 2rem;
    margin-bottom: 2rem;
    padding: 2rem;
    background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
    border-radius: 12px;
  }

  .profile-details h3 {
    font-size: 1.75rem;
    font-weight: 800;
    color: var(--text-dark);
    margin-bottom: 0.5rem;
  }

  .profile-meta {
    color: var(--text-light);
    font-size: 0.95rem;
  }

  @media (max-width: 768px) {
    .profile-grid {
      grid-template-columns: 1fr;
    }

    .page-header {
      padding: 1.5rem;
    }

    .page-title {
      font-size: 1.5rem;
    }

    .section-card {
      padding: 1.5rem;
    }

    .profile-info {
      flex-direction: column;
      text-align: center;
    }

    .avatar {
      margin: 0 auto 1rem;
    }
  }

  .alert {
    border-radius: 10px;
    border: none;
    padding: 1rem 1.25rem;
  }
</style>

<div class="page-header">
  <div class="page-title">
    <span>👤</span>
    <span>My Profile</span>
  </div>
  <div class="page-subtitle">View and manage your account settings</div>
</div>

<!-- Profile Overview -->
<div class="section-card">
  <div class="profile-info">
    <div class="avatar">
      {{ (user.full_name or user.username)[0].upper() }}
    </div>
    <div class="profile-details">
      <h3>{{ user.full_name or user.username }}</h3>
      <div class="profile-meta">
        <span class="badge badge-{{ user.role }}">
          {% if user.role == 'admin' %}👑 Admin{% else %}👁️ Viewer{% endif %}
        </span>
        <span class="ms-3">@{{ user.username }}</span>
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-6">
      <div class="info-group">
        <div class="info-label">Username</div>
        <div class="info-value">{{ user.username }}</div>
      </div>
    </div>

    <div class="col-md-6">
      <div class="info-group">
        <div class="info-label">Role</div>
        <div class="info-value">
          {% if user.role == 'admin' %}
          👑 Administrator
          {% else %}
          👁️ Viewer
          {% endif %}
        </div>
      </div>
    </div>

    <div class="col-md-6">
      <div class="info-group">
        <div class="info-label">Email</div>
        <div class="info-value">{{ user.email or 'Not set' }}</div>
      </div>
    </div>

    <div class="col-md-6">
      <div class="info-group">
        <div class="info-label">Full Name</div>
        <div class="info-value">{{ user.full_name or 'Not set' }}</div>
      </div>
    </div>

    <div class="col-md-6">
      <div class="info-group">
        <div class="info-label">Account Created</div>
        <div class="info-value">{{ user.created_at[:10] if user.created_at else 'N/A' }}</div>
      </div>
    </div>

    <div class="col-md-6">
      <div class="info-group">
        <div class="info-label">Last Login</div>
        <div class="info-value">{{ user.last_login[:16] if user.last_login else 'Never' }}</div>
      </div>
    </div>
  </div>
</div>

<!-- Edit Profile & Change Password Grid -->
<div class="profile-grid">
  <!-- Update Profile -->
  <div class="section-card">
    <div class="section-title">
      <span>✏️</span>
      <span>Update Profile</span>
    </div>
    
    <form method="POST" action="{{ url_for('update_profile') }}">
      <div class="mb-3">
        <label class="form-label">Full Name</label>
        <input type="text" name="full_name" class="form-control" value="{{ user.full_name or '' }}" placeholder="Your full name">
      </div>
      
      <div class="mb-3">
        <label class="form-label">Email</label>
        <input type="email" name="email" class="form-control" value="{{ user.email or '' }}" placeholder="your.email@example.com">
      </div>
      
      <button type="submit" class="btn btn-primary w-100">
        <span>💾</span> Save Changes
      </button>
    </form>
  </div>

  <!-- Change Password -->
  <div class="section-card">
    <div class="section-title">
      <span>🔒</span>
      <span>Change Password</span>
    </div>
    
    <form method="POST" action="{{ url_for('change_password') }}">
      <div class="mb-3">
        <label class="form-label">Current Password</label>
        <div class="input-group"><input id="currentPassword" type="password" name="current_password" class="form-control" required placeholder="Enter current password"><button class="btn btn-outline-secondary" type="button" onclick="togglePassword('currentPassword', this)">👁️</button></div>
      </div>
      
      <div class="mb-3">
        <label class="form-label">New Password</label>
        <div class="input-group"><input id="newProfilePassword" type="password" name="new_password" class="form-control" required minlength="8" placeholder="Min 8 chars, upper/lower/number/symbol"><button class="btn btn-outline-secondary" type="button" onclick="togglePassword('newProfilePassword', this)">👁️</button></div>
      </div>
      
      <div class="mb-3">
        <label class="form-label">Confirm New Password</label>
        <div class="input-group"><input id="confirmProfilePassword" type="password" name="confirm_password" class="form-control" required minlength="8" placeholder="Confirm new password"><button class="btn btn-outline-secondary" type="button" onclick="togglePassword('confirmProfilePassword', this)">👁️</button></div>
      </div>
      
      <button type="submit" class="btn btn-success w-100">
        <span>🔑</span> Change Password
      </button>
    </form>
  </div>
</div>

<!-- Account Information -->
<div class="section-card">
  <div class="section-title">
    <span>ℹ️</span>
    <span>Account Information</span>
  </div>
  
  <div class="alert alert-info">
    <strong>About Your Account:</strong>
    <ul class="mb-0 mt-2">
      <li>Your current role is <strong>{{ user.role }}</strong></li>
      <li>{% if user.role == 'admin' %}You have full administrative access to all system features{% else %}You can view and manage orders, stock, expenses, and reports{% endif %}</li>
      <li>Contact an administrator if you need to change your role or if you have account issues</li>
      <li>Keep your password secure and change it regularly</li>
    </ul>
  </div>
</div>

{% endblock %}
