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

SET @metadata := CONCAT('
{
  "version": {
    "schema-version": "1.0.0"
  },
  "fields": [
    {
      "uid": "bank_account_holder_1001",
      "name": "bank_account_holder",
      "widget": "textbox",
      "label": "Bank Account Holder",
      "attr": {
        "required": true,
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:0:250"
      ]
    },
    {
      "uid": "bank_sort_code_1002",
      "name": "bank_sort_code",
      "widget": "textbox",
      "label": "Bank Sort Code",
      "attr": {
        "required": true,
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:0:250"
      ]
    },
    {
      "uid": "bank_account_number_1003",
      "name": "bank_account_number",
      "widget": "textbox",
      "label": "Bank Account Number",
      "attr": {
        "required": true,
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:0:250"
      ]
    },
    {
      "uid": "bank_name_1004",
      "name": "bank_name",
      "widget": "textbox",
      "label": "Bank Name",
      "attr": {
        "required": true,
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:0:250"
      ]
    },
    {
      "uid": "bank_branch_1005",
      "name": "bank_branch",
      "widget": "textbox",
      "label": "Bank Branch",
      "attr": {
        "required": true,
        "rows": "1",
        "style": "min-height: 45px"
      },
      "validations": [
        "#required",
        "#length:0:250"
      ]
    }
  ],
  "views": {
    "form": {
      "uid": "form009",
      "action": "POST",
      "endpoint": "forms/9",
      "body": [
        {
          "uid": "form009_title",
          "tag": "oxd-text",
          "props": {
            "type": "card-title"
          },
          "slots": {
            "default": [
              "Bank Details"
            ]
          }
        },
        {
          "uid": "form009_br",
          "tag": "br"
        },
        {
          "uid": "form009_grid",
          "tag": "oxd-grid",
          "props": {
            "cols": 2,
            "style": "margin-left: -10px;margin-right: -10px;"
          },
          "slots": {
            "default": [
              {
                "uid": "form009_grid_item_1",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "bank_account_holder_1001"
                    }
                  ]
                }
              },
              {
                "uid": "form009_grid_item_2",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "bank_sort_code_1002"
                    }
                  ]
                }
              },
              {
                "uid": "form009_grid_item_3",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "bank_account_number_1003"
                    }
                  ]
                }
              },
              {
                "uid": "form009_grid_item_4",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "bank_name_1004"
                    }
                  ]
                }
              },
              {
                "uid": "form009_grid_item_5",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "bank_branch_1005"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "uid": "form009_divider",
          "tag": "oxd-divider",
          "props": {
            "style": "margin-bottom: 1rem;"
          }
        },
        {
          "uid": "form009_buttons",
          "tag": "div",
          "props": {
            "style": "display: flex; justify-content: space-between;margin-top: 1rem"
          },
          "slots": {
              "default": [
                {
                  "uid": "form009_required_text",
                  "tag": "oxd-required-text"
                },
                {
                  "uid": "form009_button_save",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Save",
                    "display-type": "secondary",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickSaveButton"
                  }
                },
                {
                  "uid": "form009_button_submit",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Submit",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickSubmitButton"
                  }
                },
                {
                  "uid": "form009_button_reject",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Reject",
                    "display-type": "danger",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickRejectButton"
                  }
                },
                {
                  "uid": "form009_button_approve",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Approve",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickApproveButton"
                  }
                }
              ]
            }
        }
      ],
      "model": {
        "bank_account_holder": "",
        "bank_sort_code": "",
        "bank_account_number": "",
        "bank_name": "",
        "bank_branch": ""
      },
      "computed": {},
      "methods": {
        "onClickSubmitButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Bank details will be submitted.",
            "confirmDisplayType": "label-feedback-success"
          },
          "#decodeB64Files": {
            "model": ["photo"]
          },
          "#save": {
            "action": "Submit"
          },
          "#toast": {
            "error": "Failed to Submit",
            "success": "Successfully Submitted"
          }
        },
        "onClickSaveButton": {
          "#validate": {},
          "#decodeB64Files": {
            "model": ["photo"]
          },
          "#save": {
             "action": "Save"
          },
          "#toast": {
              "error": "Failed to Save",
              "success": "Successfully Saved"
          }
        },
        "onClickApproveButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Bank details will be Approved.",
            "confirmDisplayType": "label-feedback-success"
          },
          "#save": {
            "action": "Approve",
            "skipFormData": true
          },
          "#toast": {
            "error": "Failed to Approve",
            "success": "Bank details has been Approved"
          }
        },
        "onClickRejectButton": {
          "#validate": {},
          "#prompt": {
            "title": "Rejection Confirmation",
            "confirmDisplayType": "label-feedback-danger",
            "confirmText": "Reject",
            "inputType": "textarea",
            "inputLabel": "Comment"
          },
          "#save": {
            "action": "Reject",
            "skipFormData": true
          },
          "#toast": {
            "error": "Failed to Reject",
            "success": "Bank 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;
