import axios from 'axios'
import router from './router'
import {Message} from 'iview'
Message.config({
  top: 150,
  duration: 3,
  closable: true
})
const api = axios.create({
  baseURL: process.env.NODE_ENV === 'production'?'http://120.78.85.251:5000':'http://127.0.0.1:5000',
  timeout: 20000,
  headers: {
    'Content-Type': 'application/json'
  }
})
let ajax_toast
let needLoading = (time)=>{
  if (time>2000) {
    ajax_toast = Message.loading({
      content:'loading...',
      closable:true,
      duration:0
    })
    clearInterval(timer)
  }
}
let timer
let time_count = 0
api.interceptors.request.use(function(config) {
  if (window.localStorage) {
    if(localStorage.token) {
      config.headers['token'] = localStorage.token
    }
  }
  timer = setInterval(()=>{
    time_count = 0
    time_count++
    needLoading(time_count)
  })
  return config
}, function(error) {
  return Promise.reject(error);
})

api.interceptors.response.use(response => {
  console.log('本次请求耗时'+time_count/1000+'秒')
  clearInterval(timer)
  let res = response.data
  if (ajax_toast) {
    ajax_toast.destroy()
  }
  if (res.code === 200) {
    return res.data
  } else {
    if (res.err_msg!=='用户认证失败') {
      Message.error(res.err_msg||'数据获取失败')
    }
    if (res.redirect) {
      localStorage.removeItem('token')
      localStorage.removeItem('user')
      router.replace('/')
    }
  }
}, error => {
  return Promise.reject(error)
})

