import request from '@/utils/request';
import { formateField, tableList, wrapTreeNode } from '@/utils/format';
import type { SearchAuditModel, EndaccountChangeModel, RecordModel } from './data';
import session from '@/utils/session';
// 获取服务区列表
export async function getServerpartOption(ProvinceCode?: number | string,
  ServerpartCodes?: string, ShowWholePower?: boolean): Promise<{
    label: string; value: string | number
  }[]> {

  const serverpartOptions = session.get(`${ProvinceCode || ''}serverpartOptions`);
  if (serverpartOptions) {
    return serverpartOptions;
  }
  const data = await request(`/BaseInfo/GetServerpartDDL?ProvinceCode=${ProvinceCode || ''}&
		ServerpartCodes=${ServerpartCodes || ''}&ShowWholePower=${ShowWholePower || false}`, {
    method: 'GET',
  });
  if (data.Result_Code !== 100) {
    return [];
  }
  const options = formateField(data.Result_Data.List);
  session.set(`${ProvinceCode || ''}serverpartOptions`, options);
  return options;
}



// 获取列表数据
export async function getList(params?: any) {
  const search = params ? { ...params, PageIndex: params.current } : {};
  const data = await request(`/Verification/GetEndaccountList?EndaccountState=1`, { // 573
    method: 'GET',
    params: search,
  })

  if (data.Result_Code === 500) {
    return []
  }

  return tableList(data.Result_Data)
}
// 获取账期详情数据
export async function getDetail(EndaccountId: number) {

  const data = await request(`/Verification/GetEndaccountDetail?EndaccountId=${EndaccountId}`, {
    method: 'GET',
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}
// 获取收银稽核列表数据
export async function getAuditList(params: SearchAuditModel) {

  const data = await request(`/Audit/GetAuditList`, {
    method: 'GET',
    params
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}
// 获取收银机状态
export async function getMachineState(params: SearchAuditModel) {

  const data = await request(`/Equipment/GetMachineState`, {
    method: 'GET',
    params
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

// 获取账期单品数据
export async function getEndaccountSaleInfo(params: SearchAuditModel) {

  const data = await request(`/Sales/GetEndaccountSaleInfo`, {
    method: 'GET',
    params
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

// 提交日结数据校验
export async function SubmitEndaccountState(params: EndaccountChangeModel) {

  const data = await request(`/Verification/SubmitEndaccountState`, {
    method: 'POST',
    data: params,
    requestType: 'form'
  })

  if (data.Result_Code !== 100) {
    return data.Result_Data
  }

  return true
}

//  记录单品销售日志
export async function recordSaleData(params: RecordModel) {

  const data = await request(`/Sales/RecordSaleData`, {
    method: 'POST',
    data: params,
    requestType: 'form'
  })

  if (data.Result_Code !== 100) {
    return data.Result_Data
  }

  return data.Result_Data
}

//  上传图片
export async function uploadEndaccountEvidence(params: { EndaccountId: string, ImageInfo: string }) {

  const data = await request(`/Picture/UploadEndaccountEvidence`, {
    method: 'POST',
    data: params,
    requestType: 'form'
  })

  if (data.Result_Code === 500) {
    return false
  }

  return data.Result_Data
}



//  获取校验凭据
export async function getEndaccountPhoto(EndaccountId: number) {

  const data = await request(`/Picture/GetEndaccountEvidence?EndaccountId=${EndaccountId}`, {
    method: 'GET',
    // data: params,
    // requestType: 'form'
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}


// 获得数据校验列表
export async function handleDateList(params: any) {

  const data = await request(`/Verification/GetDataVerificationList`, {
    method: 'GET',
    params
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}


// 获取详情里面的数据校验列表
export async function handleDetailList(params: any) {

  const data = await request(`/Verification/GetEndaccountList`, {
    method: 'GET',
    params
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

// 获得详情
export async function handleDetail(params: any) {

  const data = await request(`/Verification/GetEndaccountDetail`, {
    method: 'GET',
    params
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

// 获得品牌详情的列表
export async function handleBrandDetailList(params: any) {

  const data = await request(`/Verification/GetShopEndaccountSum`, {
    method: 'GET',
    params
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

// 获取单品数据 获取移动支付的调用接口
export async function handleGetShopData(params: any) {

  const data = await request(`/Verification/GetEndAccountData`, {
    method: 'GET',
    params
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

// 获取数据校验流水冲正
export async function handleGetDataRight(params: any) {

  const data = await request(`/Verification/GetEndaccountSupplement`, {
    method: 'GET',
    params
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

export async function handleGetDataList(params: any) {
  const data = await request(`/Verification/GetCommoditySaleList`, {
    method: 'GET',
    params
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

// 获取移动支付
export async function handleGetMobilePayment(params: any) {
  const data = await request(`/Verification/GetMobilePayDataList`, {
    method: 'GET',
    params
  })

  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

// 商品类型列表
export async function handleGetShopTypeTree(params: any) {
  const data = await request(`/BaseInfo/GetNestingCOMMODITYTYPETree`, {
    method: 'GET',
    params
  })
  if (data.Result_Code !== 100) {
    return []
  }
  const treeTable = wrapTreeNode(data.Result_Data.List);

  return [...treeTable];
}

// 拿到图片列表
export async function handleGetPictureList(params: any) {
  const data = await request(`/Picture/GetPictureList`, {
    method: 'GET',
    params
  })
  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

// 保存流水冲正
export async function handleSaveEndAccount(params: EndaccountChangeModel) {
  return request(`/Verification/SaveCorrectData`, {
    method: 'POST',
    data: params,
  })
}


// 流水冲正点进去的添加列表
export async function handleGetAddShopList(params: any) {
  const data = await request(`/Commodity/GetCommodityList`, {
    method: 'GET',
    params
  })
  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

// 稽查反馈的详情
export async function handleGetAuditDetail(params: any) {
  const data = await request(`/Audit/GetAuditDetils`, {
    method: 'GET',
    params
  })
  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}

// 保存添加单品数据 的方法
export async function handleSaveAddShopData(params: any) {
  return request(`/Verification/SaveSaleSupplement`, {
    method: 'POST',
    data: params,
  })
}

// 稽查反馈的审核
export async function handleExamine(params: any) {
  return request(`/ShopVideo/SynchroVIDEOLOG`, {
    method: 'POST',
    data: params, 
  })
}


// 拿到收银人员列表
export async function handleGetCASHWORKERList(params: any) {
  const data = await request(`/BaseInfo/GetCASHWORKERList`, {
    method: 'POST',
    data: params
  })
  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data.List
}

// 结账数据校验详情
export async function handleGetENDACCOUNTModel(params: any) {
  const data = await request(`/Verification/GetENDACCOUNTModel`, {
    method: 'GET',
    params
  })
  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data
}


// 心跳表格的数据
export async function handleGetStateFeedBackList(params: any) {
  const data = await request(`/Equipment/GetStateFeedBackList`, {
    method: 'GET',
    params
  })
  if (data.Result_Code === 500) {
    return []
  }

  return data.Result_Data.List
}


// 同步日结账期
export async function handleSynchroENDACCOUNT(params: any) {
  const data = await request(`/Verification/SynchroENDACCOUNT`, {
    method: 'POST',
    data: params
  })
  if (data.Result_Code === 500) {
    return []
  }

  return data
}

// 删除日结账期
export async function handleDeleteENDACCOUNT(params: any) {
  const data = await request(`/Verification/DeleteENDACCOUNT`, {
    method: 'GET',
    params
  })
  if (data.Result_Code === 500) {
    return []
  }

  return data
}