{% extends "base.html" %}
{% load static %}
{% block title %}Create Service{% endblock %}
{% block extra_css %}
  <link href="https://cdn.jsdelivr.net/npm/summernote@0.9.0/dist/summernote.min.css" rel="stylesheet">
{% endblock %}
{% block content %}
<div class="container mt-4">
    <h2>Create Service</h2>
    <form method="post" enctype="multipart/form-data" class="row g-3">
        {% csrf_token %}
        <div class="col-md-6">
            <label for="service_name" class="form-label">Service Name</label>
            <input type="text" class="form-control" id="service_name" name="service_name" required>
        </div>
        <div class="col-md-6">
            <label for="status" class="form-label">Status</label>
            <select class="form-select" id="status" name="status">
                <option value="active">Active</option>
                <option value="inactive">Inactive</option>
            </select>
        </div>
        <div class="col-12">
            <label for="description" class="form-label">Description</label>
            <textarea class="form-control summernote" id="description" name="description" rows="3" required></textarea>
        </div>
    <div class="col-12">
      <label class="form-label">Conditions</label>
      <div id="conditions-list">
        <div class="row mb-2 condition-row">
          <div class="col-md-5">
            <input type="text" class="form-control" name="condition_key[]" placeholder="Key">
          </div>
          <div class="col-md-5">
            <input type="text" class="form-control" name="condition_value[]" placeholder="Value">
          </div>
          <div class="col-md-2">
            <button type="button" class="btn btn-success add-condition">+</button>
          </div>
        </div>
      </div>
    </div>
        <div class="col-12">
            <label for="image" class="form-label">Image</label>
            <input type="file" class="form-control" id="image" name="image">
        </div>
        <div class="col-12">
            <button type="submit" class="btn btn-primary">Create Service</button>
            <a href="{% url 'service-management' %}" class="btn btn-secondary">Back</a>
        </div>
    </form>
</div>
{% endblock %}
{% block extra_js %}
  <script src="https://cdn.jsdelivr.net/npm/summernote@0.9.0/dist/summernote.min.js"></script>
  <script>
    $(function() {
      $('.summernote').summernote({
        height: 150
      });

      function updateConditionButtons() {
        var rows = $('#conditions-list .condition-row');
        rows.find('.add-condition').remove();
        rows.find('.remove-condition').remove();
        rows.each(function(i) {
          if (i === rows.length - 1) {
            $(this).find('.col-md-2').append('<button type="button" class="btn btn-success add-condition">+</button>');
          } else {
            $(this).find('.col-md-2').append('<button type="button" class="btn btn-danger remove-condition">-</button>');
          }
        });
      }
      updateConditionButtons();
      $('#conditions-list').on('click', '.add-condition', function() {
        var row = `<div class="row mb-2 condition-row">
            <div class="col-md-5">
                <input type="text" class="form-control" name="condition_key[]" placeholder="Key">
            </div>
            <div class="col-md-5">
                <input type="text" class="form-control" name="condition_value[]" placeholder="Value">
            </div>
            <div class="col-md-2"></div>
        </div>`;
        $('#conditions-list').append(row);
        updateConditionButtons();
      });
      $('#conditions-list').on('click', '.remove-condition', function() {
        $(this).closest('.condition-row').remove();
        updateConditionButtons();
      });
    });
  </script>
{% endblock %}
