<script>
import Swal from "sweetalert2";
import { authenticatedRequest } from "@/components/session/authenticatedRequest.vue";
import { ObjectsGlobal } from "@/components/session/token/SessionUser/Objects/ObjectsGlobal.vue";
import { SessionUser } from "@/components/session/token/SessionUser.vue";
import {ref} from "vue";

/**
 * The DepartmentNotificationSms object
 */
export const DepartmentNotificationSms = {
  meta: {
    title: "Afdelings telefonnumre",
    icon: "fas fa-list",
    description: "Oversigt over afdelings SMS-notifikations telefonnumre.",
    endpoint: "/department/notification/sms",
    labels: {
      single: "telefonnummer",
      multiple: "telefonnumre"
    }
  },
  columns: {
    id: {
      label: "ID",
      type: "number",
      sortable: true,
      creation: {
        required: false
      }
    },
    department: {
      label: "Afdeling",
      type: "select",
      sortable: true,
      creation: {
        required: true
      },
      options: async () => {
        return SessionUser.objects.departments.get.all();
      }
    },
    label: {
      label: "Label",
      type: "string",
      sortable: true,
      creation: {
        required: true
      }
    },
    phone_country_code: {
      label: "Landekode",
      type: "select",
      sortable: true,
      creation: {
        required: true
      },
      options: async () => {
        return [
        {
          name: "Danmark (+45)",
          id: 45
        },
        {
          name: "Sverige (+46)",
          id: 46
        },
        {
          name: "Norge (+47)",
          id: 47
        },
        {
          name: "Finland (+358)",
          id: 358
        }
      ];
      }
    },
    phone: {
      label: "Telefonnummer",
      type: "number",
      sortable: true,
      creation: {
        required: true
      }
    },
    enabled: {
      label: "Aktiveret",
      type: "boolean",
      sortable: true,
      creation: {
        required: false
      }
    },
  },
  add: async (department, label, phone_country_code, phone) => {
    return ObjectsGlobal.add.object(
        DepartmentNotificationSms.meta.endpoint,
        {
          department: parseInt(department),
          label: label,
          phone_country_code: parseInt(phone_country_code),
          phone: parseInt(phone)
        }
    );
  },
  set: {
    label: async (id, label) => {
      return ObjectsGlobal.set.column(
        DepartmentNotificationSms.meta.endpoint,
        id,
        "label",
        label
      )
    },
    phone_country_code: async (id, phone_country_code) => {
      return ObjectsGlobal.set.column(
        DepartmentNotificationSms.meta.endpoint,
        id,
        "phone_country_code",
        parseInt(phone_country_code)
      )
    },
    phone: async (id, phone) => {
      return ObjectsGlobal.set.column(
        DepartmentNotificationSms.meta.endpoint,
        id,
        "phone",
        parseInt(phone)
      )
    },
    enabled: async (id, enabled = false) => {
      return ObjectsGlobal.set.column(
        DepartmentNotificationSms.meta.endpoint,
        id,
        "enabled",
        SessionUser.objects.global.parse.boolean(enabled)
      )
    },
    department: async (id, department) => {
      return ObjectsGlobal.set.column(
        DepartmentNotificationSms.meta.endpoint,
        id,
        "department",
        parseInt(department)
      )
    }
  },
  get: {
    all: async () => {
      return ObjectsGlobal.get.objects(DepartmentNotificationSms.meta.endpoint);
    },
    single: async (id) => {
      return ObjectsGlobal.get.object(DepartmentNotificationSms.meta.endpoint, id);
    }
  },
  delete: async (id) => {
    return ObjectsGlobal.delete.object(DepartmentNotificationSms.meta.endpoint, id);
  },
  functions: {
    showDeleteObjectForm: (id, onAfterSubmit = null) => {
      return ObjectsGlobal.showDeleteObjectForm(DepartmentNotificationSms, id, onAfterSubmit);
    },
  },
  /**
   * Show the create object form
   * @param onAfterSubmit
   * @param lockedValues
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showCreateObjectForm: (onAfterSubmit = null, lockedValues = {}) => {
    return ObjectsGlobal.showCreateObjectForm(DepartmentNotificationSms, onAfterSubmit, lockedValues);
  },
  /**
   * Show the edit object field form
   * @param id
   * @param column
   * @param value
   * @param onAfterSubmit
   * @returns {Promise<SweetAlertResult<Awaited<any>>>}
   */
  showEditObjectFieldForm: (id, column, value, onAfterSubmit = null) => {
    return ObjectsGlobal.showEditObjectFieldForm(
        DepartmentNotificationSms,
        id,
        column,
        value,
        onAfterSubmit
    );
  }
};
</script>