import config from '@/config';
import { getCompanyApplyPendingList } from '@/api/companyApply.js';

let taskList = [
  {
    name: '访客指引',
    remarks: 'Wi-Fi、洗手间使用等信息',
    icon: config.h5StaticPath + '/test-data/home/task/visitor-guidance.png',
    belongRoleList: [4],
    navigationUrl:
      '/h5/pages/common/h5?url=/visitor-guide-tenantslink&pageTitle=访客指引&navigationBarBgColor=#ffffff',
  },
  {
    name: '全面塑形瑜伽',
    remarks: '下午 06:15',
    icon: config.h5StaticPath + '/test-data/home/task/sport.png',
    belongRoleList: [1, 2, 3],
    navigationUrl:
      'fitness/homeDetail?isReserved=1&title=' +
      encodeURIComponent('全面塑形瑜伽训练'),
  },
  {
    name: '京东快递未领取',
    remarks: '33号储物柜',
    belongRoleList: [1, 2, 3],
    icon: config.h5StaticPath + '/test-data/home/task/jingdong.png',
  },
];

export default {
  state: {
    taskList: taskList,
  },
  mutations: {
    addEnergyTask(state, data) {
      let energyTask = {
        type: 'energy',
        name: '能量站食品待领取',
        remarks: '约 ' + data.recevieTime,
        icon: config.h5StaticPath + '/test-data/home/task/energy.png',
        navigationUrl: 'energy/order-detail',
        shoppingList: data.shoppingList,
        recevieTime: data.recevieTime,
      };
      state.taskList.unshift(energyTask);
    },
    addMeetingRoomTask(state, data) {
      let energyTask = {
        type: 'meetingRoom',
        name: data.roomName + '会议室预约',
        remarks: data.reserveTimeDesc,
        icon: config.h5StaticPath + '/test-data/home/task/meeting.png',
        navigationUrl: '/meetingroom/pages/detail?roomName=' + data.roomName,
        ...data,
      };
      state.taskList.unshift(energyTask);
    },
    initCompanyApplyTask(state) {
      initCompanyApplyTask(state.taskList);
    },
  },
  actions: {
    addEnergyTask({ commit }, data) {
      commit('addEnergyTask', data);
    },
    addMeetingRoomTask({ commit }, data) {
      commit('addMeetingRoomTask', data);
    },
    initCompanyApplyTask({ commit }) {
      commit('initCompanyApplyTask');
    },
  },
};

function initCompanyApplyTask(taskList) {
  let filteredList = taskList.filter((item) => {
    return item.type != 'apply';
  });
  getCompanyApplyPendingList().then((res) => {
    let list = res.data || [];
    list.forEach((item) => {
      filteredList.unshift({
        type: 'apply',
        name: '加入企业申请',
        remarks: item.companyName,
        belongRoleList: [1, 2, 3, 4],
        statusText: item.statusText,
        navigationUrl: `/account/pages/company-apply?applyId=${item.id}`,
      });
    });
    taskList.splice(0, taskList.length, ...filteredList);
  });
}
