import service from '../service';
import type {
  UserListParams,
  UserListItem,
  UserListParamsV2,
  UserInfo,
  StaffsInfo,
  PayInfo,
  StaffQuery,
  DocumentInfo,
  DocsListParams,
  BillingListParams,
  PaymentInfo
} from './data.d';


//查询用户列表
export async function queryDocsList(params: DocsListParams) {

  // params.current = Number(params.current)
  // params.current = params.current ? params.current - 1 : params.current
  console.log(params)
  let result = await service.post('/api/v1/customer/list/doc', {
    "staff_id": params.id,
    "page_no": params.current,
    "page_size": params.pageSize
  });

  return Promise.resolve({
    data: result.data.list,
    success: true,
  })
}

//查询用户列表
export async function queryStaffList(params: StaffQuery) {

  // params.current = Number(params.current)
  // params.current = params.current ? params.current - 1 : params.current
  let page_no = params.page_no || 0
  let page_size = params.page_size || 100
  console.log(params)

  let result = await service.post('/api/v1/customer/list/staff', {
    id: params.id,
    name: params.name,
    serial_no: params.serial_no,
    page_no: page_no,
    page_size: page_size,

  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}

export async function queryProductionList() {

  // params.current = Number(params.current)
  // params.current = params.current ? params.current - 1 : params.current
  let result = await service.post('/api/v1/customer/list/production', {
    // "user_name": params.user_name,
    // "page_no": params.current,
    // "page_size": params.pageSize
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}


//删除文档
export async function removeDocs(params: { staff_id: number, ids: number[] }) {
  let result = await service.post('/api/v1/customer/delete/doc', {
    staff_id: params.staff_id, //员工ID
    ids: params.ids, // 要删除的文档ID列表
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}

// 触发同步消息给到AI服务器，更新知识库
export async function syncDocs(params: { staff_id: number }) {
  let result = await service.post('/api/v1/customer/sync/doc', {
    staff_id: params.staff_id, //员工ID
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}


//添加员工
export async function addStaff(params: StaffsInfo) {
  let result = await service.post('/api/v1/customer/create/staff', {
    // name: params.name,
    // org_name: params.org_name,
    // tone: params.tone,
    // language_mode: params.language_mode,
    // language_response: params.language_response,
    // output_length: params.output_length,
    // description: params.description,
    // avatar_url: params.avatar_url,
    ...params
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}


// ## 2.3 编辑员工
// Token: required
// POST   /api/v1/customer/edit/staff 
export async function editStaff(params: StaffsInfo) {
  let result = await service.post('/api/v1/customer/edit/staff', {
    ...params
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}

//更新用户
export async function updateUser(params: UserInfo) {
  let result = await service.post('/api/v1/platform/edit/user', {
    user_name: params.user_name,
    user_alias: params.user_alias,
    phone_number: params.phone_number,
    password: params.password,
    remark: params.remark,
    role_name: params.role_name,
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}

//更新用户
export async function payStaff(params: PayInfo) {
  let result = await service.post('/api/v1/customer/pay/staff', {
    id: params.id,
    production_id: params.production_id,
    pay_type: params.pay_type,
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}


// ## 2.4 删除员工
// Token: required
//  POST   /api/v1/customer/delete/staff 
//  **请求JSON**
// ```json
// {
//     "id": 1                 //自增ID (必填)
// }
// ```
//更新用户
export async function deleteStaff(id: number) {
  let result = await service.post('/api/v1/customer/delete/staff', {
    id: id,
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}


// ## 2.5 冻结/解冻员工
// Token: required
// 冻结：POST   /api/v1/customer/frozen/staff
// 解冻：POST   /api/v1/customer/unfrozen/staff
// **请求JSON**
// ```json
// {
//     "id": 1  //员工ID
// }
// ```
export async function frozenStaff(id: number) {
  let result = await service.post('/api/v1/customer/frozen/staff', {
    id: id,
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}
export async function unfrozenStaff(id: number) {
  let result = await service.post('/api/v1/customer/unfrozen/staff', {
    id: id,
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}

//查询支付状态
// "state": 0,          //订单状态(0=待支付 1=处理中 2=已成功支付 3=已取消 4=已退款 5=支付平台异常)
// "message": "Success" //订单交易状态信息
export async function queryPayStatus(trade_no: string) {
  let result = await service.post('/api/v1/customer/query/trade', {
    trade_no: trade_no,
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}


// {
//   "staff_id": 1, //员工ID
//   "docs":[
//      {
//            "file_name":"文档1.docx",    //原始文件名
//           "file_path":"2141242323_文档1.docx", //实际文件存储路径
//           "file_size": 1412423,   //文件大小(字节)
//           "file_url": ""          //文件访问URL
//      }
//   ]
// }

//添加文档
export async function addDocument(staff_id: number, params: DocumentInfo[]) {
  let result = await service.post('/api/v1/customer/update/doc', {
    staff_id,
    docs: params
    // {
    //   file_name: params.file_name,
    //   file_path: params.file_path,
    //   file_size: params.file_size,
    //   file_url: params.file_url,
    //   description: params.description,
    // }
  });


  return Promise.resolve({
    data: result,
    success: true,
  })
}


// ## 2.14 序列号查询员工
// Token: no
// POST   /api/v1/customer/query/staff
// **请求JSON**
// ```json
// {
//     "serial_no": "safasdfsdfs" //按员工序列号查询
// }
export async function queryStaff(serial_no: string) {
  let result = await service.post('/api/v1/customer/query/staff', {
    serial_no,
  });

  return Promise.resolve({
    data: result,
    success: true,
  })
}


// ## 2.15 账单列表
// Token: required
// POST   /api/v1/customer/list/billing
// **请求JSON**
// ```json
// {
//     "id": 0, //按账单ID查询
//     "page_no": 0,    
//     "page_size": 20
// }
// ```
export async function queryBillingList(params: BillingListParams) {

  console.log(params)
  let result = await service.post('/api/v1/customer/list/billing', {
    // "id": params.id,
    "page_no": params.current,
    "page_size": params.pageSize
  });

  return Promise.resolve({
    data: result.data.list, //as PaymentInfo[],
    success: true,
  })
}