import {
  getPayAccountDataAPI,
  getSupplierDataAPI,
  getFinanorgsBySaleorgsAPI,
  getEnterpriseAccountsAPI,
  getPayBillEnterpriseAccountsAPI,
  getBillreceiptTypeDataAPI,
  getSettlementDataAPI,
  getOrderListDataAPI,
  savePaymentNoticeAPI,
  getPaymentNoticeListDataAPI,
  getPaymentNoticeByIdAPI,
  operationPaymentAPI,
  getOnlinePayListDataAPI,
  getOnlinePaychanpayAPI,
  getOnlinePayalipayAPI,
  getOnlinePayByIdAPI,
  saveOnlinePayAPI,
  saveTempToPayAPI,
  getPayDataAPI
} from "../services/api";
import { getCustomer } from "@/utils/request.js";
export default {
  namespace: "paymentNotice",

  state: {
    paymentNoticeData: {},
    onlinePayData: {},
    returnPaymentNoticeData: {},
    payAccountData: [],
    supplierData: [],
    finanorgsBySaleorgData: [],
    finanorgsBySaleorgDataDistinct: [],
    receiptAccountData: [],
    payBillReceiptAccountData: [],
    billreceiptTypeData: [],
    settlementData: [],
    productLineData: [],
    billclaimDetailDto: [],
    payBillItemSet: [],
    orderPayBillSet: {},
    paymentNoticeListData: [],
    onlinePayListData: [],
    payData: [],
    onlinePayChanpayData: [],
    onlinePayAlipayData: [],
    returnOnlinePayData: [],
    returnTempToPayData: []
  },

  effects: {
    *getPaymentNoticeStatusData({ payload }, { call, put }) {
      const response = {
        data: [
          {
            name: "暂存",
            code: 7
          },
          {
            name: "提交",
            code: 2
          },
          {
            name: "审批通过",
            code: 3
          },
          {
            name: "审批中",
            code: 4
          },
          {
            name: "收回",
            code: 5
          },
          {
            name: "审批不通过",
            code: 6
          }
        ]
      };
      yield put({
        type: "paymentStatusData",
        paymentStatusData: response.data
      });
    },
    // 获取付款通知单（认领单）
    *getPaymentNoticeListData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload[`search_IN_customer`] = getCustomer().id;
      payload[`search_EQ_claimFrom`] = "PORTAL";
      const response = yield call(getPaymentNoticeListDataAPI, payload);
      yield put({
        type: "paymentNoticeListData",
        paymentNoticeListData: response
      });
    },
    // 获取付款方（客户档案账户信息）
    *getPayAccountData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getPayAccountDataAPI, payload);
      if (response && response.length > 0) {
        response.forEach(item => {
          item.name = item.accountName;
        });
      }
      yield put({
        type: "payAccountData",
        payAccountData: response
      });
    },
    // 获取供应商
    *getSupplierData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload.customer = getCustomer().id;
      const response = yield call(getSupplierDataAPI, payload);
      yield put({
        type: "supplierData",
        supplierData: response
      });
    },
    // 获取收款方（财务组织）
    *getFinanorgsBySaleorgData({ payload }, { call, put }) {
      const response = yield call(getFinanorgsBySaleorgsAPI, payload);
      if (response && response.length > 0) {
        response.forEach(item => {
          item.id = item.finanOrgId;
          item.name = item.finanOrgName;
        });
      }
      yield put({
        type: "finanorgsBySaleorgData",
        finanorgsBySaleorgData: response
      });
    },
    //获取收款方（财务组织）
    //排除重复财务组织
    *getFinanorgsBySaleorgDataDistinct({ 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) {
            response[i].id=response[i].finanOrgId
            response[i].name=response[i].finanOrgName
            temp.push(response[i]);
          }
        }
      }
      yield put({
        type: "finanorgsBySaleorgDataDistinct",
        finanorgsBySaleorgDataDistinct: temp
      });
    },
    // 获取收款方账号
    *getEnterpriseAccountsData({ payload }, { call, put }) {
      const response = yield call(getEnterpriseAccountsAPI, payload);
      const content = response.content || [];
      if (content && content.length > 0) {
        content.forEach(item => {
          item.name = `${item.accountName} ${item.accountNumber}`;
        });
      }
      yield put({
        type: "receiptAccountData",
        receiptAccountData: content
      });
    },
    // 支付单新增获取收款方账号
    *getEnterprisePayBillAccountsData({ payload }, { call, put }) {
      payload[`search_EQ_customerId`] = getCustomer().id;
      const response = yield call(getPayBillEnterpriseAccountsAPI, payload);
      const content = response || [];
      if (content && content.length > 0) {
        content.forEach(item => {
          item.name = `${item.name}`;
        });
      }
      yield put({
        type: "payBillReceiptAccountData",
        payBillReceiptAccountData: content
      });
    },
    // 获取收款业务类型
    *getBillreceiptTypeData({ payload }, { call, put }) {
      const response = yield call(getBillreceiptTypeDataAPI, payload);
      const content = [].concat((response || {}).content);
      yield put({
        type: "billreceiptTypeData",
        billreceiptTypeData: content
      });
    },
    // 获取结算方式
    *getSettlementData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      const response = yield call(getSettlementDataAPI, payload);
      yield put({
        type: "settlementData",
        settlementData: response
      });
    },
    // 请求订单列表
    *getOrderListData({ payload }, { call, put }) {
      payload[`search_EQ_customerId`] = getCustomer().id;
      const response = yield call(getOrderListDataAPI, payload);
      yield put({
        type: "orderListData",
        orderListData: response
      });
    },
    // 付款单登记保存
    *savePaymentNotice({ payload }, { call, put }) {
      payload.customerId = getCustomer().id;
      const response = yield call(savePaymentNoticeAPI, payload);
      yield put({
        type: "returnPaymentNoticeData",
        returnPaymentNoticeData: response
      });
    },
    // 操作付款单 提交 or 删除
    *operationPayment({ payload }, { call, put }) {
      const response = yield call(operationPaymentAPI, payload);
      yield put({
        type: "operationResult",
        operationResult: response
      });
    },
    // 根据主键查询付款单
    *getPaymentNoticeById({ payload }, { call, put }) {
      const response = yield call(getPaymentNoticeByIdAPI, payload);
      yield put({
        type: "paymentNoticeData",
        paymentNoticeData: response
      });
    },
    // 获取在线支付单列表
    *getOnlinePayListData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      payload[`search_IN_customer`] = getCustomer().id;
      const response = yield call(getOnlinePayListDataAPI, payload);
      yield put({
        type: "onlinePayListData",
        onlinePayListData: response
      });
    },
    // 请求支付方式--支付宝
    *getOnlinePayalipayAPI({ payload }, { call, put }) {
      const response = yield call(getOnlinePayalipayAPI, payload);
      yield put({
        type: "onlinePayAlipayData",
        onlinePayAlipayData: response
      });
    },
    //请求支付方式--畅捷通
    *getOnlinePaychanpayAPI({ payload }, { call, put }) {
      const response = yield call(getOnlinePaychanpayAPI, payload);
      yield put({
        type: "onlinePayChanpayData",
        onlinePayChanpayData: response
      });
    },
    // 根据主键查询付款单
    *getOnlinePayById({ payload }, { call, put }) {
      const response = yield call(getOnlinePayByIdAPI, payload);
      yield put({
        type: "onlinePayData",
        onlinePayData: response
      });
    },
    // 支付单保存
    *saveOnlinePay({ payload }, { call, put }) {
      payload.customerId = getCustomer().id;
      const response = yield call(saveOnlinePayAPI, payload);
      yield put({
        type: "returnOnlinePayData",
        returnOnlinePayData: response
      });
    },
    // 支付时暂存单转支付单
    *tempBillToPayBill({ payload }, { call, put }) {
      payload.customerId = getCustomer().id;
      const response = yield call(saveTempToPayAPI, payload);
      yield put({
        type: "returnTempToPayData",
        returnTempToPayData: response
      });
    },
    // 获取支付方式
    *getPayData({ payload }, { call, put }) {
      payload = payload ? payload : {};
      const response = yield call(getPayDataAPI, payload);
      yield put({
        type: "payData",
        payData: response
      });
    },
  },

  reducers: {
    paymentStatusData(state, payload) {
      return {
        ...state,
        paymentStatusData: payload.paymentStatusData
      };
    },
    payAccountData(state, payload) {
      return {
        ...state,
        payAccountData: payload.payAccountData
      };
    },
    supplierData(state, payload) {
      return {
        ...state,
        supplierData: payload.supplierData
      };
    },
    finanorgsBySaleorgData(state, payload) {
      return {
        ...state,
        finanorgsBySaleorgData: payload.finanorgsBySaleorgData
      };
    },
    finanorgsBySaleorgDataDistinct(state, payload) {
      return {
        ...state,
        finanorgsBySaleorgDataDistinct: payload.finanorgsBySaleorgDataDistinct
      };
    },
    receiptAccountData(state, payload) {
      return {
        ...state,
        receiptAccountData: payload.receiptAccountData
      };
    },
    payBillReceiptAccountData(state, payload) {
      return {
        ...state,
        payBillReceiptAccountData: payload.payBillReceiptAccountData
      };
    },
    billreceiptTypeData(state, payload) {
      return {
        ...state,
        billreceiptTypeData: payload.billreceiptTypeData
      };
    },
    settlementData(state, payload) {
      return {
        ...state,
        settlementData: payload.settlementData
      };
    },
    orderListData(state, payload) {
      return {
        ...state,
        orderListData: payload.orderListData
      };
    },
    paymentNoticeData(state, payload) {
      return {
        ...state,
        paymentNoticeData: payload.paymentNoticeData
      };
    },
    onlinePayData(state, payload) {
      return {
        ...state,
        onlinePayData: payload.onlinePayData
      };
    },
    returnPaymentNoticeData(state, payload) {
      return {
        ...state,
        returnPaymentNoticeData: payload.returnPaymentNoticeData
      };
    },
    billclaimDetailDto(state, payload) {
      return {
        ...state,
        billclaimDetailDto: payload.billclaimDetailDto
      };
    },
    payBillItemSet(state, payload) {
      return {
        ...state,
        payBillItemSet: payload.payBillItemSet
      };
    },
    orderPayBillSet(state, payload) {
      return {
        ...state,
        orderPayBillSet: payload.orderPayBillSet
      };
    },
    paymentNoticeListData(state, payload) {
      return {
        ...state,
        paymentNoticeListData: payload.paymentNoticeListData
      };
    },
    onlinePayListData(state, payload) {
      return {
        ...state,
        onlinePayListData: payload.onlinePayListData
      };
    },
    returnOnlinePayData(state, payload) {
      return {
        ...state,
        returnOnlinePayData: payload.returnOnlinePayData
      };
    },
    returnTempToPayData(state, payload) {
      return {
        ...state,
        returnTempToPayData: payload.returnTempToPayData
      };
    },
    payData(state, payload) {
      return {
        ...state,
        payData: payload.payData
      };
    },
    operationResult(state, payload) {
      return {
        ...state,
        operationResult: payload.operationResult
      };
    },
  }
};
