// 销售订单详情
import { h } from "vue";
import { ElTag } from "element-plus";
import { createTooltip } from "../tootip";
import { DescriptionColumns } from "/@/components/BasicDescriptions";
import { xs_order_source_options, xs_order_type_options } from "/@/utils/status";

const SEND_TYPE = [
  { value: "1", label: "供应商包邮" },
  { value: "2", label: "公司自提" }
];

export const SALE_STATUS = [
  { value: "0", label: "未核销" },
  { value: "1", label: "核销中" },
  { value: "2", label: "不参与核销" }
];

export const SALE_GOOD_TYPE = [
  { value: "1", label: "正常商品" },
  { value: "2", label: "赠品" },
  { value: "3", label: "样品" }
];

const PAY_STATUS = [
  { value: "1", label: "未回款" },
  { value: "2", label: "部分回款" },
  { value: "3",  label: "已回款" }
];

const INV_STATUS = [
  { value: "1", label: "未开票" },
  { value: "2", label: "部分开票" },
  { value: "3", label: "已开票" }
];

export const ORDER_COLUMNS: DescriptionColumns = [
  {
    field: "sequenceNo",
    label: "销售订单编码",
    span: 6
  },
  {
    field: "platName",
    span: 6,
    label: "平台名称"
  },
  {
    field: "ownerName",
    span: 6,
    label: "销售员",
    render: (ownerName, { department }) =>
      createTooltip(ownerName, "所属部门 : " + department, 150)
  },
  {
    field: "qrdSource",
    label: "订单来源",
    span: 6,
    render(data) {
      return h(ElTag, { size: "small" }, { default: () => xs_order_source_options.find(t => t.value === data)?.label });
    }
  },

  {
    field: "companyName",
    label: "业务公司",
    span: 12,
    render: (company, { companyNo }) => createTooltip(company, "业务公司编码 : " + companyNo, 270)
  },
  {
    field: "customerName",
    label: "客户公司",
    span: 12,
    render: (customer, { customerNo }) =>
      createTooltip(customer, "客户公司编码 : " + customerNo, 270)
  },
  {
    field: 'manager',
    label: '项目经理',
    span: 6
  },
  {
    field: "poCode",
    label: "平台订单编码",
    span: 18
  },
  {
    field: "sendType",
    label: "发货方式",
    span: 6,
    render(data) {
      return h(
        ElTag,
        { size: "small" },
        {
          default: () => SEND_TYPE.find(t => t.value === data)?.label
        }
      );
    }
  },
  {
    field: "goodNum",
    label: "商品数量",
    span: 6
  },
  {
    field: "goodPrice",
    label: "商品单价",
    span: 6
  },
  {
    field: "totalPrice",
    label: "总金额",
    span: 6
  },
  {
    field: "inv_status",
    label: "开票状态",
    span: 6,
    render(data) {
      return h(
        ElTag,
        { size: "small" },
        {
          default: () => INV_STATUS.find(t => t.value === data)?.label
        }
      );
    }
  },

  {
    field: "ainv_fee",
    span: 6,
    label: "已开票金额"
  },
  {
    field: "inv_fee",
    span: 6,
    label: "开票中金额"
  },
  {
    field: "winv_fee",
    span: 6,
    label: "未开票金额"
  },
  {
    field: "pay_status",
    label: "付款状态",
    span: 6,
    render(data) {
      return h(
        ElTag,
        { size: "small" },
        {
          default: () => PAY_STATUS.find(t => t.value === data)?.label
        }
      );
    }
  },

  {
    field: "apay_fee",
    span: 6,
    label: "已付款金额"
  },
  {
    field: "pay_fee",
    span: 6,
    label: "付款中金额"
  },
  {
    field: "wpay_fee",
    span: 6,
    label: "未付款金额"
  },

  {
    field: "thNum",
    span: 6,
    label: "退货数量"
  },
  {
    field: "th_fee",
    span: 6,
    label: "退货金额"
  },

  {
    field: "wsendNum",
    span: 6,
    label: "未发货"
  },
  {
    field: "sendNum",
    span: 6,
    label: "已发货"
  },
  {
    field: "cxCode",
    span: 6,
    label: "销售主单号"
  },
  {
    field: "diff_weight",
    span: 6,
    label: "工差重量"
  },
  {
    field: "diff_fee",
    span: 6,
    label: "工差金额"
  },
  {
    field: "createdTime",
    span: 6,
    label: "新建时间"
  },
  {
    label: '类目',
      span: 24,
      render(_, context){
        if(!context || !context.orderTax) return ''
        if(context.orderTax.length === 0) return ''
        const {cat_code, cat_name, tax, short_name } = context.orderTax || {}
        return cat_code + '-' + cat_name + '-' + short_name  + '-' + tax + '%'
      }
    },
  {
    label: '订单商品名称',
    span: 9,
    render(_, context) {
      return context.goodName
    }
  },
    {
      label: '开票商品名称',
      span: 9,
      render(_, context){
        if(!context || !context.orderTax) return ''
        const { inv_good_name } = context.orderTax || {}
        return inv_good_name
      }
    },
    {
      label: '组合品开',
      field: 'open_type',
      span: 6,
      render(open_type){
        return  String(open_type) === "1" ? "原商品开票" : String(open_type) === "2"  ? "子商品开票" : '--' 
      }
    },
    {
      label: '备注',
      field: 'remark',
      span: 24
    }
    // {
    //   label: '商品开票类目编码',
    //   field: 'TODO:CATEGORY',
    //   span: 6
    // },
    // {
    //   label: '货物和劳务名称',
    //   field: 'TODO:CATEGORY',
    //   span: 6
    // },
    // {
    //   label: '商品和服务分类简称',
    //   field: 'TODO:CATEGORY',
    //   span: 6
    // },
  // {
  //   label: '商品开票类目编码',
  //   field: 'TODO:CATEGORY',
  //   span: 6
  // },
  // {
  //   label: '货物和劳务名称',
  //   field: 'TODO:CATEGORY',
  //   span: 6
  // },
  // {
  //   label: '商品和服务分类简称',
  //   field: 'TODO:CATEGORY',
  //   span: 6
  // },
];

