import { scenicOrderCancel, scenicOrderGetPayment } from '../../services'; // 取消订单

export const cancellationOfOrder = parameter => {
  return new Promise((resolve, reject) => {
    my.confirm({
      title: '温馨提示',
      content: '您是否确定取消订单？',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: async ({
        confirm
      }) => {
        if (confirm) {
          const [err, result] = await scenicOrderCancel(parameter);
          err ? reject() : resolve(result);
        } else {
          reject();
        }
      }
    });
  });
}; // 传入 orderId 调用收银台

export const payImmediately = ({
  orderId
}) => {
  return new Promise(async (resolve, reject) => {
    const [err, result] = await scenicOrderGetPayment({
      orderId
    });

    if (err) {
      my.showToast({
        type: 'fail',
        content: '支付失败',
        duration: 3000
      });
      reject();
      return;
    }

    my.tradePay({
      tradeNO: result.tradeNo,
      success: ({
        resultCode
      }) => {
        resultCode === '9000' ? resolve() : reject();
      }
    });
  });
};
export default {
  cancellationOfOrder,
  payImmediately
};