import { upcloud_base_strategy } from '@/config/serviceSettings'

const remote = function(value) {
  return {
    autoLoad: true,
    remoteUrl: {
      url: upcloud_base_strategy + '/enum/v1',
      params: { typeNname: value },
      headers: {
        'Content-Type': 'application/JSON'
      },
      method: 'get'
    },
    doneBefore(data) {
      if (data && data.success) {
        var da = data.data
        var arr = da.map(x => {
          return {
            label: x.key,
            value: x.value
          }
        })
        data.data = arr
      }
      return data
    }
  }
}
const remoteUrl = {
  method: 'get',
  url: upcloud_base_strategy + '/factor/v1/match'
}
const remoteUrl1 = {
  method: 'get',
  url: upcloud_base_strategy + '/factor/v1/search'
}
const rule = function(datasourceType, valueType) {
  let a = {
    name: 'value',
    label: '值',
    required: true,
    value: '',
    disabled: false,
    rules: [
      {
        validator: function(rule, value, callback) {
          //   if (value) {
          //     callback('不符合规则')
          //   }
          callback()
        }
      }
    ]
  }

  if (datasourceType == 'input' && valueType == 'string') {
    a.type = 'input'
    return a
  }
  // 2
  if (datasourceType == 'input' && valueType == 'number') {
    a.type = 'input'
    return a
  }
  // 3
  if (datasourceType == 'input' && valueType == 'boolean') {
    a.type = 'select'
    a.options = [
      { value: '1', label: '是' },
      { value: '0', label: '否' }
    ]
    return a
  }
  // 4
  if (datasourceType == 'input' && valueType == 'datetime') {
    a.type = 'date'
    a.format = 'YYYY-MM-DD HH:mm:ss'
    return a
  }
  // 5
  if (datasourceType == 'enum') {
    a.type = 'input'
    return a
  }
  // 6
  if (datasourceType == 'range') {
    a.type = 'input'
    return a
  }
  //7
  if (datasourceType == 'interface') {
    a.type = 'select'
    a.options = [
      { value: '1', label: '是' },
      { value: '0', label: '否' }
    ]
    return a
  }
}
const genCargoowner = function(that, remoteUrl) {
  return [
    {
      type: 'input',
      name: 'datasourceType',
      label: '数据源类型',
      value: '',
      hidden: true
    },
    {
      type: 'input',
      name: 'valueType',
      label: '值类型',
      value: '',
      hidden: true
    },

    {
      type: 'input',
      name: 'id',
      label: '因子ID',
      value: '',
      hidden: true
    },
    {
      type: 'searchModal',
      name: 'name',
      label: '因子名称',
      ellipsis: true,
      value: '',
      required: true,
      title: '因子名称',
      rowKey: 'id',
      targetValueKey: 'name',
      modalWidth: 700,
      targetClick: true,
      remoteUrl: remoteUrl,

      // rules: [{ required: true, message: '数据不能为空' }],
      //   toolBar: {
      //     items: [
      //       {
      //         type: 'input',
      //         name: 'cargoOwnerName',
      //         label: '货主名称',
      //         span: 6
      //       },

      //       {
      //         type: 'input',
      //         name: 'cargoOwnerType',
      //         label: '货主类型',
      //         span: 6
      //       },
      //       {
      //         type: 'input',
      //         name: 'cargoOwnerId',
      //         label: '货主ID',
      //         span: 6
      //       }
      //     ]
      //   },
      tableConfig: {
        remoteBefore: (res, remoteParams, form) => {
          remoteParams.params.category = that.strategy.category
          return remoteParams
        },
        // remoteBefore: (res, remoteParams) => {
        // },
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          {
            title: '因子名称',
            dataIndex: 'name',
            key: 'name'
          },

          {
            title: '值类型',
            key: 'valueType',
            dataIndex: 'valueType'
          },
          {
            title: '字段名',
            dataIndex: 'fieldName',
            key: 'fieldName'
          },

          {
            title: '数据源类型',
            key: 'datasourceType',
            dataIndex: 'datasourceType'
          }
        ]
      },
      on: {
        ok(event, value, rowData, form) {
          let arr = rowData[0]
          form.setFieldsValue({
            name: arr.name,
            id: arr.id,
            valueType: arr.valueType,
            datasourceType: arr.datasourceType
            // cargoOwnerId: rowData[0].cargoOwnerId
          })

          let list = that.items
          if (list.length > 5) {
            that.items = list.slice(0, 5)
          }
          let b = rule(arr.datasourceType, arr.valueType)
          that.items = [...that.items, b]
        }
      }
    }
  ]
}
const genCargoowner1 = function(that, remoteUrl) {
  return [
    {
      type: 'input',
      name: 'datasourceType',
      label: '数据源类型',
      value: '',
      hidden: true
    },
    {
      type: 'input',
      name: 'valueType',
      label: '值类型',
      value: '',
      hidden: true
    },

    {
      type: 'input',
      name: 'id',
      label: '因子ID',
      value: '',
      hidden: true
    },
    {
      type: 'searchModal',
      name: 'name',
      label: '因子名称',
      value: '',
      required: true,
      title: '因子名称',
      rowKey: 'id',
      targetValueKey: 'name',
      modalWidth: 700,
      targetClick: true,
      remoteUrl: remoteUrl,

      // rules: [{ required: true, message: '数据不能为空' }],
      //   toolBar: {
      //     items: [
      //       {
      //         type: 'input',
      //         name: 'cargoOwnerName',
      //         label: '货主名称',
      //         span: 6
      //       },

      //       {
      //         type: 'input',
      //         name: 'cargoOwnerType',
      //         label: '货主类型',
      //         span: 6
      //       },
      //       {
      //         type: 'input',
      //         name: 'cargoOwnerId',
      //         label: '货主ID',
      //         span: 6
      //       }
      //     ]
      //   },
      tableConfig: {
        remoteBefore: (res, remoteParams, form) => {
          remoteParams.params.category = that.strategy.category
          return remoteParams
        },
        // remoteBefore: (res, remoteParams) => {
        // },
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          {
            title: '因子名称',
            dataIndex: 'name',
            ellipsis: true,
            key: 'name'
          },
          {
            title: '值类型',
            key: 'valueType',
            dataIndex: 'valueType'
          },
          {
            title: '字段名',
            dataIndex: 'fieldName',
            key: 'fieldName'
          },
          {
            title: '数据源类型',
            key: 'datasourceType',
            dataIndex: 'datasourceType'
          }
        ]
      },
      on: {
        ok(event, value, rowData, form) {
          let arr = rowData[0]
          form.setFieldsValue({
            name: rowData[0].name,
            id: rowData[0].id,
            valueType: arr.valueType,
            datasourceType: arr.datasourceType
            // cargoOwnerId: rowData[0].cargoOwnerId
          })

          let list = that.items1
          if (list.length > 5) {
            that.items1 = list.slice(0, 5)
          }
          let b = rule(arr.datasourceType, arr.valueType)
          that.items1 = [...that.items1, b]
        }
      }
    }
  ]
}
export const items = function(that) {
  return [
    ...genCargoowner(that, remoteUrl),
    {
      //   匹配类型 matchType
      //   匹配类型 Equal-等于  Incloudeone-包含于
      type: 'select',
      name: 'matchType',
      label: ' 匹配类型',
      required: true,
      value: '',
      //   labelCol: { span: 6 },
      //   span: 24,
      on: {
        change(value, label, options, form) {
          if (value == 'in' || value == 'between') {
            that.items.forEach(item => {
              if (item.name == 'value') {
                item.rules = [
                  {
                    validator: function(rule, value, callback) {
                      if (value.indexOf(',') > -1) {
                        callback()
                        return
                      }
                      callback('数值请用,分隔')
                    }
                  }
                ]
              }
            })
          } else {
            that.items.forEach(item => {
              if (item.name == 'value') {
                item.rules = [
                  {
                    validator: function(rule, value, callback) {
                      callback()
                    }
                  }
                ]
              }
            })
          }
          let list = that.items
          if (list.length == 4) {
            //   that.items = list.slice(0, 4)
            that.items = [...that.items]
          }
        }
      },
      ...remote('match')
    }
  ]
}