export default {
  //登录
  login(data){
    return api.post('auth/login',data)
  },
  //注销
  logout(){
    return api.get('/auth/logout')
  },
  //使用部门id读取用户
  user_read_by_department(department_id){
    return api.post('/user/read/by_department',{department_id:department_id})
  },
  user_read_by_id(_id){
    return api.post('/user/read/by_id',{_id:_id})
  },
  //获取计划字段
  plan_keys(){
    return api.get('/plan/keys')
  },
  plan_keys_read(){
    return api.get('/plan/keys/read')
  },
  plan_keys_update(_id){
    return api.post('/plan/keys/update',{_id,_id})
  },
  plan_keys_subtask(_id){
    return api.post('/plan/keys/subtask',{_id,_id})
  },
  plan_delete(_id){
    return api.post('/plan/delete',{_id,_id})
  },
  plan_update(data){
    return api.post('/plan/update',data)
  },
  plan_update_check(_id){
    return api.post('/plan/update/check',{_id,_id})
  },
  plan_update_state(_id){
    return api.post('/plan/update/state',{_id,_id})
  },
  plan_update_subtask_sort(data){
    return api.post('/plan/update/subtask_sort',data)
  },
  plan_update_subtask_ratio(data){
    return api.post('plan/update/subtask_ratio',{subtasks:data})
  },
  plan_update_restart(_id){
    return api.post('/plan/update/restart',{_id,_id})
  },
  //读取计划
  plan_read(){
    return api.get('/plan/read')
  },
  plan_read_subtask(_id){
    return api.post('/plan/read/subtask',{_id,_id})
  },
  plan_read_search(data){
    return api.post('/plan/read/search',data)
  },
  plan_update_apply(data){
    return api.post('/plan/update/apply',data)
  },
  //简报的字段
  briefing_keys(plan_id){
    return api.post('/briefing/keys',{plan_id:plan_id})
  },
  briefing_add(data){
    return api.post('/briefing/add',data)
  },
  briefing_read(plan_id){
    return api.post('/briefing/read',{plan_id:plan_id})
  },
  briefing_read_by_id(_id){
    return api.post('/briefing/read/by_id',{_id:_id})
  },
  briefing_get_by_id(_id){
    return api.get('/briefing/read/by_id?_id='+_id)
  },
  appraisal_keys(briefing_id){
    return api.get('/appraisal/keys')
  },
  appraisal_keys_type(type,plan_id){
    return api.post('/appraisal/keys/type',{type:type,plan_id:plan_id})
  },
  appraisal_keys_briefing(briefing_id){
    return api.post('/appraisal/keys/briefing',{briefing_id:briefing_id})
  },
  appraisal_add(data){
    return api.post('/appraisal/add',data)
  },
  appraisal_read_by_plan(plan_id){
    return api.post('/appraisal/read/by_plan',{plan_id:plan_id})
  },
  appraisal_read_by_id(_id){
    return api.post('/appraisal/read/by_id',{_id:_id})
  },
  appraisal_read_by_type(data){
    return api.post('/appraisal/read/by_type',data)
  },
  appraisal_read_by_briefing(briefing_id){
    return api.post('/appraisal/read/by_briefing',{briefing_id:briefing_id})
  },
  appraisal_read_not_check(plan_id){
    return api.post('/appraisal/read/not_check',{plan_id:plan_id})
  },
  appraisal_read_not_update(plan_id){
    return api.post('/appraisal/read/not_update',{plan_id:plan_id})
  },
  //提醒字段
  remind_keys(){
    return api.get('/remind/keys')
  },
  remind_keys_type(){
    return api.get('/remind/keys/type')
  },
  remind_add(data){
    return api.post('/remind/add',data)
  },
  remind_add_plan_apply(plan_id){
    return api.post('/remind/add/plan_apply',{plan_id:plan_id})
  },
  remind_update_check(_id){
    return api.post('/remind/update/check',{_id:_id})
  },
  remind_update_check_all(){
    return api.get('/remind/update/check/all')
  },
  remind_read(text){
    if (text) {
      return api.get('/remind/read?text='+text)
    }else{
      return api.get('/remind/read')
    }
  },
  remind_read_count(){
    return api.get('/remind/read/count')
  },
  remind_read_by_plan_and_text(data){
    return api.post('/remind/read/by_plan_and_text',data)
  },
  //读取所有部门
  department_read_all(){
    return api.get('/department/read/all')
  },
  //读取所有公司
  company_read_all(){
    return api.get('/company/read/all')
  },
  //新增计划
  plan_add(data){
    return api.post('/plan/add',data)
  },
  plan_add_subtask(data){
    return api.post('/plan/add/subtask',data)
  },
  get_plan(id){
    return api.get('/plan/read/by_id?_id='+id)
  },
  plan_read_by_id(id){
    return api.post('/plan/read/by_id',{_id:id})
  },
  plan_read_subtask_withBriefing(id){
    return api.get('/plan/read/subtask/withBriefing?id='+id)
  },
  //初始化钉钉管理员和公司
  initAdminForDing(data) {
    //super_admin_id corpid dingId name phone companyName
    return api.post('/user/initAdminForDing', data)
  },
  //添加部门
  department_add_for_ding(data){
    return api.post('/department/add_for_ding', data)
  },
  //添加员工
  user_add_for_ding(data){
    return api.post('/user/add_for_ding', data)
  },
  //获取所有部门
  department_read_by_companyId(company_id){
    return api.post('/department/read_by_companyId', {companyId:company_id})
  },
  //获取公司所有员工
  user_read_by_companyId(company_id){
    return api.post('/user/read_by_companyId', {companyId:company_id})
  },
  user_read_subordinates(_id){
    return api.post('/user/read/subordinates',{_id:_id})
  },
  //获取公司所有计划
  plan_read_by_companyId(company_id){
    return api.post('/plan/read_by_companyId', {companyId:company_id})
  },
  //获取钉钉token
  getTokenFromDing(data){
    return api.post('/ding/getToken',data)
  },
  //获取钉钉getTicket
  getTicketFromDing(access_token){
    return api.post('/ding/getTicket',{access_token:access_token})
  },
  //钉钉免登流程
  autoLoginForDing(data) {
    return api.post('/ding/auto_login', data)
  },
  //获取用户信息
  get_user_by_code(data){
    return api.post('/ding/get_user_by_code', data)
  },
  notice_read_count(){
    return api.get('/notice/read/count')
  },
  notice_read(){
    return api.get('/notice/read')
  },
  notice_update_check(_id){
    return api.post('/notice/update/check',{_id,_id})
  },
  ding_message(data){
    return api.post('/ding/message',data)
  },
  ding_remind(data){
    return api.post('/ding/remind',data)
  }
}
