import { ElButton, ElTable, ElTableColumn, ElTag } from "element-plus"
import { cg_order_type_options, xs_inv_type_list } from "/@/utils/status"
import { h } from "vue"
import { statusOptions } from "./shared"
import { createTooltip } from "/@/utils/tootip"
import { useRenderIcon } from "/@/components/ReIcon/src/hooks"

export const goodColumns = [
  {
    span: 12,
    field: 'seller_name',
    label: '销售方公司'
  },
  {
    span: 12,
    field: 'buyer_name',
    label: '购买方公司'
  },
  {
    span: 12,
    field: 'goodName',
    label: '订单商品名称'
  },
  {
    span: 12,
    field: 'inv_good_name',
    label: '发票商品名称'
  },
  {
    span: 6,
    label: '商品类型',
    render(_, { good_type }) {
      return h(ElTag, { size: 'small' }, {
        default: () => cg_order_type_options.find(item => item.value == good_type)?.label || '--'
      })
    }
  },
  {
    span: 6,
    field: 'good_code',
    label: '商品代码'
  },
  {
    span: 6,
    field: 'spec',
    label: '规格'
  },
  {
    span: 6,
    field: 'unit',
    label: '单位'
  },
  {
    span: 6,
    field: 'unit_price',
    label: '税前单价'
  },
  {
    span: 6,
    field: 'subunit_price',
    label: '税后单价'
  },
  {
    span: 6,
    field: 'unit_weight',
    label: '重量'
  },
  {
    span: 6,
    field: 'spectral',
    label: '分光'
  },
  {
    field: 'cat_name',
    label: '进项类目',
    span: 24
  }
]


export const orderGoodColumns = [
  {
    field: 'num',
    label: '计提数量',
    span: 6
  },
  {
    field: 'out_fee',
    label: '计提金额',
    span: 6
  },
  {
    span: 6,
    label: '状态',
    render(_, { status }) {
      return h(ElTag, { size: 'small', type: statusOptions.find(item => item.value == status)?.type }, {
        default: () => statusOptions.find(item => item.value == status)?.label
      })
    }
  },
  {
    span: 6,
    label: '是否加入调整单',
    render(_, { is_checkOrder }) {
      return is_checkOrder == 0 ? '否' : '是'
    }
  },
  {
    span: 6,
    label: '创建人',
    field: 'apply_name'
  },
  {
    span: 6,
    label: '创建时间',
    field: 'create_time'
  },
  {
    span: 6,
    label: '操作时间',
    field: 'update_time'
  }
]


export const importOrderGoodColumns = (handleShowDetail: any) => (
  [{
    label: '订单购买方公司',
    span: 12,
    render(_, row) {
      return createTooltip(row.buyer_code, "购买方公司纳税识别号 : " + row.buyer_code, 340)
    }
  },
  {
    label: '订单销售方公司',
    span: 12,
    render(_, row) {
      return createTooltip(row.seller_name, "销售方公司纳税识别号 : " + row.seller_code, 340)
    }
  },
  {
    label: '订单编号',
    field: 'orderCode',
    span: 6
  },
  {
    label: '订单主编号',
    field: 'cxCode',
    span: 6
  },
  {
    label: '类目编码',
    span: 12,
    render(_, { cat_code, cat_name, tax }) {
      return `${cat_code}-${cat_name}-${(Number(tax)) + '%'}`
    }
  },
  {
    label: '商品类型',
    span: 6,
    render(_, { goodType }) {
      return h(ElTag, { size: 'small' }, {
        default: () => cg_order_type_options.find(item => item.value == goodType)?.label
      })
    }
  },
  {
    label: '商品编号',
    span: 6,
    field: 'goodNo'
  },
  {
    label: '商品名称',
    span: 12,
    field: 'goodName'
  },
  {
    label: '单位',
    field: 'unit',
    span: 6
  },
  {
    label: '商品数量',
    field: 'num',
    span: 6
  },
  {
    label: '商品单价',
    field: 'goodPrice',
    span: 6
  },
  {
    label: '订单总金额',
    field: 'totalPrice',
    span: 6
  },
  {
    label: '购买方公司',
    span: 12,
    render(_, row) {
      return createTooltip(row.inv_buyer_name, "购买方公司纳税识别号 : " + row.inv_buyer_code, 340)
    }
  },
  {
    label: '销售方公司',
    span: 12,
    render(_, row) {
      return createTooltip(row.inv_seller_name, "销售方公司纳税识别号 : " + row.inv_seller_code, 340)
    }
  },
  {
    label: '发票号码',
    field: 'inv_number',
    span: 6
  },
  {
    label: '开票日期',
    field: 'inv_open_date',
    span: 6
  },
  {
    label: '发票类型',
    span: 6,
    render(_, row) {
      return xs_inv_type_list.find(item => item.value == row.inv_type)?.label
    }
  },
  {
    label: '发票明细ID',
    field: 'inv_item_id',
    span: 6
  },
  {
    label: '货物或应税劳务、服务名称',
    field: 'inv_good_name',
    span: 12,
  },
  {
    label: '类目编号',
    span: 6,
    render(_, row) { return row.inv_cat_code }
  },
  {
    label: '规格型号',
    span: 6,
    field: 'inv_spec'
  },
  {
    label: '单位',
    span: 6,
    field: 'inv_unit'
  },
  {
    label: '数量',
    span: 6,
    field: 'inv_num'
  },
  {
    label: '税前单价',
    span: 6,
    field: 'inv_subprice'
  },
  {
    label: '税前总价',
    span: 6,
    field: 'inv_subtotal'
  },
  {
    label: '税率',
    span: 6,
    field: 'inv_tax',
    render(_, { inv_tax }) {
      return Number(inv_tax) + '%'
    }
  },
  {
    span: 6,
    label: '税额',
    field: 'inv_tax_total'
  },
  {
    label: '税后单价',
    span: 6,
    field: 'inv_price'
  },
  {
    label: '税后总额',
    span: 6,
    field: 'inv_subtotal'
  },
  {
    label: '出库商品',
    span: 24,
    render(_, { Manager }) {
      return h(ElTable, { size: 'small', border: true, style: { width: '100 %' }, data: Manager?.ProductRela || [] }, [
        h(ElTableColumn, { prop: 'product_id', label: 'ID', width: '60px', showOverflowTooltip: true }),
        h(ElTableColumn, { prop: 'skuCode', label: '商品编号', showOverflowTooltip: true }),
        h(ElTableColumn, { prop: 'goodName', label: '商品名称', showOverflowTooltip: true }),
        h(ElTableColumn, { prop: 'num', label: '操作数量', showOverflowTooltip: true }),
        h(ElTableColumn, { prop: 'apply_name', label: '操作人', showOverflowTooltip: true }),
        h(ElTableColumn, { prop: 'create_time', label: '创建时间', showOverflowTooltip: true }),
        h(ElTableColumn, { label: '操作', showOverflowTooltip: true }, {
          default: ({ row, $index }) => h(ElButton, { 
            onClick: () => handleShowDetail(row.product_id, $index),
            icon: useRenderIcon('eye-view'), 
            type: 'primary', 
            size: 'small',
            link: true,
          })
        }),
      ])
    }
  }
])