INSERT INTO ohrm_data_group (name, description, can_read, can_create, can_update, can_delete)
VALUES ('recruitment_preboarding_emergency_contacts', 'Recruitment (ATS) - Onboarding - Emergency Contacts', 1, 0, 1, 0);

SET @data_group_id := (SELECT LAST_INSERT_ID());
SET @admin_user_role_id := (SELECT `id`
                            FROM `ohrm_user_role`
                            WHERE `name` = 'Admin');
SET @hiring_manager_user_role_id := (SELECT `id`
                                     FROM `ohrm_user_role`
                                     WHERE `name` = 'HiringManager');
SET @preboarding_event_manager_user_role_id := (SELECT `id`
                                     FROM `ohrm_user_role`
                                     WHERE `name` = 'PreboardingEventManager');

SET @candidate_user_role_id := (SELECT `id`
                                     FROM `ohrm_user_role`
                                     WHERE `name` = 'CandidateUser');

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),
       (@hiring_manager_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL),
       (@candidate_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL),
       (@hiring_manager_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL),
       (@preboarding_event_manager_user_role_id, @data_group_id, 1, 0, 1, 0, 0, NULL);

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

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);


INSERT INTO ohrm_dynamic_form (`name`, description, `action`, endpoint, meta_data, data_group_id, created_by,
                               updated_by, created_by_name, version)
VALUES ('Emergency Contact Record', 'Emergency Contact Record', 'POST', 'emergency-contact-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 = 'Emergency Contact 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 ('Emergency Contact Record', 'Emergency Contact Record', 'POST', 'emergency-contact-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 = 'Emergency Contact Record');

SET @child_meta := CONCAT('
{
  "version": {
    "schema-version": "1.0.0"
  },
  "fields": [
    {
      "uid": "name_1001",
      "name": "name",
      "label": "Name",
      "widget": "textbox",
      "attr": {
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:1,250"
      ]
    },
    {
      "uid": "relationship_1002",
      "name": "relationship",
      "label": "Relationship",
      "widget": "textbox",
      "attr": {
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:1,250"
      ]
    },
    {
      "uid": "email_1003",
      "name": "email",
      "widget": "textbox",
      "label": "Email Address",
      "attr": {
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#email",
        "#length:0,50"
      ]
    },
    {
      "uid": "home_phone_1004",
      "name": "home_phone",
      "widget": "text",
      "label": "Home Telephone",
      "validations": [
        "#phone",
        "#length:0,25"
      ]
    },
    {
      "uid": "mobile_phone_1005",
      "name": "mobile_phone",
      "widget": "text",
      "label": "Mobile",
      "validations": [
        "#phone",
        "#length:0,25"
      ]
    },
    {
      "uid": "work_phone_1006",
      "name": "work_phone",
      "widget": "text",
      "label": "Work Telephone",
      "validations": [
        "#phone",
        "#length:0,25"
      ]
    }
  ],
  "views": {
    "form": {
      "uid": "form005",
      "action": "POST",
      "endpoint": "forms/1",
      "body": [
        {
          "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": "name_1001"
                    }
                  ]
                }
              },
              {
                "uid": "form005_grid_item_2",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "relationship_1002"
                    }
                  ]
                }
              },
              {
                "uid": "form005_grid_item_3",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "email_1003"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "uid": "form005_text",
          "tag": "oxd-text",
          "props": {
            "style": "font-size: 12px;font-weight: 700;"
          },
          "slots": {
            "default": [
              "Telephone*"
            ]
          }
        },
        {
          "uid": "form005_divider",
          "tag": "oxd-divider",
          "props": {
            "style": "margin-bottom: 1rem;"
          }
        },
        {
          "uid": "form005_grid2",
          "tag": "oxd-grid",
          "props": {
            "cols": 3,
            "style": "margin-left: -10px;margin-right: -10px;"
          },
          "slots": {
            "default": [
              {
                "uid": "form005_grid2_item_4",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "home_phone_1004"
                    }
                  ]
                }
              },
              {
                "uid": "form005_grid2_item_5",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "mobile_phone_1005"
                    }
                  ]
                }
              },
              {
                "uid": "form005_grid2_item_6",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "work_phone_1006"
                    }
                  ]
                }
              }
            ]
          }
        }
      ],
      "model": {
        "name": "",
        "relationship": "",
        "email": "",
        "home_phone": "",
        "mobile_phone": "",
        "work_phone": ""
      },
      "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 ('Emergency Contacts', 'Emergency Contacts', 'POST', 'preboarding-emergency-contacts', null, @data_group_id,
        @system_user_id, @system_user_id, 'System', 1);
