{% extends 'dashboard/admin_dashboard/main.html' %}
{% load static %}
{% block content %}
<h4 class="fw-bold py-3 mb-4">Add Users</h4>
<div style="padding: 10px;">
  {% if messages %}
    {% for message in messages %}
      <div class="alert {% if 'success' in message.tags %}alert-success alert-dismissible{% elif 'danger' in message.tags %}alert-danger alert-dismissible{% endif %}" role="alert" id="success-alert">
        {{ message }}
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
      </div>
    {% endfor %}
  {% endif %}
</div>

<div class="row">
  <div class="col-md-12">
    <div class="card mb-4">
      <form method="POST" enctype="multipart/form-data" action="" autocomplete="off">
        {% csrf_token %}
        <div class="card-body">
          <!-- Name Fields -->
          <div class="row py-2">
            <div class="col-md-6">
              <label class="form-label">First Name<span style="color:red">*</span></label>
              <input type="text" class="form-control" name="first_name" required>
            </div>
            <div class="col-md-6">
              <label class="form-label">Last Name<span style="color:red">*</span></label>
              <input type="text" class="form-control" name="last_name" required>
            </div>
          </div>

          <!-- Email, Employee Code, Password -->
          <div class="row py-2">
            <div class="col-md-4">
              <label class="form-label">Email<span id="email-required" style="color:red">*</span></label>
              <input type="email" class="form-control" name="email" id="email">
            </div>
            <div class="col-md-4">
              <label class="form-label">Employee Code<span style="color:red">*</span></label>
              <input type="text" class="form-control" name="employee_code" required>
            </div>
            <div class="col-md-4">
              <label class="form-label">Password<span style="color:red">*</span></label>
              <input type="password" class="form-control" name="password" required>
            </div>
          </div>

          <!-- Company & Department -->
          <div class="row py-2">
            <div class="col-md-6">
              <label class="form-label">Company<span style="color:red">*</span></label>
              <select name="company_id" class="form-control" required>
                <option value="">-- Select Company --</option>
                {% for companyes in companyes %}
                <option value="{{ companyes.id }}">{{ companyes.company_name }}</option>
                {% endfor %}
              </select>
            </div>
            <div class="col-md-6">
              <label class="form-label">Department<span style="color:red">*</span></label>
              <select name="department_id" class="form-control" required>
                <option value="">-- Select Department --</option>
                {% for departments in departmentes %}
                <option value="{{ departments.id }}">{{ departments.department_name }}</option>
                {% endfor %}
              </select>
            </div>
          </div>

          <!-- Mobile & Location -->
          <div class="row py-2">
            <div class="col-md-6">
              <label class="form-label">Mobile<span style="color:red">*</span></label>
              <input type="text" class="form-control" name="mobile" minlength="10" maxlength="11" oninput="validateNumericInput(this)" required>
            </div>
            <div class="col-md-6">
              <label class="form-label">Location<span style="color:red">*</span></label>
              <select name="location_id" class="form-control" required>
                <option value="">-- Select Location --</option>
                {% for locationes in locationes %}
                <option value="{{ locationes.id }}">{{ locationes.location_name }}</option>
                {% endfor %}
              </select>
            </div>
          </div>

          <!-- Roles -->
          <div class="col-md-12 py-2">
            <label class="form-label">Roles<span style="color:red">*</span></label>
            <select name="roles_id[]" id="rolest_id" class="form-control" multiple="multiple" required>
              {% for roles in roles %}
              <option value="{{ roles.name }}">{{ roles.name }}</option>
              {% endfor %}
            </select>
          </div>

          <!-- Designation -->
          <div class="col-md-12 py-2">
            <label class="form-label">Designation<span style="color:red">*</span></label>
            <select name="designation_id" class="form-control" required>
              <option value="">-- Select Designation --</option>
              {% for designation in designationes %}
              <option value="{{ designation.id }}">{{ designation.name }}</option>
              {% endfor %}
            </select>
          </div>

          <!-- Approval Require -->
          <div class="col-md-12 py-2" id="approval_require_div" style="display:none;">
            <label class="form-label">Approval require or not?<span style="color:red">*</span></label>
            <select name="approval_require" id="approval_require" class="form-control">
              <option value="">-- Select Approval Require --</option>
              <option value="1">Yes</option>
              <option value="0">No</option>
            </select>
          </div>

          <!-- Gender -->
          <div class="col-md-12 py-2">
            <label class="form-label">Gender<span style="color:red">*</span></label>
            <select name="gender" class="form-control" required>
              <option value="">-- Select Gender --</option>
              <option>Male</option>
              <option>Female</option>
            </select>
          </div>

          <!-- Address -->
          <div class="col-md-12 py-2">
            <label class="form-label">Address<span style="color:red">*</span></label>
            <textarea class="form-control" name="address" rows="3" required></textarea>
          </div>

          <!-- Image -->
          <div class="col-md-12 py-2">
            <label class="form-label">User Image</label>
            <input type="file" name="image" class="form-control" accept=".png,.jpg,.jpeg">
          </div>

          <!-- Status -->
          <div class="col-md-12 py-2">
            <label class="form-label">Status<span style="color:red">*</span></label>
            <select name="status" class="form-control" required>
              <option value="1">Active</option>
              <option value="0">Inactive</option>
            </select>
          </div>

          <!-- Submit -->
          <div class="mt-3 d-flex justify-content-end">
            <button type="submit" class="btn btn-primary" id="btn_submit">Submit</button>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- JS -->
<script src="{% static 'plugins/jQuery/jQuery-2.1.3.min.js' %}"></script>
<script src="{% static 'select2/js/select2.min.js' %}"></script>

<script>
  $(document).ready(function () {
    $('#rolest_id').select2({
      placeholder: "Select Roles",
      width: '100%'
    });

    function toggleEmailRequirement() {
      let selectedRoles = $('#rolest_id').val() || [];
      if (selectedRoles.includes('Employee')) {
        $('#email').removeAttr('required');
        $('#email-required').hide();
        $('#approval_require_div').show();
        $('#approval_require').attr('required', 'required');
      } else {
        $('#email').attr('required', 'required');
        $('#email-required').show();
        $('#approval_require_div').hide();
        $('#approval_require').removeAttr('required').val('');
      }
    }

    toggleEmailRequirement();

    $('#rolest_id').on('change', function () {
      toggleEmailRequirement();
    });
  });

  function validateNumericInput(input) {
    input.value = input.value.replace(/[^0-9]/g, '');
  }
</script>
{% endblock %}
