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

let URL = api.serverURI;
export default {
  namespaced: true,
  state: {
    files: [],
    selectedTypeName: null,
    selectedTime: "",
    repairEvaluateForm: {
      actualPayTime: null,
      coperatorId: 0,
      coperatorName: "",
      evaluate: "",
      evaluateLevel: 0,
      freeFlag: 0,
      guaranteeFlag: 0,
      hisGoTime: null,
      id: 0,
      operType: 0,
      operationTime: null,
      operatorId: 0,
      operatorName: "",
      operatorPhone: "",
      payOrderId: 0,
      payOrderNo: "",
      payWay: "",
      props: [],
      remark: "",
      repairActualFee: 0,
      repairFee: 0,
      repairFlag: 0,
      repairId: 0,
      thirdCompany: 0,
      thirdCompanyFlag: 0
    },
    //状态（以下状态为 pc端状态/app端状态） 1待受理 2.已撤销3.待上门维修/已受理 4.待处理（上门维修未维修好） 5.待支付(15天内维保期公寓支付给第三方) 6.待收款/待支付（客户支付） 7.待评价 8.已完成 ,
    //operType (integer, optional): 操作类型 1.申请维保 2.受理维保 3.撤销维保 4.上门维修 5.收款（客户到店支付，及pc端支付） 6.付款（类型是第三方公司） 7.支付（客户在客户端支付） 8.评价 9.重新申请维保 ,
    cycleList: [], //循环周期数组
    cycleRepairList: [],
    maintenanceList: [],
    maintenanceDetail: {
      operationList: []
    },
    repairForm: {
      acceptTime: "",
      accepter: 0,
      applicantId: 0,
      applicantName: "",
      applicantPhone: "",
      createTime: "",
      createrId: 0,
      createrName: "",
      goRepairTime: "",
      handleTimes: 0,
      houseCode: "",
      houseId: 0,
      id: 0,
      keyWords: "",
      lastModifier: 0,
      lastModifyTime: "",
      maxTime: "",
      minTime: "",
      operationList: [],
      org: 0,
      overtime: 0,
      props: [],
      regionId: 0,
      remark: "",
      repairCode: "",
      status: 0,
      typeName: "",
      typeId: 0 //维保类型
    },
    mainteTypeTree: []
  },
  actions: {
    initRepairForm({ commit, state, rootState }, params) {
      //
      state.repairForm.applicantName =
				rootState.loginModule.loginInfo.zhName;
      //state.repairForm.applicantPhone = rootState.loginModule.loginInfo.phone
    },
    cPayment({ commit, state }, params) {
      return api
        .request("get", api.getURL("RepairManage/cPayment"), {
          id: params.id,
          repairFee: params.repairFee
        })
        .then(res => {
          return res;
        });
    },
    cAgainMake({ commit, state }, params) {
      return api
        .request("get", api.getURL("RepairManage/cAgainMake"), params)
        .then(res => {
          return res;
        });
    },
    getMainteTypeTreeInfo({ commit, state }, params) {
      return api
        .request("get", api.getURL("RepairType/linkageQuery"), {
          AppStatus: 1,
          repair: 1
        })
        .then(res => {
          //
          let tree = treeMainte(res.data.data);
          //
          commit("SET_MENU_TREE_LIST", tree);
          return state.mainteTypeTree;
        });
    },
    cEvaluate({ commit, state }, params) {
      return api
        .request(
          "post",
          api.getURL("RepairManage/cEvaluate"),
          state.repairEvaluateForm
        )
        .then(res => {
          return res;
        });
    },
    cRevokeRepair({ commit, state }, id) {
      return api
        .request("get", api.getURL("RepairManage/cRevokeRepair"), {
          id: id
        })
        .then(res => {
          return res;
        });
    },
    getDetails({ commit, state }, id) {
      return api
        .request("get", api.getURL("RepairManage/getDetails"), {
          id
        })
        .then(res => {
          return res;
        });
    },
    cGetDetails({ commit, state }, id) {
      return api
        .request("get", api.getURL("RepairManage/cGetDetails"), {
          id
        })
        .then(res => {
          if (res) {
            commit("INIT_REPAIR_STATUS_LIST");
            commit("SET_REPAIR_CYCLE", res.data.data.operationList);
            state.cycleList.map(list => {
              commit("SET_REPAIR_STATUS", list);
            });
            commit("SET_MAINTENANCE_DETAIL", res.data.data);
          }
          //
          return res;
        });
    },
    findMaintenanceList({ commit, state }, params) {
      return api
        .request("get", api.getURL("RepairManage/cFindpage"), {
          pn: 1,
          pageSize: 1000000,
          keyWords: ""
        })
        .then(res => {
          commit("SET_REPAIR_LIST", res.data.data.list);
          return res;
        });
    },
    cLaunchRepair({ commit, state }, typeId) {
      state.repairForm.typeId = typeId;
      
      if (!state.repairForm.id) {
        return api
          .request(
            "post",
            api.getURL("RepairManage/cLaunchRepair"),
            state.repairForm
          )
          .then(res => {
            return res;
          });
      } 
      return api
        .request("get", api.getURL("RepairManage/cAgainMake"), {
          id: state.repairForm.id,
          goRepairTime: state.repairForm.goRepairTime
        })
        .then(res => {
          return res;
        });
			
    },
    fetchData({ commit, state }, ids) {
      return api.request("get", api.getURL("houseLayout/delete"), {
        ids: ids.join(",")
      });
    }
  },
  mutations: {
    AGAIN_REPAIR_FORM(state, data) {
      if (data.goRepairTime) {
        data.goRepairTime = new Date(data.goRepairTime).Format(
          "yyyy-MM-dd"
        );
      }
      state.repairForm = {
        applicantName: data.applicantName,
        applicantPhone: data.applicantPhone,
        remark: data.remark,
        goRepairTime: data.goRepairTime,
        props: data.props,
        typeName: data.typeName,
        typeId: data.typeId,
        id: data.id
      };
      state.selectedTypeName = data.typeName;
      state.selectedTime = data.goRepairTime;
      state.files = data.props.map(item => {
        return {
          status: "success",
          url: `${URL}${item.propValue}`
        };
      });
    },
    INIT_REPAIR_FORM(state) {
      state.selectedTime = null;
      state.selectedTypeName = null;
      state.repairForm = {
        applicantName: "",
        applicantPhone: "",
        remark: "",
        goRepairTime: "",
        props: [],
        typeId: "",
        id: 0
      };
    },
    CHANGE_SELECTED_TIME(state, data) {
      Vue.set(state, "selectedTime", data);
    },
    CHANGE_TYPE_NAME(state, data) {
      Vue.set(state, "selectedTypeName", data);
    },
    INIT_DATA_STATUS(state, data) {},
    SET_MENU_TREE_LIST(state, data) {
      Vue.set(state, "mainteTypeTree", data);
    },
    EVALUATE_ADD_IMG_INFO(state, data) {
      state.repairEvaluateForm.props.push(data);
    },
    EVALUATE_REMOVE_IMG_INFO(state, url) {
      let targetIndex = 0;
      state.repairEvaluateForm.props.map((item, index) => {
        if (item.propValue === url) {
          targetIndex = index;
        }
      });
      state.repairEvaluateForm.props.splice(targetIndex, 1);
    },
    ADD_IMG_INFO(state, data) {
      state.repairForm.props.push(data);
    },
    REMOVE_IMG_INFO(state, url) {
      let targetIndex = 0;
      state.repairForm.props.map((item, index) => {
        if (item.propValue === url) {
          targetIndex = index;
        }
      });
      state.repairForm.props.splice(targetIndex, 1);
    },
    SET_GO_REPAIR_TIME(state, data) {
      state.repairForm.goRepairTime = data;
    },
    SET_REPAIR_LIST(state, data) {
      Vue.set(state, "maintenanceList", data);
    },
    SET_REPAIR_CYCLE(state, list) {
      state.cycleList = [];
      let divideListIndex = []; //存储需要区分的index索引值
      let currIndex = 0;
      list.map((item, index) => {
        if (item.operType === 9) {
          divideListIndex.push(index);
        }
      });
      if (divideListIndex.length > 0) {
        divideListIndex.map(item => {
          state.cycleList.push(list.slice(currIndex, item));
          currIndex = item;
        });
        state.cycleList.push(list.slice(currIndex, list.length));
      } else {
        state.cycleList = [list]; //只有一个周期
      }
      //
    },
    SET_REPAIR_STATUS(state, list) {
      let repairStatus = {
        1: {
          name: "申请维保",
          list: []
        },
        2: {
          name: "维保受理",
          list: []
        },
        3: {
          name: "撤销维保",
          list: []
        },
        4: {
          name: "上门维修",
          list: []
        },
        5: {
          name: "支付",
          list: []
        },
        6: {
          name: "付款",
          list: []
        },
        7: {
          name: "客户端支付",
          list: []
        },
        8: {
          name: "评价",
          list: []
        },
        9: {
          name: "评价",
          list: []
        }
      };
      list.map(item => {
        repairStatus[item.operType].list.push(item);
      });
      state.cycleRepairList.push(repairStatus);
      //
    },
    INIT_REPAIR_STATUS_LIST(state) {
      state.cycleRepairList = [];
      state.cycleRepairList.map(item => {
        for (let key in item.repairStatus) {
          //
          item.repairStatus[key].list = [];
        }
      });
    },
    SET_MAINTENANCE_DETAIL(state, data) {
      Vue.set(state, "maintenanceDetail", data);
    },
    SET_EVALUATE_REPAIR_ID(state, id) {
      state.repairEvaluateForm.repairId = id;
    },
    CHANGE_REPAIR_STATUS(state, status) {
      state.maintenanceDetail.status = status;
    }
  },
  getters: {}
};
