import axios from 'axios'

export default {
  namespaced: true,
  state: {
    Issue: {},
    IssueList: [],
    Resolution: {},
    ResolutionList: [],
    list: []
  },
  mutations: {
    setIssue (state, Issue) {
      state.Issue = Issue
    },
    setIssueList (state, IssueList) {
      state.IssueList = IssueList
    },
    setResolution (state, Resolution) {
      state.Resolution = Resolution
    },
    setResolutionList (state, ResolutionList) {
      state.ResolutionList = ResolutionList
    },
    concatList (state, list) {
      let newList = state.list.concat(list)
      state.list = newList
    },
    resetList (state) {
      state.list = []
    }
  },
  actions: {
    async post ({commit, state}) { // 新增議題
      let result = await axios.post('/issue', state.Issue)
      commit('setIssue', result.data)
    },
    async initIssue ({commit, rootState}) { // 初始議題
      const currentToday = new Date()
      let currentUser = rootState.user.current
      let init = {
        item_number: '', // 編號
        description: '', // 工作事項
        undertaker: currentUser.name, // 承辦人
        initial_date: currentToday, // 建案日期
        limit_date: currentToday, // 作業期限
        remark: '', // 備註
        issue_status: '列管中' // 狀態
      }
      commit('setIssue', init)
    },
    async getIssueList ({commit}, data) { // 議題列表
      let result = await axios.get('/issue/list', {
        params: data
      })
      commit('setIssueList', result.data)
    },
    async lookupIssue ({commit}, id) { // 檢視議題
      let result = await axios.get(`/issue/lookups/${id}`)
      commit('setIssue', result.data)
    },
    async lookupResolution ({commit}, id) { // 檢視處理情況
      let result = await axios.get(`/issue/lookup/resolution/${id}`)
      commit('setResolution', result.data)
    },
    async lookupSearchList ({keyword, operator, issueDateStart, issueDateEnd}) {
      let result = await axios.post('/issue/searchList', {
        operator,
        issueDateStart: (issueDateStart === undefined) ? '' : issueDateStart,
        issueDateEnd: (issueDateEnd === undefined) ? '' : issueDateEnd,
        keyword
      })
      if (result.data.length === 0) alert('查無資料')
      return result.data
    },
    async patch ({state}) { // 修改議題
      await axios.patch(`/issue/${state.Issue.id}`, state.Issue)
    },
    async patchResolution ({state}) { // 修改處理情況
      await axios.patch(`/issue/resolution/${state.Issue.id}`, state.Resolution)
    },
    async deleteIssue ({state}, IssueId) { // 刪除議題
      await axios.delete(`/issue/${IssueId}`, state.Issue)
    },
    async deleteResolution ({state}, resolutionId) { // 刪除處理情況
      await axios.delete(`/issue/${state.Issue.id}/resolution/${resolutionId}`, state.Resolution)
    },
    async lookupIssueList ({state}, data) { // 議題列表
      let result = await axios.get('/issue/list', {
        params: data
      })
      return result.data
    },
    async lookupReferenceList ({commit, dispatch}, referenceNumbers) { // 相關議題(查詢用，目前沒用)
      let result = await axios.post('/issue/issueSearch', referenceNumbers)
      commit('concatList', result.data)
      dispatch('forLoop', result.data)
    },
    forLoop ({dispatch}, list) {
      for (let i = 0; i < list.length; i++) {
        const element = list[i]
        dispatch('lookupReferenceList', element.IssueId)
      }
    }
  },
  getters: {
    // issueList (state) {
    //   let list = Object.values(state.IssueList).map(IssueList => IssueList.content)
    //   return list
    // }
  }
}
