import Vue from 'vue';
import Vuex from 'vuex';
import _ from 'lodash';

Vue.use(Vuex);
const api = {
  fairs: '/api/jobs/fairs',
  fairsInfo: '/api/jobs/fairs/{id}',
  fairsSimple: '/api/jobs/fairs/{id}/simple',
  infos: '/api/jobs/infos',
  infoss: '/api/jobs/infos/{id}',
  talks: '/api/jobs/talks',
  talksinfo: '/api/jobs/talks/{id}',
  posts: '/api/jobs/posts',
  postsinfo: '/api/jobs/posts/{id}',
  corpBase: '/api/corp/corps/{corpid}',
  corpInfo: '/api/corp/corps/{corpid}/info',
  corpIdentity: '/api/corp/corps/{corpid}/identity',
  userReg: '/api/stud/registers/{id}',
  tickets: '/api/jobs/tickets',
  ticketsinfo: '/api/jobs/tickets/{id}',
  resumes: '/api/jobs/resumes',
  resumesInfo: '/api/jobs/resumes/{id}',
  letters: '/api/jobs/letters',
  //微信部分
  connection: '/weixin/qrcode/create',
  wxtoken: '/weixin/qrcode/{qrcode}/token',
  studLogin: '/api/stud/login',
  //双选会企业信息
  fairCorp: '/api/jobs/faircorps',
  fairJobs: '/api/jobs/faircorps/{id}/jobs',
  //分站菜单
  menuQuery: '/api/cms/menu/list',
  menuSearch: `/adminapi/menu/fetch/{id}`,
  // 站点管理
  siteQuery: '/api/cms/site/query',
  siteFetch: `/api/cms/site/config`,
  // 新闻管理
  query: '/api/cms/news/list',
  fetch: '/api/cms/news/fetch/{id}',
  update: '/api/cms/news/update/{id}',
  delete: '/api/cms/news/delete/{id}',
  // news: '/adminapi/cms/news/create',
  // query: '/adminapi/cms/news/query',
  // fetch: id => `/adminapi/cms/news/fetch/${id}`,
  // update: id => `/adminapi/cms/news/update/${id}`,
  // delete: id => `/adminapi/cms/news/delete/${id}`,
};

