import BaseStore from '@src/stores/base';

export default class OrderStore extends BaseStore {
  allCheckout() {
    return this.apiGet('/order/checkout/all')
      .then(result => {
        this.setState({ number: result.checkouts.length });
        return result;
      });
  }

  addCheckout({ productType, productId, service, param, number }) {
    return this.apiPost('/order/checkout/add', { productType, productId, service, param, number })
      .then(result => {
        this.setState({ number: result });
        return result;
      });
  }

  changeCheckout(id, number) {
    return this.apiPut('/order/checkout/number', { id, number })
      .then(result => {
        this.setState({ number: result });
        return result;
      });
  }

  removeCheckout(id) {
    return this.apiDel('/order/checkout/delete', { id })
      .then(result => {
        this.setState({ number: result });
        return result;
      });
  }

  confirmPay(courseId) {
    return this.apiPost('/order/pay/confirm', { courseId });
  }

  speedPay({ productType, productId, service, param, number }) {
    return this.apiPost('/order/pay/speed', { productType, productId, service, param, number });
  }

  wechatQr(orderId) {
    return this.apiPost('/order/pay/wechat/qr', { orderId });
  }

  wechatJs(orderId) {
    return this.apiPost('/order/pay/wechat/js', { orderId });
  }

  alipayQr(orderId) {
    return this.apiPost('/order/pay/alipay/qr', { orderId });
  }

  query(orderId) {
    return this.apiGet('/order/pay/query', { orderId });
  }

  list({ page, size }) {
    return this.apiGet('/order/list', { page, size });
  }

  getOrder(id) {
    return this.apiGet('/order/detail', { id });
  }

  /**
   * 获取所有已购记录
   * @param {*} param0
   */
  listRecord({ page, size, productType, productId, service, isUsed, isExpire, filterChildren }) {
    return this.apiGet('/order/record/list', { page, size, productType, productId, service, isUsed, isExpire, filterChildren });
  }

  /**
   * 获取订单记录
   * @param {*} id
   */
  getRecord(id) {
    return this.apiGet('/order/record/detail', { id });
  }

  /**
   * 开通服务、课程等
   * @param {*} id
   */
  useRecord(recordId, isSubscribe) {
    return this.apiPost('/order/record/use', { recordId, isSubscribe });
  }

  openInvoice({ orderId, invoiceType, title, identity }) {
    return this.apiPost('/order/invoice/open', { orderId, invoiceType, title, identity });
  }
}

export const Order = new OrderStore({ key: 'order', local: true });