INSERT INTO ohrm_data_group (name, description, can_read, can_create, can_update, can_delete)
VALUES ('preboarding_personal_details', 'Preboarding - Stage 05: Personal Details', 1, 0, 1, 0);

SET @data_group_id := (SELECT `id` FROM `ohrm_data_group` WHERE `name` = 'preboarding_personal_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 @preboarding_new_hire_user_role_id = (SELECT `id` FROM `ohrm_user_role` WHERE `name` = 'PreboardingNewHire');
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 Personal Details', 'Preboarding Personal Details', 'POST',
        'preboarding-personal-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 Personal 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 Personal Details', 'Preboarding Personal Details', 'POST', 'preboarding-personal-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 Personal Details');

SET @metadata = '{
   "version":{
      "schema-version":"1.0.0"
   },
   "fields":[
      {
         "uid":"first_name_005",
         "name":"first_name",
         "widget":"text",
         "label":"First Name",
         "validations":[
            "#required",
            "#length:0,30"
         ]
      },
      {
         "uid":"middle_name_005",
         "name":"middle_name",
         "widget":"text",
         "label":"Middle Name",
         "validations":[
            "#length:0,30"
         ]
      },
      {
         "uid":"last_name_005",
         "name":"last_name",
         "widget":"text",
         "label":"Last Name",
         "validations":[
            "#required",
            "#length:0,30"
         ]
      },
      {
         "uid":"date_of_birth_005",
         "name":"date_of_birth",
         "widget":"datepicker",
         "label":"Date Of Birth",
         "validations":[
            "#date_format:yyyy-mm-dd",
            "#date",
            "#valid_past_date"
         ]
      },
      {
          "uid": "gender_005",
          "name": "gender",
          "label": "Gender",
          "widget": "oxd-master-data-input",
          "attr": {
            "service": "Gender"
          },
          "validations": [
            "#masterDataSelect:Gender"
          ]
      },
      {
          "uid": "marital_status_005",
          "name": "marital_status",
          "label": "Marital Status",
          "widget": "oxd-master-data-input",
          "attr": {
            "service": "MaritalStatus"
          },
          "validations": [
            "#masterDataSelect:MaritalStatus"
          ]
      },
      {
          "uid": "nationality_005",
          "name": "nationality",
          "label": "Nationality",
          "widget": "oxd-master-data-input",
          "attr": {
            "service": "Nationality"
          },
          "validations": [
            "#masterDataSelect:Nationality"
          ]
      },
      {
         "uid":"driver_license_number_005",
         "name":"driver_license_number",
         "widget":"text",
         "label":"Driver''s License Number",
         "validations":[
            "#length:0,30"
         ]
      },
      {
         "uid":"license_expiry_date_005",
         "name":"license_expiry_date",
         "widget":"datepicker",
         "label":"License Expiry Date",
         "validations":[
            "#date_format:yyyy-mm-dd",
            "#date"
         ]
      }
   ],
   "views":{
      "form":{
         "uid":"form005",
         "action":"POST",
         "endpoint":"forms/5",
         "body":[
            {
               "uid":"form005_header",
               "tag":"div",
               "props":{
                  "style":"display: flex;"
               },
               "slots":{
                  "default":[
                     {
                        "uid":"form005_title",
                        "tag":"oxd-text",
                        "props":{
                           "type":"card-title"
                        },
                        "slots":{
                           "default":[
                              "Personal Details"
                           ]
                        }
                     },
                     {
                        "uid":"form005_status",
                        "tag":"oxd-status-chip",
                        "props":{
                           "style":"margin-left: 10px;",
                           "status":"##advancedTask.status##"
                        }
                     }
                  ]
               }
            },
            {
               "uid":"form005_br",
               "tag":"br"
            },
            {
               "uid":"form005_grid",
               "tag":"oxd-grid",
               "props":{
                  "cols":2,
                  "style":"margin-left: -10px;margin-right: -10px;"
               },
               "slots":{
                  "default":[
                     {
                        "uid":"form005_grid_item_1",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"first_name_005"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form005_grid_item_2",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"middle_name_005"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form005_grid_item_3",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"last_name_005"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form005_grid_item_4",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"date_of_birth_005"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form005_grid_item_5",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"gender_005"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form005_grid_item_6",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"nationality_005"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form005_grid_item_7",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"marital_status_005"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form005_grid_item_8",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"driver_license_number_005"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form005_grid_item_9",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"license_expiry_date_005"
                              }
                           ]
                        }
                     }
                  ]
               }
            },
            {
               "uid":"form005_divider",
               "tag":"oxd-divider",
               "props":{
                  "style":"margin-bottom: 1rem;"
               }
            },
            {
               "uid":"form005_buttons",
               "tag":"div",
               "props":{
                  "class":"orangehrm-form-actions"
               },
               "slots":{
                  "default":[
                     {
                        "uid":"form005_required_text",
                        "tag":"oxd-required-text"
                     },
                     {
                        "uid":"form005_button_save",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Save",
                           "display-type":"secondary",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickSaveButton"
                        }
                     },
                     {
                        "uid":"form005_button_submit",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Submit",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickSubmitButton"
                        }
                     },
                     {
                        "uid":"form005_button_reject",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Reject",
                           "display-type":"label-feedback-reject",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickRejectButton"
                        }
                     },
                     {
                        "uid":"form005_button_approve",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Approve",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickApproveButton"
                        }
                     }
                  ]
               }
            }
         ],
         "model":{
            "first_name":"",
            "middle_name":"",
            "last_name":"",
            "date_of_birth":"",
            "gender":"",
            "nationality":"",
            "marital_status":"",
            "driver_license_number":"",
            "license_expiry_date":""
         },
         "computed":{

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

               },
               "#confirmation":{
                  "title":"Submit Personal 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 Personal Details",
                  "message":"By approving, you confirm that all personal details are accurate and satisfactory.",
                  "confirmText":"Yes, Approve",
                  "confirmDisplayType":"label-feedback-success"
               },
               "#save":{
                  "action":"Approve",
                  "skipFormData":true
               },
               "#toast":{
                  "error":"Personal Details Approval Failed",
                  "success":"Personal Details Has Been Approved"
               }
            },
            "onClickRejectButton":{
               "#validate":{

               },
               "#prompt":{
                  "title":"Reject Personal Details",
                  "message":"Rejecting the personal 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":"Personal Details Rejection Failed",
                  "success":"Personal 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;
