
export const state = () => ({
  items : [],
  item : [],
  canUpdateCourse: false
})

export const actions = {
  async fetchInstructorCourses({commit}) {
    try{ 
      console.log('courses.js actions call fetchInstructorCourses')
      const courses = await this.$axios.$get('/api/v1/products/user-products')
      console.log('courses.js actions done fetchInstructorCourses')
      console.log('courses.js mutations call setCourses')
      commit('setCourses', courses)
      console.log('courses.js mutations done setCourses')
      return courses
    }catch(error){
      return error
    }
  },
  async createCourse(_, courseData) {
    try {
      console.log('instructor/course.js actions createCourse call axios.$post->products')
      const result = await this.$axios.$post('/api/v1/products', courseData)
      console.log('instructor/course.js actions createCourse done axios.$post->products')
    } catch (error) {
      return error
    }
  },
  async fetchCourseById({commit, state}, courseId){
    try {
      console.log('instructor/course.js actions fetchCourseById call axios.$get->courseId')
      const course = await this.$axios.$get(`/api/v1/products/${courseId}`)
      console.log('instructor/course.js actions fetchCourseById done axios.$get->courseId')
      console.log('courses.js mutations call setCourse')
      commit('setCourse', course)
      console.log('courses.js mutations done setCourse')
      return state.item
    } catch (error) {
      return error
    }
  },
  updateLine({commit}, {index, value, field}) {
    commit('setLineValue', {index, value, field})
    // Surprise commit for next lectures (:
    commit('setCanUpdateCourse', true)

  },
  updateCourseValue({commit}, {value, field}) {
    commit('setCourseValue', {value, field})
    commit('setCanUpdateCourse', true)

  },
  async updateCourse({state, commit}) {
    try {
      console.log('instructor/course.js actions updateCourse call axios.$patch->course')
      const course = await this.$axios.$patch(`/api/v1/products/${state.item._id}`, state.item)
      console.log('instructor/course.js actions updateCourse done axios.$patch->course')
      console.log('courses.js mutations call setCourse')
      commit('setCourse', course)
      console.log('courses.js mutations done setCourse')
      commit('setCanUpdateCourse', false)
      return state.item
    } catch (error) {
      return error
    }
  },
}

export const mutations = {
  setCourses(state, courses) {
    state.items = courses
  },
  setCourse(state, course) {
    state.item = course
  },
  addLine(state, field) {
    console.log('course.js mutations call addLine params > ', field)
    state.item[field].push({value: ''})
    console.log('course.js mutations done addLine params')
  },
  removeLine(state, {field, index}) {
    console.log('course.js mutations call removeLine params > ', field, ':', index)
    state.item[field].splice(index, 1)
    console.log('course.js mutations done removeLine params')
  },
  setLineValue(state, {index, value, field}) {
    console.log('course.js mutations call setLineValue params > ', {index, value, field})
    state.item[field][index].value = value
    console.log('course.js mutations done setLineValue params')
  },
  setCourseValue(state, {value, field}){
    state.item[field] = value
  },
  setCanUpdateCourse(state, canUpdate) {
    state.canUpdateCourse = canUpdate
  },
}