import {
  GET_OPERATION_RECORD_API,
  GET_OPERATION_COMPANY_API,
  GET_SINGLE_OPERATION_RECORD_API,
  GET_OPERATION_DEVICELIST_API,
  POST_OPERATION_RECORD_API,
  GET_OPERATION_EXPORT_API,
  GET_OPERATION_FORMID_API,
  DELETE_OPERATION_RECORD_API,
  POST_OPERATION_COMPANY_API,
  UPDATE_OPERATION_COMPANY_API,
  DELETE_OPERATION_COMPANY_API,
} from "./api";
import instance from "@/util/request";
import apihandler from "@/util/apihandler";
import dayjs from "dayjs";

export const getOperationRecord = async ({
  work_type,
  start_created_at,
  end_created_at,
  serial_number,
  sub_system_tag,
}) => {
  const res = await instance.post(GET_OPERATION_RECORD_API, {
    work_type: parseInt(work_type),
    // start_created_at: dayjs(start_created_at).format("YYYY-MM-DDTHH:mm:ss"),
    // end_created_at: dayjs(end_created_at)
    //   .date(dayjs(end_created_at).get("date") + 1)
    //   .format("YYYY-MM-DDTHH:mm:ss"),
    serial_number: serial_number || null,
    main_system_tag: null,
    sub_system_tag:
      typeof sub_system_tag === "string" ? [sub_system_tag] : sub_system_tag,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getOperationExportRecord = async ({
  work_type,
  start_created_at,
  end_created_at,
}) => {
  const res = await instance.post(GET_OPERATION_EXPORT_API, {
    work_type: parseInt(work_type),
    startdate: dayjs(start_created_at).format("YYYY-MM-DDTHH:mm:ss"),
    enddate: dayjs(end_created_at)
      .date(dayjs(end_created_at).get("date") + 1)
      .format("YYYY-MM-DDTHH:mm:ss"),
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getOperationCompanyList = async () => {
  const res = await instance.post(GET_OPERATION_COMPANY_API, {
    sub_system_tag: [],
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getOperationDeviceList = async ({
  list_sub_system_tag,
  device_building_tag,
  device_area_tag,
}) => {
  const res = await instance.post(GET_OPERATION_DEVICELIST_API, {
    list_sub_system_tag:
      typeof list_sub_system_tag === "string"
        ? [list_sub_system_tag]
        : list_sub_system_tag,
    device_building_tag,
    device_area_tag,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getOperationEditRecord = async (formId) => {
  const res = await instance.post(GET_SINGLE_OPERATION_RECORD_API, {
    formId,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const postOperationRecord = async (formData) => {
  const res = await instance.post(POST_OPERATION_RECORD_API, formData);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getOperationFormId = async () => {
  const res = await instance.post(GET_OPERATION_FORMID_API, {});
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const deleteOperationRecord = async (id) => {
  const res = await instance.post(DELETE_OPERATION_RECORD_API, { id });
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

// 公司
export const postOperationCompany = async ({
  name,
  contact_person,
  phone,
  email,
  city,
  address,
  tax_id_number,
  remark,
}) => {
  const res = await instance.post(POST_OPERATION_COMPANY_API, {
    name,
    contact_person,
    phone,
    email,
    city,
    address,
    tax_id_number,
    remark,
  });
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const updateOperationCompany = async ({
  name,
  contact_person,
  phone,
  email,
  city,
  address,
  tax_id_number,
  remark,
  id,
}) => {
  const res = await instance.post(UPDATE_OPERATION_COMPANY_API, {
    id,
    name,
    contact_person,
    phone,
    email,
    city,
    address,
    tax_id_number,
    remark,
  });
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const deleteOperationCompany = async (id) => {
  const res = await instance.post(DELETE_OPERATION_COMPANY_API, { id });
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};
