import ajax from '../utils/request.js';
import config from '../config/index.js';

/**
 * 创建对比项目
 * @param {object} ori
 * @returns
 */
export function createPlag(ori) {
  return ajax({
    api: '/bid/project/create',
    base: 'videoProcessing',
    data: ori,
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token')
    }
  });
}

/**
 * 对比项目列表
 * @param {object} ori
 * @returns
 */
export function getList(ori) {
  return ajax({
    api: '/bid/project/list',
    base: 'videoProcessing',
    data: ori.data,
    noload: ori.noload,
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token')
    }
  });
}

/**
 * 删除项目
 * @param {object} ori
 * @returns
 */
export function delList(ori) {
  return ajax({
    api: '/bid/project/delete/' + ori.id,
    base: 'videoProcessing',
    method: 'GET',
    headers: {
      Authorization: localStorage.getItem('token')
    }
  });
}

/**
 * 更新项目
 * @param {object} ori
 * @returns
 */
export function upList(ori) {
  return ajax({
    api: '/bid/project/update',
    base: 'videoProcessing',
    method: 'POST',
    data: ori,
    headers: {
      Authorization: localStorage.getItem('token')
    }
  });
}

/**
 * 导入项目文件
 * @param {object} ori
 * @returns
 */
export function importFiles(ori) {
  return ajax({
    api: '/bid/files/create',
    base: 'videoProcessing',
    method: 'POST',
    data: ori,
    headers: {
      Authorization: localStorage.getItem('token')
    }
  });
}

/**
 * 上传项目文件
 * @param {object} ori
 * @returns
 */
export function upFiles(ori) {
  return new Promise((resolve, reject) => {
    // 创建XMLHttpRequest对象
    var xhr = new XMLHttpRequest();
    // 配置请求
    xhr.open('POST', config.base['videoProcessing'] + '/bid/files/uploads', true);

    xhr.onreadystatechange = function() {
      if (this.readyState !== 4) return;
      var data = JSON.parse(this.responseText || '{}');
      if (this.status !== 200) {
        // 网络错误
        return reject({ ...data, status: this.status });
      }
      if (data.code !== 0) {
        // 业务错误
        return reject({ ...data, status: this.status });
      }

      return resolve(data.data);
    };

    xhr.setRequestHeader('Authorization', localStorage.getItem('token'));

    // 发送请求
    xhr.send(ori);
  });
}
