let api = require('../../api').default
import axios from 'axios'
import moment from 'moment'

function diff(d1,d2){
  var a = moment(moment.unix(d1).format('YYYY-MM-DD'))
  var b = moment(moment.unix(d2).format('YYYY-MM-DD'))
  let result = a.diff(b, 'days')
  if (result>0) {
    return result
  }else {
    return 0
  }
}
function toTime(unix){
  return moment.unix(unix).format('YYYY-MM-DD HH:mm')
}
function toTimestamp(unix){
  return moment.unix(unix).format('YYYY-MM-DD HH:mm:ss')
}
function nowUnix(){
  return Number(Date.parse(new Date()))/1000
}

function arr2name(arr) {
  let strArr = []
  if (arr && arr instanceof Array && arr.length>0) {
    arr.forEach((item) => {
      strArr.push(item.name)
    })
  }
  if (strArr.length>0) {
    return strArr.join('、')
  }else{
    return '无'
  }
}
async function send(payload) {
  let {link,msg,accept_user,type,text,opText,plan_id} = payload
  let form = []
  if (payload.form != undefined && payload.form instanceof Array) {
    form = payload.form 

  }
  form.push({
    key:'操作时间：',
    value:toTimestamp(nowUnix())
  })
  let op_user
  if (localStorage.user) {
    op_user = JSON.parse(localStorage.user)
  }else{
    alert('未找到操作人,请尝试重新登录')
  }
  let req = {
    plan_id: plan_id,
    accept_user: accept_user,
    type: type,
    op_user: op_user._id,
    msg: msg,
    link: link,
    opText: opText||'查看',
    text: text||'未分类提醒',
    notify:payload.notify != undefined?payload.notify:true,
  }
  if (payload.briefing_id) {
    req.briefing_id = payload.briefing_id
  }
  if (payload.appraisal_id) {
    req.appraisal_id = payload.appraisal_id
  }
  let remind_res = await api.remind_add(req)
  let send_ding = false
  if (payload.notify != undefined) {
    if (payload.notify === true) {
      send_ding = true
    }
  }else{
    send_ding = true
  }
  if (send_ding && accept_user.length>0) {
    let ding_message = await api.ding_message({
      accept_user: accept_user,
      link: link+'&text='+text||'未分类提醒',
      msg: msg,
      author: op_user.name,
      form: form||[]
    })
  }
  return true
}

