import {
  getCookie
} from '@/utils/util.js';
import {
  cookieUserId,
  cookieUserName
} from '@/config/config.js';
import axios from '@/config/axios.js';
import ElementUI from 'element-ui';

let userName = getCookie(cookieUserName);
let userId = getCookie(cookieUserId);

function discard (url, multipleSelection, callback, notice) {
  if (multipleSelection.length > 0) {
    for (var i = 0; i < multipleSelection.length; i++) {
      multipleSelection[i].updateManName = userName;
      multipleSelection[i].updateManNo = userId;
    }
    axios.post(url, multipleSelection, {
      diy: true,
      headers: {
        'Content-type': 'application/json;charset=UTF-8'
      }
    }).then(res => {
      if (res.succeed) {
        if (notice) {
          callBackSuccess(notice, callback);
        } else {
          callBackSuccess('废弃成功', callback);
        }
      } else {
        callBackError(res.message, callback)
      }
    }).catch(e => {
      return callback(new Error(e.message))
    })
  } else {
    callBackWarning('请至少勾选一条数据', callback);
  }
}
function discardOne (url, multipleSelection, callback, notice) {
  if (typeof multipleSelection !== 'undefined' && multipleSelection !== null && multipleSelection !== '') {
    for (var i = 0; i < multipleSelection.length; i++) {
      multipleSelection[i].updateManName = userName;
      multipleSelection[i].updateManNo = userId;
    }
    axios.post(url, multipleSelection, {
      diy: true,
      headers: {
        'Content-type': 'application/json;charset=UTF-8'
      }
    }).then(res => {
      if (res.succeed) {
        if (notice) {
          callBackSuccess('停役成功', callback);
        } else {
          callBackSuccess('废弃成功', callback);
        }
      } else {
        callBackError(res.message, callback)
      }
    }).catch(e => {
      return callback(new Error(e.message))
    })
  } else {
    callBackWarning('请选择要操作的数据！', callback);
  }
}

function Recovery (url, multipleSelection, callback, notice) {
  if (multipleSelection.length > 0) {
    for (var i = 0; i < multipleSelection.length; i++) {
      multipleSelection[i].updateManName = userName;
      multipleSelection[i].updateManNo = userId;
    }
    axios.post(url, multipleSelection, {
      diy: true,
      headers: {
        'Content-type': 'application/json;charset=UTF-8'
      }
    }).then(res => {
      if (res.succeed) {
        if (notice) {
          callBackSuccess(notice, callback);
        } else {
          callBackSuccess('启用成功', callback);
        }
      } else {
        callBackError(res.message, callback);
      }
    }).catch(e => {
      return callback(new Error(e.message))
    })
  } else {
    callBackWarning('请至少勾选一条数据', callback);
  }
}
function upload (url, multipleSelection, callback) {
    for (var i = 0; i < multipleSelection.length; i++) {
      multipleSelection[i].createManName = userName;
      multipleSelection[i].createManNo = userId;
    }
    axios.post(url, multipleSelection, {
      diy: true,
      headers: {
        'Content-type': 'application/json;charset=UTF-8'
      }
    }).then(res => {
      if (res.succeed) {
          callBackSuccess('导入成功', callback);
      } else {
        callBackError(res.message, callback);
      }
    }).catch(e => {
      return callback(new Error(e.message))
    })
  }

