SET ":created" = COALESCE(:created, (SELECT created FROM code WHERE hash = :hash));
SET ":expiry" = COALESCE(:expiry, (SELECT expiry FROM code_detail WHERE hash = :hash));
SET ":value" = COALESCE(:value, (SELECT value FROM code_detail WHERE hash = :hash));
SET ":store" = COALESCE(:store, json_array((SELECT store FROM code_detail WHERE hash = :hash)));
SET ":used" = COALESCE(:used, (SELECT used FROM code_detail WHERE hash = :hash));
SET ":type" = COALESCE(:type, json_array((SELECT type FROM code_detail WHERE hash = :hash)));
SET ":title" = COALESCE(:type->>0, 'New')||' Voucher';
SET ":type" = COALESCE(:type, json_array('Fuel'));
SET ":content" = (SELECT json(content) FROM code WHERE hash = :hash);
SET ":validate" = 'Update';
SET ":action" = (CASE COALESCE(:action, '') WHEN '' THEN NULL ELSE :action END);
SET ":method" = 'post';
SET ":preview" = (SELECT 'data:image/svg+xml;base64,'||svg FROM code WHERE hash = :hash);
SET ":autofill" = TRUE;
SET ":filter_config" = '[
  {"name": "store[]", "required": true},
  {"name": "value", "required": true},
  {"name": "expiry", "required": true},
    { "name": "used", "label": "Used",
      "type": "radio", "value": "true",
      "width": 2
    },
    { "name": "used", "label": "Not Used",
      "type": "radio", "value": "false",
      "width": 2
    }
]';

SET ":inner" = CASE COALESCE(:action, '')
  WHEN 'Update' THEN 'code/update.sql'
  ELSE 'code/form.sql'
END;
SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/theme.sql') AS properties
WHERE COALESCE(:action, '') <> 'Update';
SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/Style.sql') AS properties
WHERE COALESCE(:action, '') <> 'Update';
SELECT 'card' AS component, 1 AS columns
WHERE COALESCE(:action, '') <> 'Update';

SELECT COALESCE(:store->>'0'||' ', '') || COALESCE(:expiry, :created, '') AS title
, '

Type: '||COALESCE(:type->>'0','')||'

Value: '||COALESCE(:value,'')||'

Expires: '||COALESCE(:expiry,'')||'

Submitted: '||COALESCE(:created,'')||'


| Type | Content |
|:-----|:--------|
| ' || COALESCE(:content->>'format', '') || ' | ' || COALESCE(:content->>'content', '') || ' |
' AS description_md
, :preview AS top_image
, :tabler_color AS color
WHERE COALESCE(:action, '') <> 'Update';
;

SELECT 'dynamic' AS component, sqlpage.run_sql(:inner) AS properties;

SELECT 'table' AS component;
SELECT * FROM code_detail
WHERE hash = :hash;
