-- Workplace Registration form and modal

INSERT INTO forms (id, name, description, mapped_table, tenant_id, branch_id)
SELECT 98, 'Workplace Registration', 'Record new workplaces', 'workplace_registrations', NULL, NULL
WHERE NOT EXISTS (SELECT 1 FROM forms WHERE id = 98);

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 98, 'company_name', 'company_name', 'company_name', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 1
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 98 AND field_name = 'company_name');

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 98, 'address', 'address', 'address', (SELECT id FROM element_types WHERE name='textarea' LIMIT 1), 2
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 98 AND field_name = 'address');

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 98, 'tin', 'tin', 'tin', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 3
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 98 AND field_name = 'tin');

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 98, 'contact_person', 'contact_person', 'contact_person', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 4
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 98 AND field_name = 'contact_person');

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 98, 'contact_email', 'contact_email', 'contact_email', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 5
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 98 AND field_name = 'contact_email');

INSERT INTO form_elements (form_id, label, field_name, data_column, type_id, order_index)
SELECT 98, 'contact_phone', 'contact_phone', 'contact_phone', (SELECT id FROM element_types WHERE name='text' LIMIT 1), 6
WHERE NOT EXISTS (SELECT 1 FROM form_elements WHERE form_id = 98 AND field_name = 'contact_phone');

INSERT INTO table_views (form_id, name, columns, sort_by, sort_dir)
SELECT 98, 'Default', '["company_name","status"]', 'company_name', 'ASC'
WHERE NOT EXISTS (SELECT 1 FROM table_views WHERE form_id = 98 AND name = 'Default');

INSERT INTO ui_modals (name, config, tenant_id, branch_id, icon, aria_label, description)
SELECT 'Workplace Registration',
       '{"title":"Workplace Registration","header":[{"type":"button","label":"New Registration","formName":"Workplace Registration"}],"body":[{"type":"table","options":{"tableName":"workplace_registrations","rowClick":"selectRecord","columnsToShow":["company_name","status"],"rowActions":[{"label":"Detail","onClick":"openSelectedDetail"}],"inlineEdit":true,"editFormId":98}}],"footer":[{"type":"button","label":"Close","onClick":"closeModal"}]}',
       NULL, NULL, 'fa-building', 'Workplace Registration', 'Workplace Registration'
WHERE NOT EXISTS (SELECT 1 FROM ui_modals WHERE name = 'Workplace Registration');
