INSERT INTO ohrm_data_group (name, description, can_read, can_create, can_update, can_delete)
VALUES ('recruitment_preboarding_reference_details', 'Recruitment (ATS) - Onboarding - Reference Details', 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),
       (@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 ('Reference Details', 'Reference Details', 'POST', 'preboarding-reference-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 = 'Reference 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 ('Reference Details', 'Reference Details', 'POST', 'preboarding-reference-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 = 'Reference Details');

SET @metadata := CONCAT('
{
  "version": {
    "schema-version": "1.0.0"
  },
  "fields": [
    {
      "uid": "title_1001",
      "name": "title",
      "widget": "dropdown",
      "label": "Title",
      "attr": {
        "choices": {
          "Mr": "Mr",
          "Mrs": "Mrs",
          "Ms": "Ms",
          "Dr": "Dr",
          "Prof": "Prof",
          "Sir": "Sir",
          "Lord": "Lord",
          "Miss": "Miss"
        }
      },
      "validations": [
        "#required",
        "#select:[Mr,Mrs,Ms,Dr,Prof,Sir,Lord,Miss]"
      ]
    },
    {
      "uid": "first_name_1002",
      "name": "first_name",
      "widget": "textbox",
      "label": "First Name",
      "attr": {
        "required": true,
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:0:250"
      ]
    },
    {
      "uid": "surname_1003",
      "name": "surname",
      "widget": "textbox",
      "label": "Surname",
      "attr": {
        "required": true,
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:0:250"
      ]
    },
    {
      "uid": "work_email_1004",
      "name": "email",
      "widget": "textbox",
      "label": "Work Email",
      "attr": {
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#email"
      ]
    },
    {
      "uid": "phone_1005",
      "name": "phone",
      "widget": "text",
      "label": "Phone",
      "validations": [
        "#phone",
        "#length:0:25"
      ]
    },
    {
      "uid": "company_1006",
      "name": "company",
      "widget": "textbox",
      "label": "Company",
      "attr": {
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#length:0:250"
      ]
    },
    {
      "uid": "designation_1007",
      "name": "designation",
      "widget": "textbox",
      "label": "Designation",
      "attr": {
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#length:0:250"
      ]
    },
    {
      "uid": "relationship_1008",
      "name": "relationship",
      "widget": "dropdown",
      "label": "Relationship",
      "attr": {
        "required": true,
        "choices": {
          "Manager/Supervisor": "Manager/Supervisor",
          "Professional Associate": "Professional Associate",
          "Landlord/Tenant": "Landlord/Tenant",
          "Teacher/Tutor": "Teacher/Tutor",
          "Work Colleague": "Work Colleague",
          "Other (Please Specify)": "Other (Please Specify)"
        }
      },
      "validations": [
        "#required",
        "#select:[Manager/Supervisor,Professional Associate,Landlord/Tenant,Teacher/Tutor,Work Colleague,Other (Please Specify)]"
      ]
    },
    {
      "uid": "specify_relationship_1009",
      "name": "specify_relationship",
      "widget": "textbox",
      "label": "Specify the relationship",
      "attr": {
        "required": true,
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:0:250"
      ]
    }
  ],
  "views": {
    "form": {
      "uid": "form008",
      "action": "POST",
      "endpoint": "forms/8",
      "body": [
        {
          "uid": "form008_title",
          "tag": "oxd-text",
          "props": {
            "type": "card-title"
          },
          "slots": {
            "default": [
              "Reference Details"
            ]
          }
        },
        {
          "uid": "form008_br",
          "tag": "br"
        },
        {
          "uid": "form008_grid",
          "tag": "oxd-grid",
          "props": {
            "cols": 2,
            "style": "margin-left: -10px;margin-right: -10px;"
          },
          "slots": {
            "default": [
              {
                "uid": "form008_grid_item_1",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "title_1001"
                    }
                  ]
                }
              },
              {
                "uid": "form008_grid_item_2",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "first_name_1002"
                    }
                  ]
                }
              },
              {
                "uid": "form008_grid_item_3",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "surname_1003"
                    }
                  ]
                }
              },
              {
                "uid": "form008_grid_item_4",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "work_email_1004"
                    }
                  ]
                }
              },
              {
                "uid": "form008_grid_item_5",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "phone_1005"
                    }
                  ]
                }
              },
              {
                "uid": "form008_grid_item_6",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "company_1006"
                    }
                  ]
                }
              },
              {
                "uid": "form008_grid_item_7",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "designation_1007"
                    }
                  ]
                }
              },
              {
                "uid": "form008_grid_item_8",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "relationship_1008"
                    }
                  ]
                }
              },
              {
                "if": "$relationship.id === \\"Other (Please Specify)\\"",
                "uid": "form008_grid_item_9",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "specify_relationship_1009"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "uid": "form008_divider",
          "tag": "oxd-divider",
          "props": {
            "style": "margin-bottom: 1rem;"
          }
        },
        {
          "uid": "form008_buttons",
          "tag": "div",
          "props": {
            "style": "display: flex; justify-content: space-between;margin-top: 1rem"
          },
          "slots": {
              "default": [
                {
                  "uid": "form008_required_text",
                  "tag": "oxd-required-text"
                },
                {
                  "uid": "form008_button_save",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Save",
                    "display-type": "secondary",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickSaveButton"
                  }
                },
                {
                  "uid": "form008_button_submit",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Submit",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickSubmitButton"
                  }
                },
                {
                  "uid": "form008_button_reject",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Reject",
                    "display-type": "danger",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickRejectButton"
                  }
                },
                {
                  "uid": "form008_button_initiate",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Initiate",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickInitiateButton"
                  }
                },
                {
                  "uid": "form008_button_approve",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Approve",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickApproveButton"
                  }
                }
              ]
            }
        }
      ],
      "model": {
        "title": "",
        "first_name": "",
        "surname": "",
        "email": "",
        "phone": "",
        "company": "",
        "designation": "",
        "relationship": "",
        "specify_relationship": ""
      },
      "computed": {},
      "methods": {
        "onClickSubmitButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Reference details 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"
          }
        },
        "onClickInitiateButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Reference process will be Initiated.",
            "confirmDisplayType": "label-feedback-success"
          },
          "#save": {
            "action": "Initiate",
            "skipFormData": true
          },
          "#toast": {
            "error": "Failed to Initiate",
            "success": "Reference details has been Approved"
          }
        },
        "onClickApproveButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Reference details will be Approved.",
            "confirmDisplayType": "label-feedback-success"
          },
          "#save": {
            "action": "Approve",
            "skipFormData": true
          },
          "#toast": {
            "error": "Failed to Approve",
            "success": "Reference details has been Approved"
          }
        },
        "onClickRejectButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Reference details will be Rejected.",
            "confirmDisplayType": "label-feedback-danger"
          },
          "#save": {
            "action": "Reject",
            "skipFormData": true
          },
          "#toast": {
            "error": "Failed to Reject",
            "success": "Reference 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;