function toDate(unix) {
  return moment.unix(unix).format('YYYY-MM-DD')
}
const state = {
  count: 0
}
const mutations = {
  set_remind_count(state, val) {
    state.count = val
  }
}
const actions = {
  async remind_plan_add({}, payload) {
    if (payload.check) {
      let idSubtask = payload.parent ? '子' : ''
      let planType = ['个人计划', '部门计划', '公司计划']
      let option = {
        plan_id:payload._id,
        link:`/app/planCheck/${payload._id}?remindTime=${payload.createTime}&appraisal_type=7`,
        msg:payload.create[0].name + ` 创建了${idSubtask}计划，请审核`,
        type:100,
        text:'计划创建审核',
        opText:'审核',
        form:[{
          key:'责任人：',
          value:payload.resman[0].name
        },{
          key:'抄送人：',
          value:arr2name(payload.ccMan)
        },{
          key:'关注人：',
          value:arr2name(payload.follow)
        },{
          key:'计划类型：',
          value:planType[payload.planType]
        },{
          key:'开始日期：',
          value:moment.unix(payload.startTime).format('YYYY-MM-DD')
        },{
          key:'结束日期：',
          value:moment.unix(payload.endTime).format('YYYY-MM-DD')
        },{
          key:'计划标题：',
          value:payload.title
        },{
          key:'计划详情：',
          value:payload.detail
        }]
      }
      let accept_user = []
      let resman = await api.user_read_by_id(payload.resman[0]._id)
      if (resman.manage_id) {
        accept_user.push(resman.manage_id)
      } else {
        if (resman.superiors.length > 0) {
          accept_user.push(resman.superiors[0]._id)
        } else {
          accept_user.push(resman._id)
        }
      }
      option.accept_user = accept_user
      await send(option)
      if (resman.superiors.length > 0 && resman.superiors[0]._id != accept_user[0]) {
        option.notify=false
        option.accept_user = [resman.superiors[0]._id]
        await send(option)
      }
    }
    return true
  },
  async remind_plan_update({}, payload) {
    let last_updata_remind = await api.remind_read_by_plan_and_text({
      plan_id: payload._id,
      text: '计划修改审核'
    })
    let has_no_check = false
    if (last_updata_remind) {
      has_no_check = last_updata_remind.check == false
    }
    if (payload.check && !has_no_check) {
      let accept_user = []
      let resman = await api.user_read_by_id(payload.resman[0]._id)
      if (resman.manage_id) {
        accept_user.push(resman.manage_id)
      } else {
        if (resman.superiors.length > 0) {
          accept_user.push(resman.superiors[0]._id)
        } else {
          accept_user.push(resman._id)
        }
      }
      let idSubtask = payload.parent ? '子' : ''
      let option = {
        plan_id:payload._id,
        link:`/app/planCheck/${payload._id}?remindTime=${payload.createTime}&appraisal_type=7`,
        msg:payload.create[0].name + ` 修改了${idSubtask}计划，请审核`,
        type:109,
        text:'修改计划审核',
        opText:'审核',
        accept_user:accept_user
      }
      await send(option)
    }
    return true
  },
  async remind_plan_check({}, payload) {
    let idSubtask = payload.parent ? '子' : ''
    let option = {
      plan_id:payload._id,
      link:`/app/planDetail/${payload._id}?remindTime=${payload.createTime}`,
      msg:payload.op_user.name + ` 审核通过了${idSubtask}计划，请查看`,
      type:210,
      text:'审核通过查看',
      opText:'查看',
      form:[{
        key:'责任人：',
        value:payload.resman[0].name
      },{
        key:'计划标题：',
        value:payload.title
      },{
        key:'计划详情：',
        value:payload.detail
      }]
    }
    let resman = await api.user_read_by_id(payload.resman[0]._id)
    let accept_user = []
    if (resman._id != resman.manage_id && resman._id != resman.manage_id) {
      accept_user.push(resman._id)
    }
    if (accept_user.length>0) {
      option.accept_user = accept_user
      await send(option)
    }
    let read_user = []
    payload.ccMan.forEach((item) => {
      if (accept_user[0] != item._id) {
        read_user.push(item._id)
      }
    })
    if (resman.superiors && resman.superiors.length>0) {
      let s_id = resman.superiors[0]._id
      if (s_id != resman._id && s_id != payload.op_user._id && s_id != resman.manage_id) {
        read_user.push(s_id)
      }
    }
    if (read_user.length>0) {
      option.text = '计划创建查看'
      option.accept_user = read_user
      option.type = 200
      await send(option)
    }
    return true
  },
  async remind_plan_state({}, payload) {
    let resman = await api.user_read_by_id(payload.resman[0]._id)
    let accept_user = []
    if (payload.op_user._id != resman._id && resman._id != resman.manage_id) {
      accept_user.push(resman._id)
    }
    if (accept_user.length > 0) {
      let idSubtask = payload.parent ? '子' : ''
      let option = {
        plan_id: payload._id,
        link:`/app/planUpdate/${payload._id}?remindTime=${payload.createTime}`,
        msg:payload.op_user.name + ` 通过了${idSubtask}计划修改申请，请修改`,
        opText:'修改',
        text:'同意修改计划',
        type: 212
      }
      if (payload.isDelete) {
        option = {}
        option = {
          plan_id: payload._id,
          link:`/app/planDetail/${payload._id}?remindTime=${payload.createTime}&isDelete=1`,
          msg:payload.op_user.name + ` 通过了${idSubtask}计划删除申请，可以进行删除`,
          opText:'删除',
          text:'同意删除计划',
          type: 213
        }
      }
      option.accept_user = accept_user
      await send(option)
    }
    return true
  },
  async remind_plan_apply({}, payload) {
    let idSubtask = payload.parent ? '子' : ''
    let option = {
      plan_id:payload._id,
      link:`/app/planCheck/${payload._id}?remindTime=${payload.createTime}&appraisal_type=8`,
      msg:payload.op_user.name + ` 申请修改${idSubtask}计划，请审批`,
      type:107,
      text:'计划修改审批',
      opText:'审批'
    }
    if (payload.isDelete) {
      option = {}
      option = {
        plan_id:payload._id,
        link:`/app/planCheck/${payload._id}?remindTime=${payload.createTime}&appraisal_type=8&isDelete=1`,
        msg:payload.op_user.name + ` 申请删除${idSubtask}计划，请审批`,
        type:108,
        text:'计划删除审批',
        opText:'审批'
      }
    }
    let accept_user = []
    let resman = await api.user_read_by_id(payload.resman[0]._id)
    if (resman.manage_id) {
      accept_user.push(resman.manage_id)
    } else {
      if (resman.superiors.length > 0) {
        accept_user.push(resman.superiors[0]._id)
      } else {
        accept_user.push(resman._id)
      }
    }
    option.accept_user = accept_user
    await send(option)
    return true
  },
  async remind_plan_apply_fail({}, payload) {
    let idSubtask = payload.parent ? '子' : ''
    let link = `/app/planCheck/${payload._id}?remindTime=${payload.createTime}&appraisal_type=${payload.appraisal_type}`
    let msg = `修改${idSubtask}计划申请被驳回，请查看原因`
    let text = '修改计划驳回'
    let type = 211
    if (payload.isDelete != undefined) {
      type = 212
      text = '删除计划驳回'
      msg = `删除${idSubtask}计划申请被驳回，请查看原因`
    }
    if (payload.appraisal_type == 7) {
      msg = `${idSubtask}计划未通过审核，请查看原因`
      text = '审核失败查看'
      type = 209
    }
    let resman = await api.user_read_by_id(payload.resman[0]._id)
    let accept_user = []
    if (resman._id != payload.op_user._id && resman._id != resman.manage_id) {
      accept_user.push(resman._id)
    }
    if (accept_user.length>0) {
      let option = {
        plan_id:payload._id,
        link:link,
        text:text,
        type:type,
        opText:'查看',
        msg:msg,
        accept_user:accept_user
      }
      option.accept_user = accept_user
      await send(option)
    }
    return true
  },
  async remind_briefing_add({}, payload) {
    if (payload.briefing.superiors.length>0) {
      await send({
        plan_id:payload.plan._id,
        type:103,
        text:'审批意见填写',
        link:`/app/briefingDetail/${payload.plan._id}?type=superiors&briefing_id=${payload.briefing._id}`,
        msg:payload.op_user.name + ` 添加了简报，请填写审批意见`,
        accept_user:[payload.briefing.superiors[0]._id],
        opText:'填写',
        briefing_id:payload.briefing._id
      })
    }else{
      let plan_superiors = payload.plan.resman[0].superiors
      if (plan_superiors&&plan_superiors.length>0&&plan_superiors[0]._id!=payload.op_user._id) {
        send({
          plan_id:payload.plan._id,
          type:201,
          text:'简报信息查看',
          link:`/app/briefingDetail/${payload.plan._id}?type=read&briefing_id=${payload.briefing._id}`,
          msg:payload.op_user.name + ` 添加了简报，请查看`,
          accept_user:[plan_superiors[0]._id],
          opText:'查看',
          briefing_id:payload.briefing._id
        })
      }
    }
    if (payload.plan.ccMan.length>0) {
      let ccMan = []
      payload.plan.ccMan.forEach((item)=>{
        ccMan.push(item._id)
      })
      send({
        plan_id:payload.plan._id,
        type:201,
        text:'简报信息查看',
        link:`/app/briefingDetail/${payload.plan._id}?type=ccMan_read&briefing_id=${payload.briefing._id}`,
        msg:payload.op_user.name + ` 添加了简报，请查看`,
        accept_user:ccMan,
        opText:'查看',
        briefing_id:payload.briefing._id
      })
    }
    if (payload.briefing.participant.length>0) {
      let participant = []
      payload.briefing.participant.forEach((item)=>{
        participant.push(item._id)
      })
      await send({
        plan_id:payload.plan._id,
        type:104,
        text:'参与意见填写',
        link:`/app/briefingDetail/${payload.plan._id}?type=participant&briefing_id=${payload.briefing._id}`,
        msg:payload.op_user.name + ` 添加了简报，请填写参与意见`,
        accept_user:participant,
        opText:'填写',
        briefing_id:payload.briefing._id
      })
    }
    if (payload.briefing.assist.length>0) {
      let assist = []
      payload.briefing.assist.forEach((item)=>{
        assist.push(item._id)
      })
      await send({
        plan_id:payload.plan._id,
        type:104,
        text:'协助意见填写',
        link:`/app/briefingDetail/${payload.plan._id}?type=assist&briefing_id=${payload.briefing._id}`,
        msg:payload.op_user.name + ` 添加了简报，请填写协助意见`,
        accept_user:assist,
        opText:'填写',
        briefing_id:payload.briefing._id
      })
    }
    return true
  },
  async remind_appraisal_add({}, payload) {
    if (payload.appraisal.type==0) {
      var ccMan = payload.plan.ccMan
      var read_user = []
      if (ccMan.length>0) {
        ccMan.forEach((item)=>{
          read_user.push(item._id)
        })
      }
      if (read_user.length>0) {
        await send({
          plan_id:payload.plan._id,
          link:`/app/appraisalDetail/${payload.plan._id}?briefing_id=${payload.briefing_id}&appraisal_id=${payload.appraisal._id}`,
          msg:payload.op_user.name + ` 添加了自我评价，请查看`,
          text:'自我评价查看',
          type:202+Number(payload.appraisal.type),
          opText:'查看',
          accept_user:read_user,
          appraisal_id:payload.appraisal._id,
          briefing_id:payload.briefing_id
        })
      }
      var superiors = []
      if (payload.plan.resman[0].superiors && payload.plan.resman[0].superiors.length>0) {
        superiors.push(payload.plan.resman[0].superiors[0]._id)
      }
      if (superiors.length>0) {
        await send({
          plan_id:payload.plan._id,
          link: `/app/planDetail/${payload.plan._id}?briefing_id=${payload.briefing_id}&appraisal_id=${payload.appraisal._id}&appraisal_type=${1}`,
          msg:payload.op_user.name + ` 填写了自我评价，请填写上级评定`,
          text:'上级评定填写',
          type:111,
          opText:'填写',
          accept_user:superiors,
          appraisal_id:payload.appraisal._id,
          briefing_id:payload.briefing_id
        })
      }
    }
    else if(payload.appraisal.type==1){
      var ccMan = payload.plan.ccMan
      var read_user = []
      if (ccMan.length>0) {
        ccMan.forEach((item)=>{
          read_user.push(item._id)
        })
      }
      if (payload.plan.resman) {
        read_user.push(payload.plan.resman[0]._id)
      }
      if (read_user.length>0) {
        await send({
          plan_id:payload.plan._id,
          link:`/app/appraisalDetail/${payload.plan._id}?briefing_id=${payload.briefing_id}&appraisal_id=${payload.appraisal._id}`,
          msg:payload.op_user.name + ` 添加了${payload.appraisal.typeText}，请查看`,
          text:'上级评定查看',
          type:202+Number(payload.appraisal.type),
          opText:'查看',
          accept_user:read_user,
          appraisal_id:payload.appraisal._id,
          briefing_id:payload.briefing_id
        })
      }
    }else{
      let arr = [payload.briefing.superiors,payload.briefing.participant,payload.briefing.assist,payload.plan.resman,payload.plan.resman[0].superiors,payload.plan.ccMan,payload.plan.follow]
      let read_user = []
      arr.forEach((arrItem)=>{
        if (arrItem && arrItem.length>0) {
          arrItem.forEach((item)=>{
            if (item._id != payload.op_user._id) {
              let pushFlag = true
              if (read_user.length >0) {
                read_user.forEach((read_user_item)=>{
                  if (read_user_item == item._id) {
                    pushFlag = false
                  }
                })
              }
              if (pushFlag) {
                read_user.push(item._id)
              }
            }
          })
        }
      })
      if (read_user.length>0) {
        await send({
          plan_id:payload.plan._id,
          link:`/app/appraisalDetail/${payload.plan._id}?briefing_id=${payload.briefing_id}&appraisal_id=${payload.appraisal._id}`,
          msg:payload.op_user.name + ` 添加了${payload.appraisal.typeText}，请查看`,
          text:payload.appraisal.typeText+'查看',
          type:202+Number(payload.appraisal.type),
          opText:'查看',
          accept_user:read_user,
          appraisal_id:payload.appraisal._id,
          briefing_id:payload.briefing_id
        })
      }
    }
    return true
  },
  async remind_plan_update_restart({}, payload) {
    if (payload.check) {
      let accept_user = []
      if (payload.resman && payload.resman[0].superiors && payload.resman[0].superiors.length > 0) {
        if (payload.resman[0]._id != payload.resman[0].superiors[0]._id) {
          accept_user.push(payload.resman[0].superiors[0]._id)
        }
      }
      payload.ccMan.forEach((item) => {
        accept_user.push(item._id)
      })
      let idSubtask = payload.parent ? '子' : ''
      await send({
        plan_id:payload._id,
        link:`/app/planDetail/${payload._id}?remindTime=${payload.createTime}`,
        msg:payload.resman[0].name + ` 重启了${idSubtask}计划，请查看`,
        type: 216,
        text:'计划重启查看',
        opText:'查看',
        accept_user:accept_user
      })
    }
    return true
  }
}

export default {
  state,
  mutations,
  actions
}
