import request from '@/router/axios';
import axios from 'axios'

// 获取订单列表
export const getList =
  (current, size, params) => {
    return request({
      url: '/api/jobslink-api/payment/businessOrder/manage/order',
      method: 'get',
      params: {
        ...params,
        current,
        size,
      }
    })
  }
// // 获取订单详情 客服
//   export const getDetailM =
//   (payOrderId) => {
//       return request({
//         url: '/customer',
//         method: 'get',
//         params: { payOrderId }
//       })
//     //     .then((resp) => {
//     //       const data = resp.data.data;
//     //       const result = {
//     //         createUser: data.createUser,
//     //         createTime: data.createTime,
//     //         status: data.status,
//     //         payTime: data.payTime,
//     //         wageTotal: data.total,
//     //         fee: data.serviceTotal,
//     //         orderTotel: data.total + data.serviceTotal,
//     //         data: [],
//     //         type: data.type
//     //       };
//     //       if (data.type === 0) {//工资订单
//     //         data.detail.forEach(element => {//工资
//     //           result.data.push({
//     //             payrollNo: element.orderId,
//     //             titles: element.name,
//     //             type: 1,
//     //             businessNo: element.orderId,
//     //             total: element.total
//     //           });
//     //           if(element.serviceTotal!=0){
//     //             result.data.push({//服务费
//     //               payrollNo: element.orderId,
//     //               titles: element.name,
//     //               type: 2,
//     //               businessNo: element.orderId,
//     //               total: element.serviceTotal
//     //             });
//     //           }
//     //         });
//     //       } else if (data.type === 1) {//服务费订单
//     //         data.detail.forEach(element => {//保险
//     //           result.data.push({
//     //             payrollNo: element.orderId,
//     //             titles: element.name,
//     //             type: 3,
//     //             businessNo: element.orderId,
//     //             total: element.total
//     //           });
//     //         });
//     //       }

//     //       resolve(result);
//     //     })
//     //     .catch(reject)
//     // })
//   }

// 获取订单详情
export const getDetail =
  (payOrderId) => {
    return new Promise(function (resolve, reject) {
      request({
        url: '/api/jobslink-api/payment/businessOrder/find/detail',
        method: 'get',
        params: { payOrderId }
      })
        .then((resp) => {
          const data = resp.data.data;
          const result = {
            createUser: data.createUser,
            createTime: data.createTime,
            status: data.status,
            payTime: data.payTime,
            wageTotal: data.total,
            fee: data.serviceTotal,
            orderTotel: data.total + data.serviceTotal,
            data: [],
            type: data.type
          };
          if (data.type === 0) {//工资订单
            data.detail.forEach(element => {//工资
              result.data.push({
                payrollNo: element.orderId,
                titles: element.name,
                type: 1,
                businessNo: element.orderId,
                total: element.total
              });
              if(element.serviceTotal!=0){
                result.data.push({//服务费
                  payrollNo: element.orderId,
                  titles: element.name,
                  type: 2,
                  businessNo: element.orderId,
                  total: element.serviceTotal
                });
              }
            });
          } else if (data.type === 1) {//服务费订单
            data.detail.forEach(element => {//保险
              result.data.push({
                payrollNo: element.orderId,
                titles: element.name,
                type: 3,
                businessNo: element.orderId,
                total: element.total
              });
            });
          }

          resolve(result);
        })
        .catch(reject)
    })
  }

// 获取是否先开票
export const configInfo =
  (companyId) => {
    return request({
      url: '/api/jobslink-api/tenant/base/config/configInfo',
      method: 'get',
      params: { companyId }
    })
  }

// 获取订单详情
export const check =
  (payOrderId) => {
    return request({
      url: '/api/jobslink-api/payment/businessOrder/check',
      method: 'get',
      params: { payOrderId }
    })
  }

// 确认到款
export const sureSave = (payOrderId, remarks) => {
  return request({
    url: '/api/jobslink-api/payment/businessOrder/manage/confirm',
    method: 'post',
    params: { payOrderId },
    data: { remarks }
  })
}

/**商保订单详情 */
export const insureDetail = (current, size,orderNo) => {
  return request({
    url: '/api/jobslink-api/tenant/insure/item/tenant/list/order',
    method: 'get',
    params: {
      current,
      size,
      orderNo
    }
  })
}
/**商保订单详情  客服端*/
export const insureDetailM = (current, size,orderNo) => {
  return request({
    url: '/api/jobslink-api/tenant/insure/item/tenant/list/order/customer',
    method: 'get',
    params: {
      current,
      size,
      orderNo
    }
  })
}

//获取用户信息
export const getUserInfoById = (userId) => {
  return request({
    url: '/api/jobslink-api/system/tuser/info-by-userid',
    method: 'get',
    params: {
      userId,
    }
  })
}

export const getInfo = (userId, orderNo,current, size) => {
  return new Promise(function(reslove, reject) {
    axios.all([getUserInfoById(userId), insureDetail(current, size,orderNo)])
        .then(axios.spread(function(e, c) {
          reslove({data: {...e.data.data},records:{...c.data.data}})
        }))
        .catch(reject)
  })
}