SET @parent_dynamic_form_id := (SELECT `id`
                         FROM ohrm_dynamic_form
                         WHERE name = 'Emergency Contacts');

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 ('Emergency Contacts', 'Emergency Contacts', 'POST', 'preboarding-emergency-contacts', null, @data_group_id,
        @system_user_id, @system_user_id, 'System', 1, @parent_dynamic_form_id);
SET @parent_dynamic_form_version_id := (SELECT `id`
                                 FROM ohrm_dynamic_form_version
                                 WHERE name = 'Emergency Contacts');
SET @metadata := CONCAT('
{
  "version": {
    "schema-version": "1.0.0"
  },
  "fields": [
    {
      "uid": "emergency_contact_001",
      "name": "emergency_contact",
      "label": "Emergency Contact",
      "widget": "oxd-subform-widget",
      "attr": {
        "min": 1,
        "max": 3,
        "endpoint": "emergency-contact-record"
      },
      "validations": [
        "#childForm:4,1,3"
      ]
    }
  ],
  "views": {
    "form": {
      "uid": "form006",
      "action": "POST",
      "endpoint": "forms/6",
      "body": [
        {
          "uid": "form006_title",
          "tag": "oxd-text",
          "props": {
            "type": "card-title"
          },
          "slots": {
            "default": [
              "Emergency Contacts"
            ]
          }
        },
        {
          "uid": "form006_br",
          "tag": "br"
        },
        {
          "uid": "form006_grid",
          "tag": "oxd-grid",
          "props": {
            "cols": 1,
            "style": "margin-left: -10px;margin-right: -10px;"
          },
          "slots": {
            "default": [
              {
                "uid": "form006_grid_item_1",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "emergency_contact_001"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "uid": "form006_divider",
          "tag": "oxd-divider",
          "props": {
            "style": "margin-bottom: 1rem;"
          }
        },
        {
          "uid": "form006_buttons",
          "tag": "div",
          "props": {
            "style": "display: flex; justify-content: space-between;margin-top: 1rem"
          },
          "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": "danger",
                    "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": {
        "emergency_contact": [{
          "name": "",
          "relationship": "",
          "email": "",
          "home_phone": "",
          "mobile_phone": "",
          "work_phone": ""
        }]
      },
      "computed": {},
      "methods": {
        "onClickSubmitButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Emergency Contacts will be submitted.",
            "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": {
            "message": "Emergency Contacts details will be Approved.",
            "confirmDisplayType": "label-feedback-success"
          },
          "#save": {
            "action": "Approve"
          },
          "#toast": {
            "error": "Failed to Approve",
            "success": "Emergency Contacts details has been Approved"
          }
        },
        "onClickRejectButton": {
          "#validate": {},
          "#prompt": {
            "title": "Rejection Confirmation",
            "confirmDisplayType": "label-feedback-danger",
            "confirmText": "Reject",
            "inputType": "textarea",
            "inputLabel": "Comment"
          },
          "#save": {
            "action": "Reject"
          },
          "#toast": {
            "error": "Failed to Reject",
            "success": "Emergency Contacts details has been Rejected"
          }
        }
      }
    }
  }
}
');

UPDATE ohrm_dynamic_form
SET meta_data = @metadata
WHERE id = @parent_dynamic_form_id;
UPDATE ohrm_dynamic_form_version
SET meta_data = @metadata
WHERE id = @parent_dynamic_form_version_id;
