INSERT INTO ohrm_data_group (name, description, can_read, can_create, can_update, can_delete)
VALUES ('preboarding_memberships', 'Preboarding - Stage 11: Memberships', 1, 0, 1, 0);

SET @data_group_id := (SELECT `id` FROM `ohrm_data_group` WHERE `name` = 'preboarding_memberships');
SET @api_memberships_default_data_group_id := (SELECT `id` FROM `ohrm_data_group` WHERE `name` = 'membership');
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),
    (@preboarding_new_hire_user_role_id, @api_memberships_default_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 Memberships Record', 'Preboarding Memberships Record', 'POST', 'preboarding-memberships-record', null, @data_group_id,
        @system_user_id, @system_user_id, 'System', 1);
SET @child_dynamic_form_id := (SELECT `id`
                         FROM ohrm_dynamic_form
                         WHERE name = 'Preboarding Memberships Record');

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 Memberships Record', 'Preboarding Memberships Record', 'POST', 'preboarding-memberships-record', null, @data_group_id,
        @system_user_id, @system_user_id, 'System', 1, @child_dynamic_form_id);
SET @child_dynamic_form_version_id := (SELECT `id`
                                 FROM ohrm_dynamic_form_version
                                 WHERE name = 'Preboarding Memberships Record');

SET @child_meta =  CONCAT('{
   "version":{
      "schema-version":"1.0.0"
   },
   "fields":[
      {
          "uid": "membership_011",
          "name": "membership",
          "label": "Membership",
          "widget": "oxd-master-data-input",
          "attr": {
            "service": "Memberships",
            "required": true
          },
          "validations": [
            "#required",
            "#masterDataSelect:Membership"
          ]
      },
      {
         "uid":"subscription_amount_011",
         "name":"subscription_amount",
         "label":"Subscription Amount",
         "widget":"text",
         "validations":[
            "#length:1,13",
            "#validCurrency:0,999\\\\,999\\\\,999.999,3,.,\\\\,"
         ]
      },
      {
          "uid": "currency_011",
          "name": "currency",
          "label": "Currency",
          "widget": "oxd-master-data-input",
          "attr": {
            "service": "Currency"
          },
          "validations": [
            "#masterDataSelect:CurrencyType,currency_id"
          ]
      },
      {
         "uid":"subscription_commence_date_011",
         "name":"subscription_commence_date",
         "widget":"datepicker",
         "label":"Subscription Commence Date",
         "validations":[
            "#date_format:yyyy-mm-dd",
            "#date"
         ]
      },
      {
         "uid":"subscription_renewal_date_011",
         "name":"subscription_renewal_date",
         "widget":"datepicker",
         "label":"Subscription Renewal Date",
         "validations":[
            "#date_format:yyyy-mm-dd",
            "#date",
            "#equal_or_after_date:$subscription_commence_date,Renewal date should be after commence date"
         ]
      }
   ],
   "views":{
      "form":{
         "uid":"form011",
         "action":"POST",
         "endpoint":"forms/1",
         "body":[
            {
               "uid":"form011_grid2",
               "tag":"oxd-grid",
               "props":{
                  "cols":2,
                  "style":"margin-left: -10px;margin-right: -10px;"
               },
               "slots":{
                  "default":[
                     {
                        "uid":"form011_grid_item_1",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"membership_011"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form011_grid_item_2",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"subscription_amount_011"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form011_grid_item_3",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"currency_011"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form011_grid_item_4",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"subscription_commence_date_011"
                              }
                           ]
                        }
                     },
                     {
                        "uid":"form011_grid_item_5",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"subscription_renewal_date_011"
                              }
                           ]
                        }
                     }
                  ]
               }
            }
         ],
         "model":{
            "membership":"",
            "subscription_amount":"",
            "currency":"",
            "subscription_commence_date":"",
            "subscription_renewal_date":""
         },
         "computed":[

         ],
         "methods":{

         }
      }
   }
}');

UPDATE ohrm_dynamic_form SET meta_data = @child_meta WHERE id = @child_dynamic_form_id;
UPDATE ohrm_dynamic_form_version SET meta_data = @child_meta WHERE id = @child_dynamic_form_version_id;

INSERT INTO ohrm_dynamic_form (`name`, description, `action`, endpoint, meta_data, data_group_id, created_by,
                               updated_by, created_by_name, version)
