<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 DepartmentDailyReports object
     */
    export const DepartmentDailyReports = {
      meta: {
        title: "Afdelingens dagsopgørelser",
        icon: "fas fa-list",
        description: "Oversigt over afdelingens dagsopgørelser",
        endpoint: "/departments/daily-reports",
        labels: {
          single: "dagsopgørelse",
          multiple: "dagsopgørelser"
        }
      },
      columns: {
        id: {
          label: "ID",
          type: "number",
          sortable: true,
          creation: {
            required: false
          }
        },
        department_id: {
          label: "Afdelings ID",
          type: "select",
          sortable: true,
          creation: {
            required: true
          },
          options: async () => {
            console.log("Getting departments");
            return SessionUser.objects.departments.get.all();
          }
        },
        water_usage: {
          label: "Vandtal (m3)",
          type: "number",
          sortable: true,
          creation: {
            required: true
          },
        },
        filled_by: {
          label: "Udfyldt af",
          type: "number",
          sortable: true,
          creation: {
            required: false
          },
        },
        created_at: {
          label: "Oprettet",
          type: "date",
          sortable: true,
          creation: {
            required: false
          }
        },
      },
      add: async (department_id, water_usage_morning = 0, water_usage = 0, notes = null, date = null) => {
        return ObjectsGlobal.add.object(
            DepartmentDailyReports.meta.endpoint,
            {
              department_id: parseInt(department_id),
              water_usage_morning: parseInt(water_usage_morning),
              water_usage: parseInt(water_usage),
              notes: notes ? notes : 'Daily report',
              date: date ? date : new Date().toISOString().split('T')[0]
            }
        );
      },
      set: {
        water_usage: async (id, water_usage) => {
          return ObjectsGlobal.set.column(
              DepartmentDailyReports.meta.endpoint,
              id,
              "water_usage",
              parseInt(water_usage)
          )
        },
        water_usage_morning: async (id, water_usage_morning) => {
          return ObjectsGlobal.set.column(
              DepartmentDailyReports.meta.endpoint,
              id,
              "water_usage_morning",
              parseInt(water_usage_morning)
          )
        },
      },
      get: {
        all: async () => {
          return ObjectsGlobal.get.objects(DepartmentDailyReports.meta.endpoint);
        },
        single: async (id) => {
          return ObjectsGlobal.get.object(DepartmentDailyReports.meta.endpoint, parseInt(id));
        }
      },
      delete: async (id) => {
        return ObjectsGlobal.delete.object(DepartmentDailyReports.meta.endpoint, parseInt(id));
      },
      functions: {
        showCreateObjectFormDepartment: async (department_id, onAfterSubmit = null) => {
          console.log("Department ID", department_id, "type", typeof department_id);
          return ObjectsGlobal.showCreateObjectForm(DepartmentDailyReports, onAfterSubmit, {department_id: department_id});
        },
        showDeleteObjectForm: (id, onAfterSubmit = null) => {
          return ObjectsGlobal.showDeleteObjectForm(DepartmentDailyReports, id, onAfterSubmit);
        },
        getProductCount: async (department_id, product_id, date) => {
          return authenticatedRequest(
              "/departments/daily-reports/product-count?product_id=" + product_id + "&date=" + date + "&department_id=" + department_id,
              "GET"
          );
        },
        getTransactionCount: async (department_id, date) => {
          return authenticatedRequest(
              "/departments/daily-reports/transaction-count?date=" + date + "&department_id=" + department_id,
              "GET"
          );
        },
        getLatest: async (department_id) => {
          return authenticatedRequest(
              "/departments/daily-reports/latest?id=" + department_id,
              "GET"
          );
        },
        getDepartmentDailyReport: async (department_id, date) => {
          return authenticatedRequest(
              "/departments/daily-reports/get?id=" + department_id + "&date=" + date,
              "GET"
          );
        },
        getDepartmentDailyBookingReport: async (department_id, date) => {
          return SessionUser.request(
              "/departments/daily-reports/bookings-count",
              "GET",
              {
                department_id: department_id,
                date: date
              }
          );
        },
      },
      /**
       * Show the create object form
       * @param onAfterSubmit
       * @returns {Promise<SweetAlertResult<Awaited<any>>>}
       */
      showCreateObjectForm: (onAfterSubmit = null) => {
        return ObjectsGlobal.showCreateObjectForm(DepartmentDailyReports, onAfterSubmit);
      },
      /**
       * 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(
            DepartmentDailyReports,
            id,
            column,
            value,
            onAfterSubmit
        );
      }
    };
</script>