import Common from '../common/Common';

export const tableColumns = [
  {
    title: '认证源名称',
    key: 'name',
    dataIndex: 'name',
    width: 260,
  },
  {
    title: '认证源标识',
    key: 'no',
    dataIndex: 'no',
    width: 100,
  },
  {
    title: '认证源类型',
    key: 'authType',
    dataIndex: 'authType',
    width: 260,
  },
  {
    title: '状态',
    key: 'active',
    dataIndex: 'active',
    width: 100,
  },
  {
    title: '描述',
    key: 'description',
    dataIndex: 'description',
    width: 300,
  },
  {
    title: '操作',
    key: 'operation',
    width: 200,
  },
].map(item => ({ ...item, resizable: true, maxWidth: 400, minWidth: 75 }));

// 登录时获取认证信息
export const queryLoginAuth= () => {
  var requestUrl = 'AuthSettingResource/queryBeforeAllAuthSetting';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'get',
      contentType: 'application/json',

      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};

// 查询认证信息
export const queryAllAuth= () => {
  var requestUrl = 'AuthSettingResource/queryAllAuthSetting';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'get',
      contentType: 'application/json',

      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};

// 根据ID查询信息
export const queryById = datas => {
  var requestUrl = 'AuthSettingResource/queryById';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'post',
      contentType: false,
      processData: false,
      data: datas,
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};
// 新建或更新认证源
export const saveUpdateAuth = datas => {
  var requestUrl = 'AuthSettingResource/saveUpdateAuthSetting';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'post',
      contentType: false,
      processData: false,
      data: datas,
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};



// 编号唯一性校验
export const isNoExist = datas => {
  var requestUrl = 'AuthSettingResource/isNoExist';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'post',
      contentType: false,
      processData: false,
      data: datas,
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};



// 删除认证源
export const deleteAuth = datas => {
  var requestUrl = 'AuthSettingResource/deleteAuthSetting';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'post',
      contentType: false,
      processData: false,
      data: datas,
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};

// 更新认证源状态
export const changeActive = datas => {
  var requestUrl = 'AuthSettingResource/changeAuthSetting';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'post',
      contentType: false,
      processData: false,
      data: datas,
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};


// file 转 base64
export const  getBase64 = file => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });
};

// 图片转 base64
export const imageToBase64 = img => {
  var canvas = document.createElement('canvas');
  canvas.width = img.width;
  canvas.height = img.height;
  var ctx = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0, img.width, img.height);
  var ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase();
  var dataURL = canvas.toDataURL('image/jpeg' + ext);
  return dataURL;
};
  

// base64 转file
export const base64toFile = (base64, imageName) => {
  const arr = base64.split(',');
  const mime = arr[0].match(/.*?:(.*?);/)[1];
  const suffix = mime.split('/')[1];
  const bstr = window.atob(arr[1]);
  let n = bstr.length;
  const u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  const file = new File([u8arr], `${imageName}.${suffix}`, {
    type: 'image/jpeg',
  });
  return file;
};