export const GOOD_COLUMNS: DescriptionColumns = [
  {
    field: "goodNo",
    label: "商品编码",
    span: 6
  },
  {
    field: "goodBrand",
    label: "品牌",
    span: 6
  },
  {
    field: "firstCat",
    label: "商品分类",
    render(_, { firstCat, secCat, thirdCat }) {
      return h(
        ElTag,
        { size: "small" },
        {
          default: () => firstCat + "_" + secCat + "_" + thirdCat
        }
      );
    },
    span: 12
  },
  {
    field: "goodName",
    label: "商品名称",
    span: 24,
    render: (goodName, { goodNo }) =>
      createTooltip(goodName, "商品编码 : " + goodNo, 270)
  },

  {
    field: "qrdType",
    label: "商品类型",
    span: 6,
    render(data) {
      return h(
        ElTag,
        { size: "small" },
        {
          default: () =>
            xs_order_type_options.find(t => t.value === data)?.label
        }
      );
    }
  },

  // {
  //   field: "goodModel",
  //   label: "型号",
  //   span: 6
  // },
  // {
  //   field: "metalsType",
  //   label: "贵金属种类",
  //   span: 6
  // },
  {
    field: "weight",
    label: "重量",
    span: 6
  },
  {
    field: "goldPrice",
    label: "金价",
    span: 6
  },
  {
    field: "workDay",
    label: "工期",
    span: 6
  },
  {
    field: "tax",
    label: "税率",
    span: 24
  }
];

export const SALE_COLUMNS: DescriptionColumns = [
  ...ORDER_COLUMNS,
  ...GOOD_COLUMNS
];
