-- Update Case Hour form definition and table view

UPDATE forms
SET elements = $json$
[{"label":"Case","field_name":"case_id","data_column":"case_id","type_id":4,"options_table":"cases","options_label_column":"title","options_value_column":"id","order_index":1},
 {"label":"User","field_name":"user_id","data_column":"user_id","type_id":4,"options_table":"users","options_label_column":"full_name","options_value_column":"id","order_index":2},
 {"label":"Hours","field_name":"hours","data_column":"hours","type_id":2,"order_index":3},
 {"label":"Rate","field_name":"rate","data_column":"rate","type_id":2,"order_index":4},
 {"label":"Description","field_name":"description","data_column":"description","type_id":7,"order_index":5},
 {"label":"Billed To Client","field_name":"billed_to_client","data_column":"billed_to_client","type_id":5,"order_index":6},
 {"label":"Approved","field_name":"approved","data_column":"approved","type_id":5,"order_index":7},
 {"label":"Work Type","field_name":"work_type","data_column":"work_type","type_id":1,"order_index":8},
 {"label":"Title","field_name":"title","data_column":"title","type_id":1,"order_index":9},
 {"label":"Instructions","field_name":"instructions","data_column":"instructions","type_id":7,"order_index":10},
 {"label":"Posted","field_name":"posted","data_column":"posted","type_id":16,"order_index":11},
 {"label":"Documents","field_name":"documents","data_column":"document_ids","type_id":7,"order_index":12,"options":{"linkTable":"case_hour_documents","linkField":"document_id"}}]
$json$
WHERE id = 38;

UPDATE table_views
SET columns = '["case_id","user_id","hours","rate","approved"]',
    sort_by = 'case_id',
    sort_dir = 'ASC'
WHERE form_id = 38 AND name = 'Default';

INSERT INTO table_views (form_id,name,columns,sort_by,sort_dir)
SELECT 38,'Default','["case_id","user_id","hours","rate","approved"]','case_id','ASC'
WHERE NOT EXISTS (SELECT 1 FROM table_views WHERE form_id = 38 AND name = 'Default');
