INSERT INTO ohrm_data_group (name, description, can_read, can_create, can_update, can_delete)
VALUES ('preboarding_contact_details', 'Preboarding - Stage 06: Contact Details', 1, 0, 1, 0);

SET @data_group_id := (SELECT `id` FROM `ohrm_data_group` WHERE `name` = 'preboarding_contact_details');
SET @admin_user_role_id := (SELECT `id` FROM `ohrm_user_role` WHERE `name` = 'Admin');
SET @supervisor_id := (SELECT id FROM ohrm_user_role where name = 'Supervisor');
SET @hod_user_role := (SELECT id FROM ohrm_user_role WHERE `name` = 'HeadOfDepartment');
SET @hiring_manager_user_role_id := (SELECT `id` FROM `ohrm_user_role` WHERE `name` = 'HiringManager');
SET @pre_boarding_event_manager_user_role_id := (SELECT `id` FROM `ohrm_user_role` WHERE `name` = 'PreboardingEventManager');
SET @admin_user_role_type := (SELECT `id` FROM `ohrm_user_role_type` WHERE `name` = 'Admin');
SET @supervisor_role_type := (SELECT `id` FROM `ohrm_user_role_type` WHERE `name` = 'Supervisor');
SET @ess_id := (SELECT id FROM ohrm_user_role where name = 'ESS');
SET @ess_role_type := (SELECT `id` FROM `ohrm_user_role_type` WHERE `name` = 'ESS');

INSERT INTO ohrm_user_role_data_group (user_role_id, data_group_id, can_read, can_create, can_update, can_delete, self, restriction_id) VALUES
        (@admin_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL),
        (@supervisor_id, @data_group_id, 1, 0, 1, 0, 0, NULL),
        (@hod_user_role, @data_group_id, 1, 0, 1, 0, 0, NULL),
        (@hiring_manager_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL),
        (@pre_boarding_event_manager_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL),
        (@preboarding_new_hire_user_role_id, @data_group_id, 1, 0, 1, 0, 1, NULL),
        (@ess_id, @data_group_id, 1, 0, 1, 0, 1, NULL);

INSERT INTO ohrm_user_role_type_data_group (user_role_type_id, data_group_id, can_read, can_create, can_update, can_delete) VALUES
        (@admin_user_role_type, @data_group_id, 1, 0, 1, 0),
        (@supervisor_role_type, @data_group_id, 1, 0, 1, 0),
        (@ess_role_type, @data_group_id, 1, 0, 1, 0);

SET @system_user_id := (SELECT `id` FROM `ohrm_user` WHERE `user_name` = 'System');

INSERT INTO ohrm_dynamic_form (`name`, description, `action`, endpoint, meta_data, data_group_id, created_by,
                               updated_by, created_by_name, version)
VALUES ('Preboarding Contact Details', 'Preboarding Contact Details', 'POST',
        'preboarding-contact-details', null, @data_group_id, @system_user_id, @system_user_id, 'System', 1);

SET @dynamic_form_id := (SELECT `id` FROM ohrm_dynamic_form WHERE name = 'Preboarding Contact Details');

INSERT INTO ohrm_dynamic_form_version (`name`, description, `action`, endpoint, meta_data, data_group_id, created_by,
                                       updated_by, created_by_name, version, dynamic_form_id)
VALUES ('Preboarding Contact Details', 'Preboarding Contact Details', 'POST', 'preboarding-contact-details',
        null, @data_group_id, @system_user_id, @system_user_id, 'System', 1, @dynamic_form_id);

SET @dynamic_form_version_id := (SELECT `id` FROM ohrm_dynamic_form_version WHERE name = 'Preboarding Contact Details');

