/* 
cloumn配置
const cloumn = [
    {
        dataIndex: 'billtype',
        title: '交货类型',
        remoteUrl: { url: src + 'cargoowner_type', method: 'post' },
        replaceFields: {
          value: 'dictCode',
          label: 'dictName'
        },
    },
]

vue页面配置
import { forSelectText } from '@/views/kzyWMS/utils/index.js'
  data() {
    return {
      custom: null, //货主资料gridform配置
      Healthcustom: null, //Health 业务范围 三证合一配置
      HealthScopecolumns: null, //经营范围字段
    }
  },

  created () {
    let cloumnsArrey = [ custom.custom, custom.Healthcustom, custom.HealthScopecolumns]
    forSelectText(cloumnsArrey,this).then(res => {
      this.custom = res[0]
      this.Healthcustom = res[1]
      this.HealthScopecolumns = res[2]
    })
    
  },

*/
import clonedeep from "lodash.clonedeep";
import request from '@/utils/request'
import store from '@/store';
import { SERVICE_CTRL_TOWER, upcloud_base_wh } from "@/config/serviceSettings";

export function forSelectText(data, self) {
  return Promise.all(data.map(tt => diffOption(tt)));
}

export function diffOption(params) {
  return new Promise((resolve, reject) => {
    // const paramsUrl = urls+data.substring(0,data.length-1)
    const urlParams = haveUrl(params)
    const paramsUrl = urlParams.paramsUrl
    let arr = urlParams.arr
    if (arr.length < 1) {
      resolve(params)
      return
    }
    getList(paramsUrl).then(res => {
      const value = filterValue(arr, res, params)
      resolve(value)
    })
  })
}
export function haveUrl(params) {
  // let newCloumn = clonedeep(params)
  let arr = []
  let data = ''
  let urls = ''
  params.forEach(item => {
    let obj = {}
    if (item.remoteUrl && item.replaceFields && item.remoteUrl.url.indexOf('dicttypeCodes') != -1) {
      var n = item.remoteUrl.url.indexOf('=')
      var s = item.remoteUrl.url.indexOf('?')
      const str = item.remoteUrl.url.substring(n + 1)
      const url = item.remoteUrl.url.substring(0, s)
      urls = url + '?'
      // item.remoteUrl.url = url
      item.remoteUrl.dicttypeCodes = str
      obj.remoteUrl = {
        url:url,
        dicttypeCodes: str
      }
      obj.replaceFields = item.replaceFields
      obj.name = item.name || item.dataIndex
      data += 'dicttypeCodes=' + str + '&'
      arr.push(obj)
    }
  })
  const paramsUrl = urls + data.substring(0, data.length - 1)
  return { arr: arr, paramsUrl: paramsUrl }
}

export function filterValue(arr, res, params) {
  for (let i = 0; i < arr.length; i++) {
    let list = []

    for (let q = 0; q < res.data.length; q++) {
      const obj = {}
      if (arr[i].remoteUrl.dicttypeCodes === res.data[q].dicttypeCode) {
        const obj = { label: res.data[q][arr[i].replaceFields.label], value: res.data[q][arr[i].replaceFields.value] }
        list.push(obj)
      }
    }
    arr[i].options = list
  }
  for (let i = 0; i < params.length; i++) {
    for (let q = 0; q < arr.length; q++) {
      if (params[i].name === arr[q].name || params[i].dataIndex === arr[q].name) {
        delete params[i].remoteUrl
        delete params[i].replaceFields
        params[i].options = arr[q].options
        //判断是否为p-table
        if (params[i].dataIndex && params[i].type != 'select') {
          const showtitle = arr[q].options
          params[i].customRender = function (text, record) {
            let title = /* '默认值' */''
            for (let w = 0; w < showtitle.length; w++) {
              if (showtitle[w].value == text) {
                title = showtitle[w].label
                break
              }
            }
            return title
          }
        }
      }
    }
  }
  return params
}

export function getList(params) {
  return request({
    url: params,
    method: 'post'
    //   headers: { 'Content-Type': 'application/json' },
    //   data: params
  })
}
//获取货主
export function getCargoOwner() {
  return request({
    url: SERVICE_CTRL_TOWER + "/userDataAuth/allDataAuthByUser/?companyType=H&pageNum=1&pageSize=10",
    method: 'get'
    //   headers: { 'Content-Type': 'application/json' },
    //   data: params
  })
}


//获取建筑物
export function getBldg(params) {
  return request({
    url: upcloud_base_wh + "/wmsWhBldg/v1/page",
    method: 'get',
    //   headers: { 'Content-Type': 'application/json' },
    params: params
  })
}


//获取货主信息
export function getCargoOwnerFirst() {
  getCargoOwner().then((res) => {
    if (!res.success) {
      this.$exception(res)
      return
    }
    this.$nextTick(() => {
      if (res.data.list.length < 3) {
        this.$refs.formTarget.form.setFieldsValue({
          cargoOwnerId: res.data.list[0].companyId,
          cargoOwnerCode: res.data.list[0].companyCode,
          cargoOwnerName: res.data.list[0].companyName,
        })
      }
    })
  })
}


