import {
  getSupplierDataAPI, // 获取供应商
  getFinanorgsBySaleorgsAPI, // 获取财务组织
  getsuppliersAPI, // 获取供应商列表信息
  getOrganizationsAPI, // 销售组织参照
  getSumCastBalanceAPI, // 货补余额、票扣余额
  getCustDocDefsAPI, // 兑付方式/单据状态参照
  getSumCastFlushdetailAPI, // 费用详情
  getCastBalanceAPI, // 费用
  getCustomersAPI, // 获取客户信息
  getfindByCustomerContactsAPI, // 客户信息--联系人
  getfindByCustomerAddressesAPI, // 客户信息--收货地址
  getfindByCustomeraccountsAPI, // 客户信息--银行账户信息
  getCastAccountAPI, // 费用对账
  getsuppliersJurisdictionAPI, // 获取供应商信息及产品线 -- 1级  销售组织
  getsuppliersPsrAPI, // 获取供应商信息及产品线 -- 非1级
  getPayStatisticAPI, //  应付对账 -- 应付对账统计页面
  getPayListAPI, //  应付对账 -- 应付对账详情列表
  getTranstypesAPI,
  getAccountChangeAPI, // 账户余额 -- 余额变动
  getAccountBalanceAPI // 账户余额 -- 当前预付款余额
} from "../services/api";