export default new Vuex.Store({
  state: {
    user: {},
  },
  mutations: {
    setUser(state, payload) {
      if (payload) {
        state.user = payload;
        sessionStorage.setItem('user', JSON.stringify(payload));
      } else {
        let user = sessionStorage.getItem('user');
        if (user) state.user = JSON.parse(user);
        else return false;
      }
    },
  },
  actions: {
    //招聘会
    async jobfairOperation({ state }, { type, data }) {
      let result;
      let { skip, limit } = data;
      if (type === 'list') {
        data = this.$toUndefined(data);
        let { schid } = data;
        result = await this.$axios.$get(api.fairs, {}, { schid: schid, skip: skip, limit: limit });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.fairsInfo, { id: id });
      }
      return result;
    },
    //招聘会企业信息
    async fairInfoOperation({ state }, { type, data }) {
      let result;
      let { skip, limit } = data;
      if (type === 'searchCorp') {
        let { schid, fairid, corpid, status } = data;
        result = this.$axios.$get(api.fairCorp, {}, { schid: schid, fairid: fairid, corpid: corpid, status: status, skip: skip, limit: limit });
      }
      if (type === 'searchJobs') {
        let { id } = data;
        result = this.$axios.$get(api.fairJobs, { id: id }, { skip: skip, limit: limit });
      }
      return result;
    },
    //招聘会简要信息
    async getfairsSimple({ state }, { type, data }) {
      let result;
      if (type === 'list') {
        let { id } = data;
        result = await this.$axios.$get(api.fairsSimple, { id: id });
      }
      return result;
    },
    //在线招聘
    async jobinfoOperation({ state }, { type, data }) {
      let { skip, limit } = data;
      let result;
      if (type === 'list') {
        data = this.$toUndefined(data);
        let { schid, corpname, is_practice } = data;
        result = await this.$axios.$get(api.infos, {}, { corpname: corpname, schid: schid, is_practice: is_practice, skip: skip, limit: limit, status: 1 });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.infoss, { id: id });
      }
      return result;
    },
    //宣讲会
    async postTalksInfo({ state }, { type, data }) {
      let result;
      let { skip, limit } = data;
      if (type === 'list') {
        data = this.$toUndefined(data);
        let { schid, corpname, date } = data;
        result = await this.$axios.$get(api.talks, {}, { corpname: corpname, schid: schid, date: date, skip: skip, limit: limit, status: 1 });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.talksinfo, { id: id });
      }
      return result;
    },
    //职位管理
    async postOperation({ state }, { type, data }) {
      let { skip, limit } = data;
      let result;
      if (type === 'list') {
        data = this.$toUndefined(data);
        let { schid, corpname, is_practice, corpid, is_publish } = data;
        result = await this.$axios.$get(
          `${api.posts}`,
          {},
          { schid: schid, corpid: corpid, corpname: corpname, is_practice: is_practice, skip: skip, limit: limit, is_publish: is_publish }
        );
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.postsinfo, { id: id });
      }
      return result;
    },
    //企业基本信息
    async corpOperation({ state }, { type, data }) {
      let result;
      if (type === 'search') {
        let { corpid } = data;
        result = await this.$axios.$get(api.corpInfo, { corpid: corpid });
      }
      if (type === 'component') {
        let { corpid } = data;
        let base = await this.$axios.$get(api.corpBase, { corpid: corpid });
        let info = await this.$axios.$get(api.corpInfo, { corpid: corpid });
        let identity = await this.$axios.$get(api.corpIdentity, { corpid: corpid });
        result = {
          data: { ...info.data, ...identity.data, ...base.data },
          errcode: `${info.errcode}` === '0' && `${identity.errcode}` === '0' && `${base.errcode}` === '0' ? '0' : '1',
        };
      }
      return result;
    },
    // 入场券接口
    async ticketsOperation({ state }, { type, data }) {
      let result;
      let { skip, limit } = data;
      if (type === 'add') {
        let { query, body } = data;
        result = await this.$axios.$post(api.tickets, body, {}, query);
      }
      if (type === 'list') {
        let { schid, fairid, studid } = data;
        result = await this.$axios.$get(api.tickets, {}, { schid: schid, fairid: fairid, studid: studid, skip: skip, limit: limit });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.ticketsinfo, { id: id });
      }
      if (type === 'update') {
        let { info, id } = data;
        result = await this.$axios.$post(api.ticketsinfo, info, { id: id });
      }
      if (type === 'delete') {
        let { id } = data;
        result = await this.$axios.$delete(api.ticketsinfo, {}, { id: id });
      }
      return result;
    },
    //用户
    async userOperation({ stata }, { type, data }) {
      let result;
      if (type === 'search') {
        let { studid } = data;
        result = await this.$axios.$get(api.userReg, { id: studid });
      }
      if (type === 'login') {
        let { loginType, info } = data;
        result = this.$axios.$post(api.studLogin, info, {}, { type: loginType });
      }
      return result;
    },
    //求职信
    async lettersOperation({ state }, { type, data }) {
      let result;
      let { skip, limit } = data;
      if (type === 'list') {
        let { resume_id, status, corpname, studname, post_id } = data;
        result = await this.$axios.$get(
          api.letters,
          {},
          { post_id: post_id, resume_id: resume_id, status: status, corpname: corpname, studname: studname, skip: skip, limit: limit }
        );
      }
      if (type === 'apply') {
        let { query, body } = data;
        result = await this.$axios.$post(api.letters, body, {}, query);
      }
      return result;
    },
    // 简历管理
    async resumesOperation({ state }, { type, data }) {
      let result;
      if (type === 'add') {
        let { info, schid, studid } = data;
        result = await this.$axios.$post(api.resumes, info, {}, { schid: schid, studid: studid });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.resumesInfo, { id: id });
      }
      if (type === 'update') {
        let { id, info } = data;
        result = await this.$axios.$post(api.resumesInfo, info, { id: id });
      }
      return result;
    },
    //微信部分
    async createConnection() {
      let result = await this.$axios.$post(api.connection);
      if (result.errcode != undefined && result.errcode === 0) {
        console.log('create qrcode success', result.data);
        return result.data;
      }
      console.error('create qrcode fail', result);
    },
    async getWxtoken({ state }, qrcode) {
      let result = await this.$axios.$post(api.wxtoken, {}, { qrcode: qrcode });
      if (result.errcode != undefined && result.errcode === 0) {
        console.log('qrcode login success', result);
        return result.token;
      }
      console.error('create qrcode fail', result);
    },
    //抓取信息
    async getBugInfo({ state }, { data }) {
      let { skip, limit } = data;
      let result = await this.$axios.$get('/bugInfo/newsinfo', {}, { skip: skip, limit: limit });
      return result;
    },
    //分站菜单
    async menuOperation({ state }, { type, data }) {
      let result;
      if (type === 'list') {
        let { site } = data;
        result = await this.$axios.$get(api.menuQuery, {}, { site: site });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.menuSearch, { id: id });
      }
      return result;
    },
    // 站点管理
    async siteOperation({ state }, { type, data }) {
      let result;
      if (type === 'list') {
        let { site, column } = data;
        result = await this.$axios.$get(api.siteQuery, { site: site, column: column });
      }
      if (type === 'search') {
        let { site } = data;
        //_tenant
        result = await this.$axios.$get(api.siteFetch, {}, { _tenant: site });
      }
      return result;
    },
    // 新闻管理
    async newsOperation({ state }, { type, data }) {
      let { skip, limit } = data;
      let result;
      if (type === 'list') {
        let { site, column } = data;
        console.log(data);
        result = await this.$axios.$get(api.query, {}, { site: site, column: column, skip: skip, limit: limit });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.fetch, { id: id });
      }
      return result;
    },
  },
});

const data = {
  testItem: {
    id: Math.random(),
    name: 'name',
    age: 'age',
    tel: '13099876544',
  },
};