SET @metadata = '{
   "version":{
      "schema-version":"1.0.0"
   },
   "fields":[
      {
         "uid":"address_street_1_006",
         "name":"address_street_1",
         "widget":"textbox",
         "label":"Address Street 1",
         "validations":[
            "#length:0,70"
         ],
         "attr":{
            "rows":"1",
            "style":"min-height: 45px"
         }
      },
      {
         "uid":"address_street_2_006",
         "name":"address_street_2",
         "widget":"textbox",
         "label":"Address Street 2",
         "validations":[
            "#length:0,70"
         ],
         "attr":{
            "rows":"1",
            "style":"min-height: 45px"
         }
      },
      {
         "uid":"city_006",
         "name":"city",
         "widget":"textbox",
         "label":"City",
         "validations":[
            "#length:0,70"
         ],
         "attr":{
            "rows":"1",
            "style":"min-height: 45px"
         }
      },
      {
          "uid": "country_006",
          "name": "country",
          "label": "Country",
          "widget": "oxd-master-data-input",
          "attr": {
            "service": "Country"
          },
          "validations": [
            "#masterDataSelect:Country,cou_code"
          ]
      },
      {
         "uid":"state_006",
         "name":"state",
         "widget":"textbox",
         "label":"State/Province",
         "validations":[
            "#length:0,70"
         ],
         "attr":{
            "rows":"1",
            "style":"min-height: 45px"
         }
      },
      {
         "uid":"zip_006",
         "name":"zip",
         "widget":"text",
         "label":"Zip/Postal Code",
         "validations":[
            "#length:0,10"
         ]
      },
      {
         "uid":"home_telephone_006",
         "name":"home_telephone",
         "widget":"text",
         "label":"Home Telephone",
         "validations":[
            "#telephone",
            "#length:0,25"
         ]
      },
      {
         "uid":"mobile_006",
         "name":"mobile",
         "widget":"text",
         "label":"Mobile",
         "validations":[
            "#required",
            "#telephone",
            "#length:0,25"
         ]
      },
      {
         "uid":"other_email_006",
         "name":"other_email",
         "widget":"textbox",
         "label":"Other Email",
         "attr": {
            "autocapitalize": "none",
            "rows":"1",
            "style":"min-height: 45px"
         },
         "validations":[
            "#required",
            "#email",
            "#length:0,50"
         ]
      }
   ],
   "views":{
      "form":{
         "uid":"form006",
         "action":"POST",
         "endpoint":"forms/5",
         "body":[
            {
               "uid":"form006_header",
               "tag":"div",
               "props":{
                  "style":"display: flex;"
               },
               "slots":{
                  "default":[
                     {
                        "uid":"form006_title",
                        "tag":"oxd-text",
                        "props":{
                           "type":"card-title"
                        },
                        "slots":{
                           "default":[
                              "Contact Details"
                           ]
                        }
                     },
                     {
                        "uid":"form006_status",
                        "tag":"oxd-status-chip",
                        "props":{
                           "style":"margin-left: 10px;",
                           "status":"##advancedTask.status##"
                        }
                     }
                  ]
               }
            },
            {
               "uid":"form006_br",
               "tag":"br"
            },
            {
               "uid":"form006_grid",
               "tag":"oxd-grid",
               "props":{
                  "cols":2,
                  "style":"margin-left: -10px;margin-right: -10px;"
               },
               "slots":{
                  "default":[
                     {
                        "uid":"form006_grid_item_1",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"address_street_1_006"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form006_grid_item_2",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"address_street_2_006"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form006_grid_item_3",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"city_006"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form006_grid_item_4",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"country_006"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form006_grid_item_5",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"state_006"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form006_grid_item_6",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"zip_006"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form006_grid_item_7",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"home_telephone_006"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form006_grid_item_8",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"mobile_006"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form006_grid_item_9",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"other_email_006"
                              }
                           ]
                        }
                     }
                  ]
               }
            },
            {
               "uid":"form006_divider",
               "tag":"oxd-divider",
               "props":{
                  "style":"margin-bottom: 1rem;"
               }
            },
            {
               "uid":"form006_buttons",
               "tag":"div",
               "props":{
                  "class":"orangehrm-form-actions"
               },
               "slots":{
                  "default":[
                     {
                        "uid":"form006_required_text",
                        "tag":"oxd-required-text"
                     },
                     {
                        "uid":"form006_button_save",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Save",
                           "display-type":"secondary",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickSaveButton"
                        }
                     },
                     {
                        "uid":"form006_button_submit",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Submit",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickSubmitButton"
                        }
                     },
                     {
                        "uid":"form006_button_reject",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Reject",
                           "display-type":"label-feedback-reject",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickRejectButton"
                        }
                     },
                     {
                        "uid":"form006_button_approve",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Approve",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickApproveButton"
                        }
                     }
                  ]
               }
            }
         ],
         "model":{
            "address_street_1":"",
            "address_street_2":"",
            "city":"",
            "country":"",
            "state":"",
            "zip":"",
            "home_telephone":"",
            "mobile":"",
            "other_email":""
         },
         "computed":{

         },
         "methods":{
            "onClickSubmitButton":{
               "#validate":{

               },
               "#confirmation":{
                  "title":"Submit Contact Details",
                  "message":"Please ensure the details provided are accurate.",
                  "messageAlt":"The details cannot be edited after submission.",
                  "confirmText":"Yes, Submit",
                  "confirmDisplayType":"label-feedback-success"
               },
               "#save":{
                  "action":"Submit"
               },
               "#toast":{
                  "error":"Failed to Submit",
                  "success":"Successfully Submitted"
               }
            },
            "onClickSaveButton":{
               "#validate":{

               },
               "#save":{
                  "action":"Save"
               },
               "#toast":{
                  "error":"Failed to Save",
                  "success":"Successfully Saved"
               }
            },
            "onClickApproveButton":{
               "#validate":{

               },
               "#confirmation":{
                  "title":"Approve Contact Details",
                  "message":"By approving, you confirm that all contact details are accurate and satisfactory.",
                  "confirmText":"Yes, Approve",
                  "confirmDisplayType":"label-feedback-success"
               },
               "#save":{
                  "action":"Approve",
                  "skipFormData":true
               },
               "#toast":{
                  "error":"Contact Details Approval Failed",
                  "success":"Contact Details Has Been Approved"
               }
            },
            "onClickRejectButton":{
               "#validate":{

               },
               "#prompt":{
                  "title":"Reject Contact Details",
                  "message":"Rejecting the contact details will prompt the new hire to resubmit the necessary details. The reason for rejection will be sent to the new hire.",
                  "confirmDisplayType":"label-feedback-reject",
                  "confirmText":"Yes, Reject",
                  "cancelText":"No, Cancel",
                  "inputType":"textarea",
                  "inputLabel":"Comment"
               },
               "#save":{
                  "action":"Reject",
                  "skipFormData":true
               },
               "#toast":{
                  "error":"Contact Details Rejection Failed",
                  "success":"Contact Details Has Been Rejected"
               }
            }
         }
      }
   }
}';

UPDATE ohrm_dynamic_form SET meta_data = @metadata WHERE id = @dynamic_form_id;
UPDATE ohrm_dynamic_form_version SET meta_data = @metadata WHERE id = @dynamic_form_version_id;