VALUES ('Preboarding Memberships', 'Preboarding Memberships', 'POST',
        'preboarding-memberships', 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 Memberships');

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 Memberships', 'Preboarding Memberships', 'POST', 'preboarding-memberships',
        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 Memberships');

SET @metadata := CONCAT('{
   "version":{
      "schema-version":"1.0.0"
   },
   "fields":[
      {
         "uid":"preboarding_memberships_001",
         "name":"preboarding_memberships",
         "label":"Preboarding Memberships",
         "widget":"oxd-subform-widget",
         "attr":{
            "min":1,
            "max":3,
            "endpoint":"preboarding-memberships-record"
         },
         "validations":[
            "#childForm:',@child_dynamic_form_version_id,',1,3"
         ]
      }
   ],
   "views":{
      "form":{
         "uid":"form0011",
         "action":"POST",
         "endpoint":"forms/11",
         "body":[
            {
               "uid":"form0011_header",
               "tag":"div",
               "props":{
                  "style":"display: flex;"
               },
               "slots":{
                  "default":[
                     {
                        "uid":"form0011_title",
                        "tag":"oxd-text",
                        "props":{
                           "type":"card-title"
                        },
                        "slots":{
                           "default":[
                              "Memberships"
                           ]
                        }
                     },
                     {
                        "uid":"form0011_status",
                        "tag":"oxd-status-chip",
                        "props":{
                           "style":"margin-left: 10px;",
                           "status":"##advancedTask.status##"
                        }
                     }
                  ]
               }
            },
            {
               "uid":"form0011_br",
               "tag":"br"
            },
            {
               "uid":"form0011_grid",
               "tag":"oxd-grid",
               "props":{
                  "cols":1,
                  "style":"margin-left: -10px;margin-right: -10px;"
               },
               "slots":{
                  "default":[
                     {
                        "uid":"form0011_grid_item_1",
                        "tag":"oxd-grid-item",
                        "slots":{
                           "default":[
                              {
                                 "field":"preboarding_memberships_001"
                              }
                           ]
                        }
                     }
                  ]
               }
            },
            {
               "uid":"form0011_divider",
               "tag":"oxd-divider",
               "props":{
                  "style":"margin-bottom: 1rem;"
               }
            },
            {
               "uid":"form0011_buttons",
               "tag":"div",
               "props":{
                  "class":"orangehrm-form-actions"
               },
               "slots":{
                  "default":[
                     {
                        "uid":"form0011_required_text",
                        "tag":"oxd-required-text"
                     },
                     {
                        "uid":"form0011_button_save",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Save",
                           "display-type":"secondary",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickSaveButton"
                        }
                     },
                     {
                        "uid":"form0011_button_submit",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Submit",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickSubmitButton"
                        }
                     },
                     {
                        "uid":"form0011_button_reject",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Reject",
                           "display-type":"label-feedback-reject",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickRejectButton"
                        }
                     },
                     {
                        "uid":"form0011_button_approve",
                        "tag":"oxd-button",
                        "props":{
                           "type":"button",
                           "label":"Approve",
                           "style":"margin-left: 0.5rem;"
                        },
                        "events":{
                           "onClick":"#onClickApproveButton"
                        }
                     }
                  ]
               }
            }
         ],
         "model":{
            "preboarding_memberships":[
               {
                  "membership":"",
                  "subscription_amount":"",
                  "currency":"",
                  "subscription_commence_date":"",
                  "subscription_renewal_date":""
               }
            ]
         },
         "computed":{

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

               },
               "#confirmation":{
                  "title":"Submit Membership Information",
                  "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 Membership Information",
                  "message":"By approving, you confirm that all membership information is accurate and satisfactory.",
                  "confirmText":"Yes, Approve",
                  "confirmDisplayType":"label-feedback-success"
               },
               "#save":{
                  "action":"Approve",
                  "skipFormData":true
               },
               "#toast":{
                  "error":"Preboarding Memberships Approval Failed",
                  "success":"Preboarding Memberships Have Been Approved"
               }
            },
            "onClickRejectButton":{
               "#validate":{

               },
               "#prompt":{
                  "title":"Reject Preboarding Memberships",
                  "confirmDisplayType":"label-feedback-reject",
                  "message":"Rejecting the memberships contact details will prompt the new hire to resubmit the necessary details. The reason for rejection will be sent to the new hire.",
                  "confirmText":"Yes, Reject",
                  "cancelText":"No, Cancel",
                  "inputType":"textarea",
                  "inputLabel":"Comment"
               },
               "#save":{
                  "action":"Reject",
                  "skipFormData":true
               },
               "#toast":{
                  "error":"Preboarding Memberships Rejection Failed",
                  "success":"Preboarding Memberships Have 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;
