// import { fetchTemp, saveModules } from '@/api/app-custom-center/app-index'
// import IndexTemplateSet from '@/api/app-custom-center/app-index'

// var api = new IndexTemplateSet()

const actions = {
  // 初始化首页的布局数据
  initLayoutData({ commit }, data) {
    const { api } = data
    console.log('app-index 初始化頁面布局数据', data)

    return new Promise((resolve, reject) => {
      api
        .fetchTemp({ templateId: data.id })
        .then(response => {
          console.log(response.data)
          commit('UPDATE_LAYOUT', response.data)
          resolve(response.data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // 更新首页模板布局数据
  updateLayout({ commit }, data) {
    console.log('更新的东西', data)
    commit('UPDATE_LAYOUT', data)
  },

  saveDate({ getters }, params) {
    const { api, id } = params
    const data = JSON.parse(JSON.stringify(getters.getLayout))
    data.templateId = id
    if (data.bgColor === undefined || data.bgColor === null) {
      data.bgColor = ''
    }
    console.log(JSON.stringify(data))
    return new Promise((resolve, reject) => {
      api
        .saveModules(data)
        .then(response => {
          resolve(response.data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  /**
   *  删除元素
   *
   * @param {*} { commit }
   * @param {*} row 列表行
   * @param {*} cate 二级分类
   * @param {*} index 二级分类行
   */
  deleteItem({ getters }, data) {
    const { row, cate, index, id } = data
    console.log('删除元素', row, cate, index, id)

    var tempList = getters.getLayout.moduleList

    if (cate !== undefined && index !== undefined) {
      tempList[row].listStyle[cate].moduleList.splice(index, 1)
    } else {
      tempList.splice(row, 1)
    }
  }
}

export default actions
