import { Common } from 'pc-component-v3';

export const columns = [
  {
    title: '部门名称',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '部门编号',
    dataIndex: 'no',
    key: 'no',
    width:'20%',
  },
  {
    title: '部门简介',
    dataIndex: 'description',
    key: 'description',
  },
  {
    title: '操作',
    dataIndex: 'operation',
    fixed: 'right',
  },
];

// 创建部门
export const create = organization => {
  var requestUrl = 'organizationResourceV2/create';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'post',
      data: JSON.stringify(organization),
      contentType: 'application/json; charset=utf-8',
      processData: false,
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};

// 保存更新部门
export const update = organization => {
  var requestUrl = 'organizationResourceV2/update';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'post',
      contentType: 'application/json',
      data: JSON.stringify(organization),
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};
// 删除部门
export const deleteDepartment = organization => {
  var requestUrl = 'organizationResourceV2/delete';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'post',
      dataType: 'json',
      data: JSON.stringify(organization),
      contentType: 'application/json; charset=utf-8',
      processData: false,
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};

// 获取总公司及下属所有子公司和部门(刷新)
export const getAllOrganization = id => {
  var requestUrl = 'organizationResourceV2/listClientOrganizations?clientId=' + id;
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'get',
      dataType: 'json',
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};
// 获取公司
export const getCompony = () => {
  var requestUrl = 'clientResourceV2/listClients';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'get',
      dataType: 'json',
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};

// 根据公司id查询公司下所有部门，不包括子公司的部门
export const loadSubClients = id => {
  var requestUrl = 'organizationResourceV2/queryOrganizationByClientId';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'get',
      dataType: 'json',
      data: { rootClientId: id },
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};
// 根据部门id获取部门的信息
export const loadOrganization = id => {
  var requestUrl = 'organizationResourceV2/unique?organizationId=' + id;
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'get',
      dataType: 'json',
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      processData: false,
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};

export const getUsersByName = searchQueryParam => {
  var requestUrl = 'api/userResource/getUsersByName';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiUrl2(requestUrl),
      type: 'post',
      contentType: 'application/json',
      data: JSON.stringify(searchQueryParam),
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};
// 根据公司id获取公司的信息
export const getClientInfo  = id => {
  var requestUrl = 'clientResourceV2/uniqueByClientId?clientId=' + id;
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'get',
      dataType: 'json',
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      processData: false,
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};

// 更新公司信息
export const updateClientInfo = client => {
  var requestUrl = 'clientResourceV2/update';
  return new Promise((resolve, reject) => {
    $.ajax({
      url: Common.getApiURL(requestUrl),
      type: 'post',
      contentType: 'application/json',
      data: JSON.stringify(client),
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        resolve(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        reject(XMLHttpRequest);
      },
    });
  });
};