import { getCustomer } from "@/utils/request";
export default {
  namespace: "myAccount",
  state: {},
  effects: {
    *getSupplierData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getSupplierDataAPI, payload);
      yield put({
        type: "supplierData",
        supplierData: response
      });
    },
    *getFinanorgsBySaleorgs({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getFinanorgsBySaleorgsAPI, payload);
      yield put({
        type: "finanorgsBySaleorgs",
        finanorgsBySaleorgs: response
      });
    },
    //排除重复财务组织
    *getFinanorgsBySaleorgsDistinct({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getFinanorgsBySaleorgsAPI, payload);
      const temp = [];
      if(response&&response.length){
        for(let i=0;i<response.length;i++){
          let flag = false;
          for(let j=0;j<temp.length;j++){
            if(temp[j].finanOrgId == response[i].finanOrgId){
              flag = true;
              break;
            }
          }
          if(!flag){
            temp.push(response[i]);
          }
        }
      }
      yield put({
        type: "finanorgsBySaleorgsDistinct",
        finanorgsBySaleorgsDistinct: temp
      });
    },
    *getsuppliers({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getsuppliersAPI, payload);
      yield put({
        type: "suppliers",
        suppliers: response
      });
    },
    *getOrganizations({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getOrganizationsAPI, payload);
      yield put({
        type: "organizations",
        organizations: response
      });
    },
    *getSumCastBalance({ payload }, { call, put }) {
      payload = payload ? payload : {};
      const response = yield call(getSumCastBalanceAPI, payload);
      yield put({
        type: "sumCastBalance",
        sumCastBalance: response
      });
    },
    *getCustDocDefs({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getCustDocDefsAPI, payload);
      yield put({
        type: "custDocDefs",
        custDocDefs: response
      });
    },
    *getPaymentType({ payload }, { call, put }) {
      // 获取兑付方式
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getCustDocDefsAPI, payload);
      yield put({
        type: "paymentType",
        paymentType: response
      });
    },
    *getSumCastFlushdetail({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getSumCastFlushdetailAPI, payload);
      yield put({
        type: "sumCastFlushdetail",
        sumCastFlushdetail: response
      });
    },
    *getCastBalance({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getCastBalanceAPI, payload);
      if (response) {
        for (let i = 0, len = response.content; i < len.length; i++) {
          const load = {
            id: len[i].id,
            customer: getCustomer().id
          };
          len[i].items = yield call(getSumCastFlushdetailAPI, load);
        }
      }
      yield put({
        type: "castBalance",
        castBalance: response
      });
    },
    *getCustomers({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getCustomersAPI, payload);
      yield put({
        type: "customers",
        customers: response
      });
    },
    *getfindByCustomer({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getfindByCustomerContactsAPI, payload);
      yield put({
        type: "findBycustomer",
        findBycustomer: response
      });
    },
    *getfindByCustomerAddresses({ payload }, { call, put }) {
      payload = payload ? payload : {};
      const response = yield call(getfindByCustomerAddressesAPI, payload);
      yield put({
        type: "findByCustomerAddresses",
        findByCustomerAddresses: response
      });
    },
    *getfindByCustomeraccounts({ payload }, { call, put }) {
      payload = payload ? payload : {};
      const response = yield call(getfindByCustomeraccountsAPI, payload);
      yield put({
        type: "findByCustomeraccounts",
        findByCustomeraccounts: {content:response}
      });
    },
    *getCastAccount({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getCastAccountAPI, payload);
      if (response && response.results) {
        for (let i = 0, len = response.results.content; i < len.length; i++) {
          const load = {
            id: len[i].id,
            customer: getCustomer().id
          };
          len[i].items = yield call(getSumCastFlushdetailAPI, load);
        }
      }
      yield put({
        type: "castAccount",
        castAccount: response
      });
    },
    *getsuppliersJurisdiction({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customerIds = getCustomer().id;
      const response = yield call(getsuppliersJurisdictionAPI, payload);
      yield put({
        type: "suppliersJurisdiction",
        suppliersJurisdiction: response
      });
    },
    *getsuppliersPsr({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.purchaseCustomerId = getCustomer().id;
      const response = yield call(getsuppliersPsrAPI, payload);
      yield put({
        type: "suppliersPsr",
        suppliersPsr: response
      });
    },
    // 账户余额
    *getAccountBalance({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customerId = getCustomer().id;
      const response = yield call(getAccountBalanceAPI, payload);
      yield put({
        type: "accountBalance",
        accountBalance: response
      });
    },
    // 余额变动
    *getAccountChange({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.search_EQ_customer = getCustomer().id;
      const response = yield call(getAccountChangeAPI, payload);
      yield put({
        type: "accountChange",
        accountChange: response
      });
    },
    // 应付对账统计页面
    *getPayStatistic({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customerId = getCustomer().id;
      const response = yield call(getPayStatisticAPI, payload);
      yield put({
        type: "payStatistic",
        payStatistic: response
      });
    },
    // 应付对账详情列表
    *getPayList({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.search_EQ_customer = getCustomer().id;
      const response = yield call(getPayListAPI, payload);
      yield put({
        type: "payList",
        payList: response
      });
    }
  },
  reducers: {
    supplierData(state, payload) {
      return {
        ...state,
        supplierData: payload.supplierData
      };
    },
    finanorgsBySaleorgs(state, payload) {
      return {
        ...state,
        finanorgsBySaleorgs: payload.finanorgsBySaleorgs
      };
    },
    finanorgsBySaleorgsDistinct(state, payload) {
      return {
        ...state,
        finanorgsBySaleorgsDistinct: payload.finanorgsBySaleorgsDistinct
      };
    },
    suppliers(state, payload) {
      return {
        ...state,
        suppliers: payload.suppliers
      };
    },
    organizations(state, payload) {
      return {
        ...state,
        organizations: payload.organizations
      };
    },
    sumCastBalance(state, payload) {
      return {
        ...state,
        sumCastBalance: payload.sumCastBalance
      };
    },
    castBalance(state, payload) {
      return {
        ...state,
        castBalance: payload.castBalance
      };
    },
    custDocDefs(state, payload) {
      return {
        ...state,
        custDocDefs: payload.custDocDefs
      };
    },
    paymentType(state, payload) {
      return {
        ...state,
        paymentType: payload.paymentType
      };
    },
    sumCastFlushdetail(state, payload) {
      return {
        ...state,
        sumCastFlushdetail: payload.sumCastFlushdetail
      };
    },
    customers(state, payload) {
      return {
        ...state,
        customers: payload.customers
      };
    },
    findBycustomer(state, payload) {
      return {
        ...state,
        findBycustomer: payload.findBycustomer
      };
    },
    findByCustomerAddresses(state, payload) {
      return {
        ...state,
        findByCustomerAddresses: payload.findByCustomerAddresses
      };
    },
    findByCustomeraccounts(state, payload) {
      return {
        ...state,
        findByCustomeraccounts: payload.findByCustomeraccounts
      };
    },
    castAccount(state, payload) {
      return {
        ...state,
        castAccount: payload.castAccount
      };
    },
    suppliersJurisdiction(state, payload) {
      return {
        ...state,
        suppliersJurisdiction: payload.suppliersJurisdiction
      };
    },
    suppliersPsr(state, payload) {
      return {
        ...state,
        suppliersPsr: payload.suppliersPsr
      };
    },
    accountBalance(state, payload) {
      return {
        ...state,
        accountBalance: payload.accountBalance
      };
    },
    accountChange(state, payload) {
      return {
        ...state,
        accountChange: payload.accountChange
      };
    },
    payStatistic(state, payload) {
      return {
        ...state,
        payStatistic: payload.payStatistic
      };
    },
    payList(state, payload) {
      return {
        ...state,
        payList: payload.payList
      };
    }
  }
};
