import { Message } from 'element-ui'
import request, { tenantRequest } from '@/utils/request'
import {
  add,
  del,
  submit,
  resolve,
  reject,
  send,
  messageSend,
  addStatusScope,
  addOrg,
  canDel
} from './base'
import { ScheduleType } from '@/constant'

export function getSchedules (query) {
  const { pageNum: pageIndex, pageSize, ...params } = query
  return tenantRequest({
    url: '/content/calendar/page',
    method: 'GET',
    params: addStatusScope({
      pageIndex, pageSize,
      ...params
    })
  }).then(({ data, totalCount }) => {
    data.forEach(schedule => {
      schedule.del = canDel(schedule)
    })
    return { data, totalCount }
  })
}

export function getSchedule (id, options) {
  return request({
    url: `/content/programCalendar/${id}`,
    method: 'GET',
    ...options
  }).then(({ data }) => {
    const { id, type, status, name, resolutionRatio, eventDetail } = data
    return { id, type, status, name, resolutionRatio, events: JSON.parse(eventDetail) || [] }
  })
}

export function addSchedule (data) {
  return add({
    url: '/content/calendar',
    method: 'POST',
    data: addOrg(data)
  }, tenantRequest)
}

export function deleteSchedule ({ id, name }) {
  return del({
    url: `/content/calendar/${id}`,
    method: 'DELETE'
  }, name)
}

export function saveScheduleEvents (schedule, events) {
  const { id } = schedule
  return messageSend({
    url: `/content/calendar/${id}/eventList`,
    method: 'POST',
    data: events
  }, '保存')
}

function checkSchedule (type, events) {
  if (!events.length) {
    Message({
      type: 'warning',
      message: '请先添加节目'
    })
    return false
  }
  switch (type) {
    case ScheduleType.RECUR:
      if (events.length < 2) {
        Message({
          type: 'warning',
          message: '请至少添加两个节目'
        })
        return false
      }
      break
    case ScheduleType.COMPLEX:
      if (type === ScheduleType.COMPLEX) {
        const now = Date.now()
        if (!events.some(({ until }) => !until || new Date(until) > now)) {
          Message({
            type: 'warning',
            message: '无有效节目，请添加节目'
          })
          return false
        }
      }
      break
    default:
      Message({
        type: 'warning',
        message: '不支持的类型'
      })
      return false
  }
  return true
}

export function submitSchedule ({ id, type, name }, events) {
  return new Promise((resolve, reject) => {
    if (events) {
      resolve(events)
    } else {
      send({
        url: `/content/programCalendar/${id}`,
        method: 'GET'
      }).then(({ data }) => {
        resolve(JSON.parse(data.eventDetail))
      }, reject)
    }
  }).then(events => {
    if (checkSchedule(type, events)) {
      return submit({
        url: `/content/calendar/${id}/submit`,
        method: 'POST'
      }, name)
    }
    return Promise.reject()
  })
}

export function resolveSchedule ({ id, name }) {
  return resolve({
    url: `/content/calendar/${id}/approval`,
    method: 'POST',
    data: { remark: '' }
  }, name)
}

export function rejectSchedule ({ id, name }, remark) {
  return reject({
    url: `/content/calendar/${id}/reject`,
    method: 'POST',
    data: { remark }
  }, name)
}

export function copySchedule ({ id }, name) {
  return messageSend({
    url: '/content/calendar/copy',
    method: 'POST',
    data: addOrg({ id, name })
  }, '复制')
}
