INSERT INTO ohrm_data_group (name, description, can_read, can_create, can_update, can_delete)
VALUES ('recruitment_preboarding_address_payroll_detail', 'Recruitment (ATS) - Onboarding - Payroll Detail', 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_data_field (`name`, `label`, `data_group_id`, `can_read`, `can_create`, `can_update`, `can_delete`) VALUES
    ('email', 'email', @data_group_id, 1, 0, 1, 0);
SET @email_data_field := (SELECT LAST_INSERT_ID());

INSERT INTO ohrm_user_role_data_field (user_role_id, data_field_id, can_read, can_update) VALUES
(@candidate_user_role_id,@email_data_field,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 ('Payroll Details', 'Payroll Details', 'POST', 'preboarding-address-payroll-detail', null, @data_group_id,
        @system_user_id, @system_user_id, 'System', 1);
SET @dynamic_form_id := (SELECT `id`
                         FROM ohrm_dynamic_form
                         WHERE name = 'Payroll 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 ('Payroll Details', 'Payroll Details', 'POST', 'preboarding-address-payroll-detail', 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 = 'Payroll Details');


SET @payroll_custom_field_1 := (SELECT id FROM ohrm_custom_field WHERE name = 'Do you have a P45' AND deleted = 0);
SET @payroll_custom_field_2 := (SELECT id FROM ohrm_custom_field WHERE name = 'Select one of the below' AND deleted = 0);
SET @payroll_custom_field_3 := (SELECT id FROM ohrm_custom_field WHERE name = 'I have been receipt of Employment & Support Allowance (ESA) before starting work?' AND deleted = 0);
SET @payroll_custom_field_4 := (SELECT id FROM ohrm_custom_field WHERE name = 'Are you able to produce the linking letter ESA220 (or similar)?' AND deleted = 0);
SET @payroll_custom_field_5 := (SELECT id FROM ohrm_custom_field WHERE name = 'Do you have a student loan that is not fully repaid?' AND deleted = 0);
SET @payroll_custom_field_6 := (SELECT id FROM ohrm_custom_field WHERE name = 'Are you repaying the student loan direct to the Student Loans Company by agreed monthly payments and what is your student loan plan?' AND deleted = 0);
SET @payroll_custom_field_7 := (SELECT id FROM ohrm_custom_field WHERE name = 'Did you finish your studies before the last April?' AND deleted = 0);


SET @metadata := CONCAT('
{
  "version": {
    "schema-version": "1.0.0"
  },
  "fields": [
    {
      "uid": "payroll_custom_field_1",
      "name": "payroll_custom_field_1",
      "widget": "oxd-master-data-input",
      "label": "Do you have a P45",
      "attr": {
        "required": true,
        "service":"CustomFieldOptions",
        "customFieldId":"',@payroll_custom_field_1,'"
      },
      "validations": [
        "#required"
      ]
    },
    {
      "uid": "payroll_custom_field_2",
      "name": "payroll_custom_field_2",
      "widget": "oxd-master-data-input",
      "label": "Select one of the below",
      "attr": {
        "required": true,
        "service":"CustomFieldOptions",
        "customFieldId":"',@payroll_custom_field_2,'"
      },
      "validations": [
        "#required"
      ]
    },
    {
      "uid": "payroll_custom_field_3",
      "name": "payroll_custom_field_3",
      "widget": "oxd-master-data-input",
      "label": "I have been receipt of Employment & Support Allowance (ESA) before starting work?",
      "attr": {
        "required": true,
        "service":"CustomFieldOptions",
        "customFieldId":"',@payroll_custom_field_3,'"
      },
      "validations": [
        "#required"
      ]
    },
    {
      "uid": "payroll_custom_field_4",
      "name": "payroll_custom_field_4",
      "widget": "oxd-master-data-input",
      "label": "Are you able to produce the linking letter ESA220 (or similar)?",
      "attr": {
        "required": true,
        "service":"CustomFieldOptions",
        "customFieldId":"',@payroll_custom_field_4,'"
      },
      "validations": [
        "#required"
      ]
    },
    {
      "uid": "payroll_custom_field_5",
      "name": "payroll_custom_field_5",
      "widget": "oxd-master-data-input",
      "label": "Do you have a student loan that is not fully repaid?",
      "attr": {
        "required": true,
        "widgetType":"radiogroup",
        "service":"CustomFieldOptions",
        "customFieldId":"',@payroll_custom_field_5,'"
      },
      "validations": [
        "#required"
      ]
    },
    {
      "uid": "payroll_custom_field_6",
      "name": "payroll_custom_field_6",
      "widget": "oxd-master-data-input",
      "label": "Are you repaying the student loan direct to the Student Loans Company by agreed monthly payments and what is your student loan plan?",
      "attr": {
        "required": true,
        "service":"CustomFieldOptions",
        "customFieldId":"',@payroll_custom_field_6,'"
      },
      "validations": [
        "#required"
      ]
    },
    {
      "uid": "payroll_custom_field_7",
      "name": "payroll_custom_field_7",
      "widget": "oxd-master-data-input",
      "label": "Did you finish your studies before the last April?",
      "attr": {
        "required": true,
        "widgetType":"radiogroup",
        "service":"CustomFieldOptions",
        "customFieldId":"',@payroll_custom_field_7,'"
      },
      "validations": [
        "#required"
      ]
    }
  ],
  "views": {
    "form": {
      "uid": "form0012",
      "action": "POST",
      "endpoint": "forms/12",
      "body": [
        {
          "uid": "form001_title",
          "tag": "oxd-text",
          "props": {
            "type": "card-title"
          },
          "slots": {
            "default": [
              "Payroll Details"
            ]
          }
        },
        {
          "uid": "form0012_br",
          "tag": "br"
        },
        {
          "uid": "form0012_grid",
          "tag": "oxd-grid",
          "props": {
            "cols": 2,
            "style": "margin-left: -10px;margin-right: -10px;"
          },
          "slots": {
            "default": [
              {
                "uid": "form0012_grid_item_1",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "payroll_custom_field_1"
                    }
                  ]
                }
              },
              {
                "uid": "form0012_grid_item_2",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "payroll_custom_field_2"
                    }
                  ]
                }
              },
              {
                "uid": "form0012_grid_item_3",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "payroll_custom_field_3"
                    }
                  ]
                }
              },
              {
                "uid": "form0012_grid_item_4",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "payroll_custom_field_4"
                    }
                  ]
                }
              },
              {
                "uid": "form0012_grid_item_5",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "payroll_custom_field_5"
                    }
                  ]
                }
              },
              {
                "if": "$payroll_custom_field_5 === \\"Yes\\"",
                "uid": "form0012_grid_item_6",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "payroll_custom_field_6"
                    }
                  ]
                }
              },
              {
                "uid": "form0012_grid_item_7",
                "tag": "oxd-grid-item",
                "slots": {
                  "default": [
                    {
                      "field": "payroll_custom_field_7"
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "uid": "form007_divider",
          "tag": "oxd-divider",
          "props": {
            "style": "margin-bottom: 1rem;"
          }
        },
        {
          "uid": "form0012_buttons",
          "tag": "div",
          "props": {
            "style": "display: flex; justify-content: space-between;margin-top: 1rem"
          },
          "slots": {
              "default": [
                {
                  "uid": "form0012_required_text",
                  "tag": "oxd-required-text"
                },
                {
                  "uid": "form0012_button_save",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Save",
                    "display-type": "secondary",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickSaveButton"
                  }
                },
                {
                  "uid": "form0012_button_submit",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Submit",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickSubmitButton"
                  }
                },
                {
                  "uid": "form0012_button_reject",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Reject",
                    "display-type": "danger",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickRejectButton"
                  }
                },
                {
                  "uid": "form0012_button_approve",
                  "tag": "oxd-button",
                  "props": {
                    "type": "button",
                    "label": "Approve",
                    "style": "margin-left: 0.5rem;"
                  },
                  "events": {
                    "onClick": "#onClickApproveButton"
                  }
                }
              ]
            }
        }
      ],
      "model": {
        "payroll_custom_field_1": "",
        "payroll_custom_field_2": "",
        "payroll_custom_field_3": "",
        "payroll_custom_field_4": "",
        "payroll_custom_field_5": "",
        "payroll_custom_field_6": "",
        "payroll_custom_field_7": ""
      },
      "computed": {},
      "methods": {
        "onClickSubmitButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Payroll 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"
          }
        },
        "onClickApproveButton": {
          "#validate": {},
          "#confirmation": {
            "message": "Payroll Details will be Approved.",
            "confirmDisplayType": "label-feedback-success"
          },
          "#save": {
            "action": "Approve"
          },
          "#toast": {
            "error": "Failed to Approve",
            "success": "Payroll 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": "Payroll 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;
