import { sldCommonService, safeJsonParse } from "@/utils/utils";
import moment from "moment";

export default {
  namespace: "bigscreen",

  state: {
    overviewData: {
      loading: false,
      data: {},
    },
    portalTrafficData: {
      loading: false,
      data: {},
      options: [
        {
          label: "近7日",
          value: {
            startTime:
              moment()
                .subtract(7, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
        {
          label: "近30日",
          value: {
            startTime:
              moment()
                .subtract(30, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
      ],
      current: "近7日",
    },
    enquireData: {
      loading: false,
      data: {},
      options: [
        {
          label: "近7日",
          value: {
            startTime:
              moment()
                .subtract(7, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
        {
          label: "近30日",
          value: {
            startTime:
              moment()
                .subtract(30, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
      ],
      current: "近7日",
    },
    overseasEnquireLocationData: {
      loading: false,
      data: [],
    },
    distributorEnquireLocationData: {
      loading: false,
      data: [],
    },
    memberAddTrendData: {
      loading: false,
      data: [],
      options: [
        {
          label: "昨日",
          value: {
            startTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
        {
          label: "近7日",
          value: {
            startTime:
              moment()
                .subtract(7, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
        {
          label: "近30日",
          value: {
            startTime:
              moment()
                .subtract(30, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
      ],
      current: "昨日",
    },
    productAddTrendData: {
      loading: false,
      data: [],
    },
    searchRankData: {
      loading: false,
      data: [],
      options: [
        {
          label: "昨日",
          value: {
            startTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
        {
          label: "近7日",
          value: {
            startTime:
              moment()
                .subtract(7, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
        {
          label: "近30日",
          value: {
            startTime:
              moment()
                .subtract(30, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
      ],
      current: "昨日",
    },
    storeTrafficData: {
      loading: false,
      data: [],
      options: [
        {
          label: "昨日",
          value: {
            startTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
        {
          label: "近7日",
          value: {
            startTime:
              moment()
                .subtract(7, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
        {
          label: "近30日",
          value: {
            startTime:
              moment()
                .subtract(30, "days")
                .format("YYYY-MM-DD") + " 00:00:00",
            endTime:
              moment()
                .subtract(1, "days")
                .format("YYYY-MM-DD") + " 23:59:59:999",
          },
        },
      ],
      current: "昨日",
    },
    mapContainerData: {
      loading: false,
      data: [],
    },
    orderData: {
      loading: false,
      data: [],
      options: [
        {
          label: "近7日",
          value: "7",
        },
        {
          label: "近30日",
          value: "30",
        },
      ],
      current: "近7日",
    },
    updateTime: "",
  },

  effects: {
    // 平台数据汇总
    *load_overview({ payload }, { call, put }) {
      yield put({
        type: "setOverviewData",
        payload: { loading: true },
      });
      const response = yield call(
        sldCommonService,
        payload,
        "get",
        "v3/statistics/screen/analysis/dataSummary"
      );
      yield put({
        type: "setOverviewData",
        payload: { data: response.data, loading: false },
      });
    },

    // 门户流量
    *load_portal_traffic({ payload }, { call, put, select }) {
      const current = yield select(
        (state) => state.bigscreen.portalTrafficData.current
      );
      const options = yield select(
        (state) => state.bigscreen.portalTrafficData.options
      );
      const condition = options.filter((item) => item.label == current)[0]
        .value;
      payload = { ...payload, ...condition };
      yield put({
        type: "setPortalTrafficData",
        payload: { loading: true },
      });
      const response = yield call(
        sldCommonService,
        payload,
        "get",
        "v3/statistics/screen/analysis/flowTrend"
      );
      yield put({
        type: "setPortalTrafficData",
        payload: { data: response.data, loading: false },
      });
    },

    // 询盘概况
    *load_enquire_data({ payload }, { call, put, select }) {
      const current = yield select(
        (state) => state.bigscreen.enquireData.current
      );
      const options = yield select(
        (state) => state.bigscreen.enquireData.options
      );
      const condition = options.filter((item) => item.label == current)[0]
        .value;
      payload = { ...payload, ...condition };
      yield put({
        type: "setEnquireData",
        payload: { loading: true },
      });
      const response = yield call(
        sldCommonService,
        payload,
        "get",
        "v3/statistics/screen/analysis/enquiryTrend"
      );
      yield put({
        type: "setEnquireData",
        payload: { data: response.data, loading: false },
      });
    },

    // 海外询盘地区分布
    *load_overseas_enquire_location({ payload }, { call, put, select }) {
      yield put({
        type: "setOverseasEnquireLocation",
        payload: { loading: true },
      });
      const response = yield call(
        sldCommonService,
        payload,
        "get",
        "v3/statistics/screen/analysis/enquiryDistribution"
      );
      yield put({
        type: "setOverseasEnquireLocation",
        payload: { data: response.data, loading: false },
      });
    },

    // 分销商地区分布
    *load_distributor_enquire_location({ payload }, { call, put, select }) {
      yield put({
        type: "setDistributorEnquireLocation",
        payload: { loading: true },
      });
      const response = yield call(
        sldCommonService,
        payload,
        "get",
        "v3/statistics/screen/analysis/regionDistribution"
      );
      yield put({
        type: "setDistributorEnquireLocation",
        payload: { data: response.data, loading: false },
      });
    },

    // 新增会员趋势
    *load_add_member_trend({ payload }, { put, call, select}) {
      const current = yield select(
        (state) => state.bigscreen.memberAddTrendData.current
      );
      const options = yield select(
        (state) => state.bigscreen.memberAddTrendData.options
      );
      const condition = options.filter((item) => item.label == current)[0]
        .value;
      payload = { ...payload, ...condition };
      yield put({
        type: "setMemberAddTrendData",
        payload: { loading: true },
      });
      const response = yield call(
        sldCommonService,
        payload,
        "get",
        "v3/statistics/screen/analysis/memberTrend"
      );
      yield put({
        type: "setMemberAddTrendData",
        payload: { data: response.data, loading: false },
      });
    },

    // 新增商品趋势
    *load_add_product_trend({ payload }, { put, call }) {
      yield put({
        type: "setProductAddTrendData",
        payload: { loading: true },
      });
      const response = yield call(
        sldCommonService,
        payload,
        "get",
        "v3/statistics/screen/analysis/goodsTrend"
      );
      yield put({
        type: "setProductAddTrendData",
        payload: { data: response.data, loading: false },
      });
    },

    // 门户关键词搜索
    *load_search_rank({ payload }, { put, call, select }) {
      const current = yield select(
        (state) => state.bigscreen.searchRankData.current
      );
      const options = yield select(
        (state) => state.bigscreen.searchRankData.options
      );
      const condition = options.filter((item) => item.label == current)[0]
        .value;
      payload = { ...payload, ...condition };
      yield put({
        type: "setSearchRankData",
        payload: { loading: true },
      });
      const response = yield call(
        sldCommonService,
        payload,
        "get",
        "v3/statistics/screen/analysis/searchRank"
      );
      yield put({
        type: "setSearchRankData",
        payload: { data: response.data, loading: false },
      });
    },

    // 店铺流量排行
    *load_store_traffic({ payload }, { put, call, select }) {
      const current = yield select(
        (state) => state.bigscreen.storeTrafficData.current
      );
      const options = yield select(
        (state) => state.bigscreen.storeTrafficData.options
      );
      const condition = options.filter((item) => item.label == current)[0]
        .value;
      payload = { ...payload, ...condition };
      yield put({
        type: "setStoreTrafficData",
        payload: { loading: true },
      });
      const response = yield call(
        sldCommonService,
        payload,
        "get",
        "v3/statistics/screen/analysis/storeFlowRank"
      );
      yield put({
        type: "setStoreTrafficData",
        payload: { data: response.data, loading: false },
      });
    },

    // 世界地图站点数据
    *load_world_map({ payload }, { put, call, select }) {
      yield put({
        type: "setWorldMapData",
        payload: { loading: true },
      });
      const response = yield call(
        sldCommonService,
        payload,
        "get",
        "v3/statistics/screen/analysis/memberDistribution"
      );
      yield put({
        type: "setWorldMapData",
        payload: { data: response.data, loading: false },
      });
    },

    // 订单概况
    *load_order_basic({ payload }, { put, call, select }) {
      const current = yield select(
        (state) => state.bigscreen.orderData.current
      );
      const options = yield select(
        (state) => state.bigscreen.orderData.options
      );
      const condition = options.filter((item) => item.label == current)[0].value;
      payload = { ...payload, day: condition };
      yield put({
        type: "setOrderData",
        payload: { loading: true },
      });
      const response = yield call(
        sldCommonService,
        payload,
        "get",
        "v3/business/admin/orderInfo/orderList"
      );
      yield put({
        type: "setOrderData",
        payload: { data: safeJsonParse(response.msg, []), loading: false },
      });
    },

    // 初始化数据
    *load_data({ payload }, { put, call, all }) {
      // 更新时间
      const now = new Date();
      const pad = (n) => n.toString().padStart(2, "0");
      const currentTime = `${now.getFullYear()}-${pad(
        now.getMonth() + 1
      )}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(
        now.getMinutes()
      )}:${pad(now.getSeconds())}`;
      yield put({
        type: "setUpdateTime",
        payload: currentTime,
      });
      yield all([
        put({ type: "load_overview" }),
        put({ type: "load_portal_traffic" }),
        put({ type: "load_enquire_data" }),
        put({ type: "load_overseas_enquire_location" }),
        put({ type: "load_distributor_enquire_location" }),
        put({ type: "load_add_member_trend" }),
        // put({ type: "load_add_product_trend" }),
        put({ type: "load_search_rank" }),
        put({ type: "load_store_traffic" }),
        put({ type: "load_world_map" }),
        put({ type: 'load_order_basic' })
      ]);
    },
  },

  reducers: {
    setOrderData(state, { payload }) {
      return {
        ...state,
        orderData: {
          ...state.orderData,
          ...payload,
        },
      };
    },
    setWorldMapData(state, { payload }) {
      return {
        ...state,
        mapContainerData: {
          ...state.mapContainerData,
          ...payload,
        },
      };
    },
    setStoreTrafficData(state, { payload }) {
      return {
        ...state,
        storeTrafficData: {
          ...state.storeTrafficData,
          ...payload,
        },
      };
    },
    setUpdateTime(state, { payload }) {
      return {
        ...state,
        updateTime: payload,
      };
    },
    setSearchRankData(state, { payload }) {
      return {
        ...state,
        searchRankData: {
          ...state.searchRankData,
          ...payload,
        },
      };
    },
    setOverviewData(state, { payload }) {
      return {
        ...state,
        overviewData: {
          ...state.overviewData,
          ...payload,
        },
      };
    },
    setPortalTrafficData(state, { payload }) {
      return {
        ...state,
        portalTrafficData: {
          ...state.portalTrafficData,
          ...payload,
        },
      };
    },
    setEnquireData(state, { payload }) {
      return {
        ...state,
        enquireData: {
          ...state.enquireData,
          ...payload,
        },
      };
    },
    setOverseasEnquireLocation(state, { payload }) {
      return {
        ...state,
        overseasEnquireLocationData: {
          ...state.overseasEnquireLocationData,
          ...payload,
        },
      };
    },
    setDistributorEnquireLocation(state, { payload }) {
      return {
        ...state,
        distributorEnquireLocationData: {
          ...state.distributorEnquireLocationData,
          ...payload,
        },
      };
    },
    setMemberAddTrendData(state, { payload }) {
      return {
        ...state,
        memberAddTrendData: {
          ...state.memberAddTrendData,
          ...payload,
        },
      };
    },
    setProductAddTrendData(state, { payload }) {
      return {
        ...state,
        productAddTrendData: {
          ...state.productAddTrendData,
          ...payload,
        },
      };
    },
  },

  subscriptions: {
    setup({ history }) {
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      return history.listen(({ pathname, search }) => {
        if (typeof window.ga !== "undefined") {
          window.ga("send", "pageview", pathname + search);
        }
      });
    },
  },
};
