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

Vue.use(Vuex);
const api = {
  corpInfo: '/api/corp/corps/{corpid}/info',
  corpBase: '/api/corp/corps/{corpid}',
  hr: '/api/corp/corps/{corpid}/users', //list,add
  hrImp: '/api/corp/users/{id}', //查询:id:手机号,用户肯定不知道id啊!那就手机号呗;修改:用id
  // 转让管理员
  hrChange: '/api/corp/users/{id}/role',
  corpIdentity: '/api/corp/corps/{corpid}/identity',
  corpSchInfo: '/api/corp/corps/{corpid}/schs',
  schs: '/api/corp/schs', //get:按学校查询入驻企业列表;post:申请入驻学校
  profiles: '/api/jobs/profiles', //get查询招生简章列表；post创建招生简章
  posts: '/api/jobs/posts',
  postsinfo: '/api/jobs/posts/{id}',
  // posts: `/adminapi${process.env.NODE_ENV === 'development' ? '' : ' / jobs'}/posts`,
  // postsinfo: `/adminapi${process.env.NODE_ENV === 'development' ? '' : ' / jobs'}/posts/{id}`,
  profilesinfo: '/api/jobs/profiles/{id}',
  infos: '/api/jobs/infos',
  fairs: '/api/jobs/fairs',
  fairsinfo: '/api/jobs/fairs/{id}',
  faircorps: '/api/jobs/faircorps',
  faircorpsInfo: '/api/jobs/faircorps/{id}',
  infoss: '/api/jobs/infos/{id}',
  talks: '/api/jobs/talks',
  talksinfo: '/api/jobs/talks/{id}',
  resumes: '/api/jobs/resumes',
  resumesInfo: '/api/jobs/resumes/{id}',
  letters: '/api/jobs/letters',
  lettersinfo: '/api/jobs/letters/{id}',
  userReg: '/api/corp/users/{id}',
  connection: '/weixin/qrcode/create',
  wxtoken: '/weixin/qrcode/{qrcode}/token',
  corpBind: '/api/corp/bind', //post=>query;corpid;body:mobile,wxtoken
  corpLogin: '/api/corp/login',
  dictionary: '/api/code/{type}/items', //type === xzqh:query:level(1/2);parent(无/code)
  corpReg: '/api/corp/corps', //query:type:0=>用户名+密码 account(学号@学校id)+passwd;1-微信(wxtoken)
  findOne: '/api/code/xzqh/findOne', //query:name
  //验证码
  verify: '/api/cms/verify/verify',
  checkVerify: '/api/cms/verify/check_verify',
  //短信验证码
  message: '/api/cms/verify/sendmessage',
  checkMessage: '/api/cms/verify/check_message',
  // 关注数
  stucorp: `/api/jobs/studentcorp`,
  stucorpsearch: `/api/jobs/studentcorp`,
  stucorplist: `/api/jobs/studentcorp`,
  stucorpupdate: `/api/jobs/studentcorp`,
  stucorpdelete: `/api/jobs/studentcorp/{id}`,
  //待办事项
  notice: `/api/msg/message`, //${process.env.NODE_ENV === 'development' ? '/napi' : '/api'}
  noticeDelete: `/api/msg/message/{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 userOperation({ state }, { type, data }) {
      let result;
      if (type === 'passwd') {
        let { id, info } = data;
        result = await this.$axios.$post(api.userReg, info, { id: id });
      }
      if (type === 'login') {
        let { loginType, info } = data;
        result = this.$axios.$post(api.corpLogin, info, {}, { type: loginType });
      }
      return result;
    },
    async corpSchInfo({ state, commit }, { type, data }) {
      let result;
      if (type === 'list') {
        let { corpid, skip, limit } = data;
        result = await this.$axios.$get(`${api.corpSchInfo}`, { corpid: corpid }, { skip: skip, limit: limit, status: '已审核' });
        // let newArr = result.data.filter(fil => `${fil.schid}` !== `999991`);
        // result.data = newArr;
      }

      return result;
    },
    //宣讲会
    async postTalksInfo({ state }, { type, data }) {
      let { skip, limit } = data;
      let result;
      if (type === 'add') {
        let { info, corpid, corpname, schid, schname } = data;
        result = await this.$axios.$post(api.talks, info, {}, { schid: schid, corpid: corpid, corpname: corpname, schname: schname });
      }
      if (type === 'list') {
        let { corpid, status } = data;
        result = await this.$axios.$get(api.talks, {}, { corpid: corpid, status: status, skip: skip, limit: limit });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.talksinfo, { id: id });
      }
      if (type === 'update') {
        let { info, id } = data;
        result = await this.$axios.$post(api.talksinfo, info, { id: id });
      }
      if (type === 'delete') {
        let { id } = data;
        result = await this.$axios.$delete(api.talksinfo, {}, { id: id });
      }
      return result;
    },
    //在线招聘
    async jobinfoOperation({ state }, { type, data }) {
      let { skip, limit } = data;
      let result;
      if (type === 'add') {
        let { info, corpid, corpname, schid, schname } = data;
        result = await this.$axios.$post(api.infos, info, {}, { schid: schid, corpid: corpid, corpname: corpname, schname: schname });
      }
      if (type === 'list') {
        let { corpid, is_practice, status } = data;
        result = await this.$axios.$get(api.infos, {}, { corpid: corpid, skip: skip, limit: limit, is_practice: is_practice, status: status });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.infoss, { id: id });
      }
      if (type === 'update') {
        let { info, id } = data;
        result = await this.$axios.$post(api.infoss, info, { id: id });
      }
      if (type === 'delete') {
        let { id } = data;
        result = await this.$axios.$delete(api.infoss, {}, { id: id });
      }
      return result;
    },
    //职位管理
    async postOperation({ state }, { type, data }) {
      let { skip, limit } = data;
      let result;
      if (type === 'add') {
        let { info, corpid, corpname } = data;
        result = await this.$axios.$post(api.posts, info, {}, { corpid: corpid, corpname: corpname });
      }
      if (type === 'list') {
        let { corpid, is_practice } = data;
        result = await this.$axios.$get(`${api.posts}`, {}, { corpid: corpid, skip: skip, limit: limit, is_practice: is_practice });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.postsinfo, { id: id });
      }
      if (type === 'update') {
        let { info, id } = data;
        result = await this.$axios.$post(api.postsinfo, info, { id: id });
      }
      if (type === 'delete') {
        let { id } = data;
        result = await this.$axios.$delete(api.postsinfo, {}, { id: id });
      }
      return result;
    },
    //hr一系列操作
    async hrOperation({ state }, { type, data }) {
      let result;
      if (type === `delete`) {
        let { id } = data;
        result = await this.$axios.$delete(api.hrImp, {}, { id: id });
      } else if (type === `add`) {
        let { corpid, info } = data;
        result = await this.$axios.$post(api.hr, info, { corpid: corpid });
      } else if (type === 'search') {
        let { tel } = data;
        result = await this.$axios.$get(api.hrImp, { id: tel });
      } else if (type === 'update') {
        let { info } = data;
        let { passwd, id } = info;
        result = await this.$axios.$post(api.hrImp, { passwd: passwd }, { id: id });
      } else if (type === 'change') {
        let { corpid, adminid, id } = data;
        result = await this.$axios.$post(api.hrChange, { corpid: corpid, adminid: adminid }, { id: id });
      } else {
        result = await this.$axios.$get(api.hr, { corpid: data });
      }
      return result;
    },
    //招聘简章
    async profilesOperation({ state }, { type, data }) {
      let { skip, limit } = data;
      let result;
      if (type === 'add') {
        let { info, corpid, corpname } = data;
        result = await this.$axios.$post(api.profiles, info, {}, { corpid: corpid, corpname: corpname });
      }
      if (type === 'list') {
        let { corpid } = data;
        result = await this.$axios.$get(api.profiles, {}, { corpid: corpid, skip: skip, limit: limit });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.profilesinfo, { id: id });
      }
      if (type === 'update') {
        let { info, id } = data;
        result = await this.$axios.$post(api.profilesinfo, info, { id: id });
      }
      if (type === 'delete') {
        let { id } = data;
        result = await this.$axios.$delete(api.profilesinfo, {}, { id: id });
      }
      return result;
    },
    //企业基本信息
    async corpOperation({ state }, { type, data }) {
      let result;
      if (type === 'search') {
        let { corpid } = data;
        let info = await this.$axios.$get(`${api.corpInfo}`, { corpid: corpid });
        let base = await this.$axios.$get(api.corpBase, { corpid: corpid });
        let identity = await this.$axios.$get(api.corpIdentity, { corpid: corpid });
        return { info: info.data, base: base.data, identity: identity.data };
      }
      if (type === 'info') {
        let { corpid, info } = data;
        result = await this.$axios.$post(api.corpInfo, info, { corpid: corpid });
      }
      if (type === 'identity') {
        let { corpid, info } = data;
        result = await this.$axios.$post(api.corpIdentity, info, { corpid: corpid });
      }
      return result;
    },
    //入驻学校
    async joinSch({ state }, { schid, corpid, schname }) {
      let result = await this.$axios.$post(api.schs, { schid: schid, schname: schname, corpid: corpid });
      return result;
    },
    // 简历管理
    async resumesOperation({ state }, { type, data }) {
      let result;
      if (type === 'list') {
        let { schid, year } = data;
        result = await this.$axios.$get(api.resumes, {}, { schid: schid, year: year });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.resumesInfo, { id: id });
      }
      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 === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.lettersinfo, { id: id });
      }
      if (type === 'update') {
        let { id, status } = data;
        result = await this.$axios.$post(api.lettersinfo, { status: status }, { id: id });
      }
      return result;
    },
    // 招聘会
    async jobfairOperation({ state }, { type, data }) {
      let { skip, limit } = data;
      let result;
      if (type === 'add') {
        let { info, corpid, corpname } = data;
        result = await this.$axios.$post(api.fairs, info, {}, { corpid: corpid, corpname: corpname });
      }
      if (type === 'list') {
        let { schid, status } = data;
        result = await this.$axios.$get(api.fairs, {}, { schid: schid, status: status, skip: skip, limit: limit });
      }
      if (type === 'myList') {
        let { schid, fairid, corpid, status } = data;
        result = await this.$axios.$get(api.faircorps, {}, { schid: schid, fairid: fairid, corpid: corpid, status: status, skip: skip, limit: limit });
      }
      if (type === 'search') {
        let { id } = data;
        result = await this.$axios.$get(api.fairsinfo, { id: id });
      }
      if (type === 'update') {
        let { info, id } = data;
        result = await this.$axios.$post(api.faircorps, info, { id: id });
      }
      if (type === 'delete') {
        let { id } = data;
        result = await this.$axios.$delete(api.fairsinfo, {}, { id: id });
      }
      if (type === 'apply') {
        let { schname, fairid, corpid, corpname, info } = data;
        result = await this.$axios.$post(api.faircorps, info, {}, { schname: schname, fairid: fairid, corpid: corpid, corpname: corpname, schid: 999991 });
      }
      if (type === 'searchApply') {
        let { id } = data;
        result = await this.$axios.$get(api.faircorpsInfo, { id: id });
      }
      if (type === 'applyUpdate') {
        let { id, info } = data;
        result = await this.$axios.$post(api.faircorpsInfo, 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 bindOperation({ state }, { type, data }) {
      let { corpid, ...info } = data;
      let result = await this.$axios.$post(api.corpBind, info, {}, { corpid: corpid });
      return result;
    },
    // 字典表
    async dicOperation({ state }, type) {
      let result;
      if (!_.isObject(type)) {
        result = await this.$axios.$get(api.dictionary, { type: type });
      } else {
        let { level, parent, name } = type;
        if (name) {
          result = await this.$axios.$get(api.findOne, {}, { name: name });
        } else {
          result = await this.$axios.$get(api.dictionary, { type: 'xzqh' }, { level: level, parent: parent });
        }
      }

      return result;
    },
    //登录与注册
    async corpReg({ state, commit }, { data, schid, schname }) {
      let result = await this.$axios.$post(api.corpReg, data, {}, { schid: schid, schname: schname });
      return result;
    },
    async corpLogin({ state }, { type, data }) {
      let result = this.$axios.$post(api.corpLogin, data, {}, { type: type });
      return result;
    },
    //验证码
    async verifyOperation({ state }, { type, data }) {
      let result;
      if (type === 'verify') {
        result = await this.$axios.$get(api.verify, {}, data);
      }
      if (type === 'checkVerify') {
        result = await this.$axios.$post(api.checkVerify, data);
      }
      if (type === 'message') {
        result = await this.$axios.$get(api.message, {}, data);
      }
      if (type === 'checkMessage') {
        result = await this.$axios.$post(api.checkMessage, data);
      }
      return result;
    },
    // 学生关注企业
    async stucorpOperation({ state }, { type, data }) {
      let result;
      let { skip, limit } = data;
      if (type === 'add') {
        let { query, body } = data;
        result = await this.$axios.$post(api.stucorp, body, {}, query);
      }
      if (type === 'list') {
        let { corpid } = data;
        result = await this.$axios.$get(api.stucorplist, {}, { corpid: corpid, skip: skip, limit: limit });
      }
      if (type === 'search') {
        let { id, studid, corpid } = data;
        result = await this.$axios.$get(api.stucorpsearch, {}, { studid: studid, corpid: corpid });
      }
      if (type === 'update') {
        let { info, id } = data;
        result = await this.$axios.$post(api.stucorpupdate, info, { id: id });
      }
      if (type === 'delete') {
        let { id } = data;
        result = await this.$axios.$delete(api.stucorpdelete, {}, { id: id });
      }
      return result;
    },
    //待办事项
    async noticeOperation({ state }, { type, data }) {
      let result;
      let { skip, limit } = data;
      if (type === 'list') {
        let { userid, type } = data;
        result = await this.$axios.$get(api.notice, {}, { userid: userid, type: type, skip: skip, limit: limit });
      }
      if (type === 'delete') {
        let { id } = data;
        result = await this.$axios.$delete(api.noticeDelete, {}, { id: id });
      }
      return result;
    },
  },
});

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