import { hasSpace, isSpecialSymbol } from "/@/utils/validator"

export const statusOptions =  [
  { value: '0', label: '复制待编辑' },
  { value: '1', label: '招标进行中' },
  { value: '2', label: '招标已结束' },
  { value: '3', label: '待业务下单' },
  { value: '5', label: '已成功转单' },
  { value: '6', label: '竞价单已失效' },
  { value: '7', label: '招标已暂停' }
]

export const biddingTypeOptions =  [
  { value: '0', label: '非项目竞价单' },
  { value: '1', label: '项目竞价单' },
  { value: '2', label: '业务报备' },
]

export const biddingTypes = [
  { value: "0", label: "非项目" },
  { value: "1", label: "项目" }
]

export const isMultiplePlaceOptions = [
  { value: '0', label: '一地' },
  { value: '1', label: '多地' }
]

export const isCustomizedOptions = [
  { value: '0', label: '非定制' },
  { value: '1', label: '定制' }
]

export const initialData = {
  is_project: '', // 竞价单类型
  area: '',
  name: '', // 项目名称
  platform_code: '', // 所属平台
  companyNo: '', // 销售方
  khNo: '', // 购买方
  budget_total: '0.00', // 项目总预算
  arrtime: '', // 要求到货时间
  endtime: '', // 竞价截止时间
  use_desc: '', // 竞价截止时间
  ladder: [] // 商品要求
}

export const initialBidGoodData = {
  arrival_time: '',
  budget_price: '',
  is_addrs: '',
  num: '',
  brand_id: '',
  is_custom: '',
  total_weight: '',
  unit: '',
  cat_id: '',
  good_name: '',
  cost_desc: '',
  use_desc: '',
  remark: '',
  specs: []
}

const validate_sale_price = (_, value, callback) => {
  if (!value || value === '') {
    callback(new Error('项目总预算不能为空！'))
  } else if (Number(value) !== 0) {
    callback(new Error('项目总预算不能为零！'))
  } else {
    callback()
  }
}

export const rules =  {
  companyNo: [{ required: true, message: "请选择销售方公司", trigger: "change" }],
  area:[{ required:true, message:'请选择收货区域' }],
  khNo: [{ required: true, message: "请选择购买方公司", trigger: "change", }],
  platform_code: [{ required: true, message: "请选择所属平台", trigger: "change" }],
  pay_id: [{ required: true, message: "请选择支付渠道", trigger: "change", },],
  endtime: [
    {
      required: true,
      message: "竞价截止时间不能为空",
      trigger: "change",
    },
  ],

  is_project: [
    {
      required: true,
      message: "请选择竞价类型！",
      trigger: "change",
    },
  ],
  projectNo: [
    {
      required: true,
      message: "请选择项目",
      trigger: "change",
    },
  ],
}


const validate_min_num = (rule, value, callback) => {
  if (!value || value === '') {
    callback(new Error('购买数量不能为空！'))
  } else if (value === '0') {
    callback(new Error('购买数量不能为零！'))
  } else {
    callback()
  }
}

export const ladderRules = {
  good_type: [{ required: true, message: '请选择商品类型', trigger: 'change' }],
  good_name: [{ required: false, message: '商品名称不能为空', trigger: 'blur' }],
  good_img: [{ required: false, message: '商品图片不能为空', trigger: 'change' }],
  budget_price: [{ required: true, validator: validate_sale_price, trigger: 'blur' }],
  num: [{ required: true, validator: validate_min_num, trigger: 'blur' }],
  cat_id: [{ required: true, message: '请选择商品分类', trigger: 'change' } ]
}

const validate_budget_price = (rule, value, callback) => {
  if (value === '' || !value) {
    callback(new Error('预算单价不能为空！'))
  } else if (
    value === '0' ||
    value === '0.' ||
    value === '0.0' ||
    value === '0.00'
  ) {
    callback(new Error('预算单价不能为零！'))
  } else {
    callback()
  }
}

const validate_num = (rule, value, callback) => {
  if (value === '' || !value) {
    callback(new Error('购买数量不能为空！'))
  } else if (value === '0') {
    callback(new Error('购买数量不能为零！'))
  } else {
    callback()
  }
}

const validate_good_name = (rule, value, callback) => {
  const { required } = rule
  if (required && value.length == 0) {
    callback(new Error('不能为空！'))
  } else if (hasSpace(value)) {
    callback(new Error('不能出现/回车/换行符！'))
  } else if (isSpecialSymbol(value)) {
    callback(new Error('不能使用英文特殊字符！'))
  } else {
    callback()
  }
}

const validate_total_weight = (rule, value, callback) => {
  const { required } = rule
  if (required && value === '') {
    callback(new Error('商品重量不能为空！'))
  } else if (
    required &&
    (value === '0' ||
      value === '0.' ||
      value === '0.0' ||
      value === '0.00' ||
      value === '0.000')
  ) {
    callback(new Error('商品重量不能为零！'))
  } else {
    callback()
  }
}

const validate_specs_weight = (rule, value, callback) => {
  const { required } = rule
  if (required && value === '') {
    callback(new Error('金属克重不能为空！'))
  } else if (
    required &&
    (value === '0' ||
      value === '0.' ||
      value === '0.0' ||
      value === '0.00' ||
      value === '0.000')
  ) {
    callback(new Error('金属克重不能为零！'))
  } else {
    callback()
  }
}

const validate_desc = (rule, value, callback) => {
  const { required } = rule
  if (required && value === '') {
    callback(new Error('不能为空！'))
  } else if (value.length > 0 && isSpecialSymbol(value)) {
    callback(new Error('不能使用英文特殊字符！'))
  } else {
    callback()
  }
}

export const bidRules = {
  arrival_time: [
    {
      required: true,
      message: '到货时间不能为空',
      trigger: 'change'
    }
  ],
  budget_price: [
    {
      required: true,
      validator: validate_budget_price,
      trigger: 'change'
    }
  ],
  num: [
    {
      required: true,
      validator: validate_num,
      trigger: 'change'
    }
  ],
  brand_id: [
    {
      required: true,
      message: '请选择品牌',
      trigger: 'change'
    }
  ],
  unit: [
    {
      required: true,
      message: '请选择单位',
      trigger: 'change'
    }
  ],
  cat_id: [
    {
      required: true,
      message: '请选择商品分类',
      trigger: 'change'
    }
  ],
  good_img: [
    {
      required: true,
      message: '商品图片不能为空',
      trigger: 'change'
    }
  ],
  good_name: [
    { required: true, validator: validate_good_name, trigger: 'blur' }
  ],

  model: [
    {
      required: true,
      message: '型号不能为空',
      trigger: 'blur'
    }
  ],
  color: [
    {
      required: true,
      message: '颜色不能为空',
      trigger: 'blur'
    }
  ],
  material: [
    {
      required: true,
      message: '材质不能为空',
      trigger: 'blur'
    }
  ],
  total_weight: [
    {
      required: false,
      validator: validate_total_weight,
      trigger: 'blur'
    }
  ],
  is_addrs: [
    {
      required: true,
      message: '请选择是否多地',
      trigger: 'change'
    }
  ],
  is_custom: [
    {
      required: true,
      message: '请选择是否定制',
      trigger: 'change'
    }
  ],
  is_gold_price: [
    {
      required: true,
      message: '请选择是否启用实时金价',
      trigger: 'change'
    }
  ],
  config: [
    {
      required: true,
      message: '至少选择一个配置',
      trigger: 'change'
    }
  ],
  specs_weight: [
    {
      required: true,
      validator: validate_specs_weight,
      trigger: 'blur'
    }
  ],
  other_config: [
    {
      required: true,
      message: '其他要求不能为空',
      trigger: 'blur'
    }
  ],
  cost_desc: [
    {
      required: false,
      validator: validate_desc,
      trigger: 'blur'
    }
  ],
  // use_desc: [
  //   {
  //     required: true,
  //     validator: validate_desc,
  //     trigger: 'blur'
  //   }
  // ],
  remark: [
    {
      required: true,
      validator: validate_desc,
      trigger: 'blur'
    }
  ],
  metal_id: [
    {
      required: true,
      message: '贵金属种类不能为空',
      trigger: 'blur'
    }
  ],
  gold_price: [
    {
      required: true,
      message: '当前金价不能为空',
      trigger: 'blur'
    }
  ],
  enclosure_file: [
    {
      required: false,
      message: '请上传附件',
      trigger: 'change'
    }
  ]
}