function loss (url, multipleSelection, callback) {
  if (multipleSelection.length > 0) {
    for (var i = 0; i < multipleSelection.length; i++) {
      multipleSelection[i].updateManName = userName;
      multipleSelection[i].updateManNo = userId;
    }
    axios.post(url, multipleSelection, {
      diy: true,
      headers: {
        'Content-type': 'application/json;charset=UTF-8'
      }
    }).then(res => {
      if (res.succeed) {
          callBackSuccess('挂失成功', callback);
      } else {
        callBackError(res.message, callback);
      }
    }).catch(e => {
      return callback(new Error(e.message))
    })
  } else {
    callBackWarning('请至少勾选一条数据', callback);
  }
}
function RecoveryOne (url, multipleSelection, callback, notice) {
  if (typeof multipleSelection !== 'undefined' && multipleSelection !== null && multipleSelection !== '') {
      multipleSelection.updateManName = userName;
      multipleSelection.updateManNo = userId;
    axios.post(url, multipleSelection, {
      diy: true,
      headers: {
        'Content-type': 'application/json;charset=UTF-8'
      }
    }).then(res => {
      if (res.succeed) {
        if (notice) {
          callBackSuccess('服役成功', callback);
        } else {
          callBackSuccess('启用成功', callback);
        }
      } else {
        callBackError(res.message, callback);
      }
    }).catch(e => {
      return callback(new Error(e.message))
    })
  } else {
    callBackWarning('请选择要操作的数据！', callback);
  }
}

function Delete (url, multipleSelection, callback) {
  if (multipleSelection.length > 0) {
    axios.put(url, multipleSelection, {
      diy: true,
      headers: {
        'Content-type': 'application/json;charset=UTF-8'
      }
    }).then(res => {
      if (res.succeed) {
        callBackSuccess('删除成功', callback);
      } else {
        callBackError(res.message, callback);
      }
    }).catch(e => {
      return callback(new Error(e.message))
    })
  } else {
    callBackWarning('请至少勾选一条数据', callback);
  }
}
function DeleteOne (url, callback) {
    axios.delete(url, {
      diy: true,
      headers: {
        'Content-type': 'application/json;charset=UTF-8'
      }
    }).then(res => {
      if (res.succeed) {
        callBackSuccess('删除成功', callback);
      } else {
        callBackError(res.message, callback);
      }
    }).catch(e => {
      return callback(new Error(e.message))
    })
}
function modify (url, data, callback) {
  data.updateManName = userName;
  data.updateManNo = userId;
  axios.put(url, data, {
    diy: true,
    headers: {
      'Content-type': 'application/json;charset=UTF-8'
    }
  }).then(res => {
    if (res.succeed) {
      callBackSuccess('修改成功', callback);
    } else {
      callBackError(res.message, callback);
    }
  }).catch(e => {
    callback(new Error(e.message))
  })
}

function edit (url, data, callback, notice) {
  data.updateManName = userName;
  data.updateManNo = userId;
  axios.post(url, data, {
    diy: true,
    headers: {
      'Content-type': 'application/json;charset=UTF-8'
    }
  }).then(res => {
    if (res.succeed) {
      if (notice) {
        callBackSuccess(notice, callback);
        } else {
          callBackSuccess('修改成功', callback);
        }
    } else {
      callBackError(res.message, callback);
    }
  }).catch(e => {
    callback(new Error(e.message))
  })
}

function add (url, data, callback, notice) {
  data.updateManName = '';
  data.updateManNo = '';
  data.updateTime = '';
  data.createManName = userName;
  data.createManNo = userId;
  axios.post(url, data, {
    diy: true,
    headers: {
      'Content-type': 'application/json;charset=UTF-8'
    }
  }).then(res => {
    if (res.succeed) {
      if (notice) {
        callBackSuccess(notice, callback);
      } else {
        callBackSuccess('新增成功', callback)
      }
    } else {
      callBackError(res.message, callback)
    }
  }).catch(e => {
    callback(new Error(e.message))
  })
}
function callBackSuccess (msg, callback) {
  ElementUI.Message({
    message: msg,
    type: 'success',
    duration: 2000
  });
  callback();
}
function callBackWarning (msg, callback) {
  ElementUI.Message({
    message: msg,
    type: 'warning',
    duration: 2000
  });
  callback();
}
function callBackError (msg, callback) {
  ElementUI.Message({
    message: msg,
    type: 'error',
    duration: 2000
  });
  callback(new Error(msg));
}

export {
  Recovery,
  discard,
  modify,
  add,
  Delete,
  DeleteOne,
  RecoveryOne,
  discardOne,
  loss,
  upload,
  edit
}
