import axios from 'axios'

export default {
  namespaced: true,
  state: {
    current: {},
    tenders: {},
    tenderList: [],
    list: [],
    directories: {},
    selectedProjectRole: {},
    project: {
      tenders: []
    }
  },
  mutations: {
    setCurrent (state, current) {
      state.current = current
    },
    setTenders (state, tenders) {
      state.tenders = tenders
    },
    setTendersList (state, tenderList) {
      state.tenderList = tenderList
    },
    resetTender (state) {
      state.tenders = []
    },
    setList (state, list) {
      state.list = list
    },
    setProjectRole (state, projectRole) {
      state.selectedProjectRole = projectRole
    },
    resetProjectRole (state) {
      state.selectedProjectRole = {
        projectRole: '',
        issuer: '',
        issue_text_number: '',
        archive_number: '',
        title: '',
        outputFileName: '',
        issueHandlingDay: '',
        receiveHandlingDay: ''
      }
    },
    setProject (state, project) {
      state.project = {
        tenders: {
          tender: '',
          tenderNumber: '',
          PCM: {
            group: 'PCM',
            issuer: '',
            issue_text_number: '',
            title: '',
            issueHandlingDay: '',
            receiveHandlingDay: '',
            outputFileName: '',
            checked: false
          },
          CS: {
            group: 'CS',
            issuer: '',
            issue_text_number: '',
            title: '',
            issueHandlingDay: '',
            receiveHandlingDay: '',
            outputFileName: '',
            checked: false
          },
          Other: {
            group: 'Other',
            issuer: '',
            issue_text_number: '',
            title: '',
            issueHandlingDay: '',
            receiveHandlingDay: '',
            outputFileName: '',
            checked: false
          }
        }
      }
    },
    setDirectories (state, directories) {
      state.directories = directories
    }
  },
  actions: {
    async createProject ({commit, dispatch}, data) { // 新增專案
      try {
        let result
        await new Promise((resolve, reject) => {
          result = axios.post('/project', data)
          commit('setCurrent', result.data)
        }).then(
          dispatch('paramsSetting/addParam', {
            name: 'document_classification',
            chineseName: '公文類型',
            paramsValue: ['書函', '函', '備忘錄', '會議通知', '會議記錄'],
            projectId: result.data._id
          }, {root: true}),
          dispatch('paramsSetting/addParam', {
            name: 'external_unit',
            chineseName: '外部單位',
            paramsValue: [],
            projectId: result.data._id
          }, {root: true}),
          dispatch('paramsSetting/addParam', {
            name: 'paramSetting',
            chineseName: '參數設定',
            paramsValue: [{
              issuer: '',
              issue_text_number: '',
              archive_number: '',
              issueHandlingDay: '',
              receiveHandlingDay: '',
              title: '',
              outputFileName: ''
            }],
            projectId: result.data._id,
            records: []
          }, {root: true})
        ).catch((err) => {
          console.log(err)
        })
      } catch (err) {
        alert('請填完全部資料')
        console.log(err)
      }
    },
    async addNewProject ({commit, dispatch}, data) {
      await axios.post
    },
    async lookupProject ({commit}) { // 查詢專案
      let projectId = '647708b68e560ef45be262cc'
      let result = await axios.get(`/project/${projectId}`)
      commit('setCurrent', result.data)
    },
    async updateProjectInformation ({state}) { // 修改專案資料
      await axios.patch(`/project/${state.current._id}`, state.current)
    },
    async lookupProjectFileList ({state}, id) { // 查詢專案的檔案
      let result = await axios.get(`/project/${state.current._id}/files`)
      return result.data
    },
    async deleteProjectFile ({state}, fileId) { // 刪除專案的檔案
      await axios.delete(`/project/${state.current._id}/file/${fileId}`)
    },
    async addDirectory ({state}, data) { // 新增相關廠商
      try {
        await axios.post(`/project/${state.current._id}/directory`, data)
      } catch (err) {
        // alert('委託項目名稱、發文字號、發文辦理天數、收文辦理天數尚有未填！')
        console.log(err)
      }
    },
    async deleteDirectory ({state}, directoryId) { // 刪除相關廠商
      await axios.delete(`/project/${state.current._id}/directory/${directoryId}`)
    },
    async lookupDirectories ({state, commit}) { // 讀取相關廠商列表
      let result = await axios.get(`/project/${state.current._id}/directories`)
      commit('setList', result.data)
    },
    // async addProjectRoleParamSetting ({state}, data) { // 新增委託項目參數設定
    //   await axios.post(`/project/${state.current._id}/projectRole`, data)
    // },
    async addProjectRoleParamSetting ({state, commit}) { // 新增委託項目參數設定
      let result = await axios.post(`/project/${state.current._id}/projectRole`, state.selectedProjectRole)
      commit('setProjectRole', result.data)
    },
    async updateProjectRoleParamSetting ({state}, {projectRoleId, data}) { // 修改委託項目參數設定
      await axios.patch(`/project/${state.current._id}/projectRole/${projectRoleId}`, data)
    },
    async lookupProjectRoleList ({state, commit}) { // 讀取委託項目列表
      let result = await axios.get(`/project/${state.current._id}/projectRoleList`)
      commit('setList', result.data)
    },
    async lookupProjectRoleParamSetting ({state, commit}, projectRoleId) { // 查詢委託項目參數設定
      let result = await axios.get(`/project/${state.current._id}/projectRole/${projectRoleId}`)
      commit('setProjectRole', result.data)
    },
    async addProjectRoleParamSettingRecord ({state}, {projectRoleId, recordData}) { // 新增委託項目參數修改紀錄
      await axios.post(`/project/${state.current._id}/projectRole/${projectRoleId}/record`, recordData)
    },
    async addTender ({state, commit}) { // 新增標案
      let result = await axios.post(`/project/${state.current._id}/tenders`, state.tenders)
      commit('setTenders', result.data)
    },
    async initAddTender ({state, commit}) {
      let initTender = {
        tender: '',
        tenderNumber: '',
        owner: '',
        contactNumber: '',
        PCMControl: [{
          organizer: '',
          organizerEngineer: '',
          address: '',
          contactPerson: '',
          contactNumber: '',
          email: ''
        }],
        CSControl: [{
          organizer: '',
          organizerEngineer: '',
          address: '',
          contactPerson: '',
          contactNumber: '',
          email: ''
        }],
        OtherControl: [{
          organizer: '',
          organizerEngineer: '',
          address: '',
          contactPerson: '',
          contactNumber: '',
          email: ''
        }],
        ManagementControl: [{
          organizer: '',
          organizerEngineer: '',
          address: '',
          contactPerson: '',
          contactNumber: '',
          email: ''
        }]
      }
      commit('setTenders', initTender)
    },
    async lookupTendersList ({state, commit}) { // 讀取標案列表
      let result = await axios.get(`/project/${state.current._id}/tenderList`)
      commit('setTendersList', result.data)
    },
    async getTender ({state, commit}, tenderId) { // id查詢標別
      let result = await axios.get(`/project/${state.current._id}/tender/${tenderId}`)
      commit('setTenders', result.data)
    },
    async patchTender ({state}, tenderId) { // 修改標別
      await axios.patch(`/project/${state.current._id}/tender/${tenderId}`, state.tenders)
    },
    async lookupStaffs ({state}, { selects }) { // 專案人員列表
      let result = await axios.get(`/project/staffList`, {
        params: {
          _id: state.current._id,
          selects: selects
        }
      })
      return result.data
    },
    async getDirectories ({state, commit}, directoriesId) { // 檢視個人通訊錄
      let result = await axios.get(`/project/${state.current._id}/directories/${directoriesId}`)
      commit('setDirectories', result.data)
    },
    async patchDirectories ({state}, directoriesId) { // 修改通訊錄
      await axios.patch(`/project/${state.current._id}/directories/${directoriesId}`, state.directories)
    }
  }
}
