/* eslint-disable */
import * as commonServices from "@/services/services";
import commonConfig from "@/utils/config";
import { message } from "antd-v4";

//获取所属客户
const getsCustomerData = async () => {
  const errorMessage = "获取所属客户接口出错";
  const url = `${commonConfig.feedback_host}sysworkorder/getCustomerData`;
  const { data: result = {} } = await commonServices.postValueService(
    "",
    {},
    url
  );
  const { code = -1, data, msg = errorMessage } = result;
  if (code > -1) {
    return data;
  } else {
    message.error(msg);
    return {};
  }
};

// 获取所属模块
const getsModuleData = async () => {
  const errorMessage = "获取所属模块接口出错";
  const url = `${commonConfig.feedback_host}sysworkorder/getModuleData`;
  const { data: result = {} } = await commonServices.postValueService(
    "",
    {},
    url
  );
  const { code = -1, data, msg = errorMessage } = result;
  if (code > -1) {
    return data;
  } else {
    message.error(msg);
    return {};
  }
};

// 获取人员
const getUserData = async () => {
  const errorMessage = "获取人员接口出错";
  const url = `${commonConfig.feedback_host}sysworkorder/getUserData`;
  const { data: result = {} } = await commonServices.postValueService(
    "",
    {},
    url
  );
  const { code = -1, data, msg = errorMessage } = result;
  if (code > -1) {
    return data;
  } else {
    message.error(msg);
    return {};
  }
};

// 上传附件
const upload = async files => {
  let formData = new FormData();
  for (let item of files) {
    formData.append("file", item);
  }
  const url = `${commonConfig.feedback_host}file/uploadMulti`;
  const response = await fetch(url, {
    method: "POST",
    body: formData
  });

  const result = await response.json();
  const { code = -1, dataset, msg = "接口出错" } = result;
  if (code > -1) {
    const { rows } = dataset;
    const savePathStr = rows[0].savePathStr;
    const fileData = savePathStr.split(",").map(item => ({
      sPicturePath: item,
      sPictureName: item.substring(item.indexOf("_") + 1)
    }));
    return JSON.stringify(fileData);
  } else {
    message.error(msg);
    return "[]";
  }
};

// 删除附件
const deleteFile = async (file, sId) => {
  const errorMessage = "删除接口出错";
  const url = `${commonConfig.feedback_host}sysworkorder/delFile/${sId}`;
  const dataReturn = (await commonServices.postValueService(
    "",
    { fileData: JSON.stringify(file) },
    url
  )).data;
  if (!dataReturn) {
    message.error(errorMessage);
    return false;
  } else {
    return true;
  }
};

// 额外添加附件
const uploadAdd = async (files, { sId, sMakePerson }) => {
  const fileData = await upload(files);
  if (fileData === "[]") {
    return;
  }

  const errorMessage = "额外添加附件接口出错";
  const url = `${commonConfig.feedback_host}sysworkorder/addFile/${sId}`;
  const { data: result = {} } = await commonServices.postValueService(
    "",
    { fileData, sMakePerson },
    url
  );
  const { code = -1, data, msg = errorMessage } = result;
  if (code > -1) {
    return data;
  } else {
    message.error(msg);
    return [];
  }
};

// 提交数据
const handleSubmit = submitData => {
  const url = `${commonConfig.feedback_host}sysworkorder/add`;
  commonServices
    .postValueService("", submitData, url)
    .then(({ data: result = {} }) => {
      const { code = -1, msg = "接口出错" } = result;
      if (code > -1) {
        message.success(msg);
        _props &&
          _props.onAddWorkOrderSuccess &&
          _props.onAddWorkOrderSuccess();
      } else {
        message.error(msg);
      }
    });
};

// 获取详情数据
const getDetailData = async ({ sId }) => {
  const errorMessage = "获取详情接口出错";
  const url = `${
    commonConfig.feedback_host
  }sysworkorder/getDetail/${sId}/project`;
  const { data: result = {} } = await commonServices.postValueService(
    "",
    {},
    url
  );
  const { code = -1, data, msg = errorMessage } = result;
  if (code > -1) {
    return data;
  } else {
    message.error(msg);
    return {};
  }
};

//回复 参数{ sContentMemo, sMakePerson, sLoginId, sId , status: '回复'}
const addReply = async ({ sId, ...condition }) => {
  const errorMessage = "回复出错";
  const url = `${commonConfig.feedback_host}sysworkorder/addReply/${sId}`;
  const { data: result = {} } = await commonServices.postValueService(
    "",
    condition,
    url
  );

  const { code = -1, data, msg = errorMessage } = result;
  if (code > -1) {
    return data;
  } else {
    message.error(msg);
    return {};
  }
};

// 详情更新
const detailUpdate = async ({ sId, ...condition }) => {
  const errorMessage = "详情更新出错";
  const url = `${commonConfig.feedback_host}sysworkorder/update/${sId}`;
  const { data: result = {} } = await commonServices.postValueService(
    "",
    condition,
    url
  );

  const { code = -1, data, msg = errorMessage } = result;
  if (code > -1) {
    message.success(msg);
    return data;
  } else {
    message.error(msg);
    return {};
  }
};

const addReplyHanle = async ({ sId, ...condition }) => {
  const errorMessage = "addReplyHanle接口出错";
  const url = `${commonConfig.feedback_host}sysworkorder/addReplyHanle/${sId}`;
  const { data: result = {} } = await commonServices.postValueService(
    "",
    condition,
    url
  );

  const { code = -1, data, msg = errorMessage } = result;
  if (code > -1) {
    message.success(msg);
    return data;
  } else {
    message.error(msg);
    return {};
  }
};

// 评分（关闭工单）
const evaluate = async ({ sId, ...condition }) => {
  const errorMessage = "关闭工单接口出错";
  const url = `${commonConfig.feedback_host}sysworkorder/evaluate/${sId}`;
  const { data: result = {} } = await commonServices.postValueService(
    "",
    condition,
    url
  );

  const { code = -1, msg = errorMessage } = result;
  if (code > -1) {
    message.success(msg);
    return true;
  } else {
    message.error(msg);
    return false;
  }
};

// 富文本媒体上传
const uploadFn = async ({ file, success, error }) => {
  let formData = new FormData();
  formData.append("file", file);
  message.open({
    key: "xlyUploadFn",
    type: "loading",
    content: "文件上传中...",
    duration: 0
  });
  const url = `${commonConfig.feedback_host}file/uploadMulti`;
  const response = await fetch(url, {
    method: "POST",
    body: formData
  });
  message.destroy("xlyUploadFn");
  const result = await response.json();
  const { code = -1, dataset, msg = "上传失败" } = result;
  if (code > -1) {
    const { rows } = dataset;
    const savePathStr = rows[0].savePathStr;
    success({
      url: `${
        commonConfig.feedback_host
      }file/download?savePathStr=${savePathStr}&sModelsId=100`
    });
  } else {
    message.error(msg);
    error();
  }
};

export default {
  getsCustomerData,
  getsModuleData,
  getUserData,
  upload,
  handleSubmit,
  getDetailData,
  addReply,
  deleteFile,
  uploadAdd,
  detailUpdate,
  addReplyHanle,
  evaluate,
  uploadFn
};
