import Vue from "vue";
import api from "@/api/index.js";
import store from "@/utils/store.js";

export default {
  namespaced: true,
  state: {
    communityAllActivityInfo: [],
    communityActivityDetailInfo: store("communityActivityDetailInfo"),
    applyActivityDetailInfo: {},
    communityAllActivityList: [],
    activityList: [],
    activityForm: {
      actiivtyAddres: "",
      activityCost: 0,
      activityId: 0,
      activityName: "",
      activityParticipants: 1,
      activityStartTime: "",
      applicantHouseId: 0,
      applicantHouseName: "",
      applicantId: 0,
      applicantName: "",
      applicantPhone: "",
      applicantRegion: "",
      applicantRegionId: null,
      applicationFee: 0,
      applyPropRequests: [],
      applyWay: 0,
      createTime: "",
      createrId: 0,
      createrName: "",
      id: 0,
      operatorId: 0,
      operatorName: "",
      payNo: "",
      payRemark: "",
      payTime: "",
      payWayName: "微信",
      payWayValue: "wePay",
      platform: 1,
      status: 0   //状态 -1 删除 1报名成功待支付 2报名成功已支付  3 订单关闭
    },
    newPayForm:{
      "billId": 0,
      "businessId": 0,
      "couponId": 0,
      "orderAmount": "",
      "orderTitle": "",
      "orderType": "",
      "remark": ""
    }
  },
  actions: {
    activityApplyList( { commit, state }, params) {
      return api.request("get", api.getURL("activity/findUserActicity"), {
        pn:1,
        pageSize: 10000
      }).then(res => {
        commit("SET_APPLY_ACTIVITY_LIST", res.data.data.list);
        return res;
      });
    },
    cancelApply( { commit, state }, id) {
      return api.request("get", api.getURL("activity/cancelApply"), {
        id,
      }).then(res => {
        return res;
      });
    },
    deleteApply( { commit, state }, id) {
      return api.request("get", api.getURL("activity/deleteApply"), {
        id,
      }).then(res => {
        if(res) {
          res.Toast.success("活动删除成功");
        }
        return res;
      });
    },
    addApplyOther( { commit, state, rootState }) {
      state.activityForm = Object.assign({}, state.activityForm, state.applyActivityDetailInfo);
      delete state.activityForm.id;
      return api.request("post", api.getURL("activity/addApply"), state.activityForm).then(res => {
        return res;
      });
    },
    payAppyCostOnCAPP( { commit, state }, params) {
      return api.request("post", api.getURL("activity/payAppyCostOnCAPP"), params).then(res => {
        return res;
      });
    },
    addApply({ commit, state, rootState }) {
      state.activityForm = Object.assign({}, state.activityForm, state.communityActivityDetailInfo);
      state.activityForm.activityId = state.communityActivityDetailInfo.id;
      state.activityForm.activityName = state.communityActivityDetailInfo.title;
      state.activityForm.applicationFee = state.communityActivityDetailInfo.cost;
      state.activityForm.activityCost = state.communityActivityDetailInfo.cost * state.activityForm.activityParticipants;

      state.activityForm.applicantRegion = rootState.loginModule.loginInfo.cappUserInfo.regionName;
      state.activityForm.applicantHouseId = rootState.loginModule.loginInfo.cappUserInfo.houseId;
      state.activityForm.applicantHouseName = rootState.loginModule.loginInfo.cappUserInfo.houseCode;
      //
      state.activityForm.platform = 1;
      return api.request("post", api.getURL("activity/addApply"), state.activityForm).then(res => {
        return res;
      });
    },
    findApplyDetail( { commit, state }, id) {
      return api.request("get", api.getURL("activity/findApplyDetail"), {
        id,
      }).then(res => {
        commit("SET_APPLY_ACTIVITY_INFO", res.data.data);
        return res;
      });
    },
    getCommunityActivityInfo({ commit, state }, payload) {
      // 社区活动列表
      return api
        .request("get", api.getURL("activity/findAllONCApp"), payload)
        .then(res => {
          //
          if (res) {
            commit("SET_ACTIVITY_INFO", res.data.data.list);
          }
          return res;
        });
    },
    getCommunityActivityDetailInfo({ commit, state }, payload) {
      // 社区活动详情
      return api
        .request("get", api.getURL("activity/findActivityDetail"), payload)
        .then(res => {
          
          if (res) {
            commit("SET_ACTIVITY_DETAIL_INFO", res.data.data);
          }
          return res;
        });
    }
  },
  mutations: {
    SET_ACTIVITY_INFO(state, data) {
      Vue.set(state, "communityAllActivityInfo", data);
    },
    SET_ACTIVITY_DETAIL_INFO(state, data) {
      store("communityActivityDetailInfo", data);
      Vue.set(state, "communityActivityDetailInfo", data);
    },
    SET_APPLY_ACTIVITY_LIST(state, data) {
      
      Vue.set(state, "activityList", data);
    },
    SET_ACTIVITY_LIST_ADD_INFO(state, data) {
      Vue.set(state, "communityAllActivityList", data);
      // data.map(item => {
      //   state.communityAllActivityList.push(item);
      // });
      //state.homeProjectListInfo = state.homeProjectListInfo.concat(data)
      //
      //state.homeProjectListInfo = [...state.homeProjectListInfo, ...data]
    },
    SET_APPLY_ACTIVITY_INFO(state, data) {
      Vue.set(state, "applyActivityDetailInfo", data);
    },
    CLEAN_NAME(state,data) {
      state.activityForm.applicantName = "";
    },
    CLEAN_PHONE(state,data) {
      state.activityForm.applicantPhone = "";
    },
    CLEAN_APPLY_NUM(state,data) {
      state.activityForm.activityParticipants = 1;
    },
    INTE_NEW_PAY_FORM(state,data){
      state.newPayForm.billId = null,
      state.newPayForm.businessId = null,
      state.newPayForm.couponId = null,
      state.newPayForm.orderAmount = "",
      state.newPayForm.orderTitle = "",
      state.newPayForm.orderType = "",
      state.newPayForm.remark = "";
    }       
  },
  getters: {}
};