export const items1 = function(that) {
  return [
    ...genCargoowner1(that, remoteUrl1),
    {
      //   匹配类型 matchType
      //   匹配类型 Equal-等于  Incloudeone-包含于
      type: 'select',
      name: 'matchType',
      label: ' 匹配类型',
      required: true,
      value: '',
      //   labelCol: { span: 6 },
      //   span: 24,
      on: {
        change(value, label, options, form) {
          //   form.setFieldsValue({ 'field-select': value })
        }
      },
      ...remote('match')
    }
  ]
}
export const model = function(that) {
  return {
    //商品
    goods: {
      items: [...genCargoowner(that, remoteUrl)]
    },
    //货位
    goodsallocation: {
      items: [
        ...genCargoowner(that, remoteUrl1),
        {
          type: 'number',
          name: 'value1',
          label: '值',
          value: '',
          disabled: false
        }
      ]
    }
  }
}
export const algorithmitems = [
  {
    type: 'input',
    name: 'id',
    label: '算法ID',
    value: '',
    hidden: true
  },
  {
    type: 'searchModal',
    name: 'name',
    label: '算法名称',
    value: '',
    required: true,
    title: '货主',
    rowKey: 'id',
    targetValueKey: 'name',
    modalWidth: 700,
    targetClick: true,
    remoteUrl: {
      method: 'get',
      url: upcloud_base_strategy + '/algorithm/v1/page'
    },

    tableConfig: {
      rowSelectType: 1,
      hasRowSelectCheck: true,
      columns: [
        {
          title: '算法名称',
          dataIndex: 'name',
          key: 'name'
        },
        {
          title: '算法分类',
          dataIndex: 'category',
          key: 'category'
        },

        {
          title: '语言',
          dataIndex: 'language',
          key: 'language'
        }
      ]
    },
    on: {
      ok(event, value, rowData, form) {
        form.setFieldsValue({
          name: rowData[0].name,
          id: rowData[0].id
        })
      }
    }
  }
]
// export default model
