import * as types from '../constants/ActionTypes';
import {insertTodo,getTodo,finsish_Todo,removeToto} from '../../utils/homeUtil';
import calendarUtil from '../../utils/calendarUtil'; 
import { message } from 'antd';
import moment from 'moment';

import { getCurrentMonthFirst, getNextMonthFirst } from "../../utils/commonUtils"

//新增待办
export function createTodo(values){
    return dispatch => {
        return insertTodo(values)
            .then(data => {
              if(data&&data.code>=300){
                message.error(data.message,2,)
              }else{
                message.success('创建成功',2); 
                dispatch(loadTodo(values.currentDay)); 
              }
            })
            .catch(err => { throw err; });
    };
}

//获取待办事项列表
// currentDay : moment     day;
//state       : string     0未完成,1已完成
export function loadTodo(currentDay,state){
    return dispatch => {
        const months=calendarUtil.getMonthsStartEnd(currentDay.format('YYYY'),currentDay.format('MM'));   
        let parmas=''
        if(currentDay){
          parmas=parmas+`&begin_time=${moment(months[0]).unix()}&end_time=${moment(months[1]).unix()}`;
        }
        if(state){
          parmas=parmas+`&state=${state}`
        }   
        dispatch(loadTodoForDay(currentDay));
        return getTodo(parmas)
            .then(data => {
              const {items,total_count}= data;
              dispatch({
                type: types.TO_DO_LIST,
                todoList:items,
                todoListCount:total_count
              })
            })
            .catch(err => { throw err; });
    };
}
//获取指定一天
export function loadTodoForDay(currentDay,state){
  return dispatch => {
      const start=moment(currentDay.format('YYYY-MM-DD')+' 00:00:01' );  
      const end=moment(currentDay.format('YYYY-MM-DD')+' 23:59:59');    
      let parmas=''
      if(currentDay){
        parmas=parmas+`&begin_time=${start.unix()}&end_time=${end.unix()}`;
      }
      if(state=="0"){
        parmas=parmas+`&state=${state}`
      }    
      return getTodo(parmas)
          .then(data => {
            const {items=[]}= data;
            dispatch({
              type: types.TO_DO_TODAY_LIST,
              todayList:items 
            })
          })
          .catch(err => { throw err; });
  };
} 
export function finishTodo(todo){
    return dispatch => {
        return finsish_Todo(todo.id)
            .then(data => {
              if(data&&data.state){
                message.success('操作成功',2)
                dispatch(loadTodo(moment(todo.begin_time*1000))); 
              }else{
                message.error(data.message,2)
              }
              return data;
            })
            .catch(err => { throw err; });
    };
} 
export function deleteTodo(todo){
    return dispatch => {
        return removeToto(todo.id)
            .then(data => {
              if(data&&data.action){
                message.success("删除成功",2)
                dispatch(loadTodo(moment(todo.begin_time*1000))); 
              }else{
                message.error(data.message,2)
              }
              return data;
            })
            .catch(err => { throw err; });
    };
}


export function loadDone(offset,limit){
    return dispatch => {
        return getTodo(offset,limit,1)
            .then(data => {
              const {items,total_count}= data;
              dispatch({
                type: types.DONE_LIST,
                doneList:items,
                doneListCount:total_count
              })
            })
            .catch(err => { throw err; });
    };
}

// 获取一个月的社保代办
export function getOneMonths() {
  let parmas = ""
  const begin_time = getCurrentMonthFirst();
  const end_time = getNextMonthFirst()
  parmas = `&begin_time=${begin_time}&end_time=${(end_time - 1)}&state=0`;
  return dispatch => {
    return getTodo(parmas)
      .then(data => {
        const { items, total_count } = data;
        dispatch({
          type: types.DONE_LIST,
          doneList: items,
          doneListCount: total_count
        })
        return data
      })
      .catch(err => { throw err; });
  };
}