import { xs_order_source_options, xs_order_type_options } from "/@/utils/status";
import { isChinese, isEmoticon, isMobile } from "/@/utils/validator";
import { goodTypeOptions } from "../../project/config/_options";
import { createTooltip } from "/@/utils/tootip";
import { ElTag } from "element-plus";
import { truncate } from "lodash";
import { h } from "vue";

export const statusOptions = [
  { value: "0", label: "待发货", type: 'warning' },
  { value: "1", label: "待发货完成", type: 'warning' },
  { value: "2", label: "发货已完成", type: 'success' },
  { value: "3", label: "订单已取消", type: 'danger' }
];

export const platformTypeOptions = [
  { value: '1', label: 'toB' },
  { value: '2', label: 'toC' }
];

export const xsOrderTypeOptions = [
  { value: "1", label: "库存商品" },
  { value: "2", label: "非库存商品" },
  { value: "3", label: "反馈商品" },
  { value: "4", label: "报备商品" },
];

function validatecontactor(_, value, callback){
  if(value === '' || !value){
    callback(new Error('联系人不能为空！'))
  } else {
    if(value.length < 2 || value.length > 10){
      callback(new Error('联系人规则为2~10位汉字或字母!'))
    } else {
      const isOk = value.split('').every(s => isChinese(s) || /^[A-Za-z]+$/.test(s))
      if(!isOk){
        callback(new Error('联系人规则为2~10位汉字或字母!'))
      } else if(isEmoticon(value)){
        callback(new Error('联系人规则为2~10位汉字或字母!'))
      } else {
        callback()
      }
    }
  }
}
function validatemobile(_, value, callback){
  if(value === "" || !value) {
    callback(new Error("手机号不能为空！"));
  } else {
    if(!isMobile(value)){
      callback(new Error('手机号格式不正确！'))
    } else {
      callback()
    }
  }
}

export const rules = {
  receipt_quantity: [{ required: truncate, trigger: 'blur', message: '收货数量不能为空' }],
  contactor: [{ required: true, trigger: 'blur', validator: validatecontactor }],
  mobile: [{ required: true, trigger: 'blur', validator: validatemobile }],
  addr_code: [{ required: true,  message: '收货省市区不能为空', trigger: 'change'}],
  addr: [{ required: true,  message: '收货省市区不能为空', trigger: 'blur' }]
}


export const basicRules = {
  sell_code: [{ required: true, trigger: 'blur', message: '请选择销售方公司' }],
  customer_code: [{ required: true, trigger: 'blur', message: '请选择企业客户' }],
  platform_id: [{ required: true, trigger: 'blur', message: '请选择所属平台' }],
  good_code: [{ required: true, trigger: 'blur', message: '请选择销售商品' }],
  remark: [{ required: true, trigger: 'blur', message: '请输入备注' }],
  arrtime: [{ required: true, message: "要求到货时间", trigger: "change" }],
  sendtype: [{ required: true, message: "请选择发货方式", trigger: "change" }],
  platform_order:[
    {
      trigger: 'change',
      validator:(rule, value, callback) => {
        const len = value.length
        if (value === '' || !value) {
          callback()
        } else if (len < 0 || len > 255) {
          callback(new Error('仅支持大写字母数字组合,多个编号使用逗号分隔(0~255位)!'))
        } else if (/^(?=.*[A-Z])(?=.*\d)[A-Z\d]+(?:,[A-Z\d]+)*$/.test(value) == false) {
          callback(new Error('仅支持大写字母数字组合,多个编号使用逗号分隔(0~255位)!'))
        } else {
          callback()
        }
      }
    }
  ],
  managerid: [
    {
      required: true,
      message: "请选择业务经理",
      trigger: "change"
    }
  ],
  supplierNo: [
    {
      required: true,
      message: "请选择销售方公司",
      trigger: "change"
    }
  ],
  new_good_price: [
    {
      required: true,
      message: "销售单价不能为空！",
      trigger: "blur"
    }
  ],
  proof_id: [
    {
      required: false,
      message: "请选择比价凭证",
      trigger: "change"
    }
  ],

  goodtype: [
    {
      type: "array",
      required: true,
      message: "请选择活动类型",
      trigger: "change"
    }
  ],
  good_num: [
    {
      required: true,
      message: "请选择活动类型",
      trigger: "change"
    }
  ]
}

export const columns = [
  { label: '订单编号', field: 'orderCode', span: 6 },
  { 
    label: '订单状态', 
    span: 6, 
    render(_, { status }){
      return h(ElTag, { size: 'small', type: statusOptions.find(item => item.value == status)?.type }, {
        default: () => statusOptions.find(item => item.value == status)?.label
      })
    }
   },
  { 
    label: '订单来源', 
    span: 6,
    render(_, {  order_source }){
      return h(ElTag, { size: 'small' }, {
        default: () => xs_order_source_options.find(({ value }) => value === order_source)?.label || '--'
      })
    }
  },
  { 
    label: '商品类型', 
    span: 6,
    render(_, { order_type }){
      return h(ElTag, { size: 'small' }, {
        default: () =>  xs_order_type_options.find(({ value }) => value === order_type)?.label || '--'
      })
    }
  },
  { 
    label: '销售方公司', 
    span: 12,
    render(_, { supplierName, supplierNo }){
      return createTooltip( supplierName, '销售方公司编码:' + supplierNo, 300)
    } 
  },
  { 
    label: '购买方公司', 
    span: 12,
    render: (_, { customer_code, customerName }) => createTooltip(customerName, '购买方公司编码:' + customer_code, 300)
  },
  { label: '所属平台', span: 12, field: 'platform_name' },
  { label: '创建人', span: 6, field: 'apply_name'},
  { label: '创建时间', span: 6, field: 'addtime' },
  { 
    label: '活动类型', 
    span: 12,
    render(_, { is_active, good_type }){
      let baseStr = ''
      baseStr += is_active == '1' ? '参与活动' : '不参与活动'
      if(is_active == '0'){ baseStr += (goodTypeOptions.find(({ value }) => value == good_type)?.label || '--') }
      return baseStr
    }
  },
  { field: 'arrive_time', label: '要求到货时间', span: 6 },
  { field: 'paytime', label: '承诺付款时间', span: 6 },
  { 
    label: '销项类目', 
    span: 24,
    render(_, context){
      return `${context.tax_code}-${context.tax_name}-${context.tax_short_name}-${context.tax ? context.tax + '%' : ''}`
    }
  },
  { field: 'inv_good_name', label: '开票商品名称', span: 18 },
  { 
    label: '供应商销项票开', 
    span: 6,
    render(_, { open_type }){
      return open_type == '1' ? '原商品开票' : '子商品开票'
    }
  },
  { field: 'good_num', label: '购买数量', span: 4 },
  { field: 'send_num', label: '已发数量', span: 4 },
  { field: 'wsend_num', label: '未发数量', span: 4 },
  { field: 'th_num', label: '退货数量', span: 4 },
  { field: 'sale_price', label: '售价', span: 4 },
  { field: 'total_price', label: '总金额', span: 4 },
  { field: 'returnCode', label: '售后来源单号', span: 6 },
  { label: '比价凭证', span: 6 },
  { field: 'post_fee', label: '物流费用', span: 6 },
  { field: 'send_type', label: '下单方式', span: 6 },
  { field: 'manager', label: '业务经理', span: 6 },
  { field: 'useage', label: '订单用途', span: 18 },
  { field: 'remark', label: '备注', span: 24 },
  { field: 'platform_order', label: '平台订单号', span: 24 },
  { field: 'workNo', label: '其他单号', span: 24 }
]



export const productDetailColumns = [
  {
    field: 'good_name',
    label: '商品名称',
    span: 24
  },
  {
    field: 'cat',
    label: '分类',
    span: 24,
    render(_,  row){

      console.log(row)
      return (row.cat_info || []).map(({ name }) => name).join('_')
    }
  },
  {
    field: 'is_stock',
    label: '是否库存品',
    span: 8,
    render(_, row){
      return row.is_stock === '1' ? '是' : '否'
    }
  },

  {
    field: 'unit',
    label: '单位',
    span: 8
  },
  {
    field: 'tax',
    label: '税率',
    append: '%',
    span: 8
  },

  {
    field: 'exclusive',
    label: '专属类型',
    _slot_: 'exclusive',
    span: 12
  },
  {
    field: 'brand_name',
    label: '品牌',
    span: 12
  },
  {
    field: 'weight',
    label: '商品总克重',
    _slot_: 'weight',
    span: 24
  },
  {
    field: 'lead_time',
    label: '工期',
    span: 8
  },
  {
    field: 'delivery_day',
    label: '物流',
    span: 8
  },
  {
    field: 'purchase',
    label: '采购人员',
    span: 8
  },
  {
    field: 'delivery_place_cn',
    label: '发货地',
    span: 24
  },
  {
    field: 'after_sales',
    label: '售后说明',
    span: 24
  }
]


export const goodTypeOptions2 = [
  {
    value: 'is_activity_0',
    label: '不参与活动',
    children: [
      { value: '1', label: '常规商品' },
      { value: '2', label: '赠品' },
      { value: '3', label: '样品' }
    ]
  },
  {
    value: "is_activity_1",
    label: '参与活动',
    children: []
  }
] as any
