import request from '@/utils/request'
import {SERVICE_CTRL_TOWER} from '@/config/serviceSettings'

//获取货主信息
let getAuthByUser = function (params) {
  return request({
    url: SERVICE_CTRL_TOWER + '/userDataAuth/allDataAuthByUser/?companyType=H&pageNum=1&pageSize=10',
    method: 'get',
    // headers: { 'Content-Type': 'application/json' },
    params: params
  })
}

export const setCargoOwner = {
  async mounted() {
    let warehouse = this.$store.state.user.warehouse
    // console.log(warehouse)
    if (Object.keys(warehouse).length > 0) {
      this.$refs.form.form.setFieldsValue(warehouse)
    }
    let res = await getAuthByUser()
    // console.log(res)
    if (res.success) {
      this.$refs.form.form.setFieldsValue({
        cargoOwnerCode: res.data.list[0]?.companyCode,
        cargoOwnerId: res.data.list[0]?.companyId,
        cargoOwnerName: res.data.list[0]?.companyName
      })
    }
  }
}
// 获取默认货主数据-仅到一条数据的时返回货主数据
export const getDefaultCargoOwner = async () => {
  try {
    const companyRes = await getAuthByUser()
    if (companyRes.success && Number(companyRes?.data?.totalCount) === 1) {
      return companyRes.data.list[0] || false
    }
    return false
  } catch (err) {
    console.error('获取货主数据(设置默认值)失败', err)
    return false
  }
}
export const changecargoOwnerfun = function (e, form) {
  form.setFieldsValue({
    cargoOwnerCode: null,
    cargoOwnerId: null,
    cargoOwnerName: null
  })
}
