{% extends "page.html" %}
{% block page_content %}
<div class="card">
    <div class="card-header"><h2>Объекты</h2>
        <button class="btn btn-primary btn-sm" onclick="document.getElementById('modal-object').classList.add('open')">+ Добавить</button>
    </div>
    <form method="get" class="filter-bar">
        <input type="text" name="search" placeholder="Поиск..." value="{{ search }}">
        <select name="customer_id" onchange="this.form.submit()"><option value="">Все клиенты</option>{% for c in customers_list %}<option value="{{ c.id }}" {% if customer_id == c.id %}selected{% endif %}>{{ c.name }}</option>{% endfor %}</select>
        <button class="btn btn-primary btn-sm">Найти</button>
    </form>
    <div class="table-wrap"><table><thead><tr><th>Название</th><th>Клиент</th><th>Тип</th><th>Адрес</th><th>Статус</th><th class="actions"></th></tr></thead>
        <tbody>{% for o in objects %}<tr>
            <td>{{ o.name }}</td><td>{{ o.customer.name if o.customer else '' }}</td>
            <td>{{ o.object_type }}</td><td><small>{{ o.address }}</small></td>
            <td><span class="badge badge-{{ o.status }}">{{ {'active':'Активен','inactive':'Неактивен','audit':'Аудит','prospective':'Перспективный'}[o.status] }}</span></td>
            <td class="actions">
                <button class="btn btn-secondary btn-sm" onclick="editObject({{ o.id }},'{{ o.name|e }}','{{ o.customer_id or '' }}','{{ o.object_type|e }}','{{ o.address|e }}','{{ o.contact_person|e }}','{{ o.contact_phone|e }}','{{ o.status }}','{{ o.notes|e }}')">✎</button>
                {% if user.role == 'owner' %}
                <form method="post" action="/service/objects/delete" style="display:inline" onsubmit="return confirm('Удалить объект?')"><input type="hidden" name="id" value="{{ o.id }}"><button class="btn btn-danger btn-sm">✕</button></form>
                {% endif %}
            </td>
        </tr>{% endfor %}</tbody></table>
    </div>
</div>
<div class="modal-overlay" id="modal-object"><div class="modal">
    <button type="button" class="modal-close" onclick="this.closest('.modal-overlay').classList.remove('open')">✕</button>
    <h2 id="modal-object-title">Новый объект</h2>
    <form method="post" id="modal-object-form" action="/service/objects/create">
        <input type="hidden" name="id" id="object-id">
        <div class="form-row">
            <div class="form-group"><label>Название</label><input type="text" name="name" id="object-name" required></div>
            <div class="form-group"><label>Клиент</label><select name="customer_id" id="object-customer_id"><option value="">—</option>{% for c in customers_list %}<option value="{{ c.id }}">{{ c.name }}</option>{% endfor %}</select></div>
        </div>
        <div class="form-row">
            <div class="form-group"><label>Тип объекта</label><select name="object_type" id="object-object_type"><option value="">— Выберите —</option><option value="office">Офис</option><option value="warehouse">Склад</option><option value="production">Производство</option><option value="retail">Торговое помещение</option><option value="residential">Жилой комплекс</option><option value="construction">Стройка</option><option value="other">Другое</option></select></div>
            <div class="form-group"><label>Статус</label><select name="status" id="object-status"><option value="active">Активен</option><option value="inactive">Неактивен</option><option value="audit">Аудит</option><option value="prospective">Перспективный</option></select></div>
        </div>
        <div class="form-group"><label>Адрес</label><input type="text" name="address" id="object-address"></div>
        <div class="form-row">
            <div class="form-group"><label>Контактное лицо</label><input type="text" name="contact_person" id="object-contact_person"></div>
            <div class="form-group"><label>Телефон</label><input type="text" name="contact_phone" id="object-contact_phone"></div>
        </div>
        <div class="form-group"><label>Заметки</label><textarea name="notes" id="object-notes"></textarea></div>
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Сохранить</button>
            <button type="button" class="btn btn-secondary" onclick="this.closest('.modal-overlay').classList.remove('open')">Отмена</button>
        </div>
    </form>
</div></div>
<script>
function editObject(id,name,cid,type,addr,person,phone,status,notes){
    document.getElementById('modal-object').classList.add('open');
    document.getElementById('modal-object-title').textContent = 'Редактировать объект';
    document.getElementById('modal-object-form').action = '/service/objects/edit';
    document.getElementById('object-id').value = id;
    document.getElementById('object-name').value = name;
    document.getElementById('object-customer_id').value = cid;
    document.getElementById('object-object_type').value = type;
    document.getElementById('object-address').value = addr;
    document.getElementById('object-contact_person').value = person;
    document.getElementById('object-contact_phone').value = phone;
    document.getElementById('object-status').value = status;
    document.getElementById('object-notes').value = notes;
}
</script>
{% endblock %